ValueError:设置具有序列的数组元素.最小化 [英] ValueError: setting an array element with a sequence. scipy minimize
本文介绍了ValueError:设置具有序列的数组元素.最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
""" ___ """
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)
产生一个
ValueError: setting an array element with a sequence.
有几个类似的问题,它们都指出x0不是一维数组,但这里是一维数组. (或者不是?以防万一,请解释为什么以及如何制作一维)
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)
推荐答案
发生错误是因为目标函数是矢量函数(采用矢量,返回矢量),但根据
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.)
这篇关于ValueError:设置具有序列的数组元素.最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文