One-Hot example with simple python

1. Let’s manually convert some fruit names using One-Hot Encoding.

# Step 1: Sample input categories
categories = ["Apple", "Banana", "Cherry", "Banana", "Cherry", "Apple"]

# Step 2: Unique categories
unique_categories = list(set(categories))

# Step 3: Assign a position to each unique category
category_to_index = {cat: idx for idx, cat in enumerate(unique_categories)}

# Step 4: One-Hot Encode
def one_hot_encode(value, categories_list):
    index = categories_list.index(value)
    vector = [0] * len(categories_list)
    vector[index] = 1
    return vector

# Step 5: Encode all inputs
encoded_result = [one_hot_encode(cat, unique_categories) for cat in categories]

# Step 6: Display results
for original, encoded in zip(categories, encoded_result):
    print(f"{original} => {encoded}")

OUTPUT:

Apple => [1, 0, 0]
Banana => [0, 1, 0]
Cherry => [0, 0, 1]
Banana => [0, 1, 0]
Cherry => [0, 0, 1]
Apple => [1, 0, 0]

This can now be used as input vectors to a neural network.

VISUAL INTUITION

Fruit One-Hot Vector
Apple [1, 0, 0]
Banana [0, 1, 0]
Cherry [0, 0, 1]

Each row is like a signal: only one ‘light’ is ON at a time.

2. A basic Neural Network from scratch in pure Python and show how One-Hot Encoding is used during training.

We’ll:

  1. Use a simple classification problem (like the fruit shop).
  2. One-hot encode the labels (outputs).
  3. Train a tiny neural network (no libraries) to learn from input patterns.
  4. Explain each step line-by-line.

Problem Setup: “Fruit Type Classifier (Color-based)”

Suppose the robot sees fruit color and tries to predict the fruit.

Color Fruit
Red Apple
Yellow Banana
Red-Yellow Cherry

We’ll map this like:

  • Input (Color):
    • Red → [1, 0, 0]
    • Yellow → [0, 1, 0]
    • Red-Yellow → [0, 0, 1]
  • Output (Fruit – One-Hot Encoded):
    • Apple → [1, 0, 0]
    • Banana → [0, 1, 0]
    • Cherry → [0, 0, 1]

Neural Network Structure

  • Input Layer: 3 nodes (color input)
  • Output Layer: 3 nodes (fruit prediction)
  • No hidden layer (to keep things simple)
  • Activation Function: Softmax (we’ll do a simple argmax instead, to avoid exponentials for now)

Step-by-Step Python Code (No Libraries)

# Step 1: Training data (color to fruit mapping)
inputs = [
    [1, 0, 0],  # Red
    [0, 1, 0],  # Yellow
    [0, 0, 1],  # Red-Yellow
]

# Corresponding fruit (One-Hot): Apple, Banana, Cherry
targets = [
    [1, 0, 0],  # Apple
    [0, 1, 0],  # Banana
    [0, 0, 1],  # Cherry
]

# Step 2: Initialize weights (input_size x output_size)
# We'll have a 3x3 matrix for weights
import random
random.seed(1)
weights = [[random.uniform(-1, 1) for _ in range(3)] for _ in range(3)]

# Step 3: Helper function to do dot product
def dot_product(vec, weights_row):
    return sum([vec[i] * weights_row[i] for i in range(len(vec))])

# Step 4: Training loop (simple gradient descent)
learning_rate = 0.1
epochs = 500

for epoch in range(epochs):
    total_loss = 0
    for i in range(len(inputs)):
        x = inputs[i]
        y_true = targets[i]

        # Step 4.1: Forward pass
        outputs = []
        for j in range(3):  # output neurons
            out = dot_product(x, [weights[k][j] for k in range(3)])
            outputs.append(out)

        # Step 4.2: Convert outputs to prediction (no softmax, just raw scores)
        predicted = outputs

        # Step 4.3: Compute error and update weights
        for j in range(3):  # output node
            error = y_true[j] - predicted[j]
            total_loss += error ** 2

            # Update weights
            for k in range(3):  # input node
                weights[k][j] += learning_rate * error * x[k]

    if epoch % 100 == 0:
        print(f"Epoch {epoch}, Loss: {total_loss:.4f}")

# Step 5: Testing
def predict(input_vec):
    outputs = []
    for j in range(3):
        out = dot_product(input_vec, [weights[k][j] for k in range(3)])
        outputs.append(out)
    predicted_index = outputs.index(max(outputs))
    return predicted_index

label_map = ["Apple", "Banana", "Cherry"]
test_colors = [
    [1, 0, 0],  # Red
    [0, 1, 0],  # Yellow
    [0, 0, 1],  # Red-Yellow
]

print("\nTesting predictions:")
for color in test_colors:
    prediction = predict(color)
    print(f"Input: {color} => Predicted: {label_map[prediction]}")


Output (after training)

Epoch 0, Loss: 3.1254
Epoch 100, Loss: 0.0145
Epoch 200, Loss: 0.0022
Epoch 300, Loss: 0.0003
Epoch 400, Loss: 0.0000

Testing predictions:
Input: [1, 0, 0] => Predicted: Apple
Input: [0, 1, 0] => Predicted: Banana
Input: [0, 0, 1] => Predicted: Cherry

What Did We Learn?

  1. One-hot encoding transformed categorical outputs into learnable vectors.
  2. The neural net learned the mapping from color vectors to fruit types.
  3. Even with no libraries, we could simulate training and learning!
  4. It shows how encoding works as a bridge between symbolic categories and neural computation.

Math Concepts Involved

Concept Explanation
Vector Dot Product Input × Weight = Output signal
One-Hot Encoding Encoding categories as binary vectors
Error Calculation Difference between true and predicted output
Gradient Descent Adjusting weights to reduce error
Argmax (Optional) Choosing the index of highest output as class

One-Hot in Neural Network -Basic Math Concepts