0% found this document useful (0 votes)
11 views7 pages

Comprehensive Lab 2 Guide: ML Concepts

This document provides a comprehensive guide to Lab 2, covering essential machine learning terms, models, and processes such as data augmentation. It explains key concepts like classification, regression, KNN classifiers, and the importance of measuring model performance through train and validation accuracy. The guide also outlines a step-by-step workflow for training models, applying data augmentation, tuning hyperparameters, and visualizing results.

Uploaded by

katrao39798
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Comprehensive Lab 2 Guide: ML Concepts

This document provides a comprehensive guide to Lab 2, covering essential machine learning terms, models, and processes such as data augmentation. It explains key concepts like classification, regression, KNN classifiers, and the importance of measuring model performance through train and validation accuracy. The guide also outlines a step-by-step workflow for training models, applying data augmentation, tuning hyperparameters, and visualizing results.

Uploaded by

katrao39798
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Detailed Explanation of Lab 2: Machine Learning Terms, Metrics, and Data

Augmentation (Comprehensive & Structured)


This guide explains every concept, model, and process in Lab 2, incorporating all your follow-up
questions and using simple examples. It is organized into clear sections for easy understanding,
just like the structure used for Module 1.

Section 1: Core Machine Learning Terms and Concepts

Key Terms
Term Meaning Example

Feature A measurable property of data Pixel value in an image, house age

Target/Label The value or class the model tries to predict Digit (0-9), “Apple” or “Orange”

Classification Predicting a category/class for each input Is this an apple or an orange?

Regression Predicting a continuous value What is the house price?

Classifier An algorithm/model that assigns a class label KNN, Decision Tree

A system that learns from data to make


Model 1-NN classifier
predictions

Training set Data used to teach the model 80% of your dataset

Validation Data used to tune/check the model before final


10% of your dataset
set testing

Data used to evaluate the model’s final


Test set 10% of your dataset
performance

Metric A way to measure model performance Accuracy, precision, recall

Model memorizes training data but fails on new High train accuracy, low validation
Overfitting
data accuracy

Underfitting Model fails to learn patterns from data Low train and validation accuracy

Section 2: Models Used and How They Work


1. K-Nearest Neighbors (KNN) Classifier
Purpose: Assigns a class to a new data point based on the classes of its ‘k’ closest
neighbors in the training set [1] [2] .
How it works (with k=1, “1-NN”):
1. For a new data point, calculate its distance to every point in the training set.
2. Find the single closest point (“nearest neighbor”).
3. Assign the class of that neighbor to the new point.

Simple Example
Suppose you have these fruits:

Fruit Roundness Diameter Class

Apple 8 7 Apple

Orange 6 8 Orange

A new fruit has roundness 7, diameter 8.


Calculate distance to each fruit.
The closest is Orange (distance = 1).
So, classify the new fruit as Orange.

How to Find the Closest Neighbor


Euclidean distance:

Repeat for all points, pick the smallest value.

2. Random Classifier
Purpose: Assigns a random label to each data point, without learning from data.
Why use it: It’s a baseline to show what accuracy you’d get just by guessing.
Example:
If you have 4 fruits (2 apples, 2 oranges) and guess randomly, you’ll be correct about 50%
of the time.
Section 3: Measuring Model Performance

Train Accuracy vs. Validation Accuracy


Metric How It’s Calculated Example

Train Accuracy Correct predictions on training data 5/6 correct → 83.3%

Validation Accuracy Correct predictions on validation data 3/4 correct → 75%

Why both?
High train but low validation accuracy means overfitting.
Both low means underfitting.
Both high and close means good generalization [3] .

Section 4: How Images Are Represented for the Model

Flattened Pixel Values


What does “flattened” mean?
An image is a grid (e.g., 28x28 pixels). Flattening means turning this grid into a single long
list of numbers (784 values for MNIST).
Why flatten?
Most models expect a single list (vector) of features, not a 2D grid.

Example
A 3x3 image:

0 255 0
255 0 255
0 255 0

Flattened: ``

Section 5: Data Augmentation

What is Data Augmentation?


Definition:
Creating new data samples by making small, realistic changes to the originals (e.g., rotating,
shearing, flipping images) [4] [5] [6] [7] .
Why:
Increases dataset size, reduces overfitting, helps model generalize better.
Simple Example
If you have 5 images of a cat, you can:
Flip them horizontally
Rotate them a few degrees
Adjust brightness
Now you have many more images for training, even though you started with just 5.

How Shear Changes an Image


Shearing slants the image sideways, turning straight vertical lines into slanted lines, like
pushing the top of a square sideways while keeping the bottom fixed.
Why use shear?
It helps the model learn to recognize digits that are written at an angle.

Visual Example
Original:

+-----+
| |
| |
+-----+

After horizontal shear:

+-----+
/ /
/ /
+-----+

Section 6: Model and Evaluation Process

Step-by-Step Workflow
1. Train the 1-NN classifier on original (flattened) images.
Measure baseline accuracy (e.g., 80%).
2. Apply data augmentation (rotate, shear, etc.) to create new images.
Retrain the model on the expanded dataset.
Measure new accuracy (e.g., 85%).
3. Hyperparameter Tuning (Grid Search):
Hyperparameters: Settings you choose before training (e.g., angle of rotation, amount
of shear).
Grid search: Try different values (e.g., rotate by 10°, 20°, 30°) and see which gives the
best accuracy [8] .
For two hyperparameters (e.g., angle and shear), try all combinations.
4. Visualize Results with Graphs:
One hyperparameter:
X-axis: Value (e.g., angle in degrees)
Y-axis: Test accuracy
Plot to see which value works best.
Two hyperparameters (e.g., angle and shear):
Use a heatmap (colored grid):
X-axis: Angle
Y-axis: Shear
Color: Accuracy

Simple Example of the Whole Process


Suppose you want to train a model to recognize handwritten digits (like "3" and "8"):
1. Baseline:
You train your model on the original images.
Test accuracy: 80% (model gets 8 out of 10 test images right).
2. Augmentation:
You rotate each image by a small angle (say, 10°) and add these to your training data.
Retrain the model.
Test accuracy: 85% (now gets 8.5 out of 10 right on average).
3. Hyperparameter Tuning (Grid Search):
Try rotation angles: 5°, 10°, 15°, 20°
For each angle, retrain the model and measure accuracy.
Plot angle (X-axis) vs. accuracy (Y-axis). The best angle is where accuracy is highest.
4. Combined Augmentation:
Try all combinations of angle (5°, 10°, 15°) and shear (2, 4, 6).
For each pair, retrain and measure accuracy.
Show results as a heatmap: X-axis is angle, Y-axis is shear, color is accuracy.
Section 7: Why Use a Random Classifier as a Baseline?
A random classifier just guesses the class for each data point.
If your model can't do better than random, it means it isn’t learning any useful patterns.
Example:
For a binary task (apple/orange), random guessing gives you about 50% accuracy. Your
model should do better than this.

Section 8: Summary Table of All Concepts


Concept Explanation/Example

Classification Sorting data into categories (e.g., “cat” or “dog”)

Regression Predicting continuous values (e.g., house price)

1-NN Classifier Assigns class of the closest training example to a new data point

Flattened Pixel Values Turning image grid into a single list of numbers

Data Augmentation Creating new samples by changing originals (rotate, shear, etc.)

Shear Slanting an image sideways

Train/Validation Accuracy Accuracy on training/validation data

Random Classifier Assigns classes randomly, used as a baseline

Hyperparameter Tuning Trying different settings to find the best model performance

Grid Search Systematically trying combinations of hyperparameters

Graphs/Heatmaps Visualizing how accuracy changes with different settings

Section 9: Lab 2 in a Nutshell


1. Understand and use key ML terms and models (classification, regression, KNN, random
classifier).
2. Represent images as flattened pixel values for the model.
3. Augment data (rotate, shear) to help the model learn better.
4. Evaluate model using train and validation accuracy.
5. Tune hyperparameters (like angle/shear) using grid search.
6. Visualize results with graphs and heatmaps to pick the best settings.
7. Compare with a random classifier to ensure your model is actually learning.

If you need any section explained even more simply or visually, let me know!

1. [Link]
2. [Link]
3. [Link]
4. [Link]
5. [Link]
6. [Link]
7. [Link]
8. [Link]
h/

Common questions

Powered by AI

Hyperparameter tuning aims to optimize the performance of a machine learning model by selecting the best set of parameters that govern the learning process. Grid search facilitates this by exhaustively trying all combinations of specified parameter values, allowing one to evaluate which combination yields the highest model accuracy .

Overfitting occurs when a model learns the training data too well, including its noise and details, resulting in high training accuracy but low validation accuracy because it doesn't generalize to new data . Underfitting happens when a model is too simple to effectively learn the data patterns, resulting in low accuracy both on the training and validation datasets .

A random classifier serves as a baseline to assess whether a model is learning any meaningful patterns. If a model performs no better than the random classifier, it suggests the model is not learning effectively and improvements are needed . This benchmark ensures that the model's predictions are due to learned insights rather than chance .

Validation accuracy measures how well a model performs on unseen data that is not part of the training set. High validation accuracy, close to training accuracy, indicates good generalization, showing that the model can make accurate predictions beyond the training dataset .

A classifier is specifically designed for categorizing data into discrete classes, making it more suitable for applications where the goal is to sort inputs into categories, such as image categorization. It excels in scenarios requiring clear-cut classification over predicting continuous values, as classifiers are optimized to handle class labels effectively .

Heatmaps help in hyperparameter tuning by visually representing the accuracy results for various parameter values, allowing easy identification of optimal settings. They show interactions between two hyperparameters and quickly highlight the most effective combinations through color gradients .

Data augmentation prevents overfitting by artificially expanding the training set, thereby enabling the model to encounter more diverse data configurations and generalize better. Common techniques include rotating, shearing, flipping images, and adjusting brightness, which introduce variability and robustness to models .

A 1-Nearest Neighbor (1-NN) classifier assigns a class to a data point based on the class of its closest neighbor in the training set. In the KNN algorithm, it calculates the distance of the new point to all points in the training dataset and assigns the class of the closest point, using metrics like Euclidean distance .

Using insufficient data in training can lead to overfitting because the model might learn noise instead of the underlying patterns, or generalize poorly due to a lack of diversity in the input space, making it less robust on unseen data. It can also cause underfitting if the model doesn't capture enough features .

Flattening images converts a 2D grid of pixels into a single 1D vector of values, which is the format most machine learning models require for processing. This conversion allows models to treat each pixel consistently as part of a singular dimensional input, which simplifies the computation and storage of massive image data in vectors .

You might also like