Python | numpy.cov() function Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Covariance provides the measure of strength of correlation between two variable or more set of variables. The covariance matrix element Cij is the covariance of xi and xj. The element Cii is the variance of xi. If COV(xi, xj) = 0 then variables are uncorrelatedIf COV(xi, xj) > 0 then variables positively correlatedIf COV(xi, xj) < 0 then variables negatively correlatedSyntax: numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)Parameters: m : [array_like] A 1D or 2D variables. variables are columns y : [array_like] It has the same form as that of m. rowvar : [bool, optional] If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: bias : Default normalization is False. If bias is True it normalize the data points. ddof : If not None the default value implied by bias is overridden. Note that ddof=1 will return the unbiased estimate, even if both fweights and aweights are specified. fweights : fweight is 1-D array of integer frequency weights aweights : aweight is 1-D array of observation vector weights.Returns: It returns ndarray covariance matrixExample #1: Python # Python code to demonstrate the # use of numpy.cov import numpy as np x = np.array([[0, 3, 4], [1, 2, 4], [3, 4, 5]]) print("Shape of array:\n", np.shape(x)) print("Covariance matrix of x:\n", np.cov(x)) Output: Shape of array: (3, 3)Covariance matrix of x: [[ 4.33333333 2.83333333 2. ] [ 2.83333333 2.33333333 1.5 ] [ 2. 1.5 1. ]]Example #2: Python # Python code to demonstrate the # use of numpy.cov 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 columns cov_mat = np.stack((x, y), axis = 0) print(np.cov(cov_mat)) Output[[ 2.03629167 0.9313 ] [ 0.9313 0.4498 ]]Example #3: Python # Python code to demonstrate the # use of numpy.cov 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)) Outputshape 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 Python | numpy.cov() function S shrikanth13 Follow Improve Article Tags : Python Python-numpy Python numpy-Statistics Functions Practice Tags : python Similar Reads numpy.correlate() function - Python numpy.correlate() function defines the cross-correlation of two 1-dimensional sequences. This function computes the correlation as generally defined in signal processing texts: c_{av}[k] = sum_n a[n+k] * conj(v[n]) Syntax : numpy.correlate(a, v, mode = 'valid') Parameters : a, v : [array_like] Input 1 min read numpy.cos() in Python numpy.cos(x[, out]) = ufunc 'cos') : This mathematical function helps user to calculate trigonometric cosine for all x(being the array elements). Parameters : array : [array_like]elements are in radians. 2pi Radians = 360 degrees Return : An array with trigonometric cosine of x for all x i.e. array 2 min read numpy.fromfunction() function â Python numpy.fromfunction() function construct an array by executing a function over each coordinate and the resulting array, therefore, has a value fn(x, y, z) at coordinate (x, y, z). Syntax : numpy.fromfunction(function, shape, dtype) Parameters : function : [callable] The function is called with N para 2 min read numpy.outer() function - Python numpy.outer() function compute the outer product of two vectors. Syntax : numpy.outer(a, b, out = None) Parameters : a : [array_like] First input vector. Input is flattened if not already 1-dimensional. b : [array_like] Second input vector. Input is flattened if not already 1-dimensional. out : [nda 1 min read numpy.roots() function - Python numpy.roots() function return the roots of a polynomial with coefficients given in p. The values in the rank-1 array p are coefficients of a polynomial. If the length of p is n+1 then the polynomial is described by: p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n] Syntax : numpy.roots(p) Parame 1 min read numpy.cosh() in Python The numpy.cosh() is a mathematical function that helps user to calculate hyperbolic cosine for all x(being the array elements). Equivalent to 1/2 * (np.exp(x) - np.exp(-x)) and np.cos(1j*x). Syntax : numpy.cosh(x[, out]) = ufunc 'cos') Parameters : array : [array_like] elements are in radians. 2pi R 2 min read numpy.nanstd() function - Python numpy.nanstd() function compute the standard deviation along the specified axis, while ignoring NaNs. Syntax : numpy.nanstd(arr, axis = None, dtype = None, out = None, ddof = 0, keepdims) Parameters : arr : [array_like] Calculate the standard deviation of the non-NaN values. axis : [{int, tuple of i 2 min read Numpy ndarray.dot() function | Python The numpy.ndarray.dot() function computes the dot product of two arrays. It is widely used in linear algebra, machine learning and deep learning for operations like matrix multiplication and vector projections.Example:Pythonimport numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) result = 2 min read Numpy recarray.dot() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Numpy recarray.cumprod() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 2 min read Like