函数返回一个向量,如何在via NumPy中最小化

时间:2014-08-08 10:34:23

标签: python numpy scipy mathematical-optimization

我试图最小化函数,它返回一个值向量, 这是一个错误:

  

使用序列设置数组元素

代码:

P = np.matrix([[0.3, 0.1, 0.2], [0.01, 0.4, 0.2], [0.0001, 0.3, 0.5]])  
Ps = np.array([10,14,5])

def objective(x):   
    x = np.array([x])
    res = np.square(Ps - np.dot(x, P)) 
    return res 

def main():
    x = np.array([10, 11, 15])
    print minimize(objective, x, method='Nelder-Mead')

在P,Ps的这些值,x函数返回[[47.45143225 16.81 44.89]]

感谢您的任何建议

UPD(完全追溯)

    Traceback (most recent call last):

  File "<ipython-input-125-9649a65940b0>", line 1, in <module>
    runfile('C:/Users/Roark/Documents/Python Scripts/optimize.py', wdir='C:/Users/Roark/Documents/Python Scripts')

  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 585, in runfile
    execfile(filename, namespace)

  File "C:/Users/Roark/Documents/Python Scripts/optimize.py", line 28, in <module>
    main()

  File "C:/Users/Roark/Documents/Python Scripts/optimize.py", line 24, in main
    print minimize(objective, x, method='Nelder-Mead')

  File "C:\Anaconda\lib\site-packages\scipy\optimize\_minimize.py", line 413, in minimize
    return _minimize_neldermead(fun, x0, args, callback, **options)

  File "C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py", line 438, in _minimize_neldermead
    fsim[0] = func(x0)

ValueError: setting an array element with a sequence.

UPD2:应该最小化函数(Ps是向量)

enter image description here

3 个答案:

答案 0 :(得分:3)

如果您希望结果向量是仅包含0的向量,则可以使用fsolve来执行此操作。要做到这一点,需要稍微修改目标函数,以使输入和输出成为相同的形状:

import scipy.optimize as so
P = np.matrix([[0.3, 0.1, 0.2], [0.01, 0.4, 0.2], [0.0001, 0.3, 0.5]])  
Ps = np.array([10,14,5])

def objective(x):   
    x = np.array([x])
    res = np.square(Ps - np.dot(x, P)) 
    return np.array(res).ravel() 
Root = so.fsolve(objective, x0=np.array([10, 11, 15]))
objective(Root)
#[  5.04870979e-29   1.13595970e-28   1.26217745e-29]

结果:解决方案为np.array([ 31.95419775, 41.56815698, -19.40894189])

答案 1 :(得分:2)

您的目标函数需要返回标量值,而不是向量。你可能想要返回平方误差的而不是平方误差向量:

def objective(x):
    res = ((Ps - np.dot(x, P)) ** 2).sum()
    return res 

答案 2 :(得分:0)

使用least_squares。这将需要稍微修改目标以返回差异而不是平方差异:

import numpy as np
from scipy.optimize import least_squares

P = np.matrix([[0.3, 0.1, 0.2], [0.01, 0.4, 0.2], [0.0001, 0.3, 0.5]])  
Ps = np.array([10,14,5])

def objective(x):
    x = np.array([x])
    res = Ps - np.dot(x, P)
    return np.asarray(res).flatten()

def main():
    x = np.array([10, 11, 15])
    print(least_squares(objective, x))

结果:

 active_mask: array([0., 0., 0.])
        cost: 5.458917464129402e-28
         fun: array([1.59872116e-14, 2.84217094e-14, 5.32907052e-15])
        grad: array([-8.70414856e-15, -1.25943700e-14, -1.11926469e-14])
         jac: array([[-3.00000002e-01, -1.00000007e-02, -1.00003682e-04],
       [-1.00000001e-01, -3.99999999e-01, -3.00000001e-01],
       [-1.99999998e-01, -1.99999999e-01, -5.00000000e-01]])
     message: '`gtol` termination condition is satisfied.'
        nfev: 4
        njev: 4
  optimality: 1.2594369966691647e-14
      status: 1
     success: True
           x: array([ 31.95419775,  41.56815698, -19.40894189])