使用ridge model發生錯誤
2019/10/09 06:02 下午
機器學習共學討論版
herohsu
觀看數:13
回答數:1
收藏數:0
我試著練習boston and ridge model, 執行時發生
ValueError: shapes (127,13) and (10,) not aligned: 13 (dim 1) != 10 (dim 0)
我確定輸入的資料應該沒問題, 請問問題在哪?
x_train(379, 13) x_test(127, 13) y_train(379,) y_test(127,)
# 讀取boston資料集
boston = datasets.load_boston()
# 切分訓練集/測試集
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.25, random_state=3)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
# 建立一個線性回歸模型
ridge = linear_model.Ridge(alpha=1.0)
# 將訓練資料丟進去模型訓練
ridge.fit(x_train, y_train)
# 將測試資料丟進模型得到預測結果
y_pred = regr.predict(x_test)
print(ridge.coef_)
# 預測值與實際值的差距,使用 MSE
print("Mean squared error: %.2f"
% mean_squared_error(y_test, y_pred))
Output
(379, 13) (127, 13) (379,) (127,)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-24-556f3708f82d> in <module>
12
13 # 將測試資料丟進模型得到預測結果
---> 14 y_pred = regr.predict(x_test)
15 print(ridge.coef_)
16 # 預測值與實際值的差距,使用 MSE
~/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/base.py in predict(self, X)
219 Returns predicted values.
220 """
--> 221 return self._decision_function(X)
222
223 _preprocess_data = staticmethod(_preprocess_data)
~/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/base.py in _decision_function(self, X)
204 X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
205 return safe_sparse_dot(X, self.coef_.T,
--> 206 dense_output=True) + self.intercept_
207
208 def predict(self, X):
~/anaconda3/lib/python3.7/site-packages/sklearn/utils/extmath.py in safe_sparse_dot(a, b, dense_output)
140 return ret
141 else:
--> 142 return np.dot(a, b)
143
144
ValueError: shapes (127,13) and (10,) not aligned: 13 (dim 1) != 10 (dim 0)