0
0
NumPydata~10 mins

np.abs() for absolute values 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 calculate the absolute values of the array elements.

NumPy
import numpy as np
arr = np.array([-1, -2, 3, -4])
abs_arr = np.[1](arr)
print(abs_arr)
Drag options to blanks, or click blank then click option'
Aabs
Babsval
Cabsolute
Dabs_values
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absval' or 'abs_values' which do not exist.
2fill in blank
medium

Complete the code to find the absolute values of a 2D numpy array.

NumPy
import numpy as np
arr = np.array([[-1, 2], [-3, 4]])
result = np.[1](arr)
print(result)
Drag options to blanks, or click blank then click option'
Aabsval
Babs
Cabs_values
Dabsolute
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'absval' or 'abs_values' which are not valid numpy functions.
3fill in blank
hard

Fix the error in the code to correctly compute absolute values of the array.

NumPy
import numpy as np
arr = np.array([-5, 6, -7])
abs_values = np.[1](arr)
print(abs_values)
Drag options to blanks, or click blank then click option'
Aabsolute
Babsval
Cabs_values
Dabs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absval' or 'abs_values' which are not numpy functions.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their absolute length if length is greater than 3.

NumPy
words = ['data', 'ai', 'science', 'ml']
lengths = {word: [1] for word in words if [2] > 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'len(word)' for length.
Not filtering words by length correctly.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and absolute length values for words longer than 2.

NumPy
import numpy as np
words = ['data', 'ai', 'science', 'ml']
result = { [1]: np.[2](len([3])) for word in words if len(word) > 2}
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Babsolute
Cword
Dabs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'word.upper()' for keys.