R
Rishtaara
Python: Zero to Professional
Lesson 32 of 40Article16 min

Distribution, Scatter, Scale & Train/Test

Distribution describes how values spread — uniform, skewed, or bell-shaped.

Data Distribution

Distribution describes how values spread — uniform, skewed, or bell-shaped.

Histograms and KDE plots visualize distributions before choosing models.

Real-life example: Data distribution is the shape of crowds at a mall — quiet morning vs packed evening.

Normal Data Distribution

Many natural phenomena follow a bell curve (Gaussian). Mean ≈ median when symmetric.

Linear models and some stats assume near-normal residuals.

Real-life example: Heights of adults cluster around average with fewer very short or very tall — classic bell curve.

Histogram check
import matplotlib.pyplot as plt

data = [68, 70, 72, 71, 69, 73, 71, 70]
plt.hist(data, bins=5)
plt.title("Score distribution")
plt.show()

Scatter Plot

Scatter plots show relationship between two numeric variables — linear, curved, or none.

First step before regression: eyes on the chart.

Real-life example: Scatter plot of study hours vs marks — dots climbing right mean more study helps.

Scatter with Matplotlib
import matplotlib.pyplot as plt

hours = [1, 2, 3, 4, 5]
scores = [55, 62, 70, 78, 88]
plt.scatter(hours, scores)
plt.xlabel("Hours")
plt.ylabel("Score")
plt.show()

Scale

Features on different scales (age 0–100 vs income 0–1M) can bias distance-based models.

Always scale before KNN, SVM, neural nets, and many clustering methods.

Real-life example: Scale is using the same ruler — comparing cm to miles without conversion misleads.

Train/Test

Split data: train on one part, test on unseen rows to measure real performance.

train_test_split from sklearn is the standard pattern.

Real-life example: Train/test is studying with practice papers then taking a new exam — not the same questions.

Train/test split
from sklearn.model_selection import train_test_split

X = [[1], [2], [3], [4], [5]]
y = [2, 4, 6, 8, 10]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
print(len(X_train), len(X_test))