Python Forum
cannot import scipy.optimize.Bounds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cannot import scipy.optimize.Bounds
#1
I used this:
from scipy import optimize
then it is ok to use optimize.minimize
but cannot use optimize.Bounds, cannot use optimize.LinearConstraint, saying no this module
then I tried
from scipy.optimize import Bounds
from scipy.optimize import LinearConstraint

it will report error

so the question is: I don't know how to import the Bounds and Linear Constraint module....

Please help out, thanks!
Reply
#2
I wrote a very simple program to test the optimize of scipy, it all works for unconstrained optimizations, but when it comes to constrained optimization, the Bounds, LinearConstraint module will be needed, but I cannot import any of them. the program is as shown below:
from scipy import optimize
# from scipy.optimize import minimize
# from scipy.optimize import Bounds
# from scipy.optimize import LinearConstraint
import numpy

def f(x):
	return 0.5*(1-x[0])**2+(x[1]-x[0]**2)**2

bounds = Bounds([0, -0.5], [1.0, 2.0])

result=optimize.minimize(f, [0.5, -0.5], method='BFGS', bounds=bounds)
print(result.x)
the error says:

File "testopt.py", line 13, in <module>
    bounds = Bounds([0, -0.5], [1.0, 2.0])
NameError: name 'Bounds' is not defined
I also tried to import them one by one:
from scipy.optimize import minimize
from scipy.optimize import Bounds
from scipy.optimize import LinearConstraint
but, the error becomes:
Traceback (most recent call last):
  File "testopt.py", line 3, in <module>
    from scipy.optimize import Bounds
ImportError: cannot import name 'Bounds'
so no matter what, Bounds and LinearConstraint will never be found. I am wondering what kind of problem will this be, if the installation got problems, the minimize function should not work either.

Please help out, this already hindered me for weeks.
Reply
#3
Problem solved, looks like I just need to upgrade scipy from 0.19 to 1.1 or above.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,067 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  How can I multithread to optimize a groupby task: davisc4468 0 700 Jun-30-2023, 02:45 PM
Last Post: davisc4468
  do you have an idea to optimize this code[recursion]]? netanelst 4 1,281 May-20-2022, 06:41 PM
Last Post: jefsummers
  For loop index out of bounds armitron121 2 2,658 Feb-08-2022, 04:23 PM
Last Post: armitron121
  Optimization using scipy.optimize KaneBilliot 3 1,893 Nov-30-2021, 08:03 AM
Last Post: Gribouillis
  Using curve_fit to optimize function (TypeError) Laplace12 4 2,490 Aug-30-2021, 11:15 AM
Last Post: Larz60+
  IndexError: index 10 is out of bounds for axis 0 with size 1 vahid89 1 12,223 Jan-07-2021, 06:19 PM
Last Post: deanhystad
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 5,004 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  ModuleNotFoundError: No module named 'scipy.optimize'; 'scipy' is not a package AaronKR 1 10,212 Jul-09-2020, 02:36 AM
Last Post: bowlofred
  String index out of bounds ( Python : Dict ) kommu 2 2,383 Jun-25-2020, 08:52 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020