0
0
SciPydata~10 mins

Root finding (root, brentq) 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 root function from scipy.optimize.

SciPy
from scipy.optimize import [1]

result = root(lambda x: x**2 - 4, 1.0)
print(result.x)
Drag options to blanks, or click blank then click option'
Acurve_fit
Bbrentq
Cminimize
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'brentq' instead of 'root'.
Using 'minimize' which is for optimization, not root finding.
2fill in blank
medium

Complete the code to find the root of the function f(x) = x^3 - 1 using root with initial guess 0.5.

SciPy
from scipy.optimize import root

def f(x):
    return x**3 - 1

sol = root(f, [1])
print(sol.x)
Drag options to blanks, or click blank then click option'
A0.5
B2.0
C-1.0
D1.0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1.0 as initial guess instead of 0.5.
Using negative initial guess which is not required here.
3fill in blank
hard

Fix the error in the code to correctly find the root of f(x) = cos(x) - x using brentq between 0 and 1.

SciPy
from scipy.optimize import brentq
import numpy as np

def f(x):
    return np.cos(x) - x

root_value = brentq(f, [1], 1)
print(root_value)
Drag options to blanks, or click blank then click option'
A0
B1
C-1
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the start of the interval which is the end.
Using negative values outside the valid interval.
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
words = ['data', 'science', 'ai', 'ml']
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
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'len(word)' for the value.
Using 'word > 3' which compares string to number.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

SciPy
words = ['cat', 'dog', 'a', 'elephant']
result = {{ [1]: [2] for w in words if [3] }}
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 2
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' instead of 'w.upper()' for keys.
Not filtering words by length.