How to find the pairwise correlation coefficients of columns of a pandas DataFrame?

by | Nov 9, 2022 | Machine Learning Using Python, Python Pandas

correlation coefficients. Some of the values are positive, some are negative. And all the values are 1.0 diagonally.

Correlation Coefficients: 
           0         1         2         3
0  1.000000  0.999898 -0.647059  0.993540
1  0.999898  1.000000 -0.657860  0.991822
2 -0.647059 -0.657860  1.000000 -0.556357
3  0.993540  0.991822 -0.556357  1.000000

We know that each column of a DataFrame specifies a feature or attribute that can be expressed using a random variable. So, when we call the function DataFrame.corr(), it computes the correlation coefficients between each pair of variables and gives us the correlation matrix.

So, if we want to find out the correlation coefficient of variable 0 and 1, we need to look into row 0 and column 1 or row 1 and column 0 (both the values are the same). A positive value indicates that the variables are postively correlated. In other words, if we increase one variable, the other variable also increases. And a negative correlation coefficient indicates the variables are negatively corrlated. In other words, if we increase one variable, the other variable decreases.

Moreover, the correlation coefficient is a value between -1 to +1. If the absolute value of the correlation coefficient is more and closer to 1, that means the variables are strongly correlated.

Now, let’s look into the following DataFrame.

DataFrame: 
    0   1  2      3
0  10  12  A   True
1  52  61  B  False
2   9  10  C   True

Only column 0 and column 1 contain numbers. Column 2 contains strings and column 3 contains boolean values. Now, we want to compute the correlation coefficient of column 0 and column 1 only. How should we do that?

We can use the following Python code to compute the correlation coefficient of two columns of a DataFrame.

import pandas

df2 = pandas.DataFrame([[10, 12, 'A', True], [52, 61, 'B', False], [9, 10, 'C', True]])
print("DataFrame: \n", df2)

print("Correlation Coefficients of column 0 and column 1: \n", df2.iloc[:, [0, 1]].corr())

Here, we are using DataFrame.iloc[] to select the first and the second columns only. Please note that …

Facebooktwitterredditpinterestlinkedinmail

Calculate the pseudoinverse of a matrix using Python

What is the pseudoinverse of a matrix? We know that if A is a square matrix with full rank, then A-1 is said to be the inverse of A if the following condition holds: $latex AA^{-1}=A^{-1}A=I $ The pseudoinverse or the Moore-Penrose inverse of a matrix is a...

Cholesky decomposition using Python

What is Cholesky decomposition? A square matrix A is said to have Cholesky decomposition if it can be written as a product of a lower triangular matrix and its conjugate transpose. $latex A=LL^{*} $ If all the entries of A are real numbers, then the conjugate...

Tensor Hadamard Product using Python

In one of our previous articles, we already discussed what the Hadamard product in linear algebra is. We discussed that if A and B are two matrices of size mxn, then the Hadamard product of A and B is another mxn matrix C such that: $latex H_{i,j}=A_{i,j} \times...

Perform tensor addition and subtraction using Python

We can use numpy nd-array to create a tensor in Python. We can use the following Python code to perform tensor addition and subtraction. import numpy A = numpy.random.randint(low=1, high=10, size=(3, 3, 3)) B = numpy.random.randint(low=1, high=10, size=(3, 3, 3)) C =...

How to create a tensor using Python?

What is a tensor? A tensor is a generalization of vectors and matrices. It is easily understood as a multidimensional array. For example, in machine learning, we can organize data in an m-way array and refer it as a data tensor. Data related to images, sounds, movies,...

How to combine NumPy arrays using horizontal stack?

We can use the hstack() function from the numpy module to combine two or more NumPy arrays horizontally. For example, we can use the following Python code to combine three NumPy arrays horizontally. import numpy A = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) B =...

How to combine NumPy arrays using vertical stack?

Let’s say we have two or more NumPy arrays. We can combine these NumPy arrays vertically using the vstack() function from the numpy module. For example, we can use the following Python code to combine three NumPy arrays vertically. import numpy A = numpy.array([[1, 2,...

Singular Value Decomposition (SVD) using Python

What is Singular Value Decomposition (SVD)? Let A be an mxn rectangular matrix. Using Singular Value Decomposition (SVD), we can decompose the matrix A in the following way: $latex A_{m \times n}=U_{m \times m}S_{m \times n}V_{n \times n}^T $ Here, U is an mxm matrix....

Eigen decomposition of a square matrix using Python

Let A be a square matrix. Let’s say A has k eigenvalues λ1, λ2, ... λk. And the corresponding eigenvectors are X1, X2, ... Xk. $latex X_1=\begin{bmatrix} x_{11} \\ x_{21} \\ x_{31} \\ ... \\ x_{k1} \end{bmatrix} \\ X_2=\begin{bmatrix} x_{12} \\ x_{22} \\ x_{32} \\ ......

How to calculate eigenvalues and eigenvectors using Python?

In our previous article, we discussed what eigen values and eigenvectors of a square matrix are and how we can calculate the eigenvalues and eigenvectors of a square matrix mathematically. We discussed that if A is a square matrix, then $latex (A- \lambda I) \vec{u}=0...

Amrita Mitra

Author

Ms. Amrita Mitra is an author, who has authored the books “Cryptography And Public Key Infrastructure“, “Web Application Vulnerabilities And Prevention“, “A Guide To Cyber Security” and “Phishing: Detection, Analysis And Prevention“. She is also the founder of Asigosec Technologies, the company that owns The Security Buddy.

0 Comments

Submit a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Not a premium member yet?

Please follow the link below to buy The Security Buddy Premium Membership.

Featured Posts

Translate »