Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:55:09+00:00 2026-05-31T18:55:09+00:00

When using scipy.optimize ‘s fmin I’m getting an error I don’t understand: ValueError: setting

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:55:11+00:00Added an answer on May 31, 2026 at 6:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do a fit to a given function using Scipy. Scipy.optimize.leastsq
What does the following error: Warning: overflow encountered in exp in scipy/numpy using Python
I am using scipy.optimize.curve_fit() in an iterative way. My problem is that when ever
I am using Scipy to construct a large, sparse (250k X 250k) co-occurrence matrix
I am using scipy-cluster in my application. It provides a function to plot a
I have been using the scipy.stats.invgamma.rvs function to randomly select values from an inverse
i am using this: http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html i have an list a that i want to
When testing scipy using the nose package using scipy.test() , the test fails under
I am currently using scipy's linregress function for single regression. I am unable to
I'm trying to compute the laplacian of a 2d field A using scipy.ndimage.convolve .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.