HELP

How to Build a Neural Network From Scratch in Python

Computing — March 29, 2026 — Edu AI Team

How to Build a Neural Network From Scratch in Python

If you want to learn how to build a neural network from scratch in Python, the simplest path is this: create input data, define weights and biases, pass the data through a few math steps, measure the error, and then adjust the weights little by little so the network improves. You do not need advanced maths or complex software to understand the basics. In this guide, we will build a tiny neural network in plain Python and explain every part in simple language.

A neural network is a computer system inspired loosely by the human brain. It takes in information, looks for patterns, and makes a prediction. For example, a neural network might decide whether an email is spam, whether a photo contains a cat, or whether a customer is likely to buy a product. The good news for beginners is that you can learn the core idea with a very small example.

What a neural network actually does

At its core, a neural network is just a series of calculations. It starts with inputs, which are the pieces of information you give it. It then multiplies those inputs by weights, adds a bias, and sends the result through an activation function. That activation function helps the network handle more complex patterns.

Think of it like this:

  • The inputs are ingredients.
  • The weights say how important each ingredient is.
  • The bias is a small extra adjustment.
  • The output is the final decision.

For a beginner example, imagine we want a network to learn a simple pattern. We will teach it that these inputs should lead to these outputs:

  • [0, 0] becomes 0
  • [0, 1] becomes 1
  • [1, 0] becomes 1
  • [1, 1] becomes 1

This is close to a basic logical rule: if at least one input is 1, the output should be 1.

Before we code: the 5 ideas you need

1. Inputs

Inputs are the numbers we feed into the network. In our example, each training example has two inputs.

2. Weights

Weights are adjustable numbers. They control how strongly each input affects the prediction. At first, the weights are random. During training, they get updated.

3. Bias

A bias is an extra number added to the calculation. It helps the network shift its decision boundary, which is just a simple way of saying it helps the model make better choices.

4. Activation function

An activation function changes the raw output into something more useful. We will use the sigmoid function because it is easy to understand. It turns any number into a value between 0 and 1, which is helpful for predictions like “no” or “yes”.

5. Training

Training means showing the network examples, checking how wrong it is, and adjusting the weights and bias to reduce that error. This process is repeated many times.

Build a simple neural network in Python

Below is a very small neural network with one layer. We are not using TensorFlow, PyTorch, or any heavy library. This is useful because it helps you see what is happening under the hood.

The Python code

Step 1: Import Python tools

We will use NumPy, a popular Python library for working with numbers and arrays.

import numpy as np

Step 2: Create the sigmoid function

def sigmoid(x):
return 1 / (1 + np.exp(-x))

Step 3: Create the derivative

The derivative helps us know how much to change the weights during training.

def sigmoid_derivative(x):
return x * (1 - x)

Step 4: Prepare the training data

X = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]])

y = np.array([[0],
[1],
[1],
[1]])

Step 5: Set random starting weights and bias

np.random.seed(42)
weights = np.random.rand(2, 1)
bias = np.random.rand(1)

Step 6: Train the network

learning_rate = 0.1

for epoch in range(10000):
# Forward pass
inputs = np.dot(X, weights) + bias
outputs = sigmoid(inputs)

# Error
error = y - outputs

# Backward pass
adjustments = error * sigmoid_derivative(outputs)
weights += np.dot(X.T, adjustments) * learning_rate
bias += np.sum(adjustments) * learning_rate

Step 7: Test the network

print("Final weights:")
print(weights)
print("Final bias:")
print(bias)

print("Predictions:")
print(sigmoid(np.dot(X, weights) + bias))

How this code works, line by line

Forward pass

The forward pass means the network takes the input values and makes a prediction. It does this with:

np.dot(X, weights) + bias

This multiplies each input by its weight and then adds the bias. After that, the sigmoid function squeezes the result into a number between 0 and 1.

For example, if the network outputs 0.92, that means it is very confident the answer should be 1. If it outputs 0.08, it is very confident the answer should be 0.

Error calculation

The error is simply:

error = y - outputs

If the correct answer is 1 but the network predicts 0.4, the error is 0.6. That tells the network it needs to improve.

Backward pass

The backward pass is the learning step. The network looks at the error and adjusts the weights and bias. This is the heart of learning in neural networks.

We use:

adjustments = error * sigmoid_derivative(outputs)

This tells us how much each weight should move. Then we update the weights and bias using the learning rate.

The learning rate controls the size of each step. A small learning rate means slow but careful learning. A large one means faster learning, but it may overshoot and miss the best answer.

What result should you expect?

After enough training rounds, the outputs should be close to:

  • [0, 0] → around 0.1
  • [0, 1] → around 0.9
  • [1, 0] → around 0.9
  • [1, 1] → around 0.99

These are not exact because the starting weights are random, but they should be very close to the correct pattern. In other words, the network has learned.

Why build a neural network from scratch?

If libraries like TensorFlow and PyTorch already exist, why start from scratch?

Because building one yourself helps you understand:

  • What weights and biases really do
  • How predictions are made
  • How errors drive learning
  • Why training takes many repeated steps

It is a bit like learning basic arithmetic before using a calculator. Once you understand the simple version, modern AI tools make much more sense.

If you want beginner-friendly lessons that explain Python, machine learning, and neural networks step by step, you can browse our AI courses to find a course that matches your level.

Common beginner mistakes

Using data in the wrong shape

Neural networks expect data in a certain structure. If your rows and columns are mixed up, your code may fail or give strange results.

Expecting perfect outputs

A neural network usually gives probabilities, not perfect 0s and 1s. So 0.93 is a strong “yes,” even if it is not exactly 1.

Training for too few rounds

If you stop too early, the network may not learn enough. In our example, 10,000 training rounds is reasonable because the dataset is tiny.

Starting with examples that are too complex

Many beginners jump straight into image recognition or chatbots. That can feel overwhelming. A tiny network like this is a much better first step.

What to learn after this

Once you understand a single-layer neural network, the next steps are:

  • Hidden layers: extra layers between input and output that help the network learn more complex patterns
  • Different activation functions: such as ReLU, which is widely used in deep learning
  • Loss functions: more formal ways to measure how wrong the model is
  • Real datasets: using larger, more useful examples
  • Frameworks: tools like TensorFlow and PyTorch for building larger models

You may also want to strengthen your Python basics first. That often makes machine learning much easier to understand. If you are just getting started and want a structured path instead of piecing together random tutorials, you can register free on Edu AI and explore beginner-friendly learning tracks.

Is building from scratch enough to get into AI?

Building one small neural network will not make you an AI engineer overnight, but it gives you a very strong foundation. Many people who switch into AI or data careers begin with simple projects exactly like this one. The key is to understand the logic before moving to larger tools and real-world projects.

For beginners, the best learning order is usually:

  • Learn basic Python
  • Understand simple machine learning ideas
  • Build a tiny neural network from scratch
  • Use beginner-friendly libraries
  • Create small portfolio projects

Get Started

Now you know the basic answer to how to build a neural network from scratch in Python: start with inputs, weights, a bias, an activation function, and a training loop that slowly reduces error. Keep it small, repeat the process, and focus on understanding each step.

If you would like help turning these first concepts into practical skills, Edu AI offers beginner-focused courses in Python, machine learning, and deep learning. You can browse our AI courses or view course pricing to choose a learning path that fits your goals.

Article Info
  • Category: Computing
  • Author: Edu AI Team
  • Published: March 29, 2026
  • Reading time: ~6 min