How do I make sense of the polynomial coefficients my program outputs?

Viewed 166

I am trying to obtain the polynomial equation of a surface which represents 4 variables: Leakage, pressure,dimension and speed. Basically I am trying to find the equation Leakage=f(pressure, dimension, speed). I managed to get polynomial coefficients and the intercept as shown below this post, but I don't know how to interpret them in a polynomial equation(ie: z= ao + alx + a2Y + a3XY + a4x2 + a5y2 + a6 x3 + a7x2 y + a8 x y2 + ag, etc.). Can someone help plz?:

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

# my data 
data=np.column_stack((speed, dimension3,pressure3,leakage3))

# Generate polynomial features of desired degree
d = 6
poly = PolynomialFeatures(degree=d, include_bias=False)
X = poly.fit_transform(data[:, :-1])
y = data[:,-1] 

# Define and fit linear regression 
clf = LinearRegression()
clf.fit(X, y)

# Check results
print(clf.coef_)
print(clf.intercept_)
[ 1.21064489e-09  2.51751918e-11  3.17543952e-12 -3.66443110e-13
 -3.62188623e-14  1.13794085e-14  2.33351780e-15  8.76551176e-16
  9.65867527e-16  6.69545284e-16 -1.67396381e-16 -1.57313479e-16
 -8.47927583e-17 -3.38219081e-16  1.83324692e-17 -3.10419931e-16
  1.43757683e-16 -2.25732234e-16 -2.37769462e-16 -1.25305377e-18
  4.30862718e-18 -3.03569002e-16  6.43054057e-19  2.88496876e-15
  2.13470938e-14  3.85650361e-20  4.65962202e-16 -2.18466792e-13
 -2.30089604e-13 -4.53158981e-22  3.96214571e-17  6.38462456e-13
  1.48896917e-12  1.52973108e-13 -1.18405974e-14  4.30024113e-15
 -2.52978182e-13  5.34046635e-16  2.40414556e-12  1.77892418e-11
  3.60577799e-17  3.88296991e-13 -1.82055655e-10 -1.91741331e-10
 -4.76611883e-19  3.30372428e-14  5.32052044e-10  1.24080759e-09
  1.27477605e-10  2.12356214e-18  1.47504991e-15  1.81132053e-10
  3.25304547e-10  7.36098343e-11  1.75235266e-11  2.36581268e-18
 -6.40351208e-19  4.91896560e-17 -1.01893976e-17 -3.16647219e-16
 -3.52899091e-15  1.99753728e-16 -6.70331612e-15  3.37679794e-14
  3.84231696e-14 -1.53920338e-15  1.15182270e-13 -1.08869747e-14
 -3.29823619e-13  8.93971247e-14  2.18311227e-15 -8.17692841e-13
 -4.15197656e-13 -3.45795442e-12  1.67485115e-11 -2.44352687e-11
  2.13680892e-15  1.46360317e-12  1.90178331e-12 -4.17133327e-11
  2.89651154e-10 -1.07175872e-09  1.32403379e-09]
9.16822354513272
1 Answers

Interpretation

If you print poly.powers_, you should be able to interpret the meaning of each value.

array([[1, 0, 0],
   [0, 1, 0],
   [0, 0, 1],
   [2, 0, 0],
   [1, 1, 0],
...
   [0, 4, 2],
   [0, 3, 3],
   [0, 2, 4],
   [0, 1, 5],
   [0, 0, 6]], dtype=int64)

Each row is a feature where your variables are raised to a respective power.

For example : [2, 3, 1] means speed^2 * dimension^3 * pressure.

As a degree 6 polynomial, for any set of power [x, y, z] of your variables, this rule applies : x + y + z <= 6

As you fit them to a linear regression model, you are trying to find the coefficient of each of those features that best describe the relationship between your independant and dependant variables (leakage).

Thus, you can interpret them as such :

clf.intercept_ +
y0 * speed +  # not mentioning "* dimension^0 * pressure^0" which equals  1
y1 * dimension +
y2 * pressure +
y3 * speed^2 +
y4 * speed * dimension +
... +
y79 * dimension^4 * pressure^2 +
y80 * dimension^3 * pressure^3 +
y81 * dimension^2 * pressure^4 +
y82 * dimension * pressure^5 +
y83 * pressure^6
~= leakage

Prediction

To make use of the coefficients in the equation and predict the leakage, you can call transform and predict methods made for this purpose.

import numpy as np
x = [[1, 2, 3], [4, 5, 6]]
# the input must be a matrix of shape n_rows * 3 columns
y_pred = poly.transform(np.array(x).reshape(-1, poly.n_input_features_))  
y_pred = clf.predict(y_pred)

# timeit:
# 91.5 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Thanks to numpy's broadcasting capabilities, this is equivalent to (but faster than) the following function. IMO it helps understand what is going on under the hood.

import numpy as np
def custom_predict(x, clf, poly):
    # any number of rows, 3 columns in our case
    x = np.array(x).reshape(-1, poly.n_input_features_) 
    return np.array([
        (clf.coef_ * np.product(np.power(row, poly.powers_), axis=1)).sum() + clf.intercept_
        for row in x
    ])

y_pred = custom_predict([[1,2,3], [4, 5, 6]], clf, poly)

# timeit:
# 447 µs ± 3.74 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

As said, your input x must have the same number of features (columns) as in your original fit_transform, but you can pass as many observations (rows) as you want.

Related