Challenge - 5 Problems
Histogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Histogram Bin Counts with numpy
What is the output of the following code that creates a histogram bin count using numpy?
Data Analysis Python
import numpy as np data = np.array([1, 2, 2, 3, 4, 4, 4, 5]) bins = [1, 3, 5] counts, edges = np.histogram(data, bins=bins) print(counts)
Attempts:
2 left
💡 Hint
Remember that numpy.histogram counts how many values fall into each bin interval, including the left edge but excluding the right edge except for the last bin.
✗ Incorrect
The bins are [1,3) and [3,5]. Values 1,2,2 fall into the first bin (3 values). Values 3,4,4,4,5 fall into the second bin (5 values).
❓ data_output
intermediate2:00remaining
Pandas Histogram Value Counts
Given this pandas DataFrame, what is the output of the histogram value counts?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'scores': [10, 20, 20, 30, 40, 40, 40, 50]}) bins = [10, 30, 50] hist = pd.cut(df['scores'], bins=bins).value_counts().sort_index() print(hist)
Attempts:
2 left
💡 Hint
pd.cut bins are left-inclusive and right-exclusive by default.
✗ Incorrect
Scores 10, 20, 20 fall into the first bin [10,30). Scores 30, 40, 40, 40, 50 fall into the second bin [30,50).
❓ visualization
advanced3:00remaining
Matplotlib Histogram Bin Width Effect
Which histogram plot shows the effect of changing bin width on the same data?
Data Analysis Python
import matplotlib.pyplot as plt import numpy as np data = np.random.normal(0, 1, 1000) plt.figure(figsize=(10,4)) plt.subplot(1,2,1) plt.hist(data, bins=10, color='blue') plt.title('Bins=10') plt.subplot(1,2,2) plt.hist(data, bins=30, color='green') plt.title('Bins=30') plt.tight_layout() plt.show()
Attempts:
2 left
💡 Hint
More bins means narrower bars and more detail.
✗ Incorrect
Bins=10 means each bin covers a wider range, so fewer bars. Bins=30 means narrower bins, so more bars and more detail.
🔧 Debug
advanced2:00remaining
Fix the Histogram Edge Inclusion Bug
What error does this code raise and why?
import numpy as np
data = [1, 2, 3, 4, 5]
bins = [1, 3, 5]
counts, edges = np.histogram(data, bins=bins)
print(counts)
print(edges)
# Then trying to access counts[3]
Data Analysis Python
print(counts[3])
Attempts:
2 left
💡 Hint
Check how many bins are created by np.histogram with 3 edges.
✗ Incorrect
np.histogram with 3 bin edges creates 2 bins, so counts has length 2. Accessing counts[3] is out of range and raises IndexError.
🚀 Application
expert3:00remaining
Choosing Optimal Histogram Bins for Skewed Data
You have a dataset with a strong right skew. Which binning strategy below will best reveal the data distribution in a histogram?
Attempts:
2 left
💡 Hint
Think about how bin widths can adapt to data skew to show detail where data is dense.
✗ Incorrect
Equal-width bins may hide details in skewed data. Exponentially increasing bin widths capture dense and sparse areas better.