Python NumPy Mean, Median, Sum, Variance, and Standard Deviation
python numpy mean median sum variance and standard deviation: Learn how to compute mean, median, sum, variance, and standard deviation with NumPy, including axis handl...
python numpy mean median sum variance and standard deviation requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.
When working with numerical data in Python, NumPy provides vectorized functions for computing descriptive statistics. The primary functions for mean, median, sum, variance, and standard deviation are np.mean, np.median, np.sum, np.var, and np.std. These functions operate on NumPy arrays and return results without explicit loops, making them the standard choice for statistical analysis in scientific Python.
Basic Usage of np.mean, np.median, and np.sum
The simplest case is a one-dimensional array. np.mean computes the arithmetic average, np.median finds the middle value, and np.sum totals all elements.
import numpy as np data = np.array([4, 8, 6, 5, 3, 7]) print(np.mean(data)) # 5.0 print(np.median(data)) # 5.5 print(np.sum(data)) # 33
np.mean and np.sum accept an optional dtype parameter to control the output type. For integer arrays, the default dtype is float64 for mean, while sum returns an integer type unless specified. This behavior matters when you need a specific precision or when working with large values.
Understanding the Axis Parameter for Multi-dimensional Arrays
For 2D or higher-dimensional arrays, the axis parameter determines along which dimension the operation is performed. Without axis, the function operates on the flattened array. With axis=0, the operation is column-wise; with axis=1, it is row-wise.
matrix = np.array([[1, 2, 3], [4, 5, 6]]) print(np.mean(matrix)) # 3.5 (all elements) print(np.mean(matrix, axis=0)) # [2.5 3.5 4.5] (per column) print(np.mean(matrix, axis=1)) # [2. 5.] (per row)
The same logic applies to np.sum, np.var, and np.std. For higher-dimensional arrays, you can pass a tuple to axis to operate on multiple axes simultaneously. This is useful for reducing a 3D array to a 2D result without reshaping.
Variance and Standard Deviation: Population vs Sample
np.var and np.std compute the population variance and standard deviation by default, dividing by N. To obtain the sample statistics, which divide by N−1, pass ddof=1. This is a common source of confusion when comparing results with Python's statistics module or with spreadsheet functions.
data = np.array([2, 4, 4, 4, 5, 5, 7, 9]) print(np.var(data)) # 4.0 (population) print(np.var(data, ddof=1)) # 4.571 (sample) print(np.std(data)) # 2.0 print(np.std(data, ddof=1)) # 2.138
The ddof parameter is the delta degrees of freedom. For most scientific applications, the population version is appropriate if you have the entire dataset; use ddof=1 when working with a sample that estimates a larger population.
Handling NaN Values with Nan-aware Functions
NumPy provides np.nanmean, np.nanmedian, np.nansum, np.nanvar, and np.nanstd to ignore NaN values. These are essential when working with real-world datasets that contain missing values.
data_with_nan = np.array([1.0, np.nan, 3.0, 4.0]) print(np.nanmean(data_with_nan)) # 2.6667 print(np.nanmedian(data_with_nan)) # 3.0 print(np.nansum(data_with_nan)) # 8.0
The nan-aware functions skip NaN entries and compute the statistic over the remaining values. They also support the axis parameter. Note that if all values are NaN, the result is NaN (or a warning is raised, depending on the function).
Performance Considerations: Vectorization vs Python Loops
NumPy's functions are implemented in C and operate on contiguous memory blocks. This makes them significantly faster than equivalent Python loops for large arrays. The performance advantage grows with array size because the loop overhead is eliminated and the underlying operations are optimized.
import time large_array = np.random.rand(10_000_000) # Vectorized start = time.time() mean_vec = np.mean(large_array) end = time.time() print(f"Vectorized: {end - start:.4f} seconds") # Python loop start = time.time() total = 0 for value in large_array: total += value mean_loop = total / len(large_array) end = time.time() print(f"Loop: {end - start:.4f} seconds")
The vectorized version is typically orders of magnitude faster. However, the exact speedup depends on the hardware and array size. For small arrays, the overhead of calling NumPy may be comparable to a loop, but for production workloads, vectorization is the preferred approach.
Memory Usage and dtype Considerations
When computing statistics, the memory footprint depends on the input array's dtype. For example, np.sum on an integer array may return an integer, but if the sum exceeds the maximum representable value for that integer type, it will overflow silently. Using dtype=np.int64 or dtype=np.float64 can prevent this.
int_array = np.array([2**31, 2**31], dtype=np.int32) print(np.sum(int_array)) # Overflow to -2147483648 on some platforms print(np.sum(int_array, dtype=np.int64)) # Correct result 4294967296
Similarly, np.mean always returns a floating-point result, but the precision is determined by the input and the dtype parameter. For high-precision needs, use dtype=np.float64 or dtype=np.float128 if supported.
Edge Cases: Empty Arrays and Degenerate Inputs
When an array is empty, np.mean and np.median return NaN (with a warning), while np.sum returns 0. np.var and np.std also return NaN. This behavior is consistent with mathematical definitions, but it's important to handle these cases in your code.
empty = np.array([]) print(np.mean(empty)) # nan (RuntimeWarning) print(np.sum(empty)) # 0
For np.median, an empty array raises an error if you call it directly, but np.nanmedian returns NaN. Always check for empty inputs before applying statistical functions if your data pipeline can produce them.
Choosing Between NumPy and Python's statistics Module
Python's standard library includes a statistics module with functions like statistics.mean, statistics.median, statistics.variance, and statistics.stdev. These are useful for small lists and are part of the standard library, but they lack vectorization and axis support. NumPy is the better choice for arrays, matrices, or any data that benefits from vectorized operations. Use statistics only for simple scripts or when you need to avoid a NumPy dependency.