Skip to main content

A light-weight Artificial Neural Network implementation from scratch

Project description

Annpy 🧠

GitHub Repo License: MIT Python 3.8+ NumPy

Annpy is a lightweight, efficient, and fully transparent Artificial Neural Network (ANN) implementation built from scratch using NumPy.

While many frameworks are "black boxes," Annpy is designed to be customizable and clear, making it perfect for educational purposes, lightweight integrations, and fast prototyping.


✨ Key Features

  • 🎯 Total Architectural Flexibility:
    • Any Input Size: Configure the network to accept any number of input features—from 2 to 2,000+.
    • Customizable Hidden Layer: You define the number of neurons in the hidden layer to perfectly balance learning power and processing speed.
  • ⚡ High Computational Efficiency: By leveraging optimized NumPy vectorization, Annpy executes complex matrix operations with remarkable speed, proving that clean code can be incredibly fast.
  • 🧠 From-Scratch Implementation: No heavy dependencies like PyTorch or TensorFlow. Just pure Python and NumPy logic.
  • 📊 Built-in Preprocessing: Features automated data normalization (scaling to [0, 1]) and shuffling to ensure your model trains effectively out of the box.
  • 🛠️ Developer Friendly: Clean, documented code that is easy to read, extend, and debug.

🚀 Installation

Install Annpy directly via pip:

pip install annpy-neopydev5454

🛠️ Quick Start: The Iris Dataset Example

In this example, we configure Annpy to classify a dataset of 20 samples. The network is set to receive 4 inputs and uses a hidden layer of 10 neurons.

import numpy as np
from annpy import ANN

# 1. Prepare synthetic data (20 samples, 4 features each)
X = np.array([
    [5.1, 3.5, 1.4, 0.2], [4.9, 3.0, 1.4, 0.2], [7.0, 3.2, 4.7, 1.4], [6.4, 3.2, 4.5, 1.5],
    [5.8, 2.7, 4.1, 1.0], [5.1, 2.5, 3.0, 1.1], [6.7, 3.1, 5.6, 2.4], [6.3, 2.3, 4.4, 1.3],
    [5.0, 3.4, 1.5, 0.2], [5.9, 3.0, 5.1, 1.8], [5.2, 3.5, 1.5, 0.2], [4.7, 3.2, 1.3, 0.2],
    [6.9, 3.1, 4.9, 1.5], [5.4, 3.9, 1.7, 0.4], [6.5, 2.8, 4.6, 1.5], [5.7, 2.8, 4.5, 1.3],
    [6.3, 3.3, 4.7, 1.6], [4.6, 3.1, 1.5, 0.2], [5.0, 3.6, 1.4, 0.2], [6.1, 2.8, 4.0, 1.3]
])

# 2. Prepare target labels (Must match X length)
y = np.array([0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1])

# 3. Initialize the model
# input_size=4 (features), hidden_layer_size=10 (neurons)
model = ANN(hidden_layer_size=10, input_size=4)

# 4. Train the model
# X: The training features matrix
# y: The target labels (must be the same length as X)
# learning_rate: The step size for weight updates
# epochs: Number of times to iterate over the entire dataset
model.train(X, y, learning_rate=0.1, epochs=1500)

# 5. Predict a new sample
# Note: Since this sample is similar to the Setosa class (class 0), the prediction should be as close as possible to 0.
new_sample = np.array([5.0, 3.6, 1.4, 0.3])
prediction = model.predict(new_sample)
print(f"Prediction: {prediction}")

🐣 Beginner's Guide: How it Works

If you are new to Neural Networks, here is a simple breakdown of how Annpy processes data, using the classic Iris Flower dataset:

1. The Goal

Annpy is a mathematical tool designed to classify data into two distinct categories.

  • The Species: Distinguishing between ⚪ Setosa (0) and ⚫ Versicolor (1).
  • The Logic: Let's assign a color/symbol to each classification for clarity:
    • ⚪ White: Category 0 (Setosa)
    • ⚫ Black: Category 1 (Versicolor)

2. The 4 Input Features (The Measurements)

To identify a flower, we provide the network with 4 specific measurements. Each measurement is a "Feature" with its own color:

  1. 🟢 Sepal Length: The length of the outer green leaves.
  2. 🔵 Sepal Width: The width of the outer green leaves.
  3. 🔴 Petal Length: The length of the inner colorful petals.
  4. 🟡 Petal Width: The width of the inner colorful petals.

3. The Learning Phase (Training)

The network learns the "mathematical fingerprint" of each species by looking at hundreds of labeled examples:

Example of Labeled Data:

  • Sample A (Setosa): [🟢5.1, 🔵3.5, 🔴1.4, 🟡0.2]Label: ⚪ 0
  • Sample B (Versicolor): [🟢7.0, 🔵3.2, 🔴4.7, 🟡1.4]Label: ⚫ 1

4. The Prediction Phase

Once trained, you give the network 4 new measurements. It processes these values and produces a single output between 0 and 1.

5. Understanding the Results

The result represents the network's classification. The closer to 0 (White) or 1 (Black), the higher the confidence:

  • High Confidence (Setosa): Result 0.08 — The network is very sure the flower is ⚪ White / Setosa (0).
  • High Confidence (Versicolor): Result 0.92 — The network is very sure the flower is ⚫ Black / Versicolor (1).

What does "Uncertainty" look like? When features are "blurry" or sit right on the edge between species:

  • Uncertain (Leans Setosa): Result 0.45 — Slightly favors ⚪ White / Setosa (0).
  • Uncertain (Leans Versicolor): Result 0.55 — Slightly favors ⚫ Black / Versicolor (1).

📊 How it Works

Annpy implements a Multi-Layer Perceptron (MLP) with one hidden layer:

  1. Dynamic Weights: Initialized based on your input_size and hidden_layer_size.
  2. Forward Pass: Uses the Sigmoid activation function to compute the output.
  3. Backpropagation: Updates weights using Gradient Descent.
  4. Auto-Normalization: Automatically scales input data to [0, 1] for better convergence.

📜 License

Distributed under the MIT License. See the LICENSE file for more information.


Created with ❤️ by Nehorai Yosef | Contact

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

annpy_neopydev5454-0.1.7.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

annpy_neopydev5454-0.1.7-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file annpy_neopydev5454-0.1.7.tar.gz.

File metadata

  • Download URL: annpy_neopydev5454-0.1.7.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for annpy_neopydev5454-0.1.7.tar.gz
Algorithm Hash digest
SHA256 7c2181f9818d0009dbe7f2d990239cb0f4aab70f4c43145a9d0794989614bdff
MD5 8fe26714eefa9f6a8058ee65b3a38fa4
BLAKE2b-256 7a5d8c5acaa0384ef03219a9840890dc884059d36c95d72b16101f5cbf0aa6c2

See more details on using hashes here.

File details

Details for the file annpy_neopydev5454-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for annpy_neopydev5454-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 29acfc3ad148bc554df959dde0eab1644019fc1da58b9a5567d5894d08bc62bb
MD5 e084e2592746f1db7d24dbdad8238b0e
BLAKE2b-256 34f9df83eaae94339002dfd6d0a0710e620170301bc7d282b392598323885f8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page