
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute Covariance Matrix of Two Given Numpy Arrays
Covariance is the measure of two variables that defines how they are related to each other. In other words, it measures the extent to which one variable is associated with the changes in the other variable.
When the covariance of the variables is positive, it implies that both variables are moving in the same direction i.e. if one variable tends to increase, as a result, the value of the other variable also increases. When the covariance of the variables is negative then it represents the two variables are moving in opposite direction i.e. if one variable increases the value of the other decreases.
Calculating the covariance
Mathematically the covariance is defined as the product of mean of the deviations of the two variables X and Y respectively. The magnitude and sign of the covariance depends upon the scales of the variables X and Y.
The covariance can be standardized by dividing the covariance by the product of the standard deviations of X and Y. and the resultant value is the covariance coefficient.
The mathematical formula for the covariance is given as follows.
cov(X,Y) = E[(X - E[X]) * (Y - E[Y])]
Where,
E[X] is the mean of X.
E[Y] is the mean of Y.
cov(X,Y) is the covariance of X and Y.
Covariance in Numpy
In Numpy library, we have a function named cov() using which we can calculate the covariance of the two variables. It accepts two arguments which can be 1-d array or 2-d arrays.
Example
In the following example, when we pass two 2-d arrays as the input arguments to the cov() function, then the covariance of the two arrays will be calculated.
import numpy as np arr1 = np.array([[23.5,22,14],[67,2,7]]) arr2 = np.array([[4,22,1],[7,2,24]]) covariance = np.cov(arr1,arr2) print("The covariance of 2-d arrays:",covariance)
Output
The covariance of 2-d arrays: [[ 26.08333333 104.58333333 28.25 -55. ] [ 104.58333333 1308.33333333 -182.5 -97.5 ] [ 28.25 -182.5 129. -100.5 ] [ -55. -97.5 -100.5 133. ]]
Example
Let's see another example to calculate the covariance of the 2-d arrays using the cov() function of the Numpy library.
import numpy as np arr1 = np.array([[34,19],[8,45]]) arr2 = np.array([[273,89],[90,24]]) covariance = np.cov(arr1,arr2) print("The covariance of 2-d arrays:", covariance)
Output
The covariance of 2-d arrays: [[ 112.5 -277.5 1380. 495. ] [ -277.5 684.5 -3404. -1221. ] [ 1380. -3404. 16928. 6072. ] [ 495. -1221. 6072. 2178. ]]
Example
Now let us try to calculate the covariance of 1-d arrays using the cov() function -
import numpy as np arr1 = np.array([23.5,22,14]) arr2 = np.array([4,22,1]) covariance = np.cov(arr1,arr2) print("The covariance of 1-d arrays:",covariance)
Output
The covariance of 1-d arrays: [[ 26.08333333 28.25 ] [ 28.25 129. ]]
Example
Following is an another example to calculate the covariance of the 1-d arrays which are passed as the input arguments to the cov() function of the Numpy library.
import numpy as np arr1 = np.array([67,2,7]) arr2 = np.array([7,2,24]) covariance = np.cov(arr1,arr2) print("The covariance of 1-d arrays:",covariance)
Output
The covariance of 1-d arrays: [[1308.33333333 -97.5 ] [ -97.5 133. ]]
Example
Let's see another example which deals with the cov() function of the Numpy library for calculating the covariance of the 1-d array.
import numpy as np arr1 = np.arange(10,16,4) arr2 = np.arange(20,26,4) covariance = np.cov(arr1,arr2) print("The covariance of 1-d arrays:",covariance)
Output
The covariance of 1-d arrays: [[8. 8.] [8. 8.]]