VOOZH about

URL: https://www.geeksforgeeks.org/python/convert-covariance-matrix-to-correlation-matrix-using-python/

⇱ Convert Covariance Matrix to Correlation Matrix using Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert Covariance Matrix to Correlation Matrix using Python

Last Updated : 30 Sep, 2025

In statistics, covariance measures how variables vary together, while correlation standardizes this relationship to a value between -1 and 1, making it easier to interpret. In this article, we will be discussing relationship between Covariance and Correlation and program our own function for calculating covariance and correlation using python.

Relationship Between Covariance and Correlation

Correlation is just normalized Covariance refer to the formula below:

where  are the standard deviation of x and y respectively. 

Program to convert Covariance to Correlation matrix 

We will use the Iris dataset for demonstration. The goal is to first compute the covariance matrix manually and then convert it to a correlation matrix.

1. Loading and displaying the dataset

Output

👁 irisDataset

In this example, we exclude the target column (species) since we only want numeric features:

2. Define a Function to Calculate Covariance Between Two Variables

The covariance between two variables x and y measures how much they vary together.

Explanation:

  • x.mean() and y.mean() calculate the average of each variable.
  • The formula multiplies deviations of each observation from the mean and averages them.

3. Compute the Covariance Matrix

We can now build the full covariance matrix for all numeric features.

Output

👁 Output_1

Explanation:

  • cov_matrix[i][j] stores covariance between the i-th and j-th feature.
  • The matrix is symmetric because cov(x, y) = cov(y, x)

This manual calculation matches the NumPy function:

rowvar=False ensures columns are treated as features and rows as observations.

Output

👁 Output_2

4. Convert Covariance Matrix to Correlation Matrix

Correlation is simply normalized covariance, dividing by the standard deviations of each variable.

Output

👁 Output_3

Explanation:

  • For each pair of features (i, j), the covariance is normalized by x.std() * y.std() to get correlation.
  • Values are always between -1 and 1, indicating the strength and direction of the relationship.

5. Verify Using NumPy

Output

👁 Output_4

This gives the same correlation matrix in a single step.

Comment
Article Tags:
Article Tags: