Nazanin Zinouri
Nazanin Zinouri

Reputation: 229

Unable to fit polynomial regression line correctly

I have a dataframe like below:

price |  Sales
6.62  |  64.8
8.71  |  38

It looks like this

enter image description here

I am not very familiar with non-linear regression but following a few tutorials, I used the following code to fit polynomial distribution:

X = df['price'].values
y = df['sales'].values

X = X.reshape(-1,1)

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

pre_process = PolynomialFeatures(degree=2)
X_poly = pre_process.fit_transform(X)

pr_model = LinearRegression()
# Fit our preprocessed data to the polynomial regression model
pr_model.fit(X_poly, y)
# Store our predicted Humidity values in the variable y_new
y_pred = pr_model.predict(X_poly)
# Plot our model on our data
plt.scatter(X, y, c = "black")
plt.plot(X, y_pred)

And I get this which is wrong:

enter image description here

Any ideas what I am missing that I am unable to get the right line for the fit?

Upvotes: 0

Views: 307

Answers (1)

StupidWolf
StupidWolf

Reputation: 46968

The prediction is correct:

X = np.random.uniform(0,1,100)
y = 3*X**2 + 2*X - 8 + np.random.normal(0,1,100)

X = X.reshape(-1,1)

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

pre_process = PolynomialFeatures(degree=2)
X_poly = pre_process.fit_transform(X)

pr_model = LinearRegression()
pr_model.fit(X_poly, y)

y_pred = pr_model.predict(X_poly)

plt.scatter(X, y, c = "black")
plt.scatter(X, y_pred, c="orange")

enter image description here

To plot a line, you need to sort the x values:

plt.scatter(X, y, c = "black")
x_sorted = np.sort(X,axis=0)
y_pred_sorted = pr_model.predict(pre_process.fit_transform(x_sorted))
plt.plot(x_sorted,y_pred_sorted,c="orange")

enter image description here

Upvotes: 1

Related Questions