Updated April 11, 2023
Introduction to NumPy covariance
The following article provides an outline for NumPy covariance. The measure of strength of correlation between two or more than two set of variables is called covariance and a matrix which is square and symmetric used to describe the covariance between two or more than two set of variables is called covariance matrix. And this covariance matrix is used to capture all the possibilities of how the variables present in a given dataset can change together and the function used to calculate the covariance matrix in python is called covariance function denoted by cov(). And if there are two elements i and j in a matrix, the covariance of i and j is covariance matrix element denoted by Cij.
Syntax:
numpy.cov(array_name, axis)
Where,
- arrayname is the name of the array whose covariance matrix must be found.
- axis represents the dimension of the array represented by array_name along which the covariance matrix must be found.
Working of covariance Function in NumPy
- The measure of strength of correlation between two or more than two set of variables is called covariance.
- The matrix which is square and symmetric used to describe the covariance between two or more than two set of variables is called covariance matrix.
- The covariance matrix is used to capture all the possibilities of how the variables present in a given dataset can change together.
- The function used to calculate the covariance matrix in python is called covariance function denoted by cov().
- If there are two elements i and j in a matrix, the covariance of i and j is covariance matrix element denoted by Cij.
Examples
Given below are the examples mentioned:
Example #1
Python program to demonstrate function by creating a one dimensional array and using covariance function to find the covariance matrix of the newly created array.
Code:
#importing the package numpy as pn
import numpy as pn
#creating a one dimensional array using the array function and storing it in the variable called newarray
newarray = pn.array([1,2,3,4])
#displaying the elements of the newly created one dimensional array followed by an one line space
print 'The elements of the newly created one dimensional array are:'
print newarray
print '\n'
#making use of covariance function in numpy to find the convariance matrix of the newly created array
covmatrix = pn.cov(newarray)
print 'The covariance matrix of the newly created one dimensional array is'
print covmatrix
Output:
In the above program, numpy package is imported as pn to make use of array and covariance function in numpy to create a new one dimensional array and to find the covariance matrix of the corresponding array. The covariance function in numpy is used to find the covariance matrix of newly created array and is stored in a variable called newarray and then the covariance matrix is displayed as the output on the screen.
Example #2
Python program to demonstrate function by creating a two dimensional array and using covariance function to find the covariance matrix of the newly created array.
Code:
#importing the package numpy as pn
import numpy as pn
#creating a two dimensional array using the array function and storing it in the variable called newarray
newarray = pn.array([[1,2],[3,4]])
#displaying the elements of the newly created two dimensional array followed by an one line space
print 'The elements of the newly created two dimensional array are:'
print newarray
print '\n'
#making use of covariance function in numpy to find the convariance matrix of the newly created array
covmatrix = pn.cov(newarray)
print 'The covariance matrix of the newly created two dimensional array is'
print covmatrix
Output:
In the above program, numpy package is imported as pn to make use of array and covariance function in numpy to create a new two dimensional array and to find the covariance matrix of the corresponding array. The covariance function in numpy is used to find the covariance matrix of newly created array and is stored in a variable called newarray and then the covariance matrix is displayed as the output on the screen.
Example #3
Python program to demonstrate function by creating a two dimensional array and using covariance function to find the covariance matrix of the newly created array.
Code:
#importing the package numpy as pn
import numpy as pn
#creating a two dimensional array using the array function and storing it in the variable called newarray
newarray = pn.array([[8,2],[1,3]])
#displaying the elements of the newly created two dimensional array followed by an one line space
print 'The elements of the newly created two dimensional array are:'
print newarray
print '\n'
#making use of covariance function in numpy to find the convariance matrix of the newly created array
covmatrix = pn.cov(newarray)
print 'The covariance matrix of the newly created two dimensional array is'
print covmatrix
Output:
In the above program, numpy package is imported as pn to make use of array and covariance function in numpy to create a new two dimensional array and to find the covariance matrix of the corresponding array. This function in numpy is used to find the covariance matrix of newly created array and is stored in a variable called newarray and then the covariance matrix is displayed as the output on the screen.
Recommended Articles
This is a guide to NumPy covariance. Here we discuss the introduction, working of covariance function in NumPy and examples respectively. You may also have a look at the following articles to learn more –