0
0
SciPydata~10 mins

Constrained optimization 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 function for constrained optimization from scipy.optimize.

SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Aminimize
Bcurve_fit
Clinprog
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'curve_fit' which is for curve fitting, not optimization.
Choosing 'linprog' which is for linear programming only.
Choosing 'root' which finds roots of equations, not optimization.
2fill in blank
medium

Complete the code to define the objective function to minimize: f(x) = (x - 3)^2.

SciPy
def objective(x):
    return (x[1]3)**2
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' changes the function shape.
Using '*' or '/' does not represent the intended function.
3fill in blank
hard

Fix the error in the constraint definition: x must be greater than or equal to 1.

SciPy
constraint = {'type': 'ineq', 'fun': lambda x: x[1] 1}
Drag options to blanks, or click blank then click option'
A>=
B+
C<
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' inside the lambda returns a boolean instead of a numeric value.
Using '<' reverses the constraint direction.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that squares numbers greater than 2 from a list.

SciPy
squares = {x: x[1]2 for x in numbers if x [2] 2}
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' for squaring.
Using '<' instead of '>' changes the filter condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of variable names in uppercase as keys, their values, and filter values greater than 0.

SciPy
result = [1]: [2] for [3], [2] in data.items() if [2] > 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item' instead of 'k' and 'v' in the loop.
Not converting keys to uppercase.
Using wrong variable names causing errors.