Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CINN — Neural Network from Scratch

A neural network built from scratch using NumPy for binary diabetes classification, developed for the Computational Intelligence and Neural Networks (CSEN1121) course at the German University in Cairo (GUC).

The project covers the full machine learning workflow: data preprocessing, visual exploration, model implementation, training, and evaluation — all without high-level ML frameworks. TensorFlow, Keras, and PyTorch's model.Sequential() / .fit() are strictly prohibited per course requirements.


Dataset

The project uses the CDC Diabetes Health Indicators dataset (data/diabetes_binary.csv), a balanced binary classification dataset derived from CDC survey data.

Label Meaning
0 No Diabetes
1 Diabetes

Key features include BMI, Age, GenHlth, MentHlth, PhysHlth, HighBP, HighChol, and 14 additional health indicators (21 total).


Project Structure

CINN-Project/
├── main.py                  # Entry point — runs the full pipeline
├── data/
│   └── diabetes_binary.csv  # CDC Diabetes Health Indicators dataset
├── plots/                   # Auto-generated visualizations
│   ├── target_distribution.png
│   ├── feature_histograms.png
│   ├── correlation_heatmap.png
│   ├── scatter_plots.png
│   └── training_history.png
└── src/
    ├── __init__.py
    ├── neural_network.py    # NeuralNetwork class + standalone test function
    ├── activations.py       # Sigmoid, ReLU, Softmax (and their derivatives)
    ├── metrics.py           # Binary cross-entropy loss and accuracy
    ├── preprocessing.py     # Data loading, balancing, cleaning, summaries
    └── visualizations.py    # All matplotlib/seaborn plot functions

Network Architecture

Input Layer  →  Hidden Layer (ReLU)  →  Output Layer (Sigmoid)
[21 features]      [10 neurons]             [1 neuron]
  • Weights are initialised with He initialisation for the ReLU hidden layer
  • Training uses mini-batch gradient descent with manual backpropagation
  • Loss function is Binary Cross-Entropy

Pipeline

  1. Load & inspect the dataset and print a feature summary
  2. Balance classes if needed (dataset is pre-balanced 50/50 by default)
  3. Handle missing values via median imputation
  4. Visualise — target distribution, feature histograms, correlation heatmap, scatter plots
  5. Split into train / validation / test sets (70 / 20 / 10)
  6. Scale features with StandardScaler
  7. Train the network, logging loss and accuracy each epoch
  8. Evaluate on the held-out test set and report final loss and accuracy

Quickstart

Install dependencies

pip install numpy pandas scikit-learn matplotlib seaborn

Run

python main.py

Plots are saved to the plots/ directory. Epoch-by-epoch metrics print to stdout.


Hyperparameters

Parameter Default
Learning Rate 0.001
Epochs 300
Batch Size 100
Hidden Neurons 10
Random Seed 42

All are defined at the top of main.py and easy to modify.


Module Reference

src/neural_network.py

  • NeuralNetwork — initialises weights, forward pass, backward pass, parameter update, and the full training loop with validation tracking
  • test_TestSet_Random1 — standalone function that evaluates a saved weight dictionary on the test split

src/activations.py

  • relu / relu_derivative
  • sigmoid / sigmoid_derivative
  • softmax — implemented for potential multi-class extensions

src/metrics.py

  • error_Random1 — Binary Cross-Entropy, returns per-sample error vector and aggregated scalar
  • calculate_accuracy — threshold-based accuracy (threshold = 0.5)
  • one_hot_encode — utility for multi-class targets

src/preprocessing.py

  • load_diabetes_data — reads the CSV and enforces integer target encoding
  • handle_missing_values — median imputation per column with logging
  • balance_data — under/over-samples classes to a specified target count
  • get_feature_summary — dtype, unique value count, and null count per feature
  • get_numerical_statistics — extended describe() augmented with variance and range

src/visualizations.py

  • plot_target_distribution — bar chart of class counts
  • plot_feature_histograms — KDE histograms for selected features
  • plot_correlation_heatmap — Pearson correlation heatmap across all features
  • plot_scatter_plots — scatter plots coloured by diabetes label
  • plot_training_history — side-by-side loss and accuracy curves over epochs

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages