Challenge - 5 Problems
Cumulative Histogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of cumulative histogram with uniform data
What is the output array of counts for the cumulative histogram of 10 numbers from 1 to 10 with 5 bins?
Matplotlib
import numpy as np import matplotlib.pyplot as plt data = np.arange(1, 11) counts, bins, patches = plt.hist(data, bins=5, cumulative=True) plt.close() print(counts)
Attempts:
2 left
💡 Hint
Think about how cumulative counts add up across bins.
✗ Incorrect
The data has 10 values evenly spaced from 1 to 10. With 5 bins, each bin covers 2 numbers. The cumulative counts add up the number of data points up to each bin edge, resulting in [2, 4, 6, 8, 10].
❓ data_output
intermediate1:30remaining
Number of bins in cumulative histogram
If you create a cumulative histogram with 7 bins from data of 50 random values, how many count values will the output array have?
Matplotlib
import numpy as np import matplotlib.pyplot as plt data = np.random.randn(50) counts, bins, patches = plt.hist(data, bins=7, cumulative=True) plt.close() print(len(counts))
Attempts:
2 left
💡 Hint
The counts array length matches the number of bins.
✗ Incorrect
The counts array length equals the number of bins specified, which is 7.
❓ visualization
advanced2:30remaining
Identify the cumulative histogram plot
Which plot shows a cumulative histogram of data sampled from a normal distribution?
Matplotlib
import numpy as np import matplotlib.pyplot as plt data = np.random.normal(0, 1, 1000) plt.figure(figsize=(8,4)) plt.subplot(1,2,1) plt.hist(data, bins=30) plt.title('Regular Histogram') plt.subplot(1,2,2) plt.hist(data, bins=30, cumulative=True) plt.title('Cumulative Histogram') plt.tight_layout() plt.show()
Attempts:
2 left
💡 Hint
Cumulative histograms always increase or stay the same as bins increase.
✗ Incorrect
The right plot uses cumulative=True, so the bars accumulate counts and never decrease, showing a cumulative histogram.
🧠 Conceptual
advanced1:30remaining
Effect of cumulative=True on histogram counts
What happens to the counts array when you set cumulative=True in plt.hist compared to cumulative=False?
Attempts:
2 left
💡 Hint
Cumulative means adding up counts as you move through bins.
✗ Incorrect
With cumulative=True, counts accumulate data points from all previous bins, so counts never decrease.
🔧 Debug
expert1:30remaining
Identify the error in cumulative histogram code
What error does this code raise?
import matplotlib.pyplot as plt
plt.hist([1,2,3,4,5], bins=3, cumulative='yes')
Matplotlib
import matplotlib.pyplot as plt plt.hist([1,2,3,4,5], bins=3, cumulative='yes')
Attempts:
2 left
💡 Hint
Check the type expected for the cumulative parameter.
✗ Incorrect
The cumulative parameter expects a boolean True or False, not a string. Passing 'yes' causes a TypeError.