I understood the principle, but when I try it in action I got something weird, Can someone explain me what am i doing wrong ?
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()
You can adjust n_knots
to make it fitting the points better.