Home > database >  Not getting sklearn polynomial regression right
Not getting sklearn polynomial regression right

Time:10-11

I understood the principle, but when I try it in action I got something weird, Can someone explain me what am i doing wrong ?

So I use this graph

I don't know what I am doing wrong.

CodePudding user response:

You might try spline interpolation. Here is my approach to this dataset:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures, SplineTransformer
from sklearn.pipeline import make_pipeline

df = pd.read_csv('data.csv')

x = df.values[:, 0:1]
y = df.values[:, 1:2]

plt.figure()
plt.scatter(x, y, s=2)

transformer = SplineTransformer(degree=3, n_knots=10)
model = make_pipeline(transformer, Ridge(alpha=1e-3))
model.fit(x, y)

y_plot = model.predict(x)
plt.plot(x, y_plot, label="B-spline", color='red')
plt.show()

result

You can adjust n_knots to make it fitting the points better.

  • Related