python非标测试软件,如何调整标度scikitlearn逻辑回归系数来评分非标度数据集?...

必须除以应用的缩放以使特征正常化,但也要乘以应用于目标的缩放。在

假设每个特征变量x_i都被scale_x_i缩放(除以)

目标变量按比例缩放(除以)

那么orig_coef_i = coef_i_found_on_scaled_data / scale_x_i * scale_y

下面是一个使用pandas和sklearn LinearRegression的示例

^{pr2}$

这向我们展示了我们的系数线性回归没有应用标度。在# | feature| orig_coef

# 0| CRIM | -0.107171

# 1| ZN | 0.046395

# 2| INDUS | 0.020860

# etc

我们现在使所有变量正常化# Now we normalise the data

scalerX = StandardScaler().fit(X)

scalery = StandardScaler().fit(y.reshape(-1,1)) # Have to reshape to avoid warnings

normed_X = scalerX.transform(X)

normed_y = scalery.transform(y.reshape(-1,1)) # Have to reshape to avoid warnings

normed_y = normed_y.ravel() # Turn y back into a vector again

# Check it's worked

# print np.mean(X, axis=0), np.mean(y, axis=0) # Should be 0s

# print np.std(X, axis=0), np.std(y, axis=0) # Should be 1s

我们可以对这些标准化数据再次进行回归分析。。。在# Now we redo our regression

lr = LinearRegression()

lr.fit(normed_X, normed_y)

coefs2 = pd.DataFrame(

data={

'feature' : boston.feature_names,

'orig_coef' : orig_coefs,

'norm_coef' : lr.coef_,

'scaleX' : scalerX.scale_,

'scaley' : scalery.scale_[0],

},

columns=['feature', 'orig_coef', 'norm_coef', 'scaleX', 'scaley']

)

coefs2

…然后应用缩放来恢复原始系数# We can recreate our original coefficients by dividing by the

# scale of the feature (scaleX) and multiplying by the scale

# of the target (scaleY)

coefs2['rescaled_coef'] = coefs2.norm_coef / coefs2.scaleX * coefs2.scaley

coefs2

当我们这样做时,我们看到我们重新创建了原始系数。在# | feature| orig_coef| norm_coef| scaleX| scaley| rescaled_coef

# 0| CRIM | -0.107171| -0.100175| 8.588284| 9.188012| -0.107171

# 1| ZN | 0.046395| 0.117651| 23.299396| 9.188012| 0.046395

# 2| INDUS | 0.020860| 0.015560| 6.853571| 9.188012| 0.020860

# 3| CHAS | 2.688561| 0.074249| 0.253743| 9.188012| 2.688561

对于某些机器学习方法,目标变量y和特征变量x必须标准化。如果你已经这样做了,你需要包括这个“乘以y的比例”步骤以及“除以x的比例i”来获得原始回归系数。在

希望有帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值