A simple, beginner-friendly neural network implementation in pure C that predicts house prices based on features like number of rooms, square footage, and distance to market.
This is a complete neural network implementation from scratch in C, with no external dependencies. The code is extensively commented to help beginners understand every concept, from activation functions to backpropagation.
Input Layer (3 neurons) → Hidden Layer (4 neurons) → Output Layer (1 neuron)
[rooms] [ReLU] [price]
[sqft] activation
[distance]
- Pure C implementation - No ML libraries required
- Heavily commented - Every line explained for beginners
- Forward propagation - Making predictions
- Backpropagation - Learning from mistakes
- ReLU activation - Non-linear activation function
- Gradient descent - Optimization algorithm
- Data normalization - Preprocessing for better training
The network learns from 8 sample houses:
| Rooms | Sqft | Distance (km) | Price ($k) |
|---|---|---|---|
| 3 | 1500 | 2.0 | 300 |
| 4 | 2000 | 1.5 | 400 |
| 2 | 1000 | 3.0 | 200 |
| 5 | 2500 | 1.0 | 500 |
| 3 | 1800 | 2.5 | 350 |
| 4 | 2200 | 1.2 | 450 |
| 2 | 900 | 4.0 | 180 |
| 6 | 3000 | 0.8 | 600 |
gcc neural_network.c -o neural_network -lm./neural_networkTraining Neural Network for House Price Prediction
Architecture: 3 → 4 → 1
Epoch 0, Loss: X.XXXX
Epoch 100, Loss: X.XXXX
...
=== Testing Predictions ===
Sample 1: Predicted $XXX.XXk, Actual $300.00k
...
=== New Prediction ===
House: 3 rooms, 1600 sqft, 1.8km from market
Predicted price: $XXX.XXk
The process of passing inputs through the network to get a prediction:
- Multiply inputs by weights
- Add biases
- Apply activation function (ReLU)
- Repeat for each layer
How the network learns from mistakes:
- Calculate error (prediction - actual)
- Propagate error backwards through layers
- Adjust weights to reduce future errors
A simple activation function:
- If input > 0: output = input
- If input ≤ 0: output = 0
Scaling all inputs to 0-1 range so features with larger values don't dominate training.
| Parameter | Value | Description |
|---|---|---|
INPUT_SIZE |
3 | Number of input features |
HIDDEN_SIZE |
4 | Neurons in hidden layer |
OUTPUT_SIZE |
1 | Number of outputs |
LEARNING_RATE |
0.0001 | Step size for gradient descent |
EPOCHS |
1000 | Training iterations |
Original code by @TheVixhal
Source: Original Tweet
Comments and documentation added for educational purposes.
This project is shared for educational purposes. Please credit the original author @TheVixhal if you use or modify this code.