Create a correlation Matrix using Python Last Updated : 18 May, 2025 Comments Improve Suggest changes Like Article Like Report Correlation matrix is a table that shows how different variables are related to each other. Each cell in the table displays a number i.e. correlation coefficient which tells us how strongly two variables are together. It helps in quickly spotting patterns, understand relationships and making better decisions based on data. A correlation matrix can be created using two libraries: 1. Using NumPy Library NumPy provides a simple way to create a correlation matrix. We can use the np.corrcoef() function to find the correlation between two or more variables.Example: A daily sales and temperature record is kept by an ice cream store. To find the relationship between sales and temperature, we can utilize the NumPy library where x is sales in dollars and y is the daily temperature. Python import numpy as np x = [215, 325, 185, 332, 406, 522, 412, 614, 544, 421, 445, 408], y = [14.2, 16.4, 11.9, 15.2, 18.5, 22.1, 19.4, 25.1, 23.4, 18.1, 22.6, 17.2] matrix = np.corrcoef(x, y) print(matrix) Output: [[1. 0.95750662] [0.95750662 1. ]]2. Using Pandas library Pandas is used to create a correlation matrix using its built-in corr() method. It helps in analyzing and interpreting relationships between different variables in a dataset.Example: Let's create a simple DataFrame with three variables and calculate correlation matrix. Python import pandas as pd data = { 'x': [45, 37, 42, 35, 39], 'y': [38, 31, 26, 28, 33], 'z': [10, 15, 17, 21, 12] } dataframe = pd.DataFrame(data, columns=['x', 'y', 'z']) print("Dataframe is : ") print(dataframe) matrix = dataframe.corr() print("Correlation matrix is : ") print(matrix) Output: Using PandasExample with Real Dataset (Iris Dataset) In this example we will consider Iris dataset and find correlation between the features of the dataset. dataset = datasets.load_iris(): Loads Iris dataset from sklearn which contains data on flowers features like petal and sepal length/width.dataframe["target"] = dataset.target: Adds target column which contains the species of the iris flowers to the DataFrame. Python from sklearn import datasets import pandas as pd dataset = datasets. load_iris() dataframe = pd.DataFrame(data = dataset.data,columns = dataset.feature_names) dataframe["target"] = dataset.target matrix = dataframe.corr() print(matrix) Output: Using IRIS datasetBy using libraries like NumPy and Pandas creating a correlation matrix in Python becomes easy and helps in understanding the hidden relationships between different variables in a dataset.Related Articles: Correlation: Meaning, Significance, Types and Degree of Correlation Correlation Matrix in R Programming How to Create a Correlation Matrix using Pandas? Exploring Correlation in Python Plotting Correlation Matrix using Python Comment More infoAdvertise with us Next Article Create a correlation Matrix using Python R rohanchopra96 Follow Improve Article Tags : Data Science AI-ML-DS Python-numpy Python-pandas AI-ML-DS With Python +1 More Similar Reads How to Create a Correlation Matrix using Pandas? Correlation Matrix is a statistical technique used to measure the relationship between two variables. Using Pandas, you can easily generate a correlation matrix to understand how features relate whether they move together, in opposite directions, or show no clear trend. Letâs explore various effecti 3 min read Convert covariance matrix to correlation matrix using Python In this article, we will be discussing the relationship between Covariance and Correlation and program our own function for calculating covariance and correlation using python. Covariance: It tells us how two quantities are related to one another say we want to calculate the covariance between x and 5 min read How to Plot a Correlation Matrix into a Graph Using R A correlation matrix is a table showing correlation coefficients between sets of variables. It's a powerful tool for understanding relationships among variables in a dataset. Visualizing a correlation matrix as a graph can provide clearer insights into the data. This article will guide you through t 4 min read How to Create Correlation Heatmap in R In this article let's check out how to plot a Correlation Heatmap in R Programming Language. Analyzing data usually involves a detailed analysis of each feature and how it's correlated with each other. It's essential to find the strength of the relationship between each feature or in other words how 6 min read Correlation Matrix in R Programming Correlation refers to the relationship between two variables, specifically the degree of linear association between them. In R, a correlation matrix represents this relationship as a range of values between -1 and 1.A value of -1 indicates a perfect negative linear relationship.A value of 1 indicate 5 min read How to Calculate Autocorrelation in Python? Autocorrelation measures how a signal or time series relates to a delayed version of itself over varying time lags. For example, given a time series [2, 3, 5, 7, 11], the autocorrelation at lag 1 can reveal how the series correlates with itself shifted by one time step. Letâs explore different metho 2 min read Cross-correlation Analysis in Python Cross-correlation analysis is a powerful technique in signal processing and time series analysis used to measure the similarity between two series at different time lags. It reveals how one series (reference) is correlated with the other (target) when shifted by a specific amount. This information i 5 min read How to Calculate Rolling Correlation in Python? Correlation generally determines the relationship between two variables. The rolling correlation measure the correlation between two-time series data on a rolling window Rolling correlation can be applied to a specific window width to determine short-term correlations. Calculating Rolling Correlati 2 min read Exploring Correlation in Python This article aims to give a better understanding of a very important technique of multivariate exploration. A correlation Matrix is basically a covariance matrix. Also known as the auto-covariance matrix, dispersion matrix, variance matrix, or variance-covariance matrix. It is a matrix in which the 4 min read Like