0
0
NumPydata~10 mins

Aggregation along specific axes 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 sum of all elements in the array.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
total_sum = arr.[1]()
Drag options to blanks, or click blank then click option'
Amax
Bmean
Csum
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean() instead of sum()
Using max() or min() which find extremes, not totals
2fill in blank
medium

Complete the code to calculate the mean of the array elements along axis 0 (columns).

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
col_mean = arr.[1](axis=0)
Drag options to blanks, or click blank then click option'
Asum
Bmean
Cmax
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=1 which averages rows instead of columns
Using sum() instead of mean()
3fill in blank
hard

Fix the error in the code to find the maximum value along axis 1 (rows).

NumPy
import numpy as np
arr = np.array([[5, 7, 2], [3, 8, 1]])
max_per_row = arr.[1](axis=1)
Drag options to blanks, or click blank then click option'
Amax
Bmin
Cmean
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using min() which finds smallest values
Using mean() or sum() which calculate averages or totals
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 characters.

NumPy
words = ['cat', 'elephant', 'dog', 'giraffe']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition
Using word instead of len(word) for the value
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, but only for words longer than 4 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if len(w) [3] 4 }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using w instead of w.upper() for keys
Using < instead of > in the condition
Using w instead of len(w) for values