Stats using Numpy and Scipy

NumPy for Statistics

📊 Introduction to NumPy for Statistics

NumPy (Numerical Python) is a foundational Python library for numerical and statistical analysis. It's lightning fast with large datasets and widely used in machine learning and AI workflows.

🧠 Why Use NumPy?

  • Handles arrays, matrices, and numerical computations efficiently
  • Performs statistical operations like mean, median, std dev, percentiles
  • Works well with pandas, matplotlib, and scikit-learn
Install NumPy via pip:
pip install numpy

✅ Example 1: Mean, Median, Mode

import numpy as np

from scipy import statsdata = [10, 20, 20, 30, 40]

mean = np.mean(data) median = np.median(data) mode = stats.mode(data)

print("Mean:", mean) print("Median:", median) print("Mode:", mode.mode[0], " (Count:", mode.count[0], ")")

✅ Example 2: Standard Deviation, Variance, Range

import numpy as npdata = [5, 10, 15, 20, 25]

std_dev = np.std(data) variance = np.var(data) range_val = np.max(data) - np.min(data)

print("Standard Deviation:", std_dev) print("Variance:", variance) print("Range:", range_val)

✅ Example 3: Quartiles and IQR (Interquartile Range)

import numpy as npdata = [12, 15, 14, 10, 18, 20, 25, 22]

q1 = np.percentile(data, 25) q2 = np.percentile(data, 50) q3 = np.percentile(data, 75) iqr = q3 - q1

print("Q1:", q1) print("Q2 (Median):", q2) print("Q3:", q3) print("IQR:", iqr)

📘 Summary of Functions

  • np.mean() → Mean
  • np.median() → Median
  • scipy.stats.mode() → Mode
  • np.std() → Standard Deviation
  • np.var() → Variance
  • np.percentile() → Quartiles, Percentiles

Post a Comment

0 Comments

Me