如何在Scipy线性规划中添加正则化(非负最小二乘)

时间:2016-02-16 02:24:34

标签: python numpy scipy linear-algebra

以下是我使用Scipy's NNLS的LP代码:

angular.module('test', []);
  angular.module('test').controller('DateCtrl', function($scope, $timeout) {
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; // + 1 Because the month starts to 0!
    var yyyy = today.getFullYear();

    if (dd < 10) {
       dd = '0' + dd
    }

    if (mm < 10) {
      mm = '0' + mm
    }

    $scope.startDate = today = mm + '/' + dd + '/' + yyyy;
 });

用法:

import numpy as np
from numpy import array
from scipy.optimize import nnls

def by_nnls(A=None, B=None):
    """ Linear programming by NNLS """
    #print "NOF row = ", A.shape[0]
    A = np.nan_to_num(A)
    B = np.nan_to_num(B)

    x, rnorm = nnls(A,B)
    x = x / x.sum()
    # print repr(x)
    return x

B1 = array([  22.133,  197.087,   84.344,    1.466,    3.974,    0.435,
          8.291,   45.059,    5.755,    0.519,    0.   ,   30.272,
         24.92 ,   10.095])
A1 = array([[   46.35,    80.58,    48.8 ,    80.31,   489.01,    40.98,
           29.98,    44.3 ,  5882.96],
       [ 2540.73,    49.53,    26.78,    30.49,    48.51,    20.88,
           19.92,    21.05,    19.39],
       [ 2540.73,    49.53,    26.78,    30.49,    48.51,    20.88,
           19.92,    21.05,    19.39],
       [   30.95,  1482.24,   100.48,    35.98,    35.1 ,    38.65,
           31.57,    87.38,    33.39],
       [   30.95,  1482.24,   100.48,    35.98,    35.1 ,    38.65,
           31.57,    87.38,    33.39],
       [   30.95,  1482.24,   100.48,    35.98,    35.1 ,    38.65,
           31.57,    87.38,    33.39],
       [   15.99,   223.27,   655.79,  1978.2 ,    18.21,    20.51,
           19.  ,    16.19,    15.91],
       [   15.99,   223.27,   655.79,  1978.2 ,    18.21,    20.51,
           19.  ,    16.19,    15.91],
       [   16.49,    20.56,    19.08,    18.65,  4568.97,    20.7 ,
           17.4 ,    17.62,    25.51],
       [   33.84,    26.58,    18.69,    40.88,    19.17,  5247.84,
           29.39,    25.55,    18.9 ],
       [   42.66,    83.59,    99.58,    52.11,    46.84,    64.93,
           43.8 ,  7610.12,    47.13],
       [   42.66,    83.59,    99.58,    52.11,    46.84,    64.93,
           43.8 ,  7610.12,    47.13],
       [   41.63,   204.32,  4170.37,    86.95,    49.92,    87.15,
           51.88,    45.38,    42.89],
       [   81.34,    60.16,   357.92,    43.48,    36.92,    39.13,
         1772.07,    68.43,    38.07]])

我的问题是如何在LP系统中添加regularization因子? 除了使用Scipy之外,我愿意接受解决方案。

1 个答案:

答案 0 :(得分:3)

你可以通过用包含每个变量的权重的平方根的对角矩阵扩展A矩阵并向你的{{1}添加零来表达正则化(假设典型的,对角线Tikhonov)最小二乘问题。矢量。

b

尝试将新的费用函数扩展为总和,您会发现它与在其中添加lamb = 1 n_variables = A1.shape[1] A2 = concatenate([A1, sqrt(lamb)*eye(n_variables)]) B2 = concatenate([B1, zeros(n_variables)]) by_nnls(A=A2, B=B2) 字词完全相同。