ValueError:对象太深,不适合所需的数组

时间:2012-11-08 16:52:01

标签: python numpy scipy

""" ___ """
from scipy.optimize import root
import numpy as np


LENGTH = 3


def process(x):
    return x[0, 0] + x[0, 1] * 5


def draw(process, length):
    """ """
    X = np.matrix(np.random.normal(0, 10, (length, 2)))
    y = np.matrix([process(x) for x in X])
    y += np.random.normal(3, 1, len(y))
    return y.T, X.T


def maximum_likelyhood(y, X):
    def objective(b):
        return (X.T * (y - X * b.T))
    x0 = np.matrix([0, 0])
    res = root(objective, x0=x0)
    return res.x

y, X = draw(process, LENGTH)
X = X.transpose()
b = np.matrix([[0], [1]])
print maximum_likelyhood(y, X)

产生

  Traceback (most recent call last):
File "ml.py", line 33, in <module>
  maximum_likelyhood(y, X)
File "ml.py", line 26, in maximum_likelyhood
  res = root(objective, x0=x0)
File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/_root.py", line 168, in root
  sol = _root_hybr(fun, x0, args=args, jac=jac, **options)
File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 193, in    _root_hybr
ml, mu, epsfcn, factor, diag)

ValueError: object too deep for desired array 

我甚至无法解释问题是在进入目标的b中的问题 功能?还是在输出中?

1 个答案:

答案 0 :(得分:11)

问题是fsolve和root不接受矩阵作为目标函数的返回值。

例如,这是上述问题的解决方案:

def maximum_likelyhood(y, X):
    def objective(b):
        b = np.matrix(b).T
        return np.transpose(np.array((X.T * (y - X * b))))[0]
    x0 = (1, 1)
    res = root(objective, x0=x0)
    return res.x