⚡0 XP🔥0

Introduction to Machine Learning

Lesson 1 of 11 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

What machine learning is, how it differs from normal programming, supervised vs unsupervised vs reinforcement learning, features and labels, the ML workflow, and your first scikit-learn model.

What is machine learning?#

In normal programming, you write the rules: if marks >= 40: print("Pass"). In machine learning (ML), you give the computer examples and it learns the rules by itself.

Traditional programming:   data + rules     →  answers
Machine learning:          data + answers   →  rules (a model)

A classic definition (Tom Mitchell): a program learns from experience E with respect to a task T and performance measure P, if its performance on T, measured by P, improves with experience E. For a spam filter: T = classify emails, E = thousands of emails labelled spam/not spam, P = percentage classified correctly.

ML is everywhere: UPI fraud detection, YouTube and Netflix recommendations, Google Maps arrival times, face unlock, crop-disease detection from leaf photos, and the large language models behind ChatGPT and Claude.

हिंदी में समझें

सामान्य programming में हम नियम खुद लिखते हैं। Machine Learning में हम computer को बहुत सारे उदाहरण (data + सही जवाब) देते हैं और वह खुद नियम (pattern) सीख लेता है - इसे model कहते हैं। फिर नए data पर वह model अनुमान (prediction) लगाता है।

Types of machine learning#

Type Data Goal Examples
Supervised inputs with correct answers (labels) predict the label for new inputs spam detection, price prediction, disease diagnosis
Unsupervised inputs without labels find structure or groups customer segmentation, topic grouping, anomaly detection
Reinforcement an agent acting in an environment learn actions that maximise reward game-playing AI, robotics, ad bidding

Supervised learning has two main kinds of problems:

  • Classification - the answer is a category: spam / not spam, which flower species, pass / fail.
  • Regression - the answer is a number: price, temperature, marks.

Vocabulary#

Term Meaning Iris example
Sample (row, instance) one example one flower
Feature (X) an input measurement petal length
Label / target (y) the answer to predict species
Model the learned function from X to y a trained KNN classifier
Training fitting the model to data model.fit(X, y)
Inference / prediction using the model on new data model.predict(new_X)

The machine learning workflow#

  1. Define the problem - what do we predict, and how will we measure success?
  2. Collect data - databases, CSV files, sensors, APIs, Kaggle.
  3. Explore and clean - missing values, wrong types, outliers.
  4. Prepare features - encode categories, scale numbers, create useful new features.
  5. Split into training and test sets.
  6. Train one or more models.
  7. Evaluate on the test set with the right metric.
  8. Tune and improve - better features, other algorithms, hyper-parameters.
  9. Deploy and monitor - put it in an app and watch it on real data.

Your first model#

The iris dataset (built into scikit-learn) has 150 flowers of 3 species with 4 measurements each. Let us look at it first:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
Features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Classes : ['setosa', 'versicolor', 'virginica']
Shape of X: (150, 4)
First flower: [5.1 3.5 1.4 0.2] -> setosa

The first run downloads scikit-learn into your browser, so it takes a little while. Now train a classifier and test it on flowers it has never seen:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
Test samples: 30
Accuracy: 1.0
First 10 predictions: [0 2 1 1 0 1 0 0 2 1]
First 10 true labels: [0 2 1 1 0 1 0 0 2 1]

That is the whole scikit-learn pattern - and it is the same for almost every algorithm:

model = SomeAlgorithm(settings)   # choose
model.fit(X_train, y_train)       # learn
model.predict(X_test)             # use
Tip

random_state=42 fixes the random shuffle so you get the same split every time - important for reproducible results. stratify=y keeps the same proportion of each class in the train and test sets.

Why not test on the training data?#

A model can simply memorise its training data. Then it looks perfect on that data but fails on new data - this is overfitting. The test set acts as an exam with unseen questions. You will study overfitting properly in the evaluation lesson.

Common Mistake

Never let the test set influence training - not even indirectly (for example, by computing scaling statistics on all the data before splitting). That "data leakage" gives scores that are too good to be true.

The Python ML toolkit#

Library Used for
NumPy fast arrays and maths
pandas loading and cleaning tabular data (DataFrames)
Matplotlib / Seaborn charts
scikit-learn classic ML algorithms, preprocessing, evaluation
TensorFlow / PyTorch deep learning (large neural networks)

Summary#

  • ML learns rules from examples instead of hand-written rules.
  • Supervised (labels: classification or regression), unsupervised (no labels), reinforcement (rewards).
  • Workflow: problem → data → clean → features → split → train → evaluate → tune → deploy.
  • scikit-learn pattern: create, fit, predict; always evaluate on a held-out test set.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Classify a new flower

Train a KNeighborsClassifier(n_neighbors=3) on the full iris data and predict the species of a flower with measurements [6.3, 2.9, 5.6, 1.8]. Print the species name, e.g. Prediction: setosa.

Expected output:
Prediction: virginica
2

Supervised or unsupervised?

Create a dictionary kind mapping each task to "supervised" or "unsupervised": "spam detection", "customer segmentation", "house price prediction", "grouping news articles by topic (no labels)". Then print it.

Quick Quiz

Q1.

In supervised learning, the training data contains...

Q2.

Predicting a house price is a...

Q3.

Grouping customers by buying behaviour without predefined groups is...

Q4.

Why do we keep a separate test set?

Q5.

In scikit-learn, which method trains a model?