1 from sklearn.linear_model import LinearRegression 2 lr = LinearRegression() 3 print(tr_x.shape,tr_y.shape) 4 lr.fit(tr_x,tr_y) 5 6 7 # 报错 8 (64,) (64,) 9 Traceback (most recent call last): 10 File "F:/Python_Project/sklearn2_2/zong_fu_xi/A_02.py", line 51, in <module> 11 lr.fit(tr_x,tr_y) 12 File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\linear_model\base.py", line 458, in fit 13 y_numeric=True, multi_output=True) 14 File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\utils\validation.py", line 756, in check_X_y 15 estimator=estimator) 16 File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array 17 "if it contains a single sample.".format(array)) 18 ValueError: Expected 2D array, got 1D array instead: 19 array=[ 9.1802 5.8707 7.4239 13.176 7.0708 5.6397 18.959 5.0269 8.5186 20 21.279 5.7737 11.708 8.3829 6.3654 6.4296 6.8825 6.3534 7.4764 21 5.5204 8.8254 5.5277 7.9334 22.203 5.3077 5.734 8.0959 5.5649 22 7.6031 14.164 9.2482 5.7077 9.3102 5.0365 5.8918 9.7687 5.3794 23 6.5479 6.1891 5.2524 7.5402 8.2934 13.394 10.136 20.27 7.6366 24 7.2259 10.274 12.828 12.836 5.8014 5.4069 8.2951 9.4536 8.4084 25 7.3345 5.6063 5.4901 6.5159 5.7107 5.3054 5.4994 7.2182 11.7 26 7.0931]. 27 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.
# 造成错误原因:
这是由于在新版的sklearn中,所有的数据都应该是二维矩阵,哪怕它只是单独一行或一列(比如前面做预测时,仅仅只用了一个样本数据),前面程序第3行输出的维度是(64,)一维的,所以需要使用.reshape(1,-1)进行转换,具体操作如下。
需改为
from sklearn.linear_model import LinearRegression lr = LinearRegression() tr_x = np.array(tr_x).reshape(1,-1) te_x = np.array(te_x).reshape(1,-1) tr_y = np.array(tr_y).reshape(1,-1) te_y = np.array(te_y).reshape(1,-1) print(tr_x.shape,tr_y.shape) lr.fit(tr_x,tr_y)
此时这个错误就解决了
原文地址:https://www.cnblogs.com/--wwwww/p/10476238.html
时间: 2024-09-29 14:03:03