HuntExams Academy logo
Data Science ยท Chapter 23 of 43

Intro to Machine Learning

MACHINE LEARNING lets a computer LEARN patterns from data instead of being programmed with rules.

Main flavours: supervised (labels), unsupervised (no labels), reinforcement (reward).

Example 1 (python)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
Output
0.87

Train and evaluate โ€” the same API for every scikit-learn model.

Example 2 (python)
# Supervised: classification & regression
# Unsupervised: clustering, dimensionality reduction

Two big families in supervised learning.

Key points

  • ML learns patterns from data.
  • Supervised needs labels.
  • Unsupervised finds structure.
  • scikit-learn has a consistent fit/predict API.
๐Ÿ’ก Note: Start simple: a linear/logistic regression baseline is often within a few % of the fanciest model.

Understanding Intro to Machine Learning

Machine learning is a way to construct a prediction procedure from examples. Instead of writing every decision rule yourself, you choose a model family and a training procedure that determines its behavior from data. People still define the task, select measurements, and decide what counts as success. Learning patterns is not the same as understanding their causes.

Imagine sorting parcels into two handling categories using their mass. Each parcel is one sample, and its mass is a feature: information available when making a prediction. The recorded handling category is the target, also called a label in classification. Conventionally, X contains feature rows and y contains their corresponding targets. Row order matters because each target must describe the matching sample.

Supervised learning uses examples with known targets. Classification predicts categories, such as standard or special handling; regression predicts numerical quantities, such as delivery duration. Logistic regression, despite its name, is a classification method. Unsupervised learning instead explores data without supplied target labels, for example by clustering similar parcels. Those clusters do not automatically represent useful handling categories. Reinforcement learning concerns actions and rewards over interactions with an environment, rather than simply fitting a table of labeled examples.

For supervised prediction, training and evaluation serve different purposes. Calling fit lets an estimator use training examples to establish its predictive behavior. Calling predict applies that fitted estimator to feature rows. Evaluation compares predictions against known answers withheld from training. The scikit-learn guide separates supervised methods, model evaluation, and preprocessing because a useful workflow needs all three considerations, not merely a successful fit call.

The central goal is generalization: useful predictions for unseen examples from the intended setting. A model may overfit by capturing quirks of its training data, or underfit by being too restrictive to represent important patterns. Simpler models are easier to inspect but are not guaranteed to match complex models. Evaluation also assumes the held-out examples resemble future use. Changing measurement practices, populations, or labeling rules can invalidate that assumption. Start with a simple baseline, inspect mistakes, and choose an evaluation measure appropriate to the consequences of errors. No single score establishes that a model is ready for deployment.

Practical walkthrough

Use Python 3.10 or later with a compatible scikit-learn installation, such as scikit-learn 1.4 or later. You need basic familiarity with lists, imports, and method calls. This example creates all data in memory; it needs no pandas tables, external datasets, or file access. The parcel records are invented solely to expose the mechanics, not to establish an actual handling policy.

First, define separate training and test records. Each inner list contains one mass measurement, so X is two-dimensional even though there is only one feature. Labels use 0 for standard handling and 1 for special handling. Training masses are 1, 3, 7, and 9; their labels switch from 0 to 1 across the gap.

Next, construct a decision tree with max_depth=1. This permits only one split, making the learned decision inspectable. The depth limit is a hyperparameter selected before fitting, whereas the split threshold is learned from training data. Fixing random_state removes randomness from the tree's feature-selection behavior. No scaling or other learned preprocessing is needed for this example.

Fit using only the training variables. Then predict the four test categories without supplying their answers. Use accuracy_score to compare those predictions with y_test. This explicit function makes the metric clearer than an unexplained score call, whose meaning depends on the estimator.

Finally, inspect the mismatch. Do not alter this model repeatedly to improve the same test result. On a larger project, compare choices using validation data or cross-validation within the training portion, reserving the test set for a final assessment after choices are settled.

Worked example

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# One feature per parcel: mass. Labels: 0 = standard, 1 = special.
X_train = [[1], [3], [7], [9]]
y_train = [0, 0, 1, 1]

X_test = [[2], [4], [8], [10]]
y_test = [0, 1, 1, 1]

model = DecisionTreeClassifier(max_depth=1, random_state=0)
model.fit(X_train, y_train)
predictions = model.predict(X_test)

print("Predictions:", predictions.tolist())
print("Actual:", y_test)
print(f"Accuracy: {accuracy_score(y_test, predictions):.2f}")

Expected output

Predictions: [0, 0, 1, 1]
Actual: [0, 1, 1, 1]
Accuracy: 0.75

The tree learns a threshold of 5, midway between the adjacent training masses 3 and 7. Masses at or below 5 receive label 0; larger masses receive label 1. Consequently, test masses 2 and 4 are predicted as standard, while 8 and 10 are predicted as special. The recorded label for mass 4 is special, creating the only error. Three correct predictions out of four give exactly 0.75 accuracy. This result describes these four constructed cases only. The discrepancy illustrates that a feature can be insufficient: mass alone need not determine a parcel's handling category, and labels can also be noisy.

Common mistakes and how to avoid them

  • Reporting training accuracy as evidence of future performance.

    Evaluate on held-out examples. A model can memorize training records; correctly predicting those same records does not demonstrate generalization.

  • Learning preprocessing settings from the full dataset before splitting.

    Split first. Fit scalers, imputers, and feature selectors on training data only, then reuse them unchanged on test data. A scikit-learn pipeline helps enforce this during cross-validation.

  • Treating accuracy as sufficient for every classification task.

    Inspect which categories are missed and compare with a simple baseline. With rare special-handling parcels, predicting standard every time could have high accuracy while missing every important case.

  • Reading a predictive association as a causal explanation.

    Say the model predicts handling from mass, not that mass causes the handling requirement. Establishing causality requires additional assumptions and an appropriate study design.

Review questions

Why does X_train contain inner lists?

Scikit-learn expects a feature matrix with rows for samples and columns for features. Here, four one-element rows represent four parcels measured on one feature.

What is learned here, and what is chosen beforehand?

Training determines the split threshold and predicted categories. The programmer chooses the tree family and maximum depth, which restrict the possible learned rules.

Why are test labels available but excluded from fit?

They provide independent answers for evaluation. Allowing them to influence fitting would compromise the test's role as a check on unseen examples.

Would clustering these masses solve the same task?

Not necessarily. Clustering groups samples by similarity without handling labels. Its groups might reflect mass ranges rather than the categories required by the supervised task.

Further reading

๐Ÿ“ Quick Quiz

1. Supervised learning needs:

2. Clustering is:

3. scikit-learn's core method to train is: