0
0
SciPydata~10 mins

Least squares 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 least squares function from scipy.optimize.

SciPy
from scipy.optimize import [1]
Drag options to blanks, or click blank then click option'
Aleast_squares
Bcurve_fit
Cminimize
Dlinprog
Attempts:
3 left
💡 Hint
Common Mistakes
Importing minimize instead of least_squares.
Confusing curve_fit with least squares function.
2fill in blank
medium

Complete the code to define the residuals function for least squares optimization.

SciPy
def residuals(params, x, y):
    return y - [1]
Drag options to blanks, or click blank then click option'
Aparams / x
Bparams + x
Cparams * x
Dparams - x
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or division instead of multiplication for prediction.
Subtracting x from params instead of multiplying.
3fill in blank
hard

Fix the error in the least squares call to correctly pass the residuals function and data.

SciPy
result = least_squares([1], 1.0, args=(x, y))
Drag options to blanks, or click blank then click option'
Aresiduals(params, x, y)
Bresiduals
Cresiduals(params)
Dresiduals(x, y)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the function instead of passing it.
Passing wrong number of arguments.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

SciPy
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dlen(words) > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word > 3 which compares string to number.
Using len(words) > 3 which checks length of the whole list.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their values only if the value is positive.

SciPy
result = [1]: [2] for [3] in data.items() if [2] > 0
Drag options to blanks, or click blank then click option'
Aword.upper()
Bvalue
Cword, value
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of word for iteration variable.
Not filtering values greater than zero.