0
0
Data Analysis Pythondata~20 mins

Histograms in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Histogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[3 4]
B[3 5]
C[2 6]
D[4 4]
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.
data_output
intermediate
2: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)
A
[10-30)    3
30-50)     5
dtype: int64
B
[10-30)    2
30-50)     6
dtype: int64
C
[10-30)    4
30-50)     4
dtype: int64
D
[10-30)    3
30-50)     4
dtype: int64
Attempts:
2 left
💡 Hint
pd.cut bins are left-inclusive and right-exclusive by default.
visualization
advanced
3: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()
ALeft plot is a scatter plot; right plot is a histogram.
BLeft plot has narrower bins with more bars; right plot has wider bins with fewer bars.
CBoth plots have the same number of bins and look identical.
DLeft plot has wider bins with fewer bars; right plot has narrower bins with more bars.
Attempts:
2 left
💡 Hint
More bins means narrower bars and more detail.
🔧 Debug
advanced
2: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])
AIndexError: index 3 is out of bounds for axis 0 with size 2
BTypeError: 'list' object is not subscriptable
CValueError: bins must increase monotonically
DNo error, prints the count of the fourth bin
Attempts:
2 left
💡 Hint
Check how many bins are created by np.histogram with 3 edges.
🚀 Application
expert
3: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?
AUse a single bin to summarize all data.
BUse equal-width bins spanning the entire data range.
CUse bins with widths increasing exponentially to capture skewness.
DUse bins with random widths unrelated to data distribution.
Attempts:
2 left
💡 Hint
Think about how bin widths can adapt to data skew to show detail where data is dense.