pandas+scikit-learn
from sklearn.linear_model import Ridge,LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
#Create the same data as Excel
x=np.array([0,3,12.5,18.7,25,20,16,12.8,10.24,8.192])
y=np.array([0,15,46.6,60.3,74.3,59.44,47.552,46,36.8,29.44])
#DEGREE (order)
degree = 3
# LinearRegression
# make_Gacchanko Polynomial Features and Linea Regression on pipeline
model = make_pipeline(PolynomialFeatures(degree,include_bias=False),LinearRegression(fit_intercept=False))
model.fit(x.reshape(-1,1),y)
y_model=model.predict(x.reshape(-1,1))
#Check data frame
df = pd.DataFrame({'y_model.predict':y_model,},index=x)
df.sort_index().plot(kind ='line',figsize=(10.,5.))
#coefficient
model.steps[1][1].coef_
The graph looks very similar.
Coefficient match (almost)
array([ 5.06817229e+00, -1.71343566e-01, 3.49200227e-03])
Recommended Posts