Challenge - 5 Problems
Normalized Histogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of normalized histogram with density=True
What is the output of the following code snippet that plots a normalized histogram using matplotlib?
Matplotlib
import matplotlib.pyplot as plt import numpy as np np.random.seed(0) data = np.random.normal(loc=0, scale=1, size=1000) counts, bins, patches = plt.hist(data, bins=10, density=True) plt.close() print(counts.round(3))
Attempts:
2 left
💡 Hint
Remember that density=True scales counts so the area sums to 1, not the counts themselves.
✗ Incorrect
When density=True, the histogram counts are normalized so that the total area under the histogram equals 1. The counts array represents the height of each bin's bar, not the raw counts. The values are roughly between 0 and 0.25 here, matching option A.
❓ data_output
intermediate1:30remaining
Number of bins with non-zero density in normalized histogram
Given the following code that creates a normalized histogram, how many bins have a non-zero density value?
Matplotlib
import numpy as np import matplotlib.pyplot as plt np.random.seed(1) data = np.random.uniform(0, 5, 500) counts, bins, _ = plt.hist(data, bins=5, density=True) plt.close() print(sum(counts > 0))
Attempts:
2 left
💡 Hint
Uniform data over the range 0 to 5 with 5 bins should fill all bins.
✗ Incorrect
Since the data is uniformly distributed between 0 and 5 and the bins cover this range evenly, all 5 bins will have some data points, so all bins have non-zero density.
❓ visualization
advanced2:30remaining
Effect of density parameter on histogram area
Which histogram plot correctly shows a normalized histogram where the total area under the bars sums to 1?
Matplotlib
import matplotlib.pyplot as plt import numpy as np np.random.seed(2) data = np.random.exponential(scale=1.0, size=1000) plt.figure(figsize=(10,4)) plt.subplot(1,2,1) plt.hist(data, bins=20, density=False) plt.title('Histogram with density=False') plt.subplot(1,2,2) plt.hist(data, bins=20, density=True) plt.title('Histogram with density=True') plt.close()
Attempts:
2 left
💡 Hint
density=True normalizes the histogram so the area sums to 1.
✗ Incorrect
When density=False, the histogram shows raw counts, so the total area equals the number of samples (1000). When density=True, the histogram is normalized so the total area sums to 1.
🔧 Debug
advanced1:30remaining
Identify the error in normalized histogram code
What error will this code raise when trying to plot a normalized histogram?
Matplotlib
import matplotlib.pyplot as plt import numpy as np data = np.random.randn(100) plt.hist(data, bins=10, density='True') plt.show()
Attempts:
2 left
💡 Hint
Check the type of the density parameter value.
✗ Incorrect
The density parameter expects a boolean True or False, not the string 'True'. Passing a string causes a TypeError.
🚀 Application
expert2:30remaining
Calculate probability from normalized histogram bins
Given a normalized histogram with bin edges [0, 1, 2, 3] and densities [0.2, 0.3, 0.5], what is the probability that a random value falls between 1 and 3?
Attempts:
2 left
💡 Hint
Probability for a bin = density * bin width. Sum probabilities for bins between 1 and 3.
✗ Incorrect
Bin widths are 1 each. Probability between 1 and 3 is sum of densities * widths for bins [1-2] and [2-3]: (0.3*1) + (0.5*1) = 0.8.