2017-01-23 166 views

回答

0

我認爲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.reshap 
e(1, -1) if it contains a single sample. 
    DeprecationWarning) 
... 
skipped 
... 
ValueError: Found arrays with inconsistent numbers of samples: [1 9] 

讓我們把它高興:

UPDATE:同樣也適用於x_test陣列:

In [60]: x_test = x_test.reshape(-1, 1) 

In [61]: x_test.shape 
Out[61]: (5, 1) 

In [62]: predicted= linear.predict(x_test) 

In [63]: predicted 
Out[63]: array([ 5.7559457, 5.7559457, 5.7559457, 5.7559457, 5.7559457]) 
+0

喜,抱歉,但我得到這個錯誤後改變它accordinly:「ValueError:形狀(1,5)和(1,1)不對齊ed:5(dim 1)!= 1(dim 0)「 – AwArAw

+0

@AwArAw,好吧,這同樣適用於'x_test' – MaxU

+0

@AwArAw,我已經更新了答案 - 請檢查 – MaxU