Sub topic 1
What Machine Learning Is
Prerequisites
Assume basic Python, arrays or tables of numbers, high-school algebra, and mean and variance. You should know that a function maps inputs to outputs.
What Machine Learning Is
Machine learning is a way to build programs that learn patterns from data. Instead of writing every decision by hand, you provide examples and a learning algorithm. The algorithm produces a model, which is a learned function that can make predictions about new data.
A model does not “understand” examples like a person does. It adjusts internal values so its predictions match useful patterns in the data.
Rules Written by Hand Versus Patterns Learned from Data
In rule-based programming, a developer writes conditions explicitly:
if temperature < 0:
state = "ice"
else:
state = "liquid"This works when the rules are clear. It becomes difficult when the answer depends on many interacting signals, such as recognizing a plant species from measurements. Machine learning can estimate those rules from past examples. The learned model may then classify a new plant without a developer listing every boundary.
Examples, Features, and Labels
An example is one row or observation in a dataset. A feature is an input value used to describe that example. A label is the desired output, when one is available.
For a plant dataset, one example might contain four features: sepal length, sepal width, petal length, and petal width. Its label might be the plant species.
| Dataset part | Plant example |
|---|---|
| Example | One measured plant |
| Features | Four measurements |
| Label | Species name |
The Main Ways a Model Can Learn
The learning approach depends on whether labels exist and what question you are asking.
- Supervised learning learns from examples paired with known labels. It predicts a label or number for a new example.
- Unsupervised learning receives examples without labels. It searches for structure, such as groups or compact representations.
- Other approaches include semi-supervised, self-supervised, and reinforcement learning. They use different sources of feedback.
Supervised Learning: Learning from Labeled Examples
In supervised learning, the model sees inputs and correct outputs during training. Training is the process of adjusting the model so its predictions become closer to the labels.
A supervised task can predict a category, called classification, or a number, called regression. For example, species prediction is classification, while predicting house price is regression.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y
)
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
print(model.predict(X_test[:3]))The test examples are held aside so later evaluation measures performance on data the model did not train on.
Exercise
In a dataset of emails with “spam” or “not spam” labels, is predicting the label supervised learning, classification, or both?
Show answer
It is both: supervised learning because labels are provided, and classification because the output is a category.