I'm learning ML from Udemy. From one of the lectures of Polynomial Regression, the following code is as follows:
# 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
What can I do?
int the line 29, write x_grid
instead of x
ie, from
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" )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.