NumPy – Using ndarray.var() method (5 examples)

Updated: February 27, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, short for Numerical Python, is a fundamental package for scientific computing in Python. Among its myriad of functionalities is the ndarray.var() method, used to compute the variance along a specified axis of an ndarray object. Variance is a measure of the dispersion of a set of data points in a data set. Knowing how to compute variance is essential in understanding the spread of your data, which can be critical in predictive modeling and analysis.

This tutorial delves into the use of the var() method, a part of the NumPy library which is a staple for data scientists and anyone working with large, numerical datasets in Python. Understanding how to use the var() method effectively can help in statistical analysis, machine learning models, and general data manipulation. Below, we explore 5 examples showcasing different facets of using the var() method, from basic usage to more advanced applications.

Syntax & Parameters

The ndarray.var() method in NumPy computes the variance of array elements along a specified axis. Here’s the syntax:

numpy.ndarray.var(axis=None, dtype=None, out=None, ddof=0)

Parameters:

  • axis: (Optional) Axis or axes along which the variance is computed. By default, the variance is computed over the flattened array.
  • dtype: (Optional) Data type of the returned array. If not specified, the data type of the array is used.
  • out: (Optional) Output array where the result is placed.
  • ddof: (Optional) Delta degrees of freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default, ddof is 0.

Example 1: Basic Usage

import numpy as np
data = np.array([1, 2, 3, 4, 5])
print(data.var())

Output:

2.0

This example demonstrates the most straightforward way to compute the variance of a numpy array. No axis is specified, so the var() method calculates the variance of the flattened array. Here, it is 2.0, reflecting the spread of the data around the mean.

Example 2: Variance Along an Axis

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix.var(axis=0))
print(matrix.var(axis=1))

Output:

[6.  2.  2.]
[0.66666667 0.66666667 0.66666667]

When working with multi-dimensional arrays, you can calculate the variance across different axes. This example demonstrates calculating the variance of each column (axis=0) and each row (axis=1). The variance among columns reflects a larger spread in the dataset as compared to rows.

Example 3: Setting the Degree of Freedom

import numpy as np
data = np.array([1, 2, 3, 4, 5, 6])
print(data.var(ddof=1))

Output:

3.5

The ddof parameter allows for adjusting the degrees of freedom. By default, numpy calculates the population variance with ddof=0. However, if we want to calculate the sample variance, we set ddof=1, which increases the computed variance, as seen above.

Example 4: Using with Multidimensional Arrays

import numpy as np
matrix = np.random.random((3, 4, 5))
print(matrix.var(axis=1))

Output (vary due to the randomness):

[0.08194464 0.09107845 0.08986791]

This example uses a 3-dimensional array, showcasing that var() can effectively compute variance across any dimension of an array, a useful trait when working with complex datasets.

Example 5: Weighted Variance

Numpy does not directly support weighted variance in the var() method. However, we can calculate it manually, combining mean() and sum() for a customized solution. This approach can be particularly useful in cases where data points hold different levels of importance.

import numpy as np
data = np.array([1, 2, 3, 4, 5, 6])
weights = np.array([0.1, 0.1, 0.2, 0.2, 0.2, 0.2])
average = np.average(data, weights=weights)
variance = np.sum(weights * (data - average)**2) / np.sum(weights)
print(variance)

Output:

2.066666666666667

Conclusion

The ndarray.var() method in NumPy is a versatile tool for computing variance, adaptable to various needs and data structures. Through this exploration, ranging from basic to complex examples, we’ve seen how it can fit into any data analysis or manipulation task, illuminating the spread and dispersion of data. Mastering the use of var() can enhance your data interpretive skills, a critical aspect of data science and analytics.