ValueError: setting an array element with a sequence. scipy minimize [closed]

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

""" ___ """ from scipy.optimize import minimize import numpy as np   LENGTH = 100  def process(x):     return x * 2 + 5  def draw(process, length):     """ """     y = np.random.normal(0, 10, length)     data = [process(y_) for y_ in y]     rnd = np.random.normal(3, 1, len(data))     return y, rnd + data   def maximum_likelyhood(y, X):     objective = lambda b: np.transpose(X) * (y - X * b)     x0 = np.zeros(100)     res =  minimize(objective, x0=x0)     return res.x  y, X = draw(process, LENGTH) print maximum_likelyhood(y, X) 

produces a

ValueError: setting an array element with a sequence. 

There is several similar problems, they all point out that x0 is not a 1D array, but here it is a 1D array. (or not? in case please explain why and how to make it 1D)

回答1:

The error occurs because the objective function is a vector function (takes in a vector, returns a vector) but according to scipy.optimize.minimize documentation it only takes scalar function (takes a vector returns a scalar.)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!