使用SkLearning线性回归,我如何才能将计算出的回归系数限制为大于0? [英] Using sklearn linear regression, how can I constrain the calculated regression coefficients to be greater than 0?

查看:34
本文介绍了使用SkLearning线性回归,我如何才能将计算出的回归系数限制为大于0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里使用skLearning的引用http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html,但是没有约束回归系数的选项。

有没有人知道python中还有另一个包可以执行多变量线性回归,并将回归系数约束为大于0?

这是我到目前为止拥有的代码。

'''data:
date        A            B              C
10/30/2015  0.063363323 -0.005218807    0.079777558
11/30/2015  -0.013171244    -0.008727599    0.010352028
12/31/2015  -0.017551268    8.09E-05    -0.020491923
1/29/2016   -0.042606469    0.052272139 -0.080362246
2/29/2016   -0.015224562    0.031250961 0.029988488
3/31/2016   0.058291876 -0.000238614    0.056727336
4/29/2016   0.000505675 -0.005325338    0.02854057
5/31/2016   0.012766515 0.008548162 -0.001631845
6/30/2016   -0.038981203    0.064236963 0.00570145
7/29/2016   0.033715429 0.024269606 0.02703294
8/31/2016   -0.002083837    -0.009439625    0.004129397
9/30/2016   -0.009825674    -0.01737909 -0.019251885
11/30/2016  0.0084733   -0.11668582 0.031928726
12/30/2016  0.017084282 -0.005553088    0.029372131
1/31/2017   0.014263947 0.004036504 0.00187079
2/28/2017   0.037375566 0.016081105 0.039331615
3/31/2017   -0.002494984    -0.005942793    -0.002097504
4/28/2017   -0.005054922    0.015685226 0.008243977
5/31/2017   0.002285393 0.020771375 0.002697755
6/30/2017   0.002841457 0.004886117 0.019202011
7/31/2017   0.014866638 -0.006900926    0.010126577
8/31/2017   -0.016647997    0.035687133 -0.008709075
9/29/2017   0.019523651 -0.022154361    0.020468398
10/31/2017  0.019407629 -0.000705663    0.016574416
11/30/2017  0.027486425 0.008008173 0.033427299
12/29/2017  0.007861222 0.018095096 0.017908809
1/31/2018   0.058702838 -0.032765285    0.05
'''

reg = linear_model.LinearRegression(fit_intercept=False)
reg.fit(df[['B', 'C']], df['A'])

print(reg.coef_)

# [ 0.67761268 -0.08845756]

下面的工作代码

from scipy.optimize import lsq_linear   

lb = 0
ub = np.Inf
res = lsq_linear(df[['B', 'C']], 
                 df['A'], 
                 bounds=(lb, ub))

print(res.x)

推荐答案

SkLearning仅包装不支持此功能的Scipy的lstsq

您可以轻松修改skLearning的code

    if sp.issparse(X):
        if y.ndim < 2:
            out = sparse_lsqr(X, y)
            self.coef_ = out[0]
            self._residues = out[3]
        else:
            # sparse_lstsq cannot handle y with shape (M, K)
            outs = Parallel(n_jobs=n_jobs_)(
                delayed(sparse_lsqr)(X, y[:, j].ravel())
                for j in range(y.shape[1]))
            self.coef_ = np.vstack(out[0] for out in outs)
            self._residues = np.vstack(out[3] for out in outs)
    else:
        self.coef_, self._residues, self.rank_, self.singular_ = 
            linalg.lstsq(X, y)
        self.coef_ = self.coef_.T
只需将lstsq/lsqr替换为Scipy的nnls(密集!)或手动设置边界的lsq_linear(对于大规模:Optimize.Minimize with Method lbfgs是另一种候选方法,尽管您需要准备渐变,并且至少有两种不同的常用方法:例如,使用预计算:A.T*A,这将失去稀疏性)。

备注:这些方法都是最小化不同的函数(范数vs.平方范数;0.5因子vs.1.0因子)。这不会根据找到的向量更改结果,但目标当然看起来不同,您应该注意这一点(如果需要)。

这篇关于使用SkLearning线性回归,我如何才能将计算出的回归系数限制为大于0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆