VOOZH about

URL: https://www.geeksforgeeks.org/python/compute-the-covariance-matrix-of-two-given-numpy-arrays/

⇱ Compute the covariance matrix of two given NumPy arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Compute the covariance matrix of two given NumPy arrays

Last Updated : 15 Jul, 2025

In NumPy for computing the covariance matrix of two given arrays with help of numpy.cov(). In this, we will pass the two arrays and it will return the covariance matrix of two given arrays.

Syntax: numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)

Example 1:

Output:

[0 1 1]
[2 2 1]

Covariance matrix of the said arrays:
 [[ 0.33333333 -0.16666667]
 [-0.16666667 0.33333333]]

Example 2:

Output:

[2 1 1 4]
[2 2 1 1]

Covariance matrix of the said arrays:
 [[ 2. -0.33333333]
 [-0.33333333 0.33333333]]

Example 3:

Output

[1 2]
[1 2]

Covariance matrix of the said arrays:
 [[0.5 0.5]
 [0.5 0.5]]

Example 4:

Output

shape of matrix x and y: (4, 2)
shape of covariance matrix: (4, 4)
[[ 0.88445 0.51205 0.2793 -0.36575]
 [ 0.51205 0.29645 0.1617 -0.21175]
 [ 0.2793 0.1617 0.0882 -0.1155 ]
 [-0.36575 -0.21175 -0.1155 0.15125]]
Comment