![]() |
VOOZH | about |
The numpy.cov() function is used to calculate the covariance matrix of one or more numerical variables. Covariance shows how two variables change together. A positive value means both variables increase together, a negative value means one increases while the other decreases, and zero means there is no linear relationship.
This example showing how to calculate the covariance of a single 2D array where each row represents a variable.
[[1. 1.] [1. 1.]]
Explanation:
numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None)
Parameters:
Example 1: This example calculates the covariance matrix of a 2D array where each row is a variable and columns are observations.
[[4.33333333 2.83333333 2. ] [2.83333333 2.33333333 1.5 ] [2. 1.5 1. ]]
Explanation:
Example 2: This example finds covariance between two separate lists by stacking them as rows.
[[2.0625 0.925 ] [0.925 0.43666667]]
Explanation:
Example 3: This example shows how covariance changes when variables are arranged column-wise using rowvar=False.
[[2.0625 0.925 ] [0.925 0.43666667]]
Explanation: rowvar=False tells NumPy that each column is a variable