Challenge - 5 Problems
Histogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.histogram() with bins=6
What is the output of the following code snippet?
NumPy
import numpy as np arr = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6]) hist, bins = np.histogram(arr, bins=6) print(hist, bins)
Attempts:
2 left
💡 Hint
Check how np.histogram divides the range into equal-width bins when bins=6.
✗ Incorrect
np.histogram with bins=6 creates 6 bins between min and max values. Here, the bins are automatically calculated with equal width between 1 and 6, resulting in 6 bins with counts as shown.
❓ data_output
intermediate2:00remaining
Number of bins and counts with custom bins
Given the array and bins, what is the output counts array from np.histogram?
NumPy
import numpy as np arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) bins = [0, 3, 6, 9] hist, _ = np.histogram(arr, bins=bins) print(hist)
Attempts:
2 left
💡 Hint
Count how many numbers fall into each bin range: [0-3), [3-6), [6-9).
✗ Incorrect
The bins are [0,3), [3,6), [6,9). Values 0,1,2 fall in first bin (3 items), 3,4,5 in second (3 items), 6,7,8 in third (3 items). The value 9 is excluded as bins are half-open intervals.
🔧 Debug
advanced2:00remaining
Identify the error in np.histogram bin specification
What error does the following code raise?
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5]) bins = [5, 4, 3, 2, 1] hist, edges = np.histogram(arr, bins=bins)
Attempts:
2 left
💡 Hint
Bins must be sorted in ascending order.
✗ Incorrect
np.histogram requires bins to be monotonically increasing. Here bins are in descending order, so it raises a ValueError.
❓ visualization
advanced2:00remaining
Histogram bin edges with uneven bin widths
What are the bin edges and counts for this histogram?
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) bins = [1, 3, 6, 10] hist, edges = np.histogram(arr, bins=bins) print(hist, edges)
Attempts:
2 left
💡 Hint
Count how many values fall into each bin: [1-3), [3-6), [6-10).
✗ Incorrect
Values 1 and 2 fall in first bin (2 items), 3,4,5 in second (3 items), 6,7,8,9 in third (4 items).
🚀 Application
expert3:00remaining
Calculate histogram and cumulative counts
Given the array, which option correctly computes the histogram counts and cumulative counts using np.histogram?
NumPy
import numpy as np arr = np.array([2, 4, 4, 4, 5, 5, 7, 9]) bins = [0, 3, 6, 9, 12] hist, edges = np.histogram(arr, bins=bins) cum_counts = np.cumsum(hist) print(hist, cum_counts)
Attempts:
2 left
💡 Hint
Count values in each bin and then sum counts cumulatively.
✗ Incorrect
Bins: [0-3), [3-6), [6-9), [9-12). Values: 2 in first bin (1 item), 4,4,4,5,5 in second (5 items), 7 in third (1 item), 9 in fourth (1 item). Cumulative sums add counts progressively.