【问题标题】:Extra coefficient in Ridge Regression岭回归中的额外系数
【发布时间】:2022-11-25 00:27:24
【问题描述】:

我有 9 个预测变量(干净的 df),但是当我运行模型时,我得到 10 个系数。 这是我的代码:

#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)

但是当我检查系数时,我得到的是 10。

 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])

有人知道为什么会这样吗?

【问题讨论】:

    标签: python scikit-learn model regression


    【解决方案1】:

    从我的头顶来看,您的预测变量可能有 9 个权重和一个作为整个模型偏差或偏移量的常数(我相信它被恰当地称为截距,与其他模型或回归一样)。

    【讨论】:

    • 您好,感谢您的回答,常量保存在另一个变量上:Rigemodel_cd.intercept_
    【解决方案2】:

    PolynomialFeatures默认有include_bias=True,它增加了一列全1。请注意,第一个系数恰好为零,因为 Ridge 已取消该项以支持其自身的截距。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 2016-10-21
      • 2012-10-07
      • 2018-08-31
      • 2018-02-02
      • 2020-11-04
      • 1970-01-01
      • 2013-08-12
      相关资源
      最近更新 更多