This project contains a simple perceptron implementation and two example scripts that train it on basic logic gates.
perceptron.pydefines thePerceptronclass, including parameter initialization, training, and cost tracking.and_gate.pytrains the perceptron on the AND gate.or_gate.pytrains the perceptron on the OR gate.visualising_utils.pyprovides helpers for plotting the input data, decision boundary, and cost curve.pyproject.tomllists the project metadata and Python dependencies.
- Python 3.14 or newer
numpymatplotlib
Install the dependencies with:
uv syncThe perceptron expects input features in X and labels in Y.
- Inputs are arranged as a 2D NumPy array with shape
(samples, features). - Labels use the
-1and1convention. - Training uses a simple sign activation and updates weights over a fixed number of epochs.
The example scripts use the following training set:
X = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]])For the AND gate:
Y = np.array([[-1],
[-1],
[-1],
[1]])For the OR gate:
Y = np.array([[-1],
[1],
[1],
[1]])Run either example from the project directory:
uv run python and_gate.py
uv run python or_gate.pyEach script prints the initial and trained parameters, then displays:
- the input scatter plot
- the learned decision boundary
- the cost history
- The current implementation is intentionally minimal and focused on binary classification.
- The decision boundary helper assumes two input features.