实验1-波士顿房价预测部分报错解决方法

运行

sgd = SGDRegressor()
sgd.fit(x_train, y_train)

print("r2 score of Linear regression is",r2_score(y_test,sgd.predict(x_test)))

时出现

DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True)

的报错

 

解决方法——

将代码改成

y_train = np.ravel(y_train)

# 创建并拟合模型
sgd = SGDRegressor()
sgd.fit(x_train, y_train)

# 评估模型
print("r2 score of Linear regression is",r2_score(y_test,sgd.predict(x_test)))

结果

 

解决 

y_train = np.ravel(y_train)

# 创建并拟合模型
sgd = SGDRegressor()
sgd.fit(x_train, y_train)

# 评估模型
print("r2 score of Linear regression is",r2_score(y_test,sgd.predict(x_test)))
posted @ 2024-05-09 15:28  椰子灰  阅读(3)  评论(0编辑  收藏  举报