Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is coef_ from sklearn LinearRegression? and how to interpret a formula from it

When I use LinearRegression in sklearn, I would do

m = 100
X = 6*np.random.rand(m,1)-3
y = 0.5*X**2 + X+2 + np.random.randn(m,1)
lin_reg = LinearRegression()
lin_reg.fit(X,y)
y_pred_1 = lin_reg.predict(X)
y_pred_1 = [_[0] for _ in y_pred_1]

and when I plot (X,y) and (X, y_pred_1) it seems to be correct.

I wanted create formula for line of best fit by:

y= (lin_reg.coef_)x + lin_reg.intercept_

Manually I've inserted values into formula I've got by using coef_, intercept_ and compared it to predicted value from lin_reg.predict(value) which are the same so lin_reg.predict in fact uses formula I've made above using coef, intercept.

My problem is how to I create a formula for simple polynomial regression?

I would do

poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly_2 = poly_features.fit_transform(X)

poly_reg_2 = LinearRegression()
poly_reg_2.fit(X_poly_2, y)

then poly_reg_2.coef_ gives me array([[0.93189329, 0.43283304]]) and poly_reg_2.intercept_ = array([2.20637695]).

Since it is "simple" polynomial regression it should look something like

y = x^2 + x + b where x are same variable.

from poly_reg_2.coef_ which one is x^2 and which one is not?

like image 819
haneulkim Avatar asked Oct 12 '25 13:10

haneulkim


1 Answers

Thanks to https://www.youtube.com/watch?v=Hwj_9wMXDVo I've gotten insight and found out how to interpret formula for polynomial regression.

So poly_reg_2.coef_ = array([[0.93189329, 0.43283304]])

you know simple linear regression looks like
y = b + m1x

Then 2-degree polynomial regression looks like
y = b + m1x + m2(x^2)

and 3-degree:
y = b + m1x + m2(x^2) + m3(x^3)

and so on... so for my case two coefficients are just m1 and m2 in according order.

so finally my formula becomes:

y = b + 0.93189329x + 0.43283304(x^2).

like image 193
haneulkim Avatar answered Oct 14 '25 22:10

haneulkim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!