Skip to main content
Albero Academy

Machine Learning Basics

Supervised vs unsupervised learning, the train/test split, and training your first model with scikit-learn.

Chapter 3Data Science16 min readBeginner friendly
  • #Data Science
  • #Machine Learning
  • #scikit-learn

Machine learning is teaching a program to find patterns from data instead of coding the rules by hand. Rather than writing "if experience > 5 and skills include SQL then…", you show the model examples and it learns the relationship. This chapter trains a real model with scikit-learn's beautifully consistent API.

What is machine learning?

Classic programming: rules + data → answers. Machine learning flips it: data + answers → rules (a "model"). You then apply that model to new data to predict answers you don't have yet.

Types of learning

TypeYou haveGoalExample
SupervisedLabelled examplesPredict the labelWill this student be placed?
UnsupervisedUnlabelled dataFind structureGroup customers into segments
RegressionNumeric labelsPredict a numberExpected salary
ClassificationCategory labelsPredict a classSpam / not spam

The train/test split

The cardinal rule: never evaluate a model on the data it learned from. Hold back a test set the model never sees during training, so you measure how it performs on new data — which is all that matters:

splitting the data
from sklearn.model_selection import train_test_split
X = df[["experience", "projects"]] # features (inputs)
y = df["placed"] # label (what we predict)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42 # hold back 20% for testing
)

Your first model

Every scikit-learn model follows the same three-step pattern: fit (learn), predict (apply), score (evaluate). Learn it once and every model works the same way:

fit → predict → score
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train) # learn from the training set
predictions = model.predict(X_test) # predict on unseen data
accuracy = model.score(X_test, y_test)
print(f"Accuracy: {accuracy:.2%}")
# Swap the model — the API is identical
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier().fit(X_train, y_train)

Practice

On a labelled dataset, pick two feature columns and a target, split into train/test, and fit a LogisticRegression. Print the accuracy, then swap in a RandomForestClassifier and compare — noting that only one line changed.

Key takeaways

  1. 01Machine learning learns rules from data + answers, then predicts on new data.
  2. 02Supervised learning uses labelled data (regression predicts numbers, classification predicts classes); unsupervised finds structure.
  3. 03Always hold back a test set — never evaluate on the data the model trained on.
  4. 04Every scikit-learn model shares the fit → predict → score API.
  5. 05Start with a simple, interpretable baseline before reaching for complex models.