Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the optimization function from scipy.
SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'optimize' which is the module name, not a function.
Using 'solve' which is unrelated here.
Using 'find' which is not a scipy function.
✗ Incorrect
The minimize function is the main tool in scipy to find the best (minimum) solution of a function.
2fill in blank
mediumComplete the code to define the function to minimize.
SciPy
def f(x): return (x - [1])**2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 0 which shifts the minimum to zero.
Choosing 5 which moves the minimum too far.
Choosing -1 which is negative and changes the minimum location.
✗ Incorrect
The function f(x) = (x - 2)^2 has its minimum at x=2.
3fill in blank
hardFix the error in the optimization call by completing the missing argument.
SciPy
result = minimize(f, [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer instead of a list.
Using a tuple which sometimes works but list is preferred.
Using a set which is unordered and invalid here.
✗ Incorrect
The initial guess must be an array-like object, so [2] is correct.
4fill in blank
hardFill both blanks to print the best solution and its function value.
SciPy
print('Best x:', result.[1]) print('Minimum value:', result.[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'solution' which is not an attribute.
Using 'value' which is not an attribute.
Confusing 'x' and 'fun' attributes.
✗ Incorrect
The attribute x holds the best solution, and fun holds the minimum function value.
5fill in blank
hardFill all three blanks to create a dictionary of squares for numbers greater than 3.
SciPy
squares = {word:word[1]2 for word in range(1, 6) if word [2] 3 and word [3] 4} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '**' for power.
Using '<' instead of '>' for the condition.
Using '==' instead of '!=' to exclude 4.
✗ Incorrect
The code creates squares of numbers greater than 3 and not equal to 4.