我正在运行以下代码,训练数据集的图出现错误,
import pandas as pd
import numpy as np
df = pd.read_csv('11.csv')
df.head()
AT V AP RH PE
0 8.34 40.77 1010.84 90.01 480.48
1 23.64 58.49 1011.40 74.20 445.75
2 29.74 56.90 1007.15 41.91 438.76
3 19.07 49.69 1007.22 76.79 453.09
4 11.80 40.66 1017.13 97.20 464.43
x = df.drop(['PE'], axis = 1).values
y = df['PE'].values
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = 0.2, random_state=0)
from sklearn.linear_model import LinearRegression
ml = LinearRegression()
ml.fit(x_train, y_train)
y_pred = ml.predict(x_test)
print(y_pred)
import matplotlib.pyplot as plt
plt.scatter(x_train, y_train, color = 'red')
plt.plot(x_train, ml.predict(x_test), color = 'green')
plt.show() ***
please help to reshape 2d to 1d array for plotting graphs
**ValueError: x and y must be the same size**
EDIT:既然你的问题已经修复了格式,我发现了一些错误,主题是使用1D线性回归代码来绘制多元回归问题。
plt.scatter(x_train, y_train, color = 'red')
:您试图使用x_train
在一个轴(AT、V、AP、RH(上绘制多个变量。你不能这样做,因为这是多元线性回归。(例如,无法将x轴上的压力和体积与y轴上的温度进行拟合。x轴代表什么?这没有意义。(你无法绘制你想要绘制的内容,我也无法给你建议,因为我不知道你想要绘制什么。您可以一次尝试一个变量,例如plt.scatter(x_train['AT'], y_train, color='red')
。或者你用不同的颜色在同一张图上绘制每个变量——尽管我不建议这样做,因为你的x轴可能是不同的单位。
plt.plot(x_train, ml.predict(x_test)
:您应该使用y_test进行x输入。例如plt.plot(y_test, ml.predict(x_test))
。这是数据长度的问题,而不是像上面的错误那样的宽度/列的问题。尽管如果我的建议不是你想要的(绘制y_test和y预测有点奇怪(,但当你使用多元线性回归时,你可能会(错误地(输入1D线性回归的假设/代码——这是这些错误的潜在主题。