Python | numpy.cov() function Last Updated : 11 Jul, 2025 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 info S shrikanth13 Follow Improve Article Tags : Python Python-numpy Python numpy-Statistics Functions Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like