Course topics

By WebNest Studio Engineering Team

Python Tutorial

Introduction to Machine Learning

In ordinary programming you write the rules: if the amount is over 50,000 and the country is new, flag the payment. In machine learning (ML) you give the computer examples — thousands of past payments labelled "fraud" or "not fraud" — and an algorithm learns the rules from the data, then applies them to new cases it has never seen.

ML powers spam filters, recommendations, price estimates, medical image screening, speech recognition and large language models. This lesson explains the core ideas and vocabulary, the types of machine learning, the standard workflow, and trains your first models with scikit-learn, Python's most widely used library for classical ML (pip install scikit-learn).

Why it matters

Machine learning is how software makes predictions from examples: which emails are spam, what a house is worth, which customers may leave. Understanding the basic workflow of features, a target, training and testing lets you judge when it is the right tool and read what the rest of the field is talking about.

Key Vocabulary

  • Dataset — a table of examples (rows). Features (X) are the input columns; the target or label (y) is what we want to predict.
  • Model — a mathematical function with adjustable parameters. Training (fit) adjusts them to match the examples; prediction (predict) applies the model to new inputs.
  • Training set vs test set — we train on one part of the data and measure on data the model has never seen, to estimate real-world performance.
  • Overfitting — memorising the training data (great training score, poor test score). Underfitting — a model too simple to capture the pattern.

Types of Machine Learning

  • Supervised learning — learn from labelled examples. Regression predicts a number (house price, delivery time); classification predicts a category (spam/not spam, disease type).
  • Unsupervised learning — find structure in unlabelled data: clustering (customer segments), dimensionality reduction, anomaly detection.
  • Reinforcement learning — an agent learns by trial and reward (games, robotics).
  • Deep learning — neural networks with many layers (PyTorch, TensorFlow) for images, audio and text; large language models are deep learning at huge scale.

The ML Workflow and scikit-learn

A typical project: define the problem and metric → collect and explore data → clean and prepare features → split into training and test sets → train candidate models → evaluate and tune → deploy and monitor. scikit-learn gives every algorithm the same interface: create an estimator (model = LinearRegression()), model.fit(X_train, y_train), then model.predict(X_new) and model.score(X_test, y_test). Learn the pattern once and you can try dozens of algorithms by changing one line.

Examples

Your first model: predicting marks from study hours

Python
# pip install scikit-learn
import numpy as np
from sklearn.linear_model import LinearRegression

hours = np.array([[1], [2], [3], [4], [5], [6], [7], [8]])   # features: 2-D (rows x columns)
marks = np.array([35, 45, 50, 58, 65, 72, 80, 88])             # target: 1-D

model = LinearRegression()
model.fit(hours, marks)                                        # learn from examples

print(f"learned rule: marks = {model.coef_[0]:.2f} * hours + {model.intercept_:.2f}")
print("predictions for 2.5, 6 and 9 hours:", model.predict([[2.5], [6], [9]]).round(1))
print("R^2 on the training data:", round(model.score(hours, marks), 3))
Output
learned rule: marks = 7.37 * hours + 28.46
predictions for 2.5, 6 and 9 hours: [46.9 72.7 94.8]
R^2 on the training data: 0.998

Train/test split and a first classifier on the Iris dataset

Python
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

iris = load_iris(as_frame=True)                 # 150 flowers, 4 measurements, 3 species
X, y = iris.data, iris.target
print(X.shape, iris.target_names.tolist())
print(X.head(3))

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y)   # keep class balance in both parts
print("train:", X_train.shape[0], "test:", X_test.shape[0])

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
print("test accuracy:", round(model.score(X_test, y_test), 3))

new_flower = [[5.9, 3.0, 5.1, 1.8]]              # sepal/petal length and width in cm
prediction = model.predict(pd.DataFrame(new_flower, columns=X.columns))[0]
print("predicted species:", iris.target_names[prediction])
Output
(150, 4) ['setosa', 'versicolor', 'virginica']
   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)
0                5.1               3.5                1.4               0.2
1                4.9               3.0                1.4               0.2
2                4.7               3.2                1.3               0.2
train: 112 test: 38
test accuracy: 0.974
predicted species: virginica

Overfitting in action: training vs test accuracy

Python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)

print("depth  train  test")
for depth in [1, 2, 3, 5, 10, None]:
    tree = DecisionTreeClassifier(max_depth=depth, random_state=0).fit(X_train, y_train)
    print(f"{str(depth):>5}  {tree.score(X_train, y_train):.3f}  {tree.score(X_test, y_test):.3f}")
# An unlimited tree memorises the training data (1.000) but does not do better on new data.
Output
depth  train  test
    1  0.932  0.889
    2  0.942  0.906
    3  0.980  0.901
    5  0.997  0.912
   10  1.000  0.906
 None  1.000  0.906

Common Mistakes

  • Evaluating a model on the same data it was trained on and trusting the score.
  • Passing a 1-D array as X — scikit-learn expects a 2-D table of shape (samples, features).
  • Forgetting random_state, so splits and results change on every run.
  • Reaching for complex models before trying a simple baseline.
  • Treating ML as magic: bad or biased data produces bad or biased models.

Exercise

Using scikit-learn, which must be installed with pip install scikit-learn, train a linear regression model on hours studied and marks obtained, and predict the marks for 5 hours of study. The data lies exactly on a straight line, so the prediction is exact.

Expected output: 100.0

Python · starter code
from sklearn.linear_model import LinearRegression

hours = [[1], [2], [3], [4]]   # features: one column
marks = [20, 40, 60, 80]       # target

# TODO: create the model and fit it to the data
# TODO: predict the marks for 5 hours and print the value rounded to 1 decimal
Show hints
  • Training is model.fit(hours, marks).
  • model.predict([[5]]) returns an array with one value; take element 0 and round it.
Show solution
Python · solution
from sklearn.linear_model import LinearRegression

hours = [[1], [2], [3], [4]]   # features: one column
marks = [20, 40, 60, 80]       # target

model = LinearRegression()
model.fit(hours, marks)

prediction = model.predict([[5]])
print(round(float(prediction[0]), 1))

Quick quiz

1. In supervised learning, what does the training data contain?

  • A. Only inputs
  • B. Inputs together with the correct outputs
  • C. Only outputs
  • D. Random numbers
Show answer

B. The model learns the relationship between features and the known target.

2. Why is data split into a training set and a test set?

  • A. To make training faster
  • B. To remove outliers
  • C. To measure how the model performs on data it has not seen
  • D. Because scikit-learn requires it
Show answer

C. A score on the training data says nothing about performance on new data.

3. A model scores 100% on the training data and 60% on the test data. What is this called?

  • A. Underfitting
  • B. Clustering
  • C. Regularisation
  • D. Overfitting
Show answer

D. The model has memorised the training examples and does not generalise.

Interview questions

What is the difference between supervised and unsupervised learning?

Show answer

In supervised learning the training data includes the correct answer for each example, and the model learns to predict it: a number in regression, a category in classification. In unsupervised learning there are no answers; the algorithm finds structure in the data itself, such as groups of similar items in clustering or a simpler representation in dimensionality reduction.

What are overfitting and underfitting?

Show answer

An overfitted model is too complex for the data: it learns the noise in the training set and performs well there but badly on new data. An underfitted model is too simple to capture the pattern and performs badly on both. Overfitting is reduced with more data, a simpler model, regularisation or cross-validation; underfitting with a more capable model or better features.

Key Points to Remember

  • ML learns rules from examples: features X, target y.
  • Supervised (regression, classification) vs unsupervised (clustering) learning.
  • Always hold out a test set; train_test_split with random_state and stratify.
  • Every scikit-learn estimator uses fit, predict and score.
  • Watch for overfitting: a big gap between training and test scores.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering