The Kolmogorov-Smirnov test helps us check if two sets of numbers come from the same pattern or if one set matches a known pattern.
Kolmogorov-Smirnov test in SciPy
from scipy.stats import ks_2samp result = ks_2samp(data1, data2) # or for one sample vs a distribution from scipy.stats import kstest result = kstest(data, 'distribution_name')
ks_2samp compares two data samples.
kstest compares one sample to a known distribution like 'norm' for normal.
from scipy.stats import ks_2samp sample1 = [1, 2, 3, 4, 5] sample2 = [2, 3, 4, 5, 6] result = ks_2samp(sample1, sample2) print(result)
from scipy.stats import kstest import numpy as np sample = np.random.normal(0, 1, 100) result = kstest(sample, 'norm') print(result)
This code compares exam scores from two classes to see if their score distributions differ.
from scipy.stats import ks_2samp # Two samples of exam scores scores_classA = [88, 92, 85, 91, 87, 90, 89] scores_classB = [78, 82, 80, 79, 81, 77, 83] # Perform Kolmogorov-Smirnov test result = ks_2samp(scores_classA, scores_classB) print(f"KS statistic: {result.statistic:.3f}") print(f"p-value: {result.pvalue:.3f}")
A small p-value (usually less than 0.05) means the two samples likely come from different distributions.
The KS test is sensitive to differences in both the center and shape of distributions.
Samples should be independent and continuous for best results.
The Kolmogorov-Smirnov test compares two samples or a sample to a known distribution.
It helps check if data follows a pattern or if two groups behave similarly.
Look at the p-value to decide if differences are meaningful.
Practice
scipy.stats.ks_2samp function in data analysis?Solution
Step 1: Understand the function purpose
ks_2sampis designed to compare two independent samples.Step 2: Identify what it tests
It tests if the two samples come from the same continuous distribution.Final Answer:
To compare two samples and check if they come from the same distribution -> Option DQuick Check:
Kolmogorov-Smirnov test compares distributions [OK]
- Confusing KS test with mean calculation
- Thinking KS test performs regression
- Assuming KS test generates random data
Solution
Step 1: Recall correct import syntax
Functions inside scipy.stats are imported usingfrom scipy.stats import function_name.Step 2: Match the function name
The function for KS two-sample test isks_2samp, so the correct import isfrom scipy.stats import ks_2samp.Final Answer:
from scipy.stats import ks_2samp -> Option CQuick Check:
Correct import uses 'from scipy.stats import' [OK]
- 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'
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))
Solution
Step 1: Understand the samples and test
Samples are close but not identical; KS test compares their distributions.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.Final Answer:
0.999 -> Option AQuick Check:
Close samples give very high p-value ~0.999 [OK]
- Assuming p-value is always very small for different samples
- Confusing p-value with test statistic
- Rounding errors leading to wrong choice
from scipy.stats import ks_2samp sample1 = [1, 2, 3] sample2 = [4, 5, 6] result = ks_2samp(sample1) print(result.statistic)
Solution
Step 1: Check function call parameters
ks_2samprequires two sample arguments, but only one is given.Step 2: Identify the error cause
Missing second sample causes a TypeError because the function expects two inputs.Final Answer:
Missing second sample argument in ks_2samp call -> Option BQuick Check:
ks_2samp needs two samples [OK]
- Forgetting the second sample argument
- Thinking print causes error
- Assuming integer samples cause error
Solution
Step 1: Understand the data and goal
We want to check if two samples (wait times) differ in distribution, not just means.Step 2: Choose appropriate test
ks_2sampcompares full distributions without assuming normality, ideal for wait times.Step 3: Evaluate other options
Mean subtraction ignores distribution shape; regression is unrelated;ttest_indassumes normality which may not hold.Final Answer:
Useks_2sampto compare the two samples' distributions -> Option AQuick Check:
KS test compares distributions best for this case [OK]
- Using mean difference which ignores distribution shape
- Applying t-test without checking normality
- Using regression for distribution comparison
Ready to prove it?
Pass the test and this pattern moves unknown
