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

📋 Dataset Structure (5-Sample List)

To help the model learn, we provide the data in two matching lists. Each item in the Features list corresponds directly to the same item in the Labels list.

The dataset is split into two synchronized arrays: X (The characteristics we measure) and y (The species we want to predict).

The Features List (X)

  • Format: [🟢 Sepal L, 🔵 Sepal W, 🔴 Petal L, 🟡 Petal W]
  • Sample 1: [🟢5.1, 🔵3.5, 🔴1.4, 🟡0.2]
  • Sample 2: [🟢4.9, 🔵3.0, 🔴1.4, 🟡0.2]
  • Sample 3: [🟢7.0, 🔵3.2, 🔴4.7, 🟡1.4]
  • Sample 4: [🟢6.4, 🔵3.2, 🔴4.5, 🟡1.5]
  • Sample 5: [🟢5.8, 🔵2.7, 🔴4.1, 🟡1.0]

The Labels List (y)

  • Format: [Species ID]
  • Sample 1: ⚪ 0
  • Sample 2: ⚪ 0
  • Sample 3: ⚫ 1
  • Sample 4: ⚫ 1
  • Sample 5: ⚫ 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): PREDICT([🟢5.0, 🔵3.6, 🔴1.4, 🟡0.2])Result: 0.02 (Very sure it is ⚪ Setosa)
  • High Confidence (Versicolor): PREDICT([🟢6.7, 🔵3.1, 🔴5.6, 🟡2.4])Result: 0.98 (Very sure it is ⚫ Versicolor)

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

  • Uncertain (Leans Setosa): PREDICT([🟢5.5, 🔵2.5, 🔴3.0, 🟡1.1])Result: 0.42 (Slightly favors ⚪ Setosa)
  • Uncertain (Leans Versicolor): PREDICT([🟢6.0, 🔵2.8, 🔴4.2, 🟡1.3])Result: 0.58 (Slightly favors ⚫ Versicolor)

📊 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.8.tar.gz (7.7 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.8-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: annpy_neopydev5454-0.1.8.tar.gz
  • Upload date:
  • Size: 7.7 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.8.tar.gz
Algorithm Hash digest
SHA256 868929492f99f17b4987e90e36dc1c2df2f46a667e2c1fe0cfb62b3f910430bd
MD5 1a70c4ed12fa138020fba3cc453ef94a
BLAKE2b-256 3b93b64a7dd49ef11db00b05740d68e1c8bae4d63120abe8bd6fec6669138659

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for annpy_neopydev5454-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7e596de59abf78eb274ce3e1e48ea4bdb8a219773cb7a43d1f58f8d4bc62a317
MD5 bb20bbf5e79cd5ceba7cd9f49468f0c5
BLAKE2b-256 a64a1f93b2d19d319fa09d134178826d8681fa155ed1c3054ad6b189b0e6d29c

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