Machine LearningLesson 4 of 22

Math You Actually Need for ML

You do not need to be a mathematician to do machine learning. Most of the calculus is handled by libraries. What you do need is intuition for vectors, averages, spread, and how to split data to test fairly.

You do not need to be a mathematician to do machine learning. Most of the calculus is handled by libraries. What you do need is intuition for vectors, averages, spread, and how to split data to test fairly.

A vector is just a list of numbers, like [bedrooms, sqft, age]. Models treat each house as a point in space. Mean is the center, variance tells you how spread out the points are, and correlation tells you if two columns move together.

The most important math is not a formula but a habit: always keep some data aside to test. If you train and test on the same houses, you will think you are perfect and fail on new houses.

Mean, variance, and scaling

If sqft is 2000 and bedrooms is 3, sqft dominates distance. Scaling brings columns to similar ranges so no feature bullies the model. StandardScaler makes mean 0 and variance 1.

PYTHON
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

Train/test intuition

Split 80% train, 20% test before any training. Never let the model see the test set until the final check, otherwise you leak information and your score is fake.

PYTHON
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Syntax

PYTHON
import numpy as np
mean = np.mean([1,2,3,4])  # 2.5
var = np.var([1,2,3,4])    # 1.25
# correlation
np.corrcoef([1,2,3], [2,4,6])[0,1]  # 1.0 = perfect

Not shuffling before split leaks order

If your CSV is sorted by price or date, a naive split gives train as cheap houses and test as expensive ones. Always shuffle or use train_test_split with shuffle=True (default). This Reddit r/MachineLearning top post explains 90% of “my model fails on new data”.

PYTHON
train_test_split(X, y, shuffle=True, random_state=42)  # default is True, keep it

Good to know

  • Vectors are lists of numbers per sample.
  • Scale features so large numbers do not dominate.
  • Always split train/test before training.

Keep building your ML models

Copy the code, run it locally with pip install scikit-learn pandas, and continue to the next lesson.

→ Next: Datasets, CSVs and Missing Values