1

For this code of polynomial regression implemented in python with sklearn

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import pandas as pd

m=100
X=6*np.random.rand(m,1)-3
y=0.5*X**3+X+2+np.random.rand(m,1)

poly_features=PolynomialFeatures(3)
X_poly=poly_features.fit_transform(X)
lin_reg=LinearRegression()

X_new=[[0],[3]]

lin_reg.fit(X_poly,y)

plt.plot(X,y,"b.")
plt.plot(X, lin_reg.predict(poly_features.fit_transform(X)),  "r-")

plt.show()

The output is showing as

enter image description here

But I wanted to get a smooth prediction line. How to get it?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
dip007
  • 23
  • 2
  • Possible duplicate of [Python's Matplotlib plotting in wrong order](https://stackoverflow.com/questions/37414916/pythons-matplotlib-plotting-in-wrong-order) – Jondiedoop Jan 12 '19 at 15:26

1 Answers1

0

The problem is that X array is not sorted. So when you use lines -r to plot your data, it connects the data points in the order of your unsorted X-data points. Hence, you see a random network of lines. The order doesn't matter for the plot with markers because you are just plotting dots without lines.

The solution is to sort the X data and pass the sorted X data to your plot command and accordingly to the fit_transform.

shape = X.shape
X = np.sort(X.flatten())
plt.plot(X, lin_reg.predict(poly_features.fit_transform(X.reshape((shape)))),  "r-", lw=2)

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71