This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
""" ___ """
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不是一维数组,但这里是一维数组。 (或不?以防万一,请解释为什么以及如何使其成为一维)

最佳答案

发生错误是因为目标函数是向量函数(接受向量,返回向量),但是根据scipy.optimize.minimize documentation,它仅接受标量函数(接受向量返回标量)。

关于python - ValueError:使用序列设置数组元素。 scipy最小化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13280114/

10-16 22:21