Complete the code to import the basin-hopping optimizer from scipy.
from scipy.optimize import [1]
The basinhopping function is the correct optimizer for global minima using basin-hopping.
Complete the code to define a simple quadratic function to minimize.
def func(x): return (x - [1])**2 + 1
The function has its minimum at x=2 because it is (x-2)**2 + 1.
Fix the error in the code to run basin-hopping on the function starting at x=0.
from scipy.optimize import basinhopping result = basinhopping(func, [1]) print(result.x)
The starting point must be an array-like, so [0] is correct.
Fill both blanks to create a callback function that prints the current minimum value and coordinates.
def print_callback(x, f, accept): print(f"Current minimum value: {f}, at position: {x[1]") result = basinhopping(func, [0], callback=[2])
str instead of .tolist() for printing.The callback function is named print_callback, and x.tolist() converts the array to a list for nicer printing.
Fill all three blanks to create a dictionary comprehension that maps each x in range(5) to its basin-hopping minimum starting at x.
results = [1]: basinhopping(func, [x]).x[0] for x in range([2]) if x [3] 3 print(results)
The dictionary comprehension starts with {x, loops over range(5), and filters with x < 3.