Compute the covariance matrix of two given NumPy arrays Last Updated : 29 Aug, 2020 Comments Improve Suggest changes Like Article Like Report 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: Python import numpy as np array1 = np.array([0, 1, 1]) array2 = np.array([2, 2, 1]) # Original array1 print(array1) # Original array2 print(array2) # Covariance matrix print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2)) Output: [0 1 1] [2 2 1] Covariance matrix of the said arrays: [[ 0.33333333 -0.16666667] [-0.16666667 0.33333333]] Example 2: Python import numpy as np array1 = np.array([2, 1, 1, 4]) array2 = np.array([2, 2, 1, 1]) # Original array1 print(array1) # Original array2 print(array2) # Covariance matrix print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2)) Output: [2 1 1 4] [2 2 1 1] Covariance matrix of the said arrays: [[ 2. -0.33333333] [-0.33333333 0.33333333]] Example 3: Python import numpy as np array1 = np.array([1, 2]) array2 = np.array([1, 2]) # Original array1 print(array1) # Original array2 print(array2) # Covariance matrix print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2)) Output [1 2] [1 2] Covariance matrix of the said arrays: [[0.5 0.5] [0.5 0.5]] Example 4: Python import numpy as np x = [1.23, 2.12, 3.34, 4.5] y = [2.56, 2.89, 3.76, 3.95] # find out covariance with respect # rows cov_mat = np.stack((x, y), axis = 1) print("shape of matrix x and y:", np.shape(cov_mat)) print("shape of covariance matrix:", np.shape(np.cov(cov_mat))) print(np.cov(cov_mat)) 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 More infoAdvertise with us Next Article Compute the covariance matrix of two given NumPy arrays A avengerjanus123 Follow Improve Article Tags : Python Python-numpy Python numpy-Statistics Functions Practice Tags : python Similar Reads Compute the condition number of a given matrix using NumPy In this article, we will use the cond() function of the NumPy package to calculate the condition number of a given matrix. cond() is a function of linear algebra module in NumPy package. Syntax: numpy.linalg.cond(x, p=None) Example 1: Condition Number of 2X2 matrix Python3 # Importing library impor 2 min read Compute the Kronecker product of two multidimension NumPy arrays Given an m X n matrix A and a p X q matrix B, their Kronecker product is A â B, also called their matrix direct product, is an (m*p) X (n*q) matrix. A = |â(a00)ââ(a01)â|    â|â(a10)ââ(a11)â| B = |â(b00)ââ(b01)â|    â|â(b10)ââ(b11)â| A â B = |â(a00)*(b00)ââ(a00)*(b01)ââ(a01)*(b00)ââ(a01)*(b00)â| 2 min read Generate a matrix product of two NumPy arrays We can multiply two matrices with the function np.matmul(a,b). When we multiply two arrays of order (m*n) and  (p*q ) in order to obtained matrix product then its output contains m rows and q columns where n is n==p is a necessary condition. Syntax: numpy.matmul(x1, x2, /, out=None, *, casting='same 2 min read Compute the determinant of a given square array using NumPy in Python In Python, the determinant of a square array can be easily calculated using the NumPy package. This package is used to perform mathematical calculations on single and multi-dimensional arrays. numpy.linalg is an important module of NumPy package which is used for linear algebra. We can use det() fun 2 min read Compute pearson product-moment correlation coefficients of two given NumPy arrays In NumPy, We can compute pearson product-moment correlation coefficients of two given arrays with the help of numpy.corrcoef() function. In this function, we will pass arrays as a parameter and it will return the pearson product-moment correlation coefficients of two given arrays. Syntax: numpy.corr 1 min read Like