Matplotlib polynomial regression — too many lines showing

Viewed 371

This is the code I've used — however, for the picture below, all these spiderweb like lines keep popping up and I want to make them go away. Any suggestions? I just want a curved 3rd degree polynomial line that appropriately fits the data without the spiderwebs.

df = pd.read_csv('poly_data.csv', delimiter = ' ', names = ['x', 'y'])
x = df['x'].to_numpy()
y = df['y'].to_numpy()


model = LinearRegression()
poly = PolynomialFeatures(3)
x_poly = poly.fit_transform(x.reshape(-1, 1))
model.fit(x_poly, y.reshape(-1, 1))[![enter image description here][1]][1]
y_pred = model.predict(x_poly)

plt.scatter(x, y)
plt.scatter(x, y_pred)
plt.plot(x, model.predict(x_poly), label = '3rd order')
plt.title('Polynomial Regression')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

enter image description here

2 Answers

You see the spider web because your x values are not sorted, looking the code, you can sort your data.frame first, for example, if your data.frame is like this:

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

df = pd.DataFrame({'x':np.random.uniform(-5,5,20)})
df['y'] = 5+ 2*df['x'] - 7*df['x']**2 +2*df['x']**3 + np.random.normal(20)

Sort, assign x and y and fit,plot :

df = df.sort_values('x')

x = df[['x']]
y = df.sort_values('x')['y']

model = LinearRegression()
poly = PolynomialFeatures(3)
x_poly = poly.fit_transform(x)
model.fit(x_poly,y)
y_pred = model.predict(x_poly)

plt.scatter(x, y)
plt.scatter(x, y_pred)
plt.plot(x, model.predict(x_poly))

enter image description here

Or you define a sorted grid line to plot:

plt.scatter(x, y)
plt.scatter(x, y_pred)
xl = np.linspace(x.min(),x.max(),20)
plt.plot(xl, model.predict(poly.fit_transform(xl.reshape(-1, 1))))

this is incorrect. sorting values changes the regression outcome. It changes the correlation.

Related