Python for Machine Learning
Meet the four tools every ML project leans on — NumPy, pandas, scikit-learn, and matplotlib — and learn the one workflow (split → fit → predict → score) that ties them together.
Learn Python for Machine Learning in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice exercise…
Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
🛠️ Real-World Analogy: A Workshop and Its Tools
Picture building a piece of furniture in a workshop. You don't reach for one magic machine — you reach for the right tool at the right step . The Python ML stack works the same way: four specialised tools, each doing one job well, used in a fixed order.
📐 NumPy — the workbench
Fast arrays and matrix maths. Every other tool stacks its work on this surface.
📊 pandas — the parts bins
Labelled tables (DataFrames) to load, clean, filter, and sort your raw materials.
🧪 scikit-learn — the power tools
The algorithms that do the cutting and joining: fit, predict, score — same handles on every one.
📈 matplotlib — the tape measure
Charts to check your work before and after — it measures and inspects, it doesn't build.
Keep this picture in mind: pandas prepares the materials, NumPy holds the numbers, scikit-learn does the work, matplotlib checks it.
1 NumPy Arrays — The Container for the Numbers
A NumPy array is a grid of numbers that all share one type, stored together in memory. That layout is why maths on an array is fast: one operation runs across every element at once — called a vectorised operation — instead of you writing a Python loop.
Two words you'll meet constantly: a 1D array is a vector (a single row of values), and a 2D array is a matrix (a table where rows are samples and columns are features ). The array's .shape tells you how many of each.
Worked example — read the comments, every line states its result:
2 pandas DataFrames — Your Data as a Table
Real data rarely arrives as a tidy grid of one type — it has named columns like age , city , and price , mixing numbers, text, and true/false. A DataFrame is pandas' answer: a labelled table you can program, like a spreadsheet with code.
The move you'll repeat in every project is splitting that table into X (the feature columns the model learns from) and y (the single target column it learns to predict). Everything after this lesson assumes you can do that split.
Notice df[df["temp_c"] > 12] : you build a column of True/False values, then use it to keep only the matching rows. That boolean-filter trick is the single most-used pandas operation.
3 The scikit-learn Pattern — fit / predict / score
scikit-learn's superpower is consistency : a decision tree, a linear model, and a support-vector machine all expose the same three methods. Learn them once and you can drive any model.
- .fit(X, y) — train : learn the patterns linking features X to labels y .
- .predict(X) — use : guess labels for new, unseen rows.
- .score(X, y) — check : for a classifier, the fraction of predictions that were correct (accuracy).
Two supporting pieces make those three honest. train_test_split holds back a slice of data the model never trains on, so the score is earned, not memorised. A Pipeline glues a transformer (something that reshapes the data, like a scaler that re-scales features) to the model, so the transformer is fitted on the training data only — never on the test data.
Worked example — the full pattern in one place:
4 Under the Hood — A Prediction Is Just Arithmetic
np.dot and .score sound advanced, but underneath they are plain arithmetic you can write without any library. Seeing that demystifies the whole stack — and it runs in the editor right now.
A single linear prediction is a dot product : multiply each feature by its weight, sum the results, and add a constant bias . That's all np.dot(features, weights) + bias does.
📈 Where matplotlib Fits
matplotlib (and Seaborn, which is built on top of it) is for seeing your data — it never trains a model. You reach for it at two moments: before modelling to explore patterns, and after to inspect how the model did.
Keep the boundary clear: pandas/NumPy hold the data, scikit-learn models it, matplotlib pictures it. A chart never changes a prediction — it changes your understanding.
! Common Errors (And How to Fix Them)
These four trip up almost every beginner. Spotting them early saves hours.
Scaling or fitting using the whole dataset before splitting:
✅ Fix: split first, then fit on train only (a Pipeline does this for you):
Scoring on the same rows the model trained on:
ValueError: Found input variables with inconsistent numbers of samples — X and y have different lengths, or a 1D array was passed where 2D was expected:
✅ Fix: check shapes line up, and reshape a single feature to 2D:
❌ Scaling after the split, but fitting the scaler on test too
You split correctly, then re-fit the scaler on the test set:
✅ Fix: fit once on train; only transform the test set:
📋 Quick Reference
Call
Does
np.array([1,2,3])
Make an array (vector / matrix)
arr.shape
Rows & columns: (samples, features)
arr * 2, arr + arr2
Vectorised maths, no loop
np.dot(a, b)
Dot / matrix product
arr.mean(), arr.std()
Summary statistics
pd.DataFrame(data)
Build a labelled table
df["col"]
Select one column (a Series)
df[df["x"] > 0]
Filter rows by a condition
df.groupby("g").mean()
Aggregate by group
df.describe()
Quick stats for every column
train_test_split(X, y)
Hold back a test set
model.fit(X_train, y_train)
Train the model
model.predict(X_test)
Guess labels for new data
model.score(X_test, y_test)
Accuracy on held-out data
make_pipeline(scaler, model)
Chain transformer + model, no leakage
❓ Frequently Asked Questions
🎯 Mini-Challenge: Train, Predict, Score (Plain Python)
Put the whole workflow together — no libraries. Your "model" is a simple threshold rule, and you'll score it by hand, exactly the way scikit-learn's .score() works. The starter below is a comment outline only.
Lesson 2 complete — you know the ML toolkit and the workflow!
You can describe a NumPy array and its shape, split a DataFrame into X and y, drive any scikit-learn model with fit / predict / score, split data to avoid leakage, and place matplotlib correctly in the pipeline. You even computed a prediction and an accuracy by hand — so none of it is a black box.
🚀 Up next: Data Preprocessing — turn messy, real-world data into clean features a model can actually learn from.
Practice quiz
Why use a NumPy array instead of a plain Python list for maths?
- It can store text but not numbers
- It automatically trains a model
- It runs vectorised operations in fast compiled code, often far faster than a Python loop
- It never needs a shape
Answer: It runs vectorised operations in fast compiled code, often far faster than a Python loop. NumPy stores one type contiguously and runs math in compiled C, making vectorised ops much faster.
In a 2D NumPy array used for ML, what do rows and columns usually represent?
- Rows are samples, columns are features
- Rows are features, columns are samples
- Both are labels
- Rows are predictions, columns are errors
Answer: Rows are samples, columns are features. Conventionally each row is a sample and each column is a feature; .shape is (samples, features).
What is a pandas DataFrame?
- A single number
- A neural network layer
- A type of plot
- A labelled table with named columns and indexed rows
Answer: A labelled table with named columns and indexed rows. A DataFrame is a programmable, spreadsheet-like labelled table of mixed-type columns.
When splitting a DataFrame for ML, what are X and y?
- X is the target column; y is the feature columns
- X is the feature columns the model learns from; y is the target column it predicts
- X and y are both labels
- X is the index; y is the header
Answer: X is the feature columns the model learns from; y is the target column it predicts. X holds the input feature columns; y is the single target the model learns to predict.
What do scikit-learn's fit, predict, and score methods do?
- fit trains on labelled data; predict guesses labels for new data; score measures accuracy
- fit plots data; predict scales it; score deletes it
- They all train the model
- fit and predict are identical
Answer: fit trains on labelled data; predict guesses labels for new data; score measures accuracy. Every estimator shares fit (train), predict (guess), and score (evaluate) — for classifiers score is accuracy.
Why must you split data into train and test sets before evaluating?
- To make training slower
- Because models require exactly two datasets
- So the score reflects performance on unseen data instead of memorised rows
- To remove all missing values
Answer: So the score reflects performance on unseen data instead of memorised rows. Evaluating on held-out data gives an honest estimate; scoring on training rows just measures memorisation.
What is data leakage in this context?
- Saving the model to the wrong folder
- Information from the test set sneaking into training, e.g. fitting a scaler on the whole dataset before splitting
- Using too few features
- A bug in NumPy
Answer: Information from the test set sneaking into training, e.g. fitting a scaler on the whole dataset before splitting. Leakage gives over-optimistic scores; fit transformers on the training fold only.
How does a scikit-learn Pipeline help prevent leakage?
- It deletes the test set
- It skips the scaler entirely
- It doubles the dataset
- It fits every transformer on the training fold only and reuses those parameters on the test fold
Answer: It fits every transformer on the training fold only and reuses those parameters on the test fold. A Pipeline ties transformer + model so the scaler is fit on train only, never on test.
Underneath, a single linear prediction is mostly which operation?
- Sorting the features
- A dot product: multiply each feature by its weight, sum, then add a bias
- Counting the rows
- Removing punctuation
Answer: A dot product: multiply each feature by its weight, sum, then add a bias. np.dot(features, weights) + bias is the arithmetic behind a linear prediction.
Where does matplotlib fit in the ML workflow?
- It trains the model
- It replaces scikit-learn
- It visualises data before and after modelling, but never trains the model itself
- It cleans the data automatically
Answer: It visualises data before and after modelling, but never trains the model itself. matplotlib is for seeing data and results; pandas/NumPy hold it, scikit-learn models it.
Continue this course
- Previous: Introduction to AI & ML
- Next: Data Preprocessing