""" ___ """
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不是一维数组,而这里是一维数组。(或者不是?如果请解释为什么以及如何使其成为 1D)