【发布时间】:2021-10-08 16:48:15
【问题描述】:
我正在对我的数据进行多重回归,但绘制数据会引发错误:ValueError: x and y must be the same size。
x.shape is (10000, 2)
#Since I have two independent x = dataset[['green', 'blue']]
y.shape is (10000,)
如何重塑数组?因为我在 x 中有两个自变量。
代码:
dataset = pd.read_csv('colors.csv')
x = dataset[['green', 'blue']] #independent variable
y = dataset['value']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 100)
mlr = LinearRegression()
mlr.fit(x_train, y_train)
print("Intercept: ", mlr.intercept_)
print("Coefficients:")
list(zip(x, mlr.coef_))
#Prediction of test set
y_pred_mlr= mlr.predict(x_test)
#Predicted values
print ("input test set", x_test)
print("Prediction for test set: {}".format(y_pred_mlr))
mlr_diff = pd.DataFrame({'Actual value': y_test, 'Predicted value': y_pred_mlr})
plt.scatter(x_train, y_train,color='g')
plt.plot(x_train, mlr.predict(x_train),color='k')
plt.show()
谢谢
【问题讨论】:
标签: python numpy matplotlib scikit-learn regression