[英]ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,)
I'm learning ML from Udemy.我正在从 Udemy 学习机器学习。 From one of the lectures of Polynomial Regression, the following code is as follows:从Polynomial Regression的一节课中,下面的代码如下:
# importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# importing dataset
dataset = pd.read_csv("Position_Salaries.csv")
x = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
print(x.shape)
print(y.shape)
# fitting LR to the dataset
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(x, y)
# fitting PR to the dataset
from sklearn.preprocessing import PolynomialFeatures
polreg = PolynomialFeatures(degree=2)
x_poly = polreg.fit_transform(x)
linreg2 = LinearRegression()
linreg2.fit(x_poly, y)
# visualising the polynomial regression results
x_grid = np.arange(min(x), max(x), 0.1)
x_grid = x_grid.reshape((len(x_grid), 1))
plt.scatter(x, y, color = "red")
plt.plot(x, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )
plt.title("Truth or Bluff PR")
plt.xlabel("Position")
plt.ylabel("Salary")
plt.show()
I got error as follows:我得到如下错误:
Traceback (most recent call last):
File "/home/ashutosh/Machine Learning A-Z Template Folder/Part 2 - Regression/Section 6 - Polynomial Regression/P14-Polynomial-Regression/Polynomial_Regression/plr.py", line 29, in <module>
plt.plot(x, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )
File "/usr/local/lib/python3.7/dist-packages/matplotlib/pyplot.py", line 2795, in plot
is not None else {}), **kwargs)
File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_axes.py", line 1666, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 225, in __call__
yield from self._plot_args(this, kwargs)
File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 391, in _plot_args
x, y = self._xy_from_xy(x, y)
File "/usr/local/lib/python3.7/dist-packages/matplotlib/axes/_base.py", line 270, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,)
the data set can be downloaded from here: https://sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P14-Polynomial-Regression.zip数据集可以从这里下载: https : //sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P14-Polynomial-Regression.zip
What can I do?我能做什么?
int the line 29, write x_grid
instead of x
ie, from在第 29 行,写x_grid
而不是x
即,从
plt.plot(x, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )
to到
plt.plot(x_grid, linreg2.predict(polreg.fit_transform(x_grid)), color = "blue" )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.