15

When using scipy.optimize's fmin I'm getting an error I don't understand:

ValueError: setting an array element with a sequence.

Here's a simple squared error example to demonstrate:

import numpy as np
from scipy.optimize import fmin

def cost_function(theta, X, y):    
    m = X.shape[0]
    error = X.dot(theta) - y 
    J = 1/(2*m) * error.T.dot(error)  
    return J

X = np.array([[1., 1.],
              [1., 2.],
              [1., 3.],
              [1., 4.]])

y = np.array([[2],[4],[6],[8]])   
initial_theta = np.ones((X.shape[1], 1)) * 0.01

# test cost_function
print cost_function(initial_theta, X, y)
# [[ 14.800675]] seems okay...

# but then error here...   
theta = fmin(cost_function, initial_theta, args=(X, y))

#Traceback (most recent call last):
#  File "C:\Users\me\test.py", line 21, in <module>
#    theta = fmin(cost_function, initial_theta, args=(X, y))
#  File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 278, in fmin
#    fsim[0] = func(x0)
#ValueError: setting an array element with a sequence.

I'd be grateful for any help to explain where I'm going wrong.

1
  • 4
    You're returning an array for the cost function, and fmin expects a single value. Either return J[0,0] or rewrite your cost function as J = 1/(2*m) * np.sum(error**2) Commented Mar 20, 2012 at 0:43

2 Answers 2

9

The reason is that the starting point (initial_theta) you gave to fmin is not a 1D array but a 2D array. So on a second iteration fmin passes a 1D array (that's how it supposed to work) and the result becomes non-scalar.

So you should refactor your cost function to accept 1d arrays as a first argument.

The simplest change is to make the code working is to flatten the initial_theta before passing to fmin and reshape theta inside cost_function to (X.shape[1],1) if you like.

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

1 Comment

Thanks for the explanation. Yes, the number of dimensions was confusing me. So, I've now initially set both theta and y to one dim, and the cost function return value works out as a scalar without any further adjustment. Cheers.
7

cost_function should return a scalar, but your return value J is an array of some kind.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.