Back to All Articles
Notes17 min read

AI and Machine Learning Notes with Examples | Backbencher Codes

Beginner-friendly notes on artificial intelligence, machine learning, data preparation, model training, evaluation, regression, classification, clustering, and related mathematics—with simple Python examples.

Backbencher Administrator2026-10-28

AI and Machine Learning Notes with Examples | Backbencher Codes

Website: https://backbencher.codes
Online compiler: https://www.backbencher.codes/compiler


Table of contents

  1. Introduction to AI and ML
  2. Types of machine learning
  3. The machine learning workflow
  4. Training, validation, and test data
  5. Data preprocessing
  6. Feature scaling
  7. Pipelines and column transformers
  8. Model evaluation
  9. Regression
  10. Core maths for ML
  11. Classification
  12. Decision trees and ensemble learning
  13. Clustering and unsupervised learning
  14. Neural networks and deep learning
  15. Common ML challenges
  16. Three small end-to-end examples
  17. Quick revision

1. Introduction to AI and ML

Artificial Intelligence (AI) is the broad field of building computer systems that perform tasks normally associated with human intelligence, such as understanding language, recognizing images, or making decisions.

Machine Learning (ML) is a part of AI. Instead of writing every rule by hand, we train a model using data so it can learn patterns and make predictions.

Deep Learning (DL) is a part of machine learning that uses neural networks with multiple layers.

A few everyday examples:

  • Email spam filters learn to identify unwanted messages.
  • Shopping websites recommend products based on user activity.
  • Maps estimate travel time from traffic and route information.
  • Image recognition systems identify objects in photographs.

A simple way to think about ML

  1. Collect examples.
  2. Give the examples to a learning algorithm.
  3. The algorithm trains a model.
  4. Test the model on data it has not seen.
  5. Use the model to make predictions.
# A tiny rule-based example (not a trained ML model)
temperature = 35

if temperature > 30:
    print("It is a hot day.")
else:
    print("The weather is not very hot.")

This example uses a manually written rule. A machine learning model would learn a relationship from examples rather than relying only on this fixed condition.

2. Types of machine learning

2.1 Supervised learning

The training data includes inputs and their correct answers, called labels. The model learns to predict labels for new inputs.

Common tasks:

  • Classification: predict a category, such as spam or not spam.
  • Regression: predict a number, such as a house price.

Common algorithms include linear regression, logistic regression, decision trees, random forests, and support vector machines.

2.2 Unsupervised learning

The data does not include target labels. The model looks for patterns or structure.

Examples include customer grouping, anomaly detection, and reducing the number of features for visualization.

Common methods include K-means clustering, DBSCAN, hierarchical clustering, and principal component analysis (PCA).

2.3 Semi-supervised learning

A small part of the dataset is labelled, while much of it is unlabelled. The model uses both kinds of data. This can be useful when labelling examples takes a lot of time or money.

2.4 Reinforcement learning

An agent takes actions in an environment and receives rewards or penalties. It learns a strategy that aims to earn more reward over time. Game-playing agents and some robotics systems use this approach.

2.5 Batch learning and online learning

  • Batch learning: train using a prepared dataset, then update the model by training again when needed.
  • Online learning: update a model gradually as new data arrives. It can be useful for streams of data, provided the training process is monitored carefully.

3. The machine learning workflow

A sensible workflow helps avoid misleading results.

  1. Define the problem. Decide what you want to predict and how success will be measured.
  2. Collect data. Use relevant and reliable examples.
  3. Explore the data. Check columns, data types, distributions, missing values, and unusual records.
  4. Split the data. Keep a test set aside for final evaluation.
  5. Prepare the data. Handle missing values and convert categories or scale features when required.
  6. Choose a baseline. Start with a simple model to establish a point of comparison.
  7. Train the model. Fit the algorithm on training data.
  8. Evaluate and improve. Use validation or cross-validation to compare models and tune settings.
  9. Test once at the end. Use the held-out test set for a final estimate.
  10. Deploy and monitor. Check whether the model continues to perform well on new data.

4. Training, validation, and test data

  • Training set: used to fit the model.
  • Validation set: used to compare models and tune hyperparameters.
  • Test set: held back for a final, more independent evaluation.

A common split is 70% training, 15% validation, and 15% testing, but the best split depends on the dataset size and problem. With small datasets, cross-validation can make better use of the available data.

Never fit preprocessing steps using the test set. For example, calculate imputation values and scaling parameters from training data, then apply those same fitted transformations to validation and test data. This helps prevent data leakage.

from sklearn.model_selection import train_test_split

X = [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

print("Training examples:", len(X_train))
print("Testing examples:", len(X_test))

5. Data preprocessing

Real datasets often need preparation before a model can use them.

Missing values

Missing values may be filled with a statistic such as the mean or median for numeric data, or the most common value for categorical data. The choice should match the data and problem.

Categorical data

Some models need categories converted into numbers.

  • Ordinal encoding: assign numbers to categories that have a meaningful order.
  • One-hot encoding: create a separate binary column for each category. This is useful for categories without a natural order.

Outliers and incorrect records

Check unusual values before removing them. An outlier may be a data-entry mistake, or it may be a genuine and important observation.

Example: one-hot encoding

import pandas as pd

data = pd.DataFrame({"city": ["Pune", "Mumbai", "Pune"]})
encoded = pd.get_dummies(data, columns=["city"], dtype=int)

print(encoded)

The result contains separate numeric columns for the categories. The exact column order is determined by pandas.

6. Feature scaling

Features can use very different units. For example, age may be measured in years while salary is measured in rupees. Scaling puts numeric features on comparable scales.

Min-max scaling

This transforms values to a selected range, usually 0 to 1:

[ x' = \frac{x - x_{\min}}{x_{\max} - x_{\min}} ]

Standardization

This subtracts the mean and divides by the standard deviation:

[ z = \frac{x - \mu}{\sigma} ]

Scaling is often important for K-nearest neighbors, support vector machines, and gradient-based models. Tree-based models usually do not need feature scaling.

from sklearn.preprocessing import StandardScaler

values = [[10], [20], [30]]
scaler = StandardScaler()
scaled_values = scaler.fit_transform(values)

print(scaled_values)

In a real project, fit the scaler on training data only, then use transform() on validation and test data.

7. Pipelines and column transformers

A pipeline joins preprocessing and model training into one workflow. This helps apply the same steps consistently and reduces data leakage during cross-validation.

A ColumnTransformer applies different preparation steps to different columns, such as scaling numeric columns and one-hot encoding categories.

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

model = Pipeline([
    ("scaler", StandardScaler()),
    ("regressor", LinearRegression())
])

X = [[1], [2], [3], [4]]
y = [2, 4, 6, 8]

model.fit(X, y)
print(model.predict([[5]]))

This example predicts approximately 10. A pipeline can also be passed to cross-validation and hyperparameter-search tools.

8. Model evaluation

The right metric depends on what the model is meant to do. Accuracy alone is not enough for every classification problem, and a regression model needs different measures.

8.1 Regression metrics

Mean Absolute Error (MAE) is the average absolute difference between predictions and actual values:

[ MAE = \frac{1}{n}\sum_{i=1}^{n}|y_i-\hat{y}_i| ]

Mean Squared Error (MSE) averages squared errors, so large errors have a stronger effect:

[ MSE = \frac{1}{n}\sum_{i=1}^{n}(y_i-\hat{y}_i)^2 ]

Root Mean Squared Error (RMSE) is the square root of MSE. It is expressed in the same units as the target.

8.2 Classification metrics

  • True Positive (TP): positive case correctly predicted as positive.
  • True Negative (TN): negative case correctly predicted as negative.
  • False Positive (FP): negative case incorrectly predicted as positive.
  • False Negative (FN): positive case incorrectly predicted as negative.

Accuracy is the fraction of all predictions that are correct:

[ Accuracy = \frac{TP+TN}{TP+TN+FP+FN} ]

Precision answers: of the cases predicted positive, how many were actually positive?

[ Precision = \frac{TP}{TP+FP} ]

Recall answers: of all actual positive cases, how many did the model find?

[ Recall = \frac{TP}{TP+FN} ]

F1 score combines precision and recall using their harmonic mean:

[ F1 = 2\frac{Precision \times Recall}{Precision+Recall} ]

Use these formulas when their denominators are non-zero. In practice, libraries often let you specify how to handle zero-division cases.

8.3 ROC curve and AUC

A receiver operating characteristic (ROC) curve plots the true positive rate against the false positive rate at different classification thresholds. AUC summarizes the curve: higher values generally indicate better ranking of positive examples above negative examples.

8.4 Confusion matrix example

from sklearn.metrics import confusion_matrix, accuracy_score

actual = [1, 0, 1, 1, 0]
predicted = [1, 0, 0, 1, 0]

print("Confusion matrix:")
print(confusion_matrix(actual, predicted))
print("Accuracy:", accuracy_score(actual, predicted))

For imbalanced datasets, precision, recall, F1, balanced accuracy, or precision-recall curves may be more informative than accuracy alone.

9. Regression

Regression predicts a numeric value, such as sales, temperature, or house price.

9.1 Linear regression

Simple linear regression models a relationship between one input feature and a numeric target:

[ \hat{y} = mx + b ]

  • (x): input feature
  • (\hat{y}): predicted value
  • (m): slope
  • (b): intercept

For multiple features, the model becomes:

[ \hat{y} = b + w_1x_1 + w_2x_2 + \cdots + w_px_p ]

The model learns weights that reduce prediction error, commonly by minimizing the sum of squared errors.

from sklearn.linear_model import LinearRegression

X = [[1], [2], [3], [4], [5]]
y = [2, 4, 6, 8, 10]

model = LinearRegression()
model.fit(X, y)

print(model.predict([[6]]))  # Approximately [12.]

9.2 Least-squares line

The least-squares method finds the line that minimizes the sum of squared vertical differences between observed values and predicted values. It is a common way to fit linear regression.

9.3 Correlation

Correlation measures the direction and strength of a linear relationship between two variables. A value near +1 indicates a strong positive linear relationship, a value near -1 indicates a strong negative linear relationship, and a value near 0 indicates little linear relationship. Correlation alone does not prove causation.

9.4 Logistic regression

Despite its name, logistic regression is commonly used for classification. It estimates a probability, often by applying the sigmoid function to a linear score:

[ \sigma(z)=\frac{1}{1+e^{-z}} ]

The predicted probability can be converted to a class using a chosen threshold. The threshold should reflect the costs of false positives and false negatives.

10. Core maths for ML

10.1 Slope

Slope measures vertical change divided by horizontal change:

[ m = \frac{y_2-y_1}{x_2-x_1} ]

For points (1, 2) and (3, 6), the slope is (6 - 2) / (3 - 1) = 2.

10.2 Derivatives and rate of change

A derivative describes the instantaneous rate of change of a function. In machine learning, derivatives help show how a loss function changes when model parameters change.

For a simple function:

[ f(x)=x^2,\qquad f'(x)=2x ]

At x = 3, the derivative is 6.

10.3 Gradient descent

Gradient descent is an optimization method used to reduce a loss function. It repeatedly updates parameters in the direction opposite to the gradient:

[ \theta_{\text{new}} = \theta_{\text{old}} - \eta \nabla J(\theta) ]

Here, (\theta) represents model parameters, (J) is the loss function, and (\eta) is the learning rate.

A learning rate that is too high can skip over a good solution; one that is too low can make learning slow.

10.4 Average and instantaneous rate of change

The average rate of change between two points is:

[ \frac{f(b)-f(a)}{b-a} ]

The instantaneous rate of change is the derivative at a point, when that derivative exists.

These ideas help explain how optimization algorithms adjust a model to improve its predictions.

11. Classification

Classification predicts a label or category, such as whether an email is spam or not spam.

11.1 K-nearest neighbors (KNN)

KNN predicts using the labels or values of nearby training examples. The parameter k sets how many neighbors are considered. Scaling is often important because distance is used.

11.2 Support vector machines (SVM)

An SVM tries to find a decision boundary that separates classes with a large margin. Kernel functions can help it model non-linear boundaries. Scaling numeric features is often helpful.

11.3 Decision trees

A decision tree splits data using feature-based rules. It is easy to explain, but a deep tree may overfit. Limiting depth or minimum leaf size can help.

11.4 Random forests

A random forest combines many decision trees trained with randomness in samples and/or features. Combining their predictions often improves stability compared with a single tree.

11.5 Multiclass and multilabel classification

  • Multiclass: each example belongs to one of several possible classes.
  • Multilabel: an example may have more than one label at the same time.

Some algorithms support multiclass classification directly; others use strategies such as one-vs-rest.

12. Decision trees and ensemble learning

Decision trees

Trees repeatedly split the data into groups. A split is selected using a criterion such as Gini impurity, entropy, or squared error, depending on the task.

Advantages:

  • Easy to understand and visualize.
  • Can model non-linear relationships.
  • Usually do not require feature scaling.

Limitations:

  • A deep tree can overfit.
  • Small changes in data can sometimes produce a different tree.

Ensemble learning

Ensemble methods combine predictions from multiple models.

  • Bagging: trains models on different bootstrap samples and combines their predictions. Random forests are a popular example.
  • Boosting: trains models in sequence so later models focus on mistakes or remaining errors. Examples include AdaBoost and gradient boosting.
  • Voting: combines predictions from different classifiers.
  • Stacking: trains a second-level model to combine predictions from base models.

Ensembles can improve performance, but they may require more compute and can be harder to explain.

13. Clustering and unsupervised learning

Clustering groups data points based on similarity without using target labels.

13.1 K-means

K-means divides data into a chosen number of clusters, K. Each cluster has a centroid, which is the mean of its assigned points.

Basic steps:

  1. Choose K initial centroids.
  2. Assign each point to its nearest centroid.
  3. Recalculate each centroid as the mean of its assigned points.
  4. Repeat assignment and update until the assignments or centroids stop changing significantly.
from sklearn.cluster import KMeans

X = [[1, 2], [1, 3], [2, 2], [9, 8], [8, 9], [9, 9]]

model = KMeans(n_clusters=2, random_state=42, n_init=10)
model.fit(X)

print(model.labels_)
print(model.cluster_centers_)

The numeric cluster labels are arbitrary; cluster 0 does not automatically mean a better or more important group than cluster 1.

K-means works best when clusters are reasonably compact and distance is meaningful. It can be affected by scaling, outliers, and initial centroid choices. The Elbow Method and silhouette score can help compare cluster counts.

13.2 DBSCAN

DBSCAN groups points in dense regions and can label isolated points as noise. It does not require choosing the number of clusters in advance, but its results depend on parameters such as neighborhood radius and minimum samples.

13.3 Hierarchical clustering

Hierarchical clustering builds a hierarchy of groups. The result can be represented as a dendrogram, which helps show how groups join together at different distances.

13.4 Anomaly detection

Anomaly detection tries to identify examples that differ from normal patterns. Possible applications include unusual transactions, sensor faults, and suspicious network activity. An anomaly is not automatically an error; it should be investigated in context.

13.5 Principal component analysis (PCA)

PCA is a dimensionality-reduction method that transforms numeric features into new components that capture as much variance as possible in the first few components. Scaling may be important before PCA when features use different units. PCA can help with visualization and reducing feature dimensions, but transformed components may be harder to interpret.

14. Neural networks and deep learning

A neural network contains layers of connected units. Each unit combines inputs with weights and a bias, then applies an activation function.

  • Input layer: receives features.
  • Hidden layers: learn intermediate patterns.
  • Output layer: produces the prediction.
  • Weights and biases: learned parameters.
  • Activation functions: add non-linearity, allowing the network to learn more complex patterns.
  • Loss function: measures how far predictions are from targets.
  • Backpropagation: calculates gradients used to update parameters.
  • Optimizer: updates parameters to reduce the loss.

Deep learning uses neural networks with multiple hidden layers. It is widely used for images, audio, text, and other complex data, but often needs more data and computing resources than simpler models.

15. Common ML challenges

Not enough training data

A model may fail to learn useful patterns if there are too few representative examples.

Poor-quality data

Incorrect labels, missing values, inconsistent formats, and measurement errors can hurt model performance.

Overfitting

Overfitting happens when a model learns training data too closely, including noise, and performs poorly on unseen examples. Possible fixes include simplifying the model, using regularization, collecting more data, and using cross-validation appropriately.

Underfitting

Underfitting happens when a model is too simple to capture the pattern in the data. A more expressive model or better features may help.

Data leakage

Data leakage occurs when information unavailable at prediction time influences training or evaluation. Keep the test set separate and fit preprocessing only on training data.

Changing data

Real-world patterns can change over time. Monitor the model after deployment and evaluate it on recent, representative data.

Bias and fairness

Data may underrepresent groups or reflect historical unfairness. Evaluate performance across relevant groups and consider the real-world consequences of errors.

16. Three small end-to-end examples

These examples are deliberately small so beginners can run them and understand each step.

Example 1: Predict a value with linear regression

from sklearn.linear_model import LinearRegression

# Input: hours studied; target: practice score
X = [[1], [2], [3], [4], [5]]
y = [20, 35, 50, 65, 80]

model = LinearRegression()
model.fit(X, y)

prediction = model.predict([[6]])
print("Predicted score:", round(prediction[0], 2))

What it does: learns a simple relationship between study hours and scores. The data is made up for practice and does not prove that studying always causes a particular score.

Example 2: Classify flowers with a built-in dataset

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42,
    stratify=iris.target
)

model = DecisionTreeClassifier(max_depth=3, random_state=42)
model.fit(X_train, y_train)

predictions = model.predict(X_test)
print("Accuracy:", round(accuracy_score(y_test, predictions), 3))

What it does: trains a decision tree to predict the flower class using measurements. The score may vary with the split and model settings.

Example 3: Group similar points with K-means

from sklearn.cluster import KMeans

points = [[1, 1], [1, 2], [2, 1], [8, 8], [8, 9], [9, 8]]

model = KMeans(n_clusters=2, random_state=42, n_init=10)
labels = model.fit_predict(points)

for point, label in zip(points, labels):
    print(point, "belongs to cluster", label)

What it does: groups nearby points into two clusters without needing class labels. Cluster numbers are arbitrary.

17. Quick revision

Term Meaning
AI Broad field of systems that perform intelligent tasks
ML Learning patterns from data
Deep learning ML using multi-layer neural networks
Supervised learning Learning from labelled examples
Unsupervised learning Finding patterns in unlabelled data
Classification Predicting a category
Regression Predicting a numeric value
Feature An input variable used by a model
Label/target The value the model is meant to predict
Overfitting Good training performance but poor generalization
Cross-validation Repeated train/validation splits for model assessment
Data leakage Unfair access to information that should not be available
Precision Fraction of predicted positives that are correct
Recall Fraction of actual positives that are found
Clustering Grouping similar examples without labels
Gradient descent Updating parameters to reduce a loss function

Further learning

  • Start with Python fundamentals and basic statistics.
  • Practice data preparation using pandas and scikit-learn.
  • Build a baseline model before trying more complex algorithms.
  • Evaluate models with metrics that match the problem.
  • Keep a final test set untouched until model selection is complete.

Backbencher Codes: https://backbencher.codes
Try code online: https://www.backbencher.codes/compiler

Tags:ProgrammingWebmachine learningai/mlartificial

Want to test code live?

Open our VS Code-like online compiler with 14 languages supported right in your browser.

Launch Online IDE →