问题
I'm trying to minimize function, that returns a vector of values, and here is an error:
setting an array element with a sequence
Code:
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')
At these values of P, Ps, x function returns [[ 47.45143225 16.81 44.89 ]]
Thank you for any advice
UPD (full traceback)
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: function should be minimized (Ps is a vector)
回答1:
If you want you resulting vector to be a vector containing only 0
s, you can use fsolve
to do so. To do that will require modifying your objective function a little bit to get the input and output into the same shape:
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]
Result: The solution is np.array([ 31.95419775, 41.56815698, -19.40894189])
回答2:
Your objective function needs to return a scalar value, not a vector. You probably want to return the sum of squared errors rather than the vector of squared errors:
def objective(x):
res = ((Ps - np.dot(x, P)) ** 2).sum()
return res
回答3:
Use least_squares. This will require to modify the objective a bit to return differences instead of squared differences:
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))
Result:
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])
来源:https://stackoverflow.com/questions/25201504/function-returns-a-vector-how-to-minimize-in-via-numpy