Efficient Ways to Use Numpy cov() Function in Python

In the Numpy module, we have discussed many functions used to operate on the multidimensional array. Sometimes, we come to a situation where we need to find the covariance of the given matrix. So, there is a function in the numpy library, i.e., numpy cov() function. In this tutorial, we will discuss the concept of the Numpy cov() function, which is the measure of the strength of correlation between two or more than two sets of variables is called covariance.

What is the numpy cov() function?

The Numpy cov() function is used to measure the strength of correlation between two or more than two sets of variables is called covariance. The element of covariance matrix Cij is the covariance of xi and xj. The element Cii is the variance of xi.

  • If COV(xi, xj) = 0 then variables are said to be uncorrelated
  • If COV(xi, xj) > 0, then variables are said to be positively correlated
  • and If COV(xi, xj) < 0, then variables are said to be negatively correlated

A square in form and symmetric matrix and used to describe the covariance between two or more than two sets of variables is called a covariance matrix.

Syntax

numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None, dtype = None)

Parameters

  • m: It is an array-like input. It a 1D or 2D array which is having multiple variables. In them, variables are columns.
  • y: It is an array-like optional input. It is an additional set of from which has the same form as that of m.
  • rowvar: It is a boolean value that is an optional input. If the boolean value of rowvar is True, then each row represents a variable, with observations in the columns. Otherwise, the relationship becomes transposed. By default, rowvar is equal to True.
  • bias: It is a boolean value that is an optional input. By default, the normalization is false; if set to True, it normalizes the data points.
  • ddof: It is an integer value that is an optional input. If default value equals not None then, the bias is overridden. If ddof = 1, it will return the unbiased estimate, even if both fweights and aweights are specified.
  • fweights: It is an array-like integer value and optional input. It is a 1D array that tells us about the frequency weights.
  • aweights: It is an array-like integer value and optional input. It is a 1D array that gives the observation on vector weights.
  • dtype: It is the data type that is an optional input.

Return Value of numpy cov() function

The return value of the function is an array of covariance matrix.

Examples of Numpy cov() function

Here, we will be discussing how we can write the cov() from the numpy package of python.

1. Printing covariance of a matrix

In this example, we will be importing the numpy library. Then, we will take the input as an array from numpy and apply the cov() function. Hence, we can see the covariance matrix of the input array. Let us look at the example for understanding the concept in detail.

#import numpy library
import numpy as np
  
arr = np.array([[0, 1, 2], [1, 2, 4], [3, 4, 5]])
  
print("Covarinace matrix of arr: \n", np.cov(arr))

Output:

Covarinace matrix of arr: 
[[1.         1.5        1.        ]
 [1.5        2.33333333 1.5       ]
 [1.         1.5        1.        ]]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take an input array in the variable arr with the numpy module’s help.
  • After that, we will apply the cov() function with putting inside the input array.
  • At last, we have printed the output.
  • Hence, you can see the covariance matrix as the output.

2. Taking two array and axis as parameter

In this example, we will be importing the numpy library. Then, we will take two input arrays and axis as 0 and 1. Then, apply the numpy cov() function, which will calculate the covariance matrix and print it. Let us look at the example for understanding the concept in detail.

#import numpy library
import numpy as np
  
arr = [1.23, 2.12, 3.34, 4.5]
arr1= [2.56, 2.89, 3.76, 3.95]
  
# find out covariance with respect  columns
out = np.stack((arr, arr1), axis = 0) 
print(np.cov(out))
print("\n")

out1 = np.stack((arr, arr1), axis = 1) 
print(np.cov(out1))

Output:

[[2.03629167 0.9313    ]
 [0.9313     0.4498    ]]


[[ 0.88445  0.51205  0.2793  -0.36575]
 [ 0.51205  0.29645  0.1617  -0.21175]
 [ 0.2793   0.1617   0.0882  -0.1155 ]
 [-0.36575 -0.21175 -0.1155   0.15125]]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take the input array as arr and arr1.
  • After that, we will apply the numpy stack() function with both the array and axis as 0 and 1 as the parameter.
  • We have applied both the axis and stored the output in the out and out1 variable.
  • At last, we will print both the output.
  • Hence, you can see the difference between the output by applying both the axis and the covariance gets printed.

3. Taking rowvar as a parameter

In this example, we will be importing the numpy library. Then, we will take an input array with the rowvar as a parameter. Then, we will apply the numpy cov() function to calculate the covariance matrix. Let us look at the example for understanding the concept in detail.

#import numpy library
import numpy as np
  
arr = np.array([[0, 1, 2], [1, 2, 4], [3, 4, 5]])
  
print("Covarinace matrix of arr: \n", np.cov(arr, rowvar =True))

Output:

Covarinace matrix of arr: 
[[1.         1.5        1.        ]
 [1.5        2.33333333 1.5       ]
 [1.         1.5        1.        ]]

Explanation:

  • Firstly, we will import the numpy library with an alias name as np.
  • Then, we will take an input array in the variable arr with the numpy module’s help.
  • After that, we will apply the cov() function with the array and rowvar as the parameter.
  • At last, we have printed the output.
  • Hence, you can see the covariance matrix as the output.

Also See

Conclusion

In this tutorial, we have learned about the concept of the numpy cov() function. We have seen what the numpy cov() function with its syntax and parameters is. We have also discussed all the through which we use the given function. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments