SciPy for Statistics (scipy.stats)
Statistics is the backbone of data analytics.
It helps us understand data behavior, identify patterns, and make informed decisions.
While NumPy provides basic statistical functions, SciPy takes statistics to the next level by
offering advanced and ready-to-use statistical tools through the scipy.stats module.
This module is heavily used in :
- Data analysis
- Research
- Business decision making
- Machine learning preprocessing
What is scipy.stats?
scipy.stats is a SciPy submodule that provides :
- Descriptive statistics
- Probability distributions
- Correlation and relationships
- Hypothesis testing
It works directly with NumPy arrays, making it easy to integrate into analytics workflows.
Basic Statistical Measures
Mean, Median, and Mode
These measures help understand the central tendency of data.
import numpy as np
from scipy import stats
data = np.array([10, 12, 15, 18, 20])
print("Mean:", np.mean(data))
print("Median:", np.median(data))
print("Mode:", stats.mode(data))
#Output:
Mean: 15.0
Median: 15.0
Mode: ModeResult(mode=np.int64(10), count=np.int64(1))
Real-Life Use :
- Average sales
- Typical customer spending
- Central performance value
Variance and Standard Deviation
These measure how spread out the data is.
print("Variance:", np.var(data))
print("Standard Deviation:", np.std(data))
#Output:
Variance: 13.6
Standard Deviation: 3.687817782917155
Why This Matters
- Low variance → stable data
- High variance → high fluctuations
Used in :
- Risk analysis
- Quality control
- Performance evaluation
Probability Distributions (Basic Idea)
Probability distributions describe how values are distributed.
Common Distributions
- Normal (Gaussian)
- Binomial
- Uniform
Example: Normal Distribution
from scipy.stats import norm
mean = 50
std_dev = 5
probability = norm.pdf(55, mean, std_dev)
print(probability)
#Output: 0.04839414490382867
Used in :
- Exam scores
- Heights and weights
- Financial modeling
Correlation and Relationship Between Data
Correlation tells us how strongly two variables are related.
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
correlation, _ = stats.pearsonr(x, y)
print("Correlation:", correlation)
#Output:
Correlation: 1.0
Interpretation :
- 1 → Strong positive relationship
- -1 → Strong negative relationship
- 0 → No relationship
Real-Life Example: Sales vs Advertising
Businesses often analyze :
- Advertising spend
- Sales growth
Correlation helps determine whether increased marketing actually impacts sales.
Hypothesis Testing (Basic Understanding)
Hypothesis testing helps us validate assumptions using data.
Common questions :
- Is the new strategy better?
- Is the difference significant?
- Is this result due to chance?
Example: One-Sample t-test
sample = np.array([100, 102, 98, 101, 99])
t_stat, p_value = stats.ttest_1samp(sample, 100)
print("p-value:", p_value)
#Output:
p-value: 1.0
If p-value < 0.05, the result is usually considered statistically significant.
Why Hypothesis Testing Is Important
Used in :
- A/B testing
- Product improvements
- Business experiments
- Research studies
It helps move from assumptions to evidence-based decisions.
Common Beginner Mistakes
- Confusing correlation with causation
- Ignoring data distribution
- Misinterpreting p-values
- Using stats without understanding context
Statistics should always be combined with domain understanding.
Why scipy.stats Is Important in Data Analytics
It helps analysts :
- Summarize data effectively
- Understand data variability
- Discover relationships
- Make data-driven decisions
Almost every serious analytics project uses statistical analysis.
Key Takeaways
- scipy.stats provides advanced statistical tools
- Mean, variance, and correlation are core concepts
- Probability distributions model real-world data
- Hypothesis testing validates assumptions