TutorialsPythonCorrelation Analysis

Correlation Analysis

Find relationships between variables using Pearson correlation and Seaborn heatmaps

Correlation measures how strongly two variables are related. A positive correlation means as one increases, the other tends to increase. A negative correlation means they move in opposite directions. Correlation analysis is a standard part of EDA — it reveals which factors drive your key metric and which variables can be used to predict others.

Example

Correlation analysis workflow
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_csv("sales_data.csv")

# Pearson correlation coefficient (-1 to +1)
# +1 = perfect positive, 0 = no relation, -1 = perfect negative
corr_matrix = df.select_dtypes("number").corr()
print(corr_matrix)

# Correlation with one specific column
target_corr = corr_matrix["Sales"].sort_values(ascending=False)
print(target_corr)
# Sales         1.000000  ← self
# Marketing     0.842100  ← strong positive
# Store_Size    0.614300  ← moderate positive
# Returns      -0.421000  ← moderate negative
# Price        -0.023000  ← near zero (no relation)

# Visualise
plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, annot=True, fmt=".2f",
            cmap="RdYlGn", center=0, vmin=-1, vmax=1)
plt.title("Correlation Matrix")
plt.tight_layout()
plt.show()

# Scatter plot for two correlated variables
sns.scatterplot(data=df, x="Marketing", y="Sales", alpha=0.6)
plt.title("Marketing Spend vs Sales")
plt.show()
💡 Correlation does not imply causation. High correlation between two variables might be driven by a third variable (confounding), or be coincidental.

Key Points

  • Pearson r: 0.8–1.0 = strong, 0.5–0.8 = moderate, 0–0.5 = weak correlation
  • Correlation measures LINEAR relationship — it misses non-linear patterns
  • .corr() default is Pearson; use method="spearman" for non-normal/ordinal data
  • Always visualise correlation with scatter plots — the number alone can be misleading
  • Multicollinearity (correlated predictors) is a problem in regression models

Practice Question

A correlation coefficient of -0.85 between Returns and Sales indicates:

Related Topics

Descriptive Statistics in PythonCalculate mean, median, mode, variance, standard deviation and percentilesSeaborn for Statistical ChartsCreate beautiful distribution, correlation and categorical charts with SeabornFeature Engineering for AnalystsCreate new meaningful columns from existing data to improve analysis and modelling