ValueError: Found array with dim 4. Estimator expected <= 2.

python3: ValueError: Found array with dim 4. Estimator expected <= 2.

原因:维度不匹配。数组维度为4维,现在期望的是 <= 2维

方法:改为二维形式。

本人这里是4维度,我改为个数为两维度,如下处理:

 source_x = (source_x.reshape(source_x.shape[0], source_x.shape[1] * source_x.shape[2] * source_x.shape[3]))
 target_x = (target_x.reshape(target_x.shape[0], target_x.shape[1] * target_x.shape[2] * target_x.shape[3]))

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
`MinMaxScaler`是`sklearn.preprocessing`中的标准化类,用于将数据缩放到给定范围内。但是,`MinMaxScaler`只能处理维度为1或2的数组,当输入的数组维度大于2时,就会报错`ValueError: Found array with dim 3. MinMaxScaler expected <= 2.`。 解决这个问题的方法是,将多维数组降维为二维数组,然后使用`MinMaxScaler`进行标准化。具体实现代码如下: ```python from sklearn.preprocessing import MinMaxScaler # 假设输入数据的数组为X n_samples, n_features, n_depth = X.shape X_2d = X.reshape((n_samples, n_features * n_depth)) scaler = MinMaxScaler() X_scaled = scaler.fit_transform(X_2d) # 将标准化后的二维数组再转换回多维数组 X_scaled_3d = X_scaled.reshape((n_samples, n_features, n_depth)) ``` 其中,`reshape`函数将多维数组转换为二维数组,`fit_transform`函数对二维数组进行标准化处理,然后再使用`reshape`函数将标准化后的二维数组转换为多维数组。 需要注意的是,使用`MinMaxScaler`进行标准化时,需要保证训练集和测试集在同一尺度下进行标准化。可以先对训练集进行标准化处理,然后直接使用训练集的标准化器对测试集进行标准化。具体实现如下: ```python n_samples_train, _, _ = X_train.shape n_samples_test, _, _ = X_test.shape X_train_2d = X_train.reshape((n_samples_train, n_features * n_depth)) X_test_2d = X_test.reshape((n_samples_test, n_features * n_depth)) scaler = MinMaxScaler() X_train_scaled = scaler.fit_transform(X_train_2d) X_test_scaled = scaler.transform(X_test_2d) X_train_scaled_3d = X_train_scaled.reshape((n_samples_train, n_features, n_depth)) X_test_scaled_3d = X_test_scaled.reshape((n_samples_test, n_features, n_depth)) ``` 其中,`X_train`和`X_test`分别为训练集和测试集的输入数据,其他变量的含义与之前相同。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打史莱姆的小仙女~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值