search
Search
Publish
Guest 0reps
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare Twitter Facebook

# NumPy | cov method

Programming
chevron_right
Python
chevron_right
NumPy
chevron_right
Documentation
schedule Mar 10, 2022
Last updated
local_offer PythonNumPy
Tags
expand_more

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:

$$\frac{1}{N\color{blue}{-ddof}}\sum_{i=0}^{N}\left(x_i-\bar{x}^2\right)$$

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.