Python Forum
cannot import scipy.optimize.Bounds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cannot import scipy.optimize.Bounds
#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


Messages In This Thread
RE: cannot import scipy.optimize.Bounds - by larkypython - May-05-2019, 02:48 AM

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,309 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  How can I multithread to optimize a groupby task: davisc4468 0 752 Jun-30-2023, 02:45 PM
Last Post: davisc4468
  do you have an idea to optimize this code[recursion]]? netanelst 4 1,350 May-20-2022, 06:41 PM
Last Post: jefsummers
  For loop index out of bounds armitron121 2 2,729 Feb-08-2022, 04:23 PM
Last Post: armitron121
  Optimization using scipy.optimize KaneBilliot 3 1,951 Nov-30-2021, 08:03 AM
Last Post: Gribouillis
  Using curve_fit to optimize function (TypeError) Laplace12 4 2,589 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,347 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,072 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  ModuleNotFoundError: No module named 'scipy.optimize'; 'scipy' is not a package AaronKR 1 10,387 Jul-09-2020, 02:36 AM
Last Post: bowlofred
  String index out of bounds ( Python : Dict ) kommu 2 2,438 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