1
""" ___ """
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)

0

1 Answer 1

5

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Have a look on the scipy.optimize.root function, not the scipy.optimize.minimize one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.