Bird
Raised Fist0
SciPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the scipy.stats.ks_2samp function in data analysis?
easy
A. To generate random numbers from a normal distribution
B. To calculate the mean of a dataset
C. To perform linear regression between two variables
D. To compare two samples and check if they come from the same distribution

Solution

  1. Step 1: Understand the function purpose

    ks_2samp is designed to compare two independent samples.
  2. Step 2: Identify what it tests

    It tests if the two samples come from the same continuous distribution.
  3. Final Answer:

    To compare two samples and check if they come from the same distribution -> Option D
  4. Quick Check:

    Kolmogorov-Smirnov test compares distributions [OK]
Hint: Remember KS test compares distributions, not means or regressions [OK]
Common Mistakes:
  • Confusing KS test with mean calculation
  • Thinking KS test performs regression
  • Assuming KS test generates random data
2. Which of the following is the correct way to import the Kolmogorov-Smirnov two-sample test from scipy?
easy
A. import ks_2samp from scipy.stats
B. import scipy.ks_2samp
C. from scipy.stats import ks_2samp
D. from scipy import ks_2samp

Solution

  1. Step 1: Recall correct import syntax

    Functions inside scipy.stats are imported using from scipy.stats import function_name.
  2. Step 2: Match the function name

    The function for KS two-sample test is ks_2samp, so the correct import is from scipy.stats import ks_2samp.
  3. Final Answer:

    from scipy.stats import ks_2samp -> Option C
  4. Quick Check:

    Correct import uses 'from scipy.stats import' [OK]
Hint: Import functions from scipy.stats using 'from scipy.stats import' [OK]
Common Mistakes:
  • Using 'import scipy.ks_2samp' which is invalid
  • Trying 'from scipy import ks_2samp' which misses stats
  • Using wrong import order like 'import ks_2samp from scipy.stats'
3. What will be the output of the following code snippet?
from scipy.stats import ks_2samp
sample1 = [1, 2, 3, 4, 5]
sample2 = [2, 3, 4, 5, 6]
result = ks_2samp(sample1, sample2)
print(round(result.pvalue, 3))
medium
A. 0.999
B. 0.500
C. 0.317
D. 0.050

Solution

  1. Step 1: Understand the samples and test

    Samples are close but not identical; KS test compares their distributions.
  2. Step 2: Run the KS test and check p-value

    Running the code shows p-value approximately 0.999, indicating no strong evidence to reject similarity.
  3. Final Answer:

    0.999 -> Option A
  4. Quick Check:

    Close samples give very high p-value ~0.999 [OK]
Hint: Close samples usually yield high p-values, not moderate or low [OK]
Common Mistakes:
  • Assuming p-value is always very small for different samples
  • Confusing p-value with test statistic
  • Rounding errors leading to wrong choice
4. Identify the error in this code snippet that attempts to perform a KS test:
from scipy.stats import ks_2samp
sample1 = [1, 2, 3]
sample2 = [4, 5, 6]
result = ks_2samp(sample1)
print(result.statistic)
medium
A. Incorrect import statement for ks_2samp
B. Missing second sample argument in ks_2samp call
C. Using print instead of return to show result
D. Samples contain integers instead of floats

Solution

  1. Step 1: Check function call parameters

    ks_2samp requires two sample arguments, but only one is given.
  2. Step 2: Identify the error cause

    Missing second sample causes a TypeError because the function expects two inputs.
  3. Final Answer:

    Missing second sample argument in ks_2samp call -> Option B
  4. Quick Check:

    ks_2samp needs two samples [OK]
Hint: ks_2samp always needs two sample inputs [OK]
Common Mistakes:
  • Forgetting the second sample argument
  • Thinking print causes error
  • Assuming integer samples cause error
5. You have two datasets representing customer wait times from two stores. You want to check if their wait times differ significantly. Which approach using scipy is best?
hard
A. Use ks_2samp to compare the two samples' distributions
B. Calculate the mean wait time of each and compare with subtraction
C. Use linear regression to predict wait times from store ID
D. Use ttest_ind to compare the two samples assuming normality

Solution

  1. Step 1: Understand the data and goal

    We want to check if two samples (wait times) differ in distribution, not just means.
  2. Step 2: Choose appropriate test

    ks_2samp compares full distributions without assuming normality, ideal for wait times.
  3. Step 3: Evaluate other options

    Mean subtraction ignores distribution shape; regression is unrelated; ttest_ind assumes normality which may not hold.
  4. Final Answer:

    Use ks_2samp to compare the two samples' distributions -> Option A
  5. Quick Check:

    KS test compares distributions best for this case [OK]
Hint: KS test compares distributions without normality assumptions [OK]
Common Mistakes:
  • Using mean difference which ignores distribution shape
  • Applying t-test without checking normality
  • Using regression for distribution comparison
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard