NumPy | cov method
Start your free 7-days trial now!
Numpy's cov(~) method computes the covariance given two arrays.
Parameters
1. x | array-like
Each row represents a separate column data (i.e. a variable). See the example below for clarification.
2. y | array-like | optional
Instead of specifying your dataset as a new row in x, you can just specify it as y.
3. rowvar | boolean | optional
If True, then each row in x represents a variable. If False, then each column in x represents a variable. By default, rowvar=True.
4. bias | boolean | optional
Whether to compute the biased estimate of the covariance, or the unbiased one. By default, bias=False (sample covariance).
5. ddof | int | optional
The delta degree of freedom. This can be used to modify the denominator in the front:
By default, ddof=0.
6. fweights | array-like | optional
The number of times each observation should be repeated. See example below for clarification.
7. aweights | array-like | optional
A 1D array of weights placed on each observation. The higher the weight, the more important the observation is.
Return value
If one variable is given, then the a scalar is returned. Otherwise, a Numpy array is returned.
Examples
Computing sample covariance
np.cov([2,3,5,6], [3,5,8,12])
array([[ 3.33333333, 7. ], [ 7. , 15.33333333]])
This is exactly the same as specifying a 2D array:
np.cov([[2,3,5,6], [3,5,8,12]])
array([[ 3.33333333, 7. ], [ 7. , 15.33333333]])
Computing population covariance
np.cov([2,3,5,6], [3,5,8,12], ddof=0)
array([[ 2.5 , 5.25], [ 5.25, 11.5 ]])
You could also set bias=True:
np.cov([2,3,5,6], [3,5,8,12], bias=True)
array([[ 2.5 , 5.25], [ 5.25, 11.5 ]])
Repeating values using fweights
np.cov([2,3,5,6], [3,5,8,12], fweights=[2,1,1,1])
array([[ 3.3 , 6.85], [ 6.85, 14.7 ]])
This is exactly the same as the following:
np.cov([2,2,3,5,6], [3,3,5,8,12])
array([[ 3.3 , 6.85], [ 6.85, 14.7 ]])
That is, the first value of each array was repeated twice, as specified by fweights.