Learn Machine Learning

Machine Learning is the field of building systems that improve automatically by learning patterns from data instead of following hand-written rules.

What Is Machine Learning?

Machine Learning (ML) is a branch of artificial intelligence that lets a computer program find patterns in data and make predictions or decisions without being explicitly programmed for every scenario. In Python, ML relies heavily on NumPy for fast array math and the built-in statistics module for quick summaries, so almost every ML pipeline starts by loading data into arrays and computing basic statistics before any model is trained.

Traditional Programming vs Machine Learning

In traditional programming, a developer writes explicit rules that transform input into output. In machine learning the roles reverse: we provide input data together with known outputs, and the algorithm works backward to discover the rule that connects them. That discovered rule, called a model, can then predict outputs for new, unseen inputs.

Supervised Learning

Supervised learning trains a model on labeled data, meaning every training example comes with the correct answer attached. If the label is a number the task is called regression (predicting house prices, for example); if the label is a category the task is called classification (spam vs. not spam). The model learns the mapping from features to labels by minimizing prediction error on the training examples.

Example

import numpy as np
import statistics

# Features: hours of daily study, Label: exam score (a supervised learning setup)
study_hours = np.array([1, 2, 3, 4, 5, 6, 7, 8])
exam_scores = np.array([52, 58, 63, 69, 74, 81, 88, 93])

print('Average study hours:', statistics.mean(study_hours))
print('Average exam score:', statistics.mean(exam_scores))
print('Correlation between hours and scores:', np.corrcoef(study_hours, exam_scores)[0, 1])

Unsupervised Learning

Unsupervised learning works with unlabeled data, so there is no correct answer to check against. Instead the algorithm looks for hidden structure on its own, such as grouping similar data points together (clustering) or reducing the number of variables while keeping the important patterns (dimensionality reduction). It is often used to explore data before building a supervised model.

Example

import numpy as np
import statistics

# No labels here - just raw purchase amounts to explore for structure
purchase_amounts = np.array([12, 15, 250, 14, 260, 13, 245, 16, 255, 11])

midpoint = statistics.median(purchase_amounts)
small_purchases = purchase_amounts[purchase_amounts < midpoint]
large_purchases = purchase_amounts[purchase_amounts >= midpoint]

print('Median purchase amount:', midpoint)
print('Small-purchase group:', small_purchases)
print('Large-purchase group:', large_purchases)
AspectSupervised LearningUnsupervised Learning
Training dataLabeled (inputs + known outputs)Unlabeled (inputs only)
GoalPredict a known output for new dataDiscover hidden structure or groups
Common tasksRegression, classificationClustering, dimensionality reduction
ExamplePredicting exam scores from study hoursGrouping customers by purchase behavior
  • Data collection - gather raw numbers, text, or images relevant to the problem
  • Data preparation - clean, organize, and convert data into numeric arrays
  • Model training - let an algorithm find the pattern in the data
  • Evaluation - measure how well the model performs on new, held-out data
  • Prediction - use the trained model on new, unseen inputs

Example

import numpy as np

np.random.seed(1)

# Simulate a tiny labeled dataset: house size (sqm) -> price ($1000s)
house_size = np.array([50, 60, 70, 80, 90, 100])
price = np.array([150, 175, 200, 225, 250, 275])

# A model is just a rule connecting inputs to outputs - here, a simple ratio
price_per_sqm = np.mean(price / house_size)
predicted_price_for_75sqm = price_per_sqm * 75

print('Learned price per sqm:', price_per_sqm)
print('Predicted price for a 75 sqm house:', predicted_price_for_75sqm)
Note: You do not need to memorize every algorithm before you start. Learning to describe your data with mean, median, and standard deviation - covered in the next few chapters - makes every ML technique afterward much easier to understand.
Note: NumPy arrays are the standard container for numerical data in Python's ML ecosystem. Libraries like scikit-learn, pandas, and TensorFlow are all built on top of NumPy.

Exercise: ML Getting Started

In a typical Python ML workflow, what is the main purpose of analyzing data before building a model?

Frequently Asked Questions

What is the difference between machine learning and traditional programming?
In traditional programming you write the rules and the computer applies them. In machine learning you supply examples and the system derives rules from them. That makes it suited to problems where nobody can state the rules precisely, such as recognising speech.
How much maths do I need for machine learning?
To apply existing libraries: statistics, probability and a working feel for linear algebra. To understand why a model behaves as it does, add calculus and more linear algebra. Many people start applied and deepen the maths as specific questions come up.
What is the difference between supervised and unsupervised learning?
Supervised learning trains on examples that carry the right answer, so the model learns to predict it. Unsupervised learning gets data with no answers and finds structure in it, such as clusters. Most practical business problems are supervised.
How much data do I need to train a model?
It depends far more on the problem than on any fixed number. Simple tabular problems can work with a few thousand rows; image and language tasks usually need far more, which is why people fine-tune existing models rather than training from scratch.