Skip to content

Perceptron implementation for binary classification with C core and Python wrapper. Includes Iris/synthetic data, training and visualizations.

Notifications You must be signed in to change notification settings

JvFg92/Perceptron_Data_Classify

Repository files navigation

🧠 C-Accelerated Perceptron for Binary Classification ⚙️

This project implements a Perceptron algorithm for binary classification, with its main design functions written in C for performance optimization and wrapped in a Python class for easy use, data manipulation, and visualization. 📊 The project includes functionality to use the Iris dataset (converted to a binary problem) or generate synthetic data for training and testing.

🎯 Goal: Create an efficient Perceptron classifier with a C backend and a friendly Python interface.

Example View:

Decision Boundary Example

🌟 Overview

Perceptron is one of the simplest supervised machine learning algorithms for binary classification. This project demonstrates:

  • Implementation of the Perceptron algorithm.
  • Usage of C for computationally intensive operations (neuron calculation, weight adjustment, accuracy evaluation) via ctypes in Python.
  • A Python Perceptron class that encapsulates the training, prediction, evaluation, and plotting logic.
  • Loading and preprocessing data for the Iris dataset and generating synthetic data. 🌸
  • Splitting data into training and testing sets.
  • Feature scaling (Z-score normalization).
  • Training with a defined learning rate and accuracy threshold.
  • Calculating error, accuracy, and recall. 📈
  • K-fold cross-validation. * Visualization of:
  • Training and testing data 📍
  • Decision boundary of the trained model 🗺️
  • Model accuracy over epochs 🎯
  • Evolution of weights during training 🏋️
  • Model error over epochs 📉

✨ Main Features

  • C Core ⚙️: neuron, fit, evaluate_accuracy, predict and recall functions, innovations in C for efficiency.
  • Python Wrapper 🐍: Easy-to-use Perceptron class in Python.
  • Data Sources 💾:
  • Utilize the Iris dataset (filtered for two classes and two features).
  • Synthetic data generation for classification problems (linearly separable or noisy).
  • Preprocessing 🧹:
  • Convert multiclass problems to binary.
  • Scale features using mean and standard deviation.
  • Training 🏋️‍♀️:
  • Iterate until a baseline accuracy is reached on the test set or a maximum number of epochs is reached.
  • Store historical weights, errors, and accuracy.
  • Evaluation 📊:
  • Calculate accuracy on the training and test sets.
  • Perform k-fold cross-validation.
  • Visualization 🖼️: Use matplotlib to plot:
  • Data distribution.
  • Decision boundary.
  • Learning curves (accuracy, error, weights).
  • Analysis 🖼️: * Flexibility 🛠️: Allows configuration of learning rate, reference accuracy and data generation parameters.

📂 Project Structure

├── 📄 perceptron.c # C implementation of the main Perceptron functions

├── 📄 perceptron.h # Header file for the C code

├── 🔗 perceptron.so # Compiled shared library (generated after compilation)

├── 🐍 data_treatment.py # Functions for data import, generation and plotting

├── 🐍 training.py # Python Perceptron class and ctypes interface for C

└── 📖 main.py # Main script to run training and evaluation

🛠️ Prerequisites

  • Python 3.12.3 🐍
  • C compiler (like CGC) ⚙️
  • Python libraries:
  • numpy
  • matplotlib
  • scikit-learn (used in data_treatment.py for load_iris and make_classification)

🚀 Setup and Installation

  1. Clone the repository:
git clone https://github.com/JvFg92/Perceptron_Data_Classify
cd Perceptron_Data_Classify
  1. Compile the C code to create a shared library (perceptron.so): On Linux or macOS:
gcc -shared -o perceptron.so -fPIC perceptron.c

On Windows (may require configuration depending on your compiler, e.g. with MinGW):

gcc -shared -o perceptron.so perceptron.c -Wl,--add-stdcall-alias

ℹ️ Make sure the resulting perceptron.so (or perceptron.dll on Windows) file is in the same directory as the Python scripts.

  1. Install Python dependencies:
pip install numpy matplotlib scikit-learn

⚠️ For Linux you may need:

sudo apt install python3-numpy
sudo apt install python3-matplotlib
sudo apt install python3-sklearn

⚠️ For Windows you may need:

py -m pip install numpy matplotlib scikit-learn

✅ Ready to go!

▶️ Usage

The main script to run the model is main.py.

python main.py

⚠️ For Linux you may need:

python3 main.py

⚠️ For Windows you may need:

py main.py

About

Perceptron implementation for binary classification with C core and Python wrapper. Includes Iris/synthetic data, training and visualizations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •