julian lagier
julian lagier

Reputation: 60

Extra coefficient in Ridge Regression

I have 9 predictors (Clean df) but when I run the model I get 10 coefficients. Here is my code:

#Get clean df with only more relevant columns
Clean_indices = wkospi[['Open_sp','Close_sp','Close_jp','Open_eur','High_eur','Open_kos','Close_kos','1 Mo','2 Mo','1 Yr','2 Yr','Open_oil','Open_gold']]
Clean_df = wkospi[['Close_jp','Open_sp','Open_eur','High_eur','Close_kos','3 Mo','6 Mo', '1 Yr', '2 Yr']]

#Run the test for Clean df ALIAS "cd"
cdx_train, cdx_test, cdy_train, cdy_test = train_test_split(Clean_df, Clean_indices['Close_sp'] , test_size=0.6, random_state = 4, shuffle = True)
#Prepare train data and test data as polynomials
cpr=PolynomialFeatures(degree=1)
cdp_train=cpr.fit_transform(cdx_train)
cdp_test=cpr.fit_transform(cdx_test)
RigeModel_cd=Ridge(alpha = 1000)
RigeModel_cd.fit(cdp_train, cdy_train)
yhat_cd = RigeModel_cd.predict(cdp_test)

But when I check the coefficients I get 10 instead.

 in>> RigeModel_cd.coef_ 
 out>> array([ 0.00000000e+00,  4.66393448e-03,  9.60826030e-01, -8.18000961e-01,
            8.78056763e-01, -9.08744162e-05, -3.30052619e-01, -4.24748286e-01,
           -5.42880494e-01, -6.49848520e-01])

Does anybody know why this is happening?

Upvotes: 0

Views: 260

Answers (2)

Ben Reiniger
Ben Reiniger

Reputation: 12582

PolynomialFeatures has include_bias=True by default, which adds a column of all 1s. Note that the first coefficient is exactly zero, because Ridge has killed that term in favor of its own intercept.

Upvotes: 2

Waaazzzuuuuup
Waaazzzuuuuup

Reputation: 26

From the top of my head, there are probably 9 weigths for your predictors and one constant as a whole model bias or offset (i believe it is properly called intercept, as in other models or regressions).

Upvotes: 0

Related Questions