scikit learn coefficients polynomialfeatures

I have fit a model with the help of PolynomialFeatures, but I don't know how to grab the coefficients of the model. The code is the following:

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

X = np.matrix([0,1,2,3,4,5,6,7,8,9,10]).reshape((11,1))
Y = np.matrix([0,2.2,3.5,14.3,20.4,32.1,40.3,  
           59.1,86.2,90.3,99.9]).reshape((11,1))
a = PolynomialFeatures(15)
modelo = make_pipeline(a, LinearRegression())
modelo.fit(X, Y)
plt.plot(X,Y,'.')
plt.plot(X, modelo.predict(X),'-')
plt.show()

original data


Let's begin by using a second degree polynomial, instead of 15 degree polynomial in your example, to simplify your problem (as well as to avoid overfitting).

Second Degree Polynomial fit

Using your X let's see how the values are transformed.

a = PolynomialFeatures(2)
a.fit_transform(X)

array([[   1.,    0.,    0.],
       [   1.,    1.,    1.],
       [   1.,    2.,    4.],
       [   1.,    3.,    9.],
       [   1.,    4.,   16.],
       [   1.,    5.,   25.],
       [   1.,    6.,   36.],
       [   1.,    7.,   49.],
       [   1.,    8.,   64.],
       [   1.,    9.,   81.],
       [   1.,   10.,  100.]])

We can see that the first feature is X^0, second is X^1, third is X^2.

Now, using your existing code, you are building a pipeline of two steps as modelo.

We are able to access the second step's estimator using modelo.steps[1][1]. From there we can use coef_ to obtain the coefficients, and intercept_ to obtain the intercept.

modelo.steps[1][1].coef_
# [[ 0.          3.3486014   0.76468531]]

modelo.steps[1][1].intercept_
# [-2.75244755]

From here we can see that the polynomial is y_estimated = -2.75 + 0 * X^0 + 3.35 * X^1 + 0.76 * X^2