0
0
NumPydata~10 mins

Universal functions (ufuncs) in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a numpy array and apply the square root ufunc.

NumPy
import numpy as np
arr = np.array([1, 4, 9, 16])
sqrt_arr = np.[1](arr)
print(sqrt_arr)
Drag options to blanks, or click blank then click option'
Aexp
Bsqrt
Clog
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'square' instead of 'sqrt' which squares the numbers instead of square rooting.
Using 'log' or 'exp' which perform different mathematical operations.
2fill in blank
medium

Complete the code to apply the numpy ufunc that calculates the exponential of each element.

NumPy
import numpy as np
arr = np.array([0, 1, 2])
exp_arr = np.[1](arr)
print(exp_arr)
Drag options to blanks, or click blank then click option'
Alog
Bsqrt
Cabs
Dexp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'log' which calculates the logarithm, not the exponential.
Using 'sqrt' which calculates square roots.
3fill in blank
hard

Fix the error in the code by choosing the correct numpy ufunc to calculate the absolute values.

NumPy
import numpy as np
arr = np.array([-1, -2, 3])
abs_arr = np.[1](arr)
print(abs_arr)
Drag options to blanks, or click blank then click option'
Aabs
Bfabs
Cabsolute
Dsign
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'abs', which is an alias but the full ufunc name is 'absolute'.
Using 'sign' which returns the sign of each element, not the absolute value.
Using 'fabs' which is specifically for floating-point absolute value.
4fill in blank
hard

Complete the code to apply the numpy ufunc that calculates the base-10 logarithm of each element.

NumPy
import numpy as np
arr = np.array([1, 10, 100])
log_arr = np.[1](arr)
print(log_arr)
Drag options to blanks, or click blank then click option'
Alog
Bsqrt
Clog10
Dexp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'log' which computes the natural logarithm (base e).
Using 'exp' or 'sqrt' which perform different operations.
5fill in blank
hard

Complete the code to apply the floor ufunc, which rounds each element down to the nearest integer.

NumPy
import numpy as np
arr = np.array([0.7, 1.5, -1.8, -0.1])
floor_arr = np.[1](arr)
print(floor_arr)
Drag options to blanks, or click blank then click option'
Afloor
Bceil
Crint
Dfix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ceil' which rounds up to the nearest integer.
Using 'fix' which rounds towards zero.