0
0
NumPydata~10 mins

Counting with boolean arrays 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 count how many values in the array are True.

NumPy
import numpy as np
arr = np.array([True, False, True, True, False])
count_true = np.[1](arr)
Drag options to blanks, or click blank then click option'
Asum
Bcount
Cmean
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which does not exist for numpy arrays.
Using len() which returns total length, not count of True.
Using mean() which returns average, not count.
2fill in blank
medium

Complete the code to count how many values in the array are False.

NumPy
import numpy as np
arr = np.array([True, False, True, False, False])
count_false = np.[1](~arr)
Drag options to blanks, or click blank then click option'
Asum
Blen
Ccount
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which is not a numpy function.
Using mean() which calculates average, not count.
Using len() which returns total length.
3fill in blank
hard

Fix the error in the code to count True values along axis 0.

NumPy
import numpy as np
arr = np.array([[True, False], [False, True], [True, True]])
count_true_axis0 = np.sum(arr, axis=[1])
Drag options to blanks, or click blank then click option'
A1
B2
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=1 counts per row, not per column.
Using axis=2 causes error because array is 2D.
Using axis=-1 counts last axis but may confuse beginners.
4fill in blank
hard

Fill both blanks to create a dictionary counting words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'car', 'door', 'egg']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word > 3 compares strings to numbers, causing error.
Using word instead of len(word) for length.
Missing the condition to filter words.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths if length > 3.

NumPy
words = ['apple', 'bat', 'car', 'door', 'egg']
result = { [1]: [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Cword
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' in the loop variable.
Using 'words.upper()' which is invalid.
Not filtering words by length.