NumPy — Statistics
Basic statistics
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(f"Mean: {np.mean(arr)}")
print(f"Median: {np.median(arr)}")
print(f"Std: {np.std(arr)}")
print(f"Var: {np.var(arr)}")
Min/Max
arr = np.array([3, 1, 4, 1, 5, 9, 2, 6])
print(f"Min: {np.min(arr)}")
print(f"Max: {np.max(arr)}")
print(f"Argmin: {np.argmin(arr)}")
print(f"Argmax: {np.argmax(arr)}")
Percentiles
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(f"25th: {np.percentile(arr, 25)}")
print(f"50th: {np.percentile(arr, 50)}")
print(f"75th: {np.percentile(arr, 75)}")
Correlation
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 5, 4, 5])
print(np.corrcoef(x, y))
Axis
arr = np.array([[1, 2], [3, 4]])
print(np.mean(arr, axis=0)) # Column mean
print(np.mean(arr, axis=1)) # Row mean
Mini Practice
- Calculate basic stats
- Find min/max
- Compute percentiles
- Use axis parameter
Up Next
Continue with Linear Algebra - Matrix operations.
Related Topics
Frequently Asked Questions about Statistics
What is Statistics in NumPy?
Statistics is a fundamental concept in NumPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Statistics?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Statistics.
Why is Statistics important in NumPy?
Statistics is essential for NumPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.