ValueError: Expected singleton: hw.rebuild.exam(6, 3)

期待一个值,接收到多个值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`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`分别为训练集和测试集的输入数据,其他变量的含义与之前相同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值