I would like to visualize the data points of the Boston Dataset, and plot the linear regression plane. But, I am getting a value error. I am using colab. The following is the code I run.
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
data1 = load_boston()
df1 = pd.DataFrame(data1.data)
df1.columns = data1.feature_names
df1['price']=data1.target
X = df1.iloc[:,0:13]
Y = df1.iloc[:,13]
xtrain,xtest,ytrain,ytest = train_test_split(X,Y,test_size = 0.2,random_state =0)
lin_reg = LinearRegression()
lin_reg.fit(xtrain,ytrain)
ypredict = lin_reg.predict(xtest)
plt.scatter(xtrain, ytrain, color = 'red')
plt.plot(xtrain, lin_reg.predict(xtrain), color = 'blue')
I am getting error in the last two lines.
ValueError Traceback (most recent call last)
<ipython-input-54-4f89e0554222> in <module>()
----> 1 plt.scatter(xtrain, ytrain, color = 'red')
2 plt.plot(xtrain, lin_reg.predict(xtrain), color = 'blue')
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
I know that X has 13 columns while Y has 1 column. This is why the error shows. But I don't know how to rectify it.
Can anyone please help?


