A dual-implementation machine learning library: classic algorithms from scratch (NumPy/Python) and with scikit-learn, for learning and benchmarking.
- Project Overview
- Directory Structure
- Installation
- Dataset Acquisition
- Usage
- API Reference
- Development & Testing
- Contributing
- License
- Acknowledgements
This repository contains implementations of various machine learning algorithms, each with two versions:
- From Scratch:
- Linear Regression
- Logistic Regression
- Support Vector Machine (SVM)
- K-Means Clustering
- Scikit-learn Implementations:
- Parallel implementations for each algorithm using scikit-learn for comparison and validation.
ML_algos/
├── src/
│ ├── scratch/
│ │ ├── models/
│ │ │ ├── base_model.py
│ │ │ ├── linear_regression.py
│ │ │ ├── logistic_regression.py
│ │ │ ├── svm.py
│ │ │ ├── K_Means.py
│ │ └── utils/
│ │ ├── data_utils.py
│ │ ├── math_utils.py
│ │ ├── metrics.py
│ │ ├── smo_utils.py
│ │ ├── training_utils.py
│ │ └── viz_utils.py
│ └── sklearn_impl/
│ ├── linear_regression_sk.py
│ ├── logistic_regression_sk.py
│ ├── svm_sk.py
│ └── k_means_sk.py
├── data/
│ ├── raw/
│ ├── processed/
│ └── examples/
├── notebooks/
│ ├── data_preprocessing.ipynb
│ ├── experiments/
│ │ ├── linear_regression/
│ │ ├── logistic_regression/
│ │ ├── SVM/
│ │ └── K_means/
│ └── comparisons/
├── tests/
│ ├── test_models/
│ └── test_utils/
├── requirements.txt
├── environment.yml
├── README.md
└── .gitignore
-
Clone the repository:
git clone https://github.com/moaz-loaie/ML_algos.git cd ML_algos
-
Create and activate a virtual environment (recommended):
-
Using conda:
conda env create -p .env -f environment.yml conda activate ./.env
-
Using venv:
python -m venv .env .env\Scripts\Activate.ps1
-
-
Install dependencies:
pip install -r requirements.txt
Datasets are from Kaggle. Download and extract them into data/raw/ as described below.
-
Install the Kaggle API if not already installed:
-
Using conda:
conda install -c conda-forge kaggle
-
Using pip:
pip install kaggle
-
-
Authenticate:
- Place your
kaggle.jsonAPI token in~/.kaggle/(Linux/macOS) or%USERPROFILE%\.kaggle\(Windows). See Kaggle API documentation.
- Place your
-
Download the datasets:
Windows (PowerShell) or Linux/macOS (Bash):
kaggle datasets download -d nikhil7280/student-performance-multiple-linear-regression -p data/raw/Regression_Dataset kaggle datasets download -d yasserh/breast-cancer-dataset -p data/raw/Classification_Dataset kaggle datasets download -d youssefaboelwafa/clustering-penguins-species -p data/raw/K_Means_Dataset
-
Visit the dataset URLs above and download the files manually.
-
Place the downloaded files in the appropriate subfolders under
data/raw/:data/raw/Regression_Dataset/data/raw/Classification_Dataset/data/raw/K_Means_Dataset/
-
Extract the files:
Windows (PowerShell):
Expand-Archive data/raw/Regression_Dataset/*.zip -DestinationPath data/raw/Regression_Dataset Expand-Archive data/raw/Classification_Dataset/*.zip -DestinationPath data/raw/Classification_Dataset Expand-Archive data/raw/K_Means_Dataset/*.zip -DestinationPath data/raw/K_Means_Dataset
Linux/macOS (Bash):
unzip -o 'data/raw/Regression_Dataset/'*.zip -d data/raw/Regression_Dataset unzip -o 'data/raw/Classification_Dataset/'*.zip -d data/raw/Classification_Dataset unzip -o 'data/raw/K_Means_Dataset/'*.zip -d data/raw/K_Means_Dataset
- Preprocess the data using
notebooks/data_preprocessing.ipynb. This notebook reads fromdata/raw/and writes processed files todata/processed/. - Both
data/raw/anddata/processed/are not tracked by Git. Manage these directories manually.
-
Preprocess the data:
- Run
notebooks/data_preprocessing.ipynbto generate processed datasets.
- Run
-
Experiment and train models:
- Use notebooks in
notebooks/experiments/for each algorithm (from scratch and sklearn). - Compare results in
notebooks/comparisons/.
- Use notebooks in
LinearRegression(src/scratch/models/linear_regression.py)LogisticRegression(src/scratch/models/logistic_regression.py)SVM(src/scratch/models/svm.py)KMeans(src/scratch/models/K_Means.py)
LinearRegressionSK(src/sklearn_impl/linear_regression_sk.py)LogisticRegressionSK(src/sklearn_impl/logistic_regression_sk.py)SVM_SK(src/sklearn_impl/svm_sk.py)KMeansSK(src/sklearn_impl/k_means_sk.py)
- Data:
data_utils.py - Math:
math_utils.py - Metrics:
metrics.py - Visualization:
viz_utils.py - SVM helpers:
smo_utils.py - Training helpers:
training_utils.py
-
Python version: 3.8+
-
Run tests:
# (Assuming pytest is installed) pytest tests/
-
Supported OS: Windows, Linux, macOS
Contributions are welcome! To contribute:
- Fork the repository and create a new branch.
- Make your changes with clear commit messages.
- Run tests and ensure all pass.
- Open a pull request with a description of your changes.
For major changes, please open an issue first to discuss your ideas.
- scikit-learn
- NumPy
- Matplotlib
- All contributors and the open-source community.