ValueError: Found array with dim 4. Estimator expected和ValueError: Expected 2D array, got 1D array i

python3中对numpy数组进行降维或升维

解决报错如:

1.ValueError: Found array with dim 4. Estimator expected

2.ValueError: Expected 2D array, got 1D array instead:

报错1ValueError: Found array with dim 4. Estimator expected——解决方式:
使用 np.concatenate

函数模型:concatenate((a1, a2, …), axis=0)

Parameters参数说明:
• 传入的参数(a1,a2,a3,…)必须是一个多个数组的元组或者列表
另外需要指定拼接的方向,默认axis = 0,也就是说对数组中0轴(X轴/或者说行)的对象进行拼接得到一个纵向组合的数组,(axis=1则是相反);注:一般axis = 0,就是对该轴向的数组进行操作,操作方向是另外一个轴,即axis=1。

import numpy as np
a = np.array([[1,2],[2,3]])
b = np .array([[4,5],[3,4]])
print(np.concatenate((a, b), axis=0))

print(np.concatenate((a), axis=0))

输出结果:(这样就可以将对数组进行降维了(剥除一组中括号[ ]))

[[1 2]
 [2 3]
 [4 5]
 [3 4]]
 
[1 2 2 3]

参考链接:https://blog.csdn.net/brucewong0516/article/details/79158758

报错2函数fit时出现ValueError: Expected 2D array, got 1D array instead:——解决方式:
这里我将函数报错时的代码片段截取出来,具体函数的数据就不截取了
方法1:使用中括号[ ]:
原代码:

import numpy as np  
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()  
knn.fit(x,y)                 
x_new = [50000,8,1.2]
y_pred = knn.predict(x_new)

会报错:

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

使用np.array.reshape修改:

x_new = np.array([50000,8,1.2]).reshape(1,-1)

方法2.使用 np.array.reshape(1,-1)

x_new = np.array([[50000,8,1.2]])

python3新版的sklearn中,所有的数据都应该是二维矩阵,即np.array()中应该至少是包含两对中括号[ ]

参考链接:https://www.jianshu.com/p/60596270e94e

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值