data comparison
number analysis
set comparison
statistical methods
computational techniques

How can I compare two sets of 1000 numbers against each other?

Codemia Knowledge Hub ·

Introduction

Comparing two sets of numbers requires choosing between set operations (what elements differ), statistical tests (are the distributions different), and visual methods (how do they look different). Python's built-in set type handles membership comparisons. NumPy and SciPy provide statistical tests like t-tests and KS tests. Matplotlib visualizes distributions. The right approach depends on whether you care about exact values, statistical properties, or overall distribution shape.

Set Operations (Exact Membership)

python
1set_a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
2set_b = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
3
4# Elements in both sets
5common = set_a & set_b  # or set_a.intersection(set_b)
6print(common)  # {5, 6, 7, 8, 9, 10}
7
8# Elements in A but not in B
9only_a = set_a - set_b  # or set_a.difference(set_b)
10print(only_a)  # {1, 2, 3, 4}
11
12# Elements in B but not in A
13only_b = set_b - set_a
14print(only_b)  # {11, 12, 13, 14}
15
16# Elements in either but not both (symmetric difference)
17diff = set_a ^ set_b  # or set_a.symmetric_difference(set_b)
18print(diff)  # {1, 2, 3, 4, 11, 12, 13, 14}
19
20# Are the sets identical?
21print(set_a == set_b)  # False
22
23# Overlap percentage
24overlap = len(set_a & set_b) / len(set_a | set_b)
25print(f"Jaccard similarity: {overlap:.2%}")  # 42.86%

Element-wise Comparison with NumPy

python
1import numpy as np
2
3a = np.array([10, 20, 30, 40, 50])
4b = np.array([10, 22, 30, 38, 55])
5
6# Element-wise difference
7diff = a - b
8print(diff)  # [0 -2 0 2 -5]
9
10# Absolute differences
11abs_diff = np.abs(a - b)
12print(abs_diff)  # [0 2 0 2 5]
13
14# Mean absolute difference
15print(np.mean(abs_diff))  # 1.8
16
17# Root mean squared difference
18rmse = np.sqrt(np.mean((a - b) ** 2))
19print(f"RMSE: {rmse:.2f}")  # 2.65
20
21# Percentage of elements that match exactly
22match_rate = np.mean(a == b)
23print(f"Match rate: {match_rate:.0%}")  # 40%

Statistical Tests

Student's t-test (Compare Means)

python
1from scipy import stats
2import numpy as np
3
4np.random.seed(42)
5a = np.random.normal(loc=50, scale=10, size=1000)
6b = np.random.normal(loc=52, scale=10, size=1000)
7
8# Independent samples t-test
9t_stat, p_value = stats.ttest_ind(a, b)
10print(f"t-statistic: {t_stat:.4f}")
11print(f"p-value: {p_value:.4f}")
12# p < 0.05 → means are significantly different

Kolmogorov-Smirnov Test (Compare Distributions)

python
1# KS test — compares the shape of two distributions
2ks_stat, p_value = stats.ks_2samp(a, b)
3print(f"KS statistic: {ks_stat:.4f}")
4print(f"p-value: {p_value:.4f}")
5# p < 0.05 → distributions are significantly different

Mann-Whitney U Test (Non-Parametric)

python
1# Does not assume normal distribution
2u_stat, p_value = stats.mannwhitneyu(a, b, alternative='two-sided')
3print(f"U statistic: {u_stat:.0f}")
4print(f"p-value: {p_value:.4f}")

Descriptive Statistics Comparison

python
1import numpy as np
2
3a = np.random.normal(50, 10, 1000)
4b = np.random.normal(52, 12, 1000)
5
6print(f"{'Metric':<15} {'Set A':>10} {'Set B':>10}")
7print(f"{'Mean':<15} {np.mean(a):>10.2f} {np.mean(b):>10.2f}")
8print(f"{'Median':<15} {np.median(a):>10.2f} {np.median(b):>10.2f}")
9print(f"{'Std Dev':<15} {np.std(a):>10.2f} {np.std(b):>10.2f}")
10print(f"{'Min':<15} {np.min(a):>10.2f} {np.min(b):>10.2f}")
11print(f"{'Max':<15} {np.max(a):>10.2f} {np.max(b):>10.2f}")
12print(f"{'25th pctl':<15} {np.percentile(a, 25):>10.2f} {np.percentile(b, 25):>10.2f}")
13print(f"{'75th pctl':<15} {np.percentile(a, 75):>10.2f} {np.percentile(b, 75):>10.2f}")

Visual Comparison

python
1import matplotlib.pyplot as plt
2import numpy as np
3
4a = np.random.normal(50, 10, 1000)
5b = np.random.normal(52, 12, 1000)
6
7# Overlapping histograms
8plt.figure(figsize=(10, 5))
9plt.hist(a, bins=50, alpha=0.5, label='Set A', color='blue')
10plt.hist(b, bins=50, alpha=0.5, label='Set B', color='red')
11plt.legend()
12plt.xlabel('Value')
13plt.ylabel('Frequency')
14plt.title('Distribution Comparison')
15plt.savefig('comparison.png')
16
17# Box plot
18plt.figure(figsize=(6, 5))
19plt.boxplot([a, b], labels=['Set A', 'Set B'])
20plt.title('Box Plot Comparison')
21plt.savefig('boxplot.png')
22
23# QQ plot (quantile-quantile)
24from scipy import stats
25fig, ax = plt.subplots()
26stats.probplot(a - b, dist="norm", plot=ax)
27plt.title('QQ Plot of Differences')
28plt.savefig('qqplot.png')

Correlation

python
1# If the two sets are paired (same indices correspond)
2correlation, p_value = stats.pearsonr(a, b)
3print(f"Pearson correlation: {correlation:.4f}")
4print(f"p-value: {p_value:.4f}")
5
6# Spearman (rank-based, no normality assumption)
7correlation, p_value = stats.spearmanr(a, b)
8print(f"Spearman correlation: {correlation:.4f}")

Common Pitfalls

  • Using set operations on float data: Floating-point numbers should not be compared for exact equality. {0.1 + 0.2} and {0.3} are different sets due to precision. Use np.isclose() or round values before converting to sets.
  • Choosing the wrong statistical test: The t-test assumes normal distributions. For skewed or non-normal data, use the Mann-Whitney U test or the KS test instead. Check normality with stats.shapiro() before choosing a parametric test.
  • Ignoring effect size: A p-value tells you whether a difference is statistically significant, not whether it is practically meaningful. With 1000 samples, tiny differences produce significant p-values. Calculate Cohen's d ((mean_a - mean_b) / pooled_std) for effect size.
  • Comparing unpaired data as if paired: A paired t-test (stats.ttest_rel) requires that the i-th element of set A corresponds to the i-th element of set B. If the data is not paired, use the independent samples t-test (stats.ttest_ind).
  • Not visualizing before testing: Statistical tests give numbers, but histograms and box plots reveal patterns (bimodality, outliers, skew) that numbers miss. Always plot the data before running tests.

Summary

  • Use Python set operations for exact membership comparison (intersection, difference, union)
  • Use NumPy for element-wise differences, RMSE, and descriptive statistics
  • Use SciPy's ttest_ind for comparing means and ks_2samp for comparing distributions
  • Visualize with overlapping histograms and box plots before running statistical tests
  • Calculate effect size (Cohen's d) alongside p-values for practical significance
  • Choose non-parametric tests (Mann-Whitney U) when data is not normally distributed

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Basis Lab
This one is answered in full over here

“How can I prove nothing was lost without comparing every single row?” is the same question written out properly on Basis Lab, with the course it belongs to beside it. Basis Lab is a separate product from the same team and both are free to read.

Read the answer
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms