0
0
ML Pythonprogramming~5 mins

Correlation analysis in ML Python

Choose your learning style9 modes available
Introduction

Correlation analysis helps us find how two things change together. It shows if one thing goes up when the other goes up or down.

To check if hours studied and exam scores are related.
To see if temperature affects ice cream sales.
To find if age and blood pressure move together.
To understand if advertising budget impacts product sales.
To explore relationships between features before building a model.
Syntax
ML Python
import pandas as pd
correlation = df['column1'].corr(df['column2'])
Use pandas DataFrame columns to calculate correlation.
The result is a number between -1 and 1 showing strength and direction.
Examples
This example finds correlation between height and weight.
ML Python
import pandas as pd

# Create sample data
data = {'height': [150, 160, 170, 180], 'weight': [50, 60, 65, 80]}
df = pd.DataFrame(data)

# Calculate correlation
corr = df['height'].corr(df['weight'])
print(corr)
Shows correlation close to zero for unrelated data.
ML Python
import numpy as np
import pandas as pd

# Random data with no relation
np.random.seed(0)
data = {'x': np.random.rand(100), 'y': np.random.rand(100)}
df = pd.DataFrame(data)

print(df['x'].corr(df['y']))
Sample Program

This program calculates how study hours and scores relate. A positive number means more study hours usually mean higher scores.

ML Python
import pandas as pd

# Sample data for study hours and scores
data = {'study_hours': [1, 2, 3, 4, 5], 'scores': [50, 55, 65, 70, 80]}
df = pd.DataFrame(data)

# Calculate correlation
correlation = df['study_hours'].corr(df['scores'])
print(f"Correlation between study hours and scores: {correlation:.2f}")
OutputSuccess
Important Notes

Correlation does not mean one thing causes the other.

Values near 1 or -1 show strong relationships; near 0 means weak or no relationship.

Use correlation to understand data before building models.

Summary

Correlation shows how two variables move together.

It is a number between -1 and 1.

Positive means both go up, negative means one goes up while the other goes down.