这是我的密码:
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import train_test_split
%matplotlib inline
boston_properties = load_boston()
l_distance = boston_properties['data'][:, np.newaxis, 7]
linreg = LinearRegression()
X_train, X_test, y_train, y_test = train_test_split(l_distance, boston_properties['target'], test_size = 0.3)
y_pred = cross_val_predict(linreg, l_distance, boston_properties.target, cv=5)
plt.scatter(X_test, y_test, color='black')
plt.plot(X_test, y_pred, color='blue', linewidth=2)
plt.show()
print(y_pred.shape)
我收到的错误如下:
ValueError: x and y must have same first dimension, but have shapes (152, 1) and (506,)
我怎样才能做到这一点
你做了一个
train_test_split
,但是你没有用它来训练模型。然后你预测整个训练数据,并将其与y_test
进行比较。这毫无意义。改为使用以下行:编辑:您也可以使用此线通过您的点绘制一条直线:
相关问题 更多 >
编程相关推荐