π What We Will Compare
This post compares three popular supervised ML algorithms:
- K-Nearest Neighbors (KNN)
- Support Vector Machine (SVM)
- Decision Tree (DT)
We'll evaluate them based on:
- Accuracy
- Macro Avg Metrics (Precision, Recall, F1)
- Weighted Avg Metrics (Precision, Recall, F1)
πΉ The Dataset: Glass Identification
We use the classic Glass Identification dataset which contains:
- 214 Rows
- 10 Columns: Refractive Index + 8 chemical properties + Glass Type
import pandas as pd
csv = pd.read_csv("glass.csv")
print(csv.columns)
π Features
- RI (Refractive Index)
- Na, Mg, Al, Si, K, Ca, Ba, Fe (Chemical elements)
- Type (Target class)
π Resources for Learning Algorithms
Instead of long theoretical definitions, here are great visual + article resources for each algorithm:
π’ KNN:
π³ Decision Tree:
βοΈ SVM:
π Project Structure
.
βπ glass.csv <- Dataset
βπ Dt.py <- Decision Tree class
βπ KNN.py <- KNN class
βπ SVM.py <- SVM class
βπ tools.py <- Visualization tools
βπ db.py <- Data inspection helper
βπ main.py <- Main runner
π Data Preparation
Each class implements a common interface:
x_train, x_test, y_train, y_test = model.data_preprocces()
Steps:
- Read CSV
- Drop last column for
X
- Use last column as
y
- Split into
train
andtest
(80/20)
Supported by: Dt.py
, KNN.py
, SVM.py
π‘ Model Integration
Each class supports a predict_report(choice, x_train, x_test, y_train, y_test)
method with the following choices:
-
0
: Return predictions -
1
: Return accuracy -
2
: Return confusion matrix -
3
: Return full classification report
πΏ Visualization Tools
The tools.py
module provides utility methods to plot each metric using matplotlib
:
Tools.Acc_table(report_list)
Tools.macro_prec(report_list)
Tools.macro_recall(report_list)
Tools.macro_f1(report_list)
Tools.wei_prec(report_list)
Tools.wei_recall(report_list)
Tools.wei_f1(report_list)
These will display stem charts for each metric and print scores for each model.
π Comparison Results
β Accuracy
π± Macro Avg Precision
π Macro Avg Recall
π Macro Avg F1
π Weighted Avg Precision
π Weighted Avg Recall
π Weighted Avg F1
πΉ Conclusion
β Decision Tree outperforms both SVM and KNN on every evaluation metric for this dataset.
This doesnβt mean itβs the best overall, but for Glass Type classification, DT gives the best results without any hyperparameter tuning.
π Try it Yourself
Check out the full source code here:
- π GitHub: Machine_Learning_Alg_Comp
All code is modularized for reuse. You can:
- Change the dataset
- Add new models
- Extend the visualizations
β¨ Star the Repo
If you found this project helpful, consider giving it a star on GitHub!
Thank you for reading! π
Top comments (0)