我对此比较新手,有人能告诉我这一行是什么意思吗?
predicted= linear.predict(x_test)
回答:
我认为 sklearn.linear_model.LinearRegression().fit()
期望输入一个形状为 (# of rows, 1)
的数组。
示例:
In [52]: x_train= [5.5997066,4.759385,2.573958,5.586931,3.019574,4.296047,1.586953,0.5997066,3.683957]In [53]: linear.fit(x_train, y_train)<PYTHON_PATH>\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)...skipped...ValueError: Found arrays with inconsistent numbers of samples: [1 9]
让我们使它正常工作:
In [54]: x_train = np.array(x_train).reshape(len(x_train), -1)In [55]: x_train.shapeOut[55]: (9, 1)In [56]: linear.fit(x_train, y_train)Out[56]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
更新: 这同样适用于 x_test
数组:
In [60]: x_test = x_test.reshape(-1, 1)In [61]: x_test.shapeOut[61]: (5, 1)In [62]: predicted= linear.predict(x_test)In [63]: predictedOut[63]: array([ 5.7559457, 5.7559457, 5.7559457, 5.7559457, 5.7559457])