0
0
Matplotlibdata~20 mins

KDE overlay concept in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
KDE Overlay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this KDE overlay plot code?
Look at the code below that creates two KDE plots overlaid on the same axes. What will the plot show?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

np.random.seed(0)
data1 = np.random.normal(0, 1, 100)
data2 = np.random.normal(2, 1, 100)

kde1 = gaussian_kde(data1)
kde2 = gaussian_kde(data2)

x = np.linspace(-4, 6, 200)

plt.plot(x, kde1(x), label='Data 1')
plt.plot(x, kde2(x), label='Data 2')
plt.legend()
plt.show()
ATwo smooth curves showing peaks near 0 and 2, overlapping slightly
BTwo flat lines with no peaks
CA single curve with one peak at 1
DTwo curves with peaks at -2 and 4
Attempts:
2 left
💡 Hint
Think about where the normal distributions are centered and how KDE estimates density.
data_output
intermediate
1:30remaining
How many peaks are visible in the KDE overlay plot?
Given two datasets with different centers, when overlaid using KDE plots, how many distinct peaks should appear?
Matplotlib
import numpy as np
from scipy.stats import gaussian_kde

np.random.seed(1)
data1 = np.random.normal(-1, 0.5, 150)
data2 = np.random.normal(3, 0.5, 150)

kde1 = gaussian_kde(data1)
kde2 = gaussian_kde(data2)

x = np.linspace(-3, 5, 300)

kde_values1 = kde1(x)
kde_values2 = kde2(x)

combined = kde_values1 + kde_values2

peaks = ((combined[1:-1] > combined[:-2]) & (combined[1:-1] > combined[2:])).sum()
A1
B0
C3
D2
Attempts:
2 left
💡 Hint
Each dataset is centered far apart, so each should create a peak.
visualization
advanced
2:30remaining
Which option correctly overlays KDE plots with different bandwidths?
You want to overlay KDE plots of two datasets but use a smaller bandwidth for the second dataset to get a sharper curve. Which code snippet does this correctly?
A
kde1 = gaussian_kde(data1, bw_method='scott')
kde2 = gaussian_kde(data2, bw_method='silverman')
B
kde1 = gaussian_kde(data1, bw_method=0.1)
kde2 = gaussian_kde(data2, bw_method=0.5)
C
kde1 = gaussian_kde(data1, bw_method=0.5)
kde2 = gaussian_kde(data2, bw_method=0.1)
D
kde1 = gaussian_kde(data1)
kde2 = gaussian_kde(data2, bw_method=0.1)
Attempts:
2 left
💡 Hint
Smaller bandwidth means sharper peaks. Assign smaller bandwidth to second dataset.
🔧 Debug
advanced
2:00remaining
What error does this KDE overlay code raise?
This code tries to overlay KDE plots but raises an error. What is the error?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

np.random.seed(0)
data1 = np.random.normal(0, 1, 100)
data2 = []

kde1 = gaussian_kde(data1)
kde2 = gaussian_kde(data2)

x = np.linspace(-4, 4, 100)

plt.plot(x, kde1(x), label='Data 1')
plt.plot(x, kde2(x), label='Data 2')
plt.legend()
plt.show()
ATypeError: 'list' object is not callable
BValueError: `dataset` input should have shape (n, m) with n>1
CNameError: name 'gaussian_kde' is not defined
DIndexError: index out of range
Attempts:
2 left
💡 Hint
Check the shape and content of data2 before passing to gaussian_kde.
🚀 Application
expert
3:00remaining
How to combine and visualize KDE overlays for three datasets with different colors and transparency?
You have three datasets and want to plot their KDE overlays on the same plot. You want each KDE curve to have a distinct color and some transparency so overlaps are visible. Which code snippet achieves this?
A
plt.plot(x, kde1(x), color='red', alpha=0.5)
plt.plot(x, kde2(x), color='green', alpha=0.5)
plt.plot(x, kde3(x), color='blue', alpha=0.5)
B
plt.plot(x, kde1(x), alpha=0.5)
plt.plot(x, kde2(x), alpha=0.5)
plt.plot(x, kde3(x), alpha=0.5)
C
plt.plot(x, kde1(x), color='red')
plt.plot(x, kde2(x), color='green')
plt.plot(x, kde3(x), color='blue')
D
plt.plot(x, kde1(x), color='red', alpha=1)
plt.plot(x, kde2(x), color='red', alpha=0.5)
plt.plot(x, kde3(x), color='red', alpha=0.3)
Attempts:
2 left
💡 Hint
Use both color and alpha parameters in plt.plot for distinct colors and transparency.