Correlation Matrix

 

What is a Correlation Matrix?

A correlation matrix is a table showing correlation coefficients between variables. Each cell in the table shows the correlation between two variables. The value is between -1 and 1. A value closer to 1 implies a strong positive correlation, while a value closer to -1 implies a strong negative correlation. A value around 0 implies no correlation.

Why is a Correlation Matrix Used in Data Analytics?

  1. Identify Relationships: It helps identify relationships between variables. Understanding these relationships is crucial for predictive modeling.
  2. Feature Selection: By identifying which variables are highly correlated with each other, you can select or remove variables to improve the performance of machine learning models.
  3. Multicollinearity Detection: In regression analysis, it helps to detect multicollinearity, where independent variables are highly correlated
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Example DataFrame
data = {
    'A': np.random.rand(100),
    'B': np.random.rand(100),
    'C': np.random.rand(100),
    'D': np.random.rand(100)
}

df = pd.DataFrame(data)

# Calculating the Correlation Matrix
correlation_matrix = df.corr()

# Displaying the Correlation Matrix
print(correlation_matrix)

# Plotting the Correlation Matrix
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1)
plt.title('Correlation Matrix')
plt.show()

Explanation of the Code

  1. DataFrame Creation: A sample DataFrame df with 100 random values for four variables (A, B, C, D).
  2. Correlation Matrix Calculation: df.corr() calculates the correlation matrix.
  3. Print Correlation Matrix: Display the correlation matrix in the console.
  4. Plot Correlation Matrix: Use Seaborn's heatmap to visualize the correlation matrix.

This example demonstrates how to compute and visualize a correlation matrix, providing insights into relationships among the variables in a dataset.







Comments