0
0
SciPydata~10 mins

Basin-hopping for global minima in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the basin-hopping optimizer from scipy.

SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Acurve_fit
Bminimize
Croot
Dbasinhopping
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'minimize' which is for local optimization.
Choosing 'root' which is for solving equations.
2fill in blank
medium

Complete the code to define a simple quadratic function to minimize.

SciPy
def func(x):
    return (x - [1])**2 + 1
Drag options to blanks, or click blank then click option'
A-3
B0
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 0 or other values that do not match the minimum location.
3fill in blank
hard

Fix the error in the code to run basin-hopping on the function starting at x=0.

SciPy
from scipy.optimize import basinhopping

result = basinhopping(func, [1])
print(result.x)
Drag options to blanks, or click blank then click option'
A[0]
B0
C(0,)
D{0}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a scalar instead of a list or array.
Using a tuple or set which are not accepted here.
4fill in blank
hard

Fill both blanks to create a callback function that prints the current minimum value and coordinates.

SciPy
def print_callback(x, f, accept):
    print(f"Current minimum value: {f}, at position: {x[1]")

result = basinhopping(func, [0], callback=[2])
Drag options to blanks, or click blank then click option'
A.tolist()
Bprint_callback
Cstr
Dlambda
Attempts:
3 left
💡 Hint
Common Mistakes
Using str instead of .tolist() for printing.
Passing a lambda instead of the named callback function.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each x in range(5) to its basin-hopping minimum starting at x.

SciPy
results = [1]: basinhopping(func, [x]).x[0] for x in range([2]) if x [3] 3
print(results)
Drag options to blanks, or click blank then click option'
A{x
B5
C<
D[x
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of curly braces for dictionary.
Using wrong range or comparison operator.