0
0
SciPydata~10 mins

Global optimization (differential_evolution) 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 differential_evolution function from scipy.optimize.

SciPy
from scipy.optimize import [1]

result = differential_evolution(lambda x: x**2, [(-5, 5)])
print(result.fun)
Drag options to blanks, or click blank then click option'
Aminimize
Broot
Cdifferential_evolution
Dcurve_fit
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong function like minimize or root.
Forgetting to import from scipy.optimize.
2fill in blank
medium

Complete the code to define the bounds for the variable x in the differential evolution optimizer.

SciPy
bounds = [[1]]
result = differential_evolution(lambda x: (x[0]-3)**2, bounds)
print(result.x)
Drag options to blanks, or click blank then click option'
A(-5, 5)
B(0, 1)
C(-10, 10)
D(1, 4)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple instead of a list of tuples.
Choosing bounds that do not include the minimum.
3fill in blank
hard

Fix the error in the code by completing the blank with the correct attribute to get the minimum value found.

SciPy
result = differential_evolution(lambda x: (x[0]+1)**2, [(-3, 3)])
minimum_value = result.[1]
print(minimum_value)
Drag options to blanks, or click blank then click option'
Amessage
Bsuccess
Cx
Dfun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'x' instead of 'fun' to get the minimum value.
Using 'success' or 'message' which are status indicators.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each number to its square only if the number is greater than 2.

SciPy
squares = {x: x[1]2 for x in range(1, 6) if x [2] 2}
print(squares)
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 '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word in the list to its length only if the length is greater than 3.

SciPy
lengths = { [1]: [2] for word in words if len(word) [3] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C>
Dword.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.upper()' as key instead of 'word'.
Using '<' instead of '>' in the condition.