一般优化 - 在python

时间:2016-10-02 00:05:25

标签: python numpy optimization gradient

我目前正在努力解决梯度下降实施问题,更多的是在数学方面。我有一个输入值矩阵,例如 - [[1,1,0,2],[2,3,5,1],[2,1,8,0]]。我想计算将最小化输出向量误差的权重,最小化函数是标准线性模型,因此我的假设是最小化 - > np.dot(input,weights)-y。问题是 - 权重向量的值应该添加到特定的数字,比如2.还将输出的向量归一化,例如np.dot(input,weights)/sum(np.dot(input,weights)) - 然后将该结果与期望的输出向量进行比较。我应该如何在python / numpy中定义这个任务?

人为调整程序的例子:

1)输入矩阵 [[4,0,2,0,2,0],[2,0,0,2,2,0],[2,0,0,2,2,0],[4,0,2,0,0,0],[0,0,2,0,0,2],[0,4,0,0,0,2],[0,2,0,0,0,2],[0,2,2,0,0,0],[0,0,2,0,0,2],[4,0,2,0,0,0]]

2)所需的输出[12.94275893,8.07054252,9.281123898,10.53654162,8.698251382,14.67643103,7.158870124,10.26752354,8.324615155,10.0433418]

3)以np.dot(输入,权重)/ sum(np.dot(输入,权重))正确的方式变换输入向量的权重 [11,21,18,0,20,14] - 总和为84

4)最终输出,合理地偏离2)[15.15,7.83,7.83,10.10,8.08,14.14,8.84,9.85,8.08,10.10]

1 个答案:

答案 0 :(得分:-1)

对于示例数据的比例,这是解决方案:

import numpy as np
from scipy import optimize

a = np.array([[1,1,0,2],[2,3,5,1],[2,1,8,0]], dtype=float)
target = np.random.randn(3)
target /= target.sum()

def f(p):
    p = np.r_[p, 2 - p.sum()]
    res = a.dot(p)
    res /= res.sum()
    return res - target

r, _ = optimize.leastsq(f, np.zeros(3))
print(target)
print(np.r_[r, 2 - r.sum()])

输出:

[-0.21987606  0.70869974  0.51117632]
[ 2.15713915  7.47554671  0.38959227 -8.02227813]

以下是您的真实数据的代码:

import numpy as np
from scipy import optimize

a = np.array([[4,0,2,0,2,0],
              [2,0,0,2,2,0],
              [2,0,0,2,2,0],
              [4,0,2,0,0,0],
              [0,0,2,0,0,2],
              [0,4,0,0,0,2],
              [0,2,0,0,0,2],
              [0,2,2,0,0,0],
              [0,0,2,0,0,2],
              [4,0,2,0,0,0]], dtype=float)

target = np.array([12.94275893,8.07054252,9.281123898,10.53654162,8.698251382,
                   14.67643103,7.158870124,10.26752354,8.324615155,10.0433418])

target /= target.sum()

def make_vector(x):
    return np.r_[x, 84 - x.sum()]

def calc_target(x):
    res = a.dot(make_vector(x))
    res /= res.sum()
    return res

def error(x):
    return calc_target(x) - target

x, _ = optimize.leastsq(error, np.zeros(a.shape[1] - 1))
print(make_vector(x))
print(calc_target(x) * 100)
print((calc_target(x) - target) * 100)

输出:

[  9.40552097  20.32874298  19.8199082   13.13991088  10.00062863
  11.30528834]
[ 12.90025777   8.63333209   8.63333209  10.2474406    8.25642656
  13.78390749   8.39140263  10.65003363   8.25642656  10.2474406 ]
[-0.04250116  0.56278957 -0.64779181 -0.28910102 -0.44182483 -0.89252354
  1.23253251  0.38251009 -0.0681886   0.2040988 ]

numpy.linalg.lstsq()似乎也可以解决问题,但需要将问题简化为线性效果。