Python 3.x PyPlot错误“;X和Y的尺寸必须相同;,我的一切';我在网上找到了isn';行不通

Python 3.x PyPlot错误“;X和Y的尺寸必须相同;,我的一切';我在网上找到了isn';行不通,python-3.x,pandas,matplotlib,scikit-learn,linear-regression,Python 3.x,Pandas,Matplotlib,Scikit Learn,Linear Regression,我试图在Scikit Learn中创建一个线性回归模型。虽然我遇到了一个问题。这是说x和y的大小不一样。我正在使用谷歌的“加州住房”数据集。代码如下: import numpy as np import pandas as pd import matplotlib.pyplot as plt dataset = pd.read_csv('/content/sample_data/california_housing_train.csv') x = dataset.iloc[:, :-2].va

我试图在Scikit Learn中创建一个线性回归模型。虽然我遇到了一个问题。这是说x和y的大小不一样。我正在使用谷歌的“加州住房”数据集。代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataset = pd.read_csv('/content/sample_data/california_housing_train.csv')
x = dataset.iloc[:, :-2].values
y = dataset.iloc[:, :-1].values

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 1/3)

from sklearn.linear_model import LinearRegression
lr = LinearRegression()

lr.fit(x_train, y_train)

y_pred = lr.predict(x_test)

plt.scatter(x_train, y_train, color = "red")
plt.plot(x_train, lr.predict(x_train), color = "green")
plt.title("Income vs Home Value (Training set)")
plt.xlabel("Income")
plt.ylabel("Home Value")
plt.show()

plt.scatter(x_test, y_test, color = "red")
plt.plot(x_train, lr.predict(x_train), color = "green")
plt.title("Income vs Home Value (Testing set)")
plt.xlabel("Income")
plt.ylabel("Home value")
plt.show()
错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-95095200e54b> in <module>()
     18 y_pred = lr.predict(x_test)
     19 
---> 20 plt.scatter(x_train[0], y_train[:], color = "red")
     21 plt.plot(x_train, lr.predict(x_train), color = "green")
     22 plt.title("Income vs Home Value (Training set)")

3 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, plotnonfinite, **kwargs)
   4389         y = np.ma.ravel(y)
   4390         if x.size != y.size:
-> 4391             raise ValueError("x and y must be the same size")
   4392 
   4393         if s is None:

ValueError: x and y must be the same size
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
18 y_pred=lr.预测(x_检验)
19
--->20 plt.散射(x_列[0],y_列[:],color=“红色”)
21 plt.绘图(x_系列,lr.预测(x_系列),颜色=“绿色”)
22 plt.标题(“收入与家庭价值(培训集)”)
3帧
/usr/local/lib/python3.7/dist-packages/matplotlib/axes//u axes.py散点(self、x、y、s、c、marker、cmap、norm、vmin、vmax、alpha、线宽、顶点、边色、plotnonfinite、**kwargs)
4389 y=np.ma.ravel(y)
4390如果x.size!=y、 尺寸:
->4391提升值错误(“x和y必须大小相同”)
4392
4393如果s为无:
ValueError:x和y的大小必须相同

我不知道为什么。我在其他帖子上都试过了。根据我在其他帖子上的发现,这是因为一个(x或y)是2d,另一个是1d。尽管“修复”不起作用。

查看x&y变量的维度:

[ins] In [34]: x.shape                                                                                                                     
Out[34]: (17000, 7)

[ins] In [35]: y.shape                                                                                                                     
Out[35]: (17000, 8)
y变量应为目标变量,即房价:

y = dataset.iloc[:,-1].values
您的x变量定义忽略了收入中位数,这是您试图绘制的,因此这里是一个包含收入变量的x矩阵:

x = dataset.iloc[:, :-1].values
y的定义如上所述,现在是一维的;x矩阵中有8个变量,最后一个(指数7)是收入中位数。要绘制它:

plt.scatter(x_train[:,7], y_train, color = "red")

我在上面的代码中没有看到这一行plt.scatter(x_train[0],y_train[:],color=“red”)`我试过了,但没有成功,它仍然说了同样的错误。我只是想在没有失败尝试的情况下给出简单的代码。