Machine Learning Basics
โฑ 11 min read ยท Chapter 3 ยท Foundations
Machine learning is the practice of teaching computers to find patterns in data. Rather than programming the answer, you give the algorithm many examples and let it infer the rule. The learned rule is called a model.
Key vocabularyโ
- Feature โ an input variable (e.g. the size of a house).
- Label (or target) โ the answer we want to predict (e.g. the price).
- Training โ the process of adjusting the model to fit the data.
- Inference โ using the trained model to predict on new data.
- Generalization โ how well the model performs on data it has never seen.
Supervised learningโ
In supervised learning, every training example comes with the correct answer (a label). The model learns to map inputs to outputs. Two common tasks are regression (predict a number) and classification (predict a category).
from sklearn.linear_model import LinearRegression
import numpy as np
# Feature: house size (m2). Label: price (in thousands).
X = np.array([[50], [70], [90], [110], [130]])
y = np.array([150, 210, 270, 330, 390])
model = LinearRegression()
model.fit(X, y) # training
print(model.predict([[100]])) # -> ~300 (inference)
Predicting a continuous value (price, temperature) is regression. Predicting a discrete category (spam / not-spam, cat / dog) is classification.
Here is a classification example that predicts whether a message is spam based on two simple features:
from sklearn.tree import DecisionTreeClassifier
# Features: [num_links, has_word_free]
X = [[3, 1], [0, 0], [5, 1], [1, 0], [4, 1]]
y = ["spam", "ham", "spam", "ham", "spam"]
clf = DecisionTreeClassifier().fit(X, y)
print(clf.predict([[4, 1]])) # -> ['spam']
Unsupervised learningโ
In unsupervised learning there are no labels. The algorithm discovers structure on its own โ for example grouping similar customers together (clustering) or reducing the number of dimensions in the data.
from sklearn.cluster import KMeans
import numpy as np
X = np.array([[1, 2], [1, 4], [10, 2], [10, 4], [1, 3], [10, 3]])
kmeans = KMeans(n_clusters=2, n_init=10).fit(X)
print(kmeans.labels_) # e.g. [0 0 1 1 0 1]
The train / test splitโ
A model that memorizes its training data but fails on new data is overfitting. To detect this, we always hold back some data for testing:
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
)
model.fit(X_train, y_train)
print("accuracy:", model.score(X_test, y_test))
If your model scores 99% on training data but 60% on test data, it memorized instead of learning. More data, simpler models, and regularization all help.
The typical ML workflowโ
- Collect and clean the data.
- Split it into training and test sets.
- Choose a model and train it on the training set.
- Evaluate on the test set using a suitable metric.
- Tune, repeat, and finally deploy the model.