Assignment 0.2
Assignment 0.2
2: Comparing the
Functioning of Machine Learning
Algorithms
Objective:
The goal of this assignment is to understand and compare how different machine learning
algorithms function, their strengths, weaknesses, and best use cases.
Comparison Table:
K-Nearest
Support Vector
Criteria Naïve Bayes Random Forest Neighbors
Machine (SVM)
(KNN)
Uses Bayes’
Finds an optimal Classifies based
theorem to Uses multiple
hyperplane that on the majority
How does it calculate the decision trees and
best separates the class of the k-
work? probability of a aggregates their
data points of nearest
class given the output
different classes neighbors
data
Decision Probability,
Entropy, Gini Distance metrics
Core Math boundaries, Bayes’ theorem,
impurity, (Euclidean,
Concepts Support vectors, Conditional
Information gain Manhattan, etc.)
Kernel functions independence
Training: O(n^2) Training: O(n log
Speed (Big-O Training: O(n), Training: O(1),
to O(n^3), n), Prediction:
Notation) Prediction: O(1) Prediction: O(n)
Prediction: O(n) O(log n)
High (especially
Low (if Medium to High (if High (if k is too
Overfitting Risk with a non-linear
assumptions hold) too many trees) small)
kernel)
Hard to interpret, Easy to interpret
Hard to interpret
Interpretability especially with Easy to explain for small
(black-box)
non-linear kernels datasets
Best Use Cases High-dimensional Spam detection, Complex decision Small datasets,
Pattern
Sentiment tasks, Financial
data, Text recognition,
analysis, Medical modeling, Fraud
classification Image
diagnosis detection
classification
Assumes feature Slow for large
Slow for large Prone to overfitting
independence, datasets,
datasets, Kernel if too many trees,
Limitations Can fail if features Sensitive to
selection is Computationally
are highly irrelevant
critical expensive
correlated features
Working Principle:
SVM aims to find the optimal decision boundary (hyperplane) that maximizes the
margin between different classes.
It uses support vectors (critical data points) to define the boundary.
Non-linearly separable data can be handled using kernel functions like RBF,
polynomial, or sigmoid.
Mathematical Concepts:
Use Cases:
2. Naïve Bayes
Working Principle:
Based on Bayes’ theorem, which calculates the probability of a class given input
features.
Assumes that all features are independent (hence "naïve").
Fast and efficient for large datasets with categorical data.
Mathematical Concepts:
Sentiment analysis.
Spam detection.
Medical diagnosis.
3. Random Forest
Working Principle:
An ensemble learning method that constructs multiple decision trees and aggregates
their predictions.
Uses bagging (bootstrap aggregation) to improve accuracy.
Reduces variance and overfitting compared to a single decision tree.
Mathematical Concepts:
Use Cases:
Fraud detection.
Credit scoring.
Disease prediction.
Working Principle:
Mathematical Concepts:
Use Cases:
Recommender systems.
Image recognition.
Anomaly detection.
import numpy as np
# Load dataset
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Define classifiers
classifiers = {
"SVM": SVC(kernel='linear'),
"KNN": KNeighborsClassifier(n_neighbors=3)
}
axes = axes.flatten()
clf.fit(X_train, y_train)
axes[i].set_title(name)
plt.tight_layout()
plt.show()
OUTPUT:
Naïve Bayes:
Random Forest:
KNN: