💚
Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Simple Neural Network Example
Let us put everything together and look at a complete, working neural network example that classifies whether a person has a high or low credit risk based on two features.
15 min•By Priygop Team•Updated 2026
Complete Neural Network Example
Complete Neural Network Example
import math
import random
random.seed(42)
# ====================================================
# Simple 2-layer neural network for binary classification
# Task: predict high (1) or low (0) credit risk
# Features: [income_scaled, debt_ratio]
# ====================================================
# Training data: [income_scaled, debt_ratio] -> credit_risk
data = [
([0.8, 0.2], 0), # high income, low debt -> low risk
([0.3, 0.8], 1), # low income, high debt -> high risk
([0.9, 0.1], 0), # very high income, minimal debt -> low risk
([0.2, 0.9], 1), # very low income, very high debt -> high risk
([0.6, 0.4], 0), # decent income, moderate debt -> low risk
([0.1, 0.7], 1), # low income, high debt -> high risk
]
# Helper functions
def sigmoid(z):
return 1 / (1 + math.exp(-z))
def predict(x1, x2, w1, w2, bias):
"""Simple 1-neuron model (no hidden layer for clarity)."""
z = x1 * w1 + x2 * w2 + bias
return sigmoid(z)
# Initialize weights randomly
w1 = random.uniform(-1, 1)
w2 = random.uniform(-1, 1)
b = 0.0
lr = 0.5
print("Training a neural network on credit risk data...")
print()
# Train for 200 epochs
for epoch in range(200):
total_loss = 0
for features, label in data:
x1, x2 = features
# Forward pass
output = predict(x1, x2, w1, w2, b)
# Loss (binary cross-entropy, simplified)
loss = -(label * math.log(output + 1e-8) + (1 - label) * math.log(1 - output + 1e-8))
total_loss += loss
# Gradient descent update
error = output - label
w1 -= lr * error * x1
w2 -= lr * error * x2
b -= lr * error
if epoch % 50 == 0:
accuracy = sum(
1 for f, l in data if round(predict(f[0], f[1], w1, w2, b)) == l
) / len(data)
print(f"Epoch {epoch:3d}: loss={total_loss:.3f}, accuracy={accuracy*100:.0f}%")
print()
print("Final predictions on training data:")
for features, label in data:
pred = predict(features[0], features[1], w1, w2, b)
predicted_class = "HIGH RISK" if round(pred) == 1 else "LOW RISK"
actual_class = "HIGH RISK" if label == 1 else "LOW RISK"
status = "OK" if predicted_class == actual_class else "WRONG"
print(f" Income={features[0]}, Debt={features[1]} -> {predicted_class} (actual: {actual_class}) {status}")Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Key Takeaways from This Module
- A neural network is a system of connected mathematical units (neurons) organized in layers
- Each neuron calculates: weighted sum of inputs + bias, then applies an activation function
- Weights control how much each input matters. Bias shifts the activation threshold
- ReLU is the most common activation for hidden layers. Sigmoid is used for binary output
- The forward pass flows data from input to output through all layers
- Loss measures how wrong the predictions are. The goal of training is to minimize loss
- Training adjusts weights using gradient descent: calculate the error, then update weights to reduce it
- The learning rate controls how large each weight update step is
Key Takeaways
- Let us put everything together and look at a complete, working neural network example that classifies whether a person has a high or low credit risk based on two features.
- A neural network is a system of connected mathematical units (neurons) organized in layers
- Each neuron calculates: weighted sum of inputs + bias, then applies an activation function
- Weights control how much each input matters. Bias shifts the activation threshold