0
0
SciPydata~20 mins

Kolmogorov-Smirnov test in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
KS Test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of KS test on two identical samples
What is the output of the Kolmogorov-Smirnov test when comparing two identical samples using scipy.stats.ks_2samp?
SciPy
from scipy.stats import ks_2samp
sample = [1, 2, 3, 4, 5]
result = ks_2samp(sample, sample)
print((round(result.statistic, 2), round(result.pvalue, 2)))
A(0.5, 0.5)
B(1.0, 0.0)
C(0.0, 1.0)
D(0.2, 0.8)
Attempts:
2 left
💡 Hint
Think about what happens when two samples are exactly the same.
data_output
intermediate
1:30remaining
Number of items in KS test result object
How many attributes does the result object returned by scipy.stats.ks_2samp contain?
SciPy
from scipy.stats import ks_2samp
sample1 = [1, 2, 3]
sample2 = [4, 5, 6]
result = ks_2samp(sample1, sample2)
print(len(result._fields))
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
Check the attributes of the namedtuple returned by ks_2samp.
🧠 Conceptual
advanced
1:30remaining
Interpretation of KS test p-value
If the Kolmogorov-Smirnov test returns a p-value of 0.03 when comparing two samples, what does this mean?
AThe two samples definitely come from the same distribution.
BThere is strong evidence that the two samples come from different distributions at the 5% significance level.
CThe test is inconclusive because p-value is less than 0.05.
DThe samples are identical.
Attempts:
2 left
💡 Hint
Recall what a p-value less than 0.05 usually indicates in hypothesis testing.
🔧 Debug
advanced
2:00remaining
Identify error in KS test usage
What error will this code raise? from scipy.stats import ks_2samp sample1 = [1, 2, 3] sample2 = 'abc' result = ks_2samp(sample1, sample2) print(result)
ATypeError: unsupported operand type(s) for -: 'int' and 'str'
BTypeError: object of type 'str' has no len()
CValueError: sample sizes must be equal
DNo error, prints KS test result
Attempts:
2 left
💡 Hint
Check what happens when comparing numeric and string data in ks_2samp.
🚀 Application
expert
2:30remaining
Using KS test to compare sample to normal distribution
Which code correctly performs a Kolmogorov-Smirnov test to check if a sample comes from a normal distribution with mean 0 and std 1?
A
from scipy.stats import kstest
import numpy as np
sample = np.random.normal(0, 1, 100)
result = kstest(sample, 'norm', args=(0, 1))
print(round(result.pvalue, 3))
B
from scipy.stats import ks_2samp
import numpy as np
sample = np.random.normal(0, 1, 100)
result = ks_2samp(sample, 'norm')
print(round(result.pvalue, 3))
C
from scipy.stats import kstest
import numpy as np
sample = np.random.normal(0, 1, 100)
result = kstest(sample, 'norm')
print(round(result.pvalue, 3))
D
from scipy.stats import ks_2samp
import numpy as np
sample = np.random.normal(0, 1, 100)
result = ks_2samp(sample, np.random.normal(0, 1, 100))
print(round(result.pvalue, 3))
Attempts:
2 left
💡 Hint
Use kstest with distribution name and parameters to test against a theoretical distribution.