Nexus Learn

Practical 2: Building a Perceptron from Scratch

Lab Practical CS-511 Artificial Intelligence 15 min read Updated 8 Nov 2026
On this page
  1. 1. Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (180 minutes)
  5. Part A - Guided Warm-up (type, run, observe)
  6. Part B - Predict the Output
  7. Part C - Bug Hunt
  8. Part D - Graded Tasks (20 marks)
  9. Part E - Post-Lab Questions
  10. Marking Rubric (applied to Part D)
  11. Submission

1. Objectives

  1. Build logic gates (AND, OR, NAND) with a single artificial neuron by choosing weights by hand.
  2. Apply the perceptron learning rule w ← w + η·(y − ŷ)·x step by step and trace the weight changes.
  3. Write a reusable Perceptron class with fit() and predict() using only NumPy.
  4. Show why a single perceptron cannot learn XOR (not linearly separable).
  5. Train a perceptron on real-style data (cotton farms, student results) with scaling and a train/test split.
  6. Plot the learning curve and the decision boundary, and explain them.

2. Word Bank

Word

Simple meaning

Real-world example

Perceptron

The simplest learning neuron: weighted sum + step activation

Rosenblatt's machine, 1958

Weight (w)

How important an input is

Attendance may matter more than hours

Bias (b)

A shift that moves the decision line

A stricter or easier pass mark

Step activation

Output 1 if z ≥ 0, else 0

Pass / fail

Learning rate (η)

Size of each weight correction

Small steps = slow but steady

Epoch

One full pass over all training examples

Checking all 15 students once

Converge

Stop changing because there are no more mistakes

0 errors in an epoch

Linearly separable

Two classes can be split by one straight line

AND, OR - but not XOR

Decision boundary

The line w1·x1 + w2·x2 + b = 0

Line between good and poor farms

Train / test split

Learn on some data, check on unseen data

15 students train, 5 test

3. Background

From guessing to learning

In Practical 1 (Task 5) you guessed the weights w = [1.5, 0.5] and b = -4.5 for the cotton farms and got 70% accuracy. Today the computer will FIND the weights itself. This is the first real machine learning algorithm in the course, invented by Frank Rosenblatt in 1958 (Lecture 2 history).

The perceptron learning rule

For each training example: predict ŷ; compute the error = y − ŷ (it is +1, 0 or -1). Then update w ← w + η·error·x and b ← b + η·error. If the prediction is right, error = 0 and nothing changes. If the neuron said 0 but the answer was 1, the weights move TOWARDS that input; if it said 1 but the answer was 0, they move AWAY. Repeat for many epochs until an epoch has no mistakes.

Real-world link

A bank officer learning which loan applicants repay: each wrong decision makes him adjust how much he trusts salary, past record and guarantees. The perceptron adjusts its weights in the same way, but with numbers.

Limits

A single perceptron can only draw ONE straight line. In 1969 Minsky and Papert showed it cannot learn XOR - one cause of the first AI winter (Lecture 2). Multilayer networks (Practical 3, Lecture 27) solve this.

Data files for this lab

cotton.csv

farm,district,area_acres,rain_mm,fert_bags,yield_maunds

F01,Burewala,5,310,10,160

F02,Burewala,8,280,14,232

F03,Vehari,4,350,6,96

F04,Vehari,6,300,12,186

F05,Mailsi,7,220,9,161

F06,Mailsi,3,240,4,63

F07,Burewala,10,330,20,330

F08,Vehari,5,260,7,120

F09,Mailsi,6,250,11,168

F10,Burewala,4,290,8,124

students.csv

roll,study_hours,attendance,result

S01,2,60,0

S02,8,85,1

S03,5,70,0

S04,10,90,1

S05,3,95,1

S06,6,65,0

S07,7,80,1

S08,1,75,0

S09,4,88,1

S10,9,55,0

S11,6,78,1

S12,2,85,0

S13,5,82,1

S14,3,70,0

S15,7,62,0

S16,8,72,1

S17,4,65,0

S18,6,90,1

S19,2,92,0

S20,5,75,0

4. Time Plan (180 minutes)

Minutes

Activity

Part

0-15

Hand-made AND neuron; data files

Part A1

15-60

Learning rule by hand and in code; Perceptron class; XOR; decision boundary

Part A2-A5

60-75

Predict the output

Part B

75-95

Bug hunt

Part C

95-165

Graded tasks 1-5

Part D

165-180

Post-lab questions, lab book entry, submission

Part E

Part A - Guided Warm-up (type, run, observe)

A1. An AND gate with one neuron (weights chosen by hand)

AND gives 1 only when both inputs are 1. With w = [1, 1] and b = -1.5, the sum z = x1 + x2 - 1.5 is positive only for input (1, 1). Check the truth table.

import numpy as np

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

w = np.array([1.0, 1.0])

b = -1.5

z = X @ w + b

out = np.where(z >= 0, 1, 0)

for xi, zi, oi in zip(X, z, out):

print(f"x={xi} z={zi:+.1f} output={oi}")

Output

x=[0 0] z=-1.5 output=0

x=[0 1] z=-0.5 output=0

x=[1 0] z=-0.5 output=0

x=[1 1] z=+0.5 output=1

A2. The learning rule step by step (AND gate)

Now the perceptron starts with w = [0, 0], b = 0 and learning rate 1, and corrects itself after every mistake. A learning rate of 1 keeps all numbers whole, so you can trace it on paper. Follow the first epoch, then let the loop finish.

import numpy as np

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

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

w, b, lr = np.zeros(2), 0.0, 1.0

for epoch in range(1, 11):

mistakes = 0

for xi, target in zip(X, y):

pred = 1 if xi @ w + b >= 0 else 0

error = target - pred

w = w + lr * error * xi

b = b + lr * error

if epoch == 1:

print(f" x={xi} y={target} pred={pred} error={error:+d} -> w={w.round(2)} b={b:.1f}")

mistakes += int(error != 0)

print(f"epoch {epoch}: mistakes={mistakes} w={w.round(2)} b={b:.1f}")

if mistakes == 0:

break

Output

x=[0 0] y=0 pred=1 error=-1 -> w=[0. 0.] b=-1.0

x=[0 1] y=0 pred=0 error=+0 -> w=[0. 0.] b=-1.0

x=[1 0] y=0 pred=0 error=+0 -> w=[0. 0.] b=-1.0

x=[1 1] y=1 pred=0 error=+1 -> w=[1. 1.] b=0.0

epoch 1: mistakes=2 w=[1. 1.] b=0.0

epoch 2: mistakes=3 w=[2. 1.] b=-1.0

epoch 3: mistakes=3 w=[2. 1.] b=-2.0

epoch 4: mistakes=2 w=[2. 2.] b=-2.0

epoch 5: mistakes=1 w=[2. 1.] b=-3.0

epoch 6: mistakes=0 w=[2. 1.] b=-3.0

A3. A reusable Perceptron class - save this as perceptron.py

Type this class into a new file perceptron.py in your lab folder. The tasks import it with 'from perceptron import Perceptron'. Then run the test below it.

import numpy as np

class Perceptron:

"""Single-layer perceptron with a step activation (Rosenblatt, 1958)."""

def __init__(self, lr=0.1, epochs=20):

self.lr = lr # learning rate: size of each correction

self.epochs = epochs # passes over the whole training data

self.w = None

self.b = 0.0

self.errors = [] # mistakes made in each epoch

def predict(self, X):

z = X @ self.w + self.b

return np.where(z >= 0, 1, 0)

def fit(self, X, y):

self.w = np.zeros(X.shape[1])

self.b = 0.0

self.errors = []

for epoch in range(self.epochs):

mistakes = 0

for xi, target in zip(X, y):

error = target - self.predict(xi) # +1, 0 or -1

self.w += self.lr * error * xi # perceptron learning rule

self.b += self.lr * error

mistakes += int(error != 0)

self.errors.append(mistakes)

if mistakes == 0: # converged: stop early

break

return self

# ---- quick test (run in your notebook, not inside perceptron.py)

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

p = Perceptron(lr=1.0, epochs=20).fit(X, np.array([0, 1, 1, 1])) # OR gate

print("weights:", p.w, "bias:", p.b)

print("mistakes per epoch:", p.errors)

print("predictions:", p.predict(X))

Output

weights: [1. 1.] bias: -1.0

mistakes per epoch: [2, 2, 1, 0]

predictions: [0 1 1 1]

A4. XOR: the problem a single perceptron cannot solve

XOR gives 1 when the inputs are different. No single straight line separates (0,1),(1,0) from (0,0),(1,1), so the mistakes never reach 0, even after 100 epochs.

import numpy as np

from perceptron import Perceptron

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

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

p = Perceptron(lr=1.0, epochs=100).fit(X, y_xor)

print("epochs run:", len(p.errors))

print("mistakes in last 5 epochs:", p.errors[-5:])

print("predictions:", p.predict(X), " true:", y_xor)

print("accuracy:", (p.predict(X) == y_xor).mean())

Output

epochs run: 100

mistakes in last 5 epochs: [4, 4, 4, 4, 4]

predictions: [1 1 0 0] true: [0 1 1 0]

accuracy: 0.5

A5. Seeing the decision boundary

The perceptron's line is w1·x1 + w2·x2 + b = 0, i.e. x2 = -(w1·x1 + b) / w2. Plot it with the AND points.

import numpy as np

import matplotlib.pyplot as plt

from perceptron import Perceptron

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

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

p = Perceptron(lr=1.0, epochs=20).fit(X, y)

xs = np.linspace(-0.5, 1.5, 50)

ys = -(p.w[0] * xs + p.b) / p.w[1]

plt.figure(figsize=(5, 4))

plt.scatter(X[y == 0, 0], X[y == 0, 1], s=120, marker="o", label="output 0")

plt.scatter(X[y == 1, 0], X[y == 1, 1], s=160, marker="*", label="output 1")

plt.plot(xs, ys, "k--", label="decision boundary")

plt.xlim(-0.5, 1.5); plt.ylim(-0.5, 1.5)

plt.xlabel("x1"); plt.ylabel("x2"); plt.title("Perceptron learned AND")

plt.legend(); plt.tight_layout(); plt.savefig("and_boundary.png", dpi=100)

print("w =", p.w.round(2), " b =", round(p.b, 2))

print("Saved and_boundary.png")

Output

w = [2. 1.] b = -3.0

Saved and_boundary.png

Part B - Predict the Output

Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.

B1.

import numpy as np

z = np.array([-0.3, 0.0, 0.25, -2])

print(np.where(z >= 0, 1, 0))

My prediction

B2.

import numpy as np

w, b, lr = np.array([0.2, -0.1]), 0.0, 0.1

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

pred = 1 if x @ w + b >= 0 else 0

error = y - pred

w = w + lr * error * x

b = b + lr * error

print(pred, error, w, b)

My prediction

B3.

import numpy as np

w, b = np.array([0.5, 0.5]), -0.7

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

print(X @ w + b)

print(np.where(X @ w + b >= 0, 1, 0))

My prediction

B4.

import numpy as np

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

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

print((y != pred).sum(), (y == pred).mean())

My prediction

B5.

errors = [3, 2, 2, 1, 0]

print("epochs:", len(errors), "| converged:", errors[-1] == 0)

My prediction

Part C - Bug Hunt

Each program has ONE bug. Some crash with an error message; others run but give a wrong answer (silent logic error - the dangerous kind). Find the bug, explain it and write the fix.

C1.

import numpy as np

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

w = np.array([0.5, 0.5, 0.5])

print(X @ w)

Bug, why it happens, and the fixed line(s)

C2.

import numpy as np

class Perceptron:

def __init__(self):

self.w, self.b = None, 0.0

def predict(self, X):

return np.where(X @ self.w + self.b >= 0, 1, 0)

p = Perceptron()

print(p.predict(np.array([[1, 0]])))

Bug, why it happens, and the fixed line(s)

C3.

import numpy as np

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

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

w, b, lr = np.zeros(2), 0.0, 1.0

for epoch in range(20):

for xi, t in zip(X, y):

pred = 1 if xi @ w + b >= 0 else 0

error = pred - t # error

w = w + lr * error * xi

b = b + lr * error

print("accuracy:", (np.where(X @ w + b >= 0, 1, 0) == y).mean())

It runs and prints

accuracy: 0.25

Bug, why it happens, and the fixed line(s)

C4.

import numpy as np

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

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

w, b, lr = np.zeros(2), 0.0, 1.0

for epoch in range(20):

for xi, t in zip(X, y):

pred = 1 if xi @ w + b >= 0 else 0

w = w + lr * (t - pred) * xi # bias is never updated

print("predictions:", np.where(X @ w + b >= 0, 1, 0), "true:", y)

It runs and prints

predictions: [1 1 1 1] true: [0 0 0 1]

Bug, why it happens, and the fixed line(s)

C5.

import numpy as np

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

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

for epoch in range(20):

w, b, lr = np.zeros(2), 0.0, 1.0 # start values

for xi, t in zip(X, y):

pred = 1 if xi @ w + b >= 0 else 0

w = w + lr * (t - pred) * xi

b = b + lr * (t - pred)

print("w:", w, "b:", b, "predictions:", np.where(X @ w + b >= 0, 1, 0))

It runs and prints

w: [0. 1.] b: 0.0 predictions: [1 1 1 1]

Bug, why it happens, and the fixed line(s)

Part D - Graded Tasks (20 marks)

Task

Level

Title

Marks

T1

Easy

NAND and NOT gates by hand

3

T2

Medium

Train OR and NAND with your Perceptron class

4

T3

Medium

Learning the cotton-farm neuron

4

T4

Medium

Learning curves: OR vs XOR

4

T5

Challenge

Pass / fail predictor with a train-test split

5

Total

20

Task 1 (Easy, 3 marks): NAND and NOT gates by hand

Choose weights and a bias by hand (no training) so that one neuron with a step activation computes (a) NAND of two inputs (output 0 only when both inputs are 1) and (b) NOT of one input. Print both truth tables with z values. Use w = [-1, -1], b = 1.5 for NAND and find your own for NOT.

Sample run (your output must match):

Output

NAND

x=[0 0] z=+1.5 out=1

x=[0 1] z=+0.5 out=1

x=[1 0] z=+0.5 out=1

x=[1 1] z=-0.5 out=0

NOT

x=[0] z=+0.5 out=1

x=[1] z=-0.5 out=0

Your code (or attach the file)

Task 2 (Medium, 4 marks): Train OR and NAND with your Perceptron class

Using the Perceptron class from perceptron.py (lr = 1, epochs = 20), train one perceptron for OR and one for NAND. For each, print the learned weights and bias (2 decimals), the mistakes per epoch, the number of epochs until it converged, and the predictions on all four inputs.

Sample run (your output must match):

Output

OR | w = [1. 1.] b = -1.0

mistakes per epoch: [2, 2, 1, 0]

converged after 4 epochs | predictions: [0 1 1 1]

NAND | w = [-2. -1.] b = 2.0

mistakes per epoch: [1, 3, 3, 2, 1, 0]

converged after 6 epochs | predictions: [1 1 1 0]

Your code (or attach the file)

Task 3 (Medium, 4 marks): Learning the cotton-farm neuron

Load cotton.csv. Build X with x1 = fert_bags / area_acres and x2 = rain_mm / 100, and label y = 1 if yield per acre ≥ 28 maunds (same as Practical 1, Task 5). Train a Perceptron (lr = 0.1, epochs = 100). Print the learned weights and bias, the epochs to converge, the training accuracy, and compare it with the 0.7 accuracy of the hand-picked weights from Practical 1.

Sample run (your output must match):

Output

learned w = [ 0.966 -0.41 ] b = -0.4

mistakes per epoch: [4, 6, 5, 8, 6, 7, 5, 8, 5, 4, 0]

converged after 11 epochs

training accuracy: 1.0 | hand-picked weights (P01): 0.7

Your code (or attach the file)

Task 4 (Medium, 4 marks): Learning curves: OR vs XOR

Train a Perceptron (lr = 1, epochs = 30) on OR and on XOR. Plot the mistakes per epoch of both on one figure (x = epoch, y = mistakes) with title, labels and legend, and save it as task4.png. Print the final number of mistakes for each and explain in one line why XOR never reaches zero.

Sample run (your output must match):

Output

OR : epochs = 4 final mistakes = 0

XOR: epochs = 30 final mistakes = 4

XOR is not linearly separable: no single straight line splits its 0s and 1s.

Your code (or attach the file)

Task 5 (Challenge, 5 marks): Pass / fail predictor with a train-test split

students.csv has study hours per week, attendance (%) and result (1 = pass) for 20 students. Use the first 15 rows for training and the last 5 for testing. (a) Scale both features to 0-1 with min-max scaling using the TRAINING minimum and maximum only. (b) Train a Perceptron (lr = 0.1, epochs = 100). (c) Print the training and test accuracy and the roll numbers of wrongly predicted test students. (d) Predict the result of a new student who studies 6 hours a week with 80% attendance. (e) In two lines, explain why test accuracy is lower than training accuracy.

Sample run (your output must match):

Output

w = [0.167 0.362] b = -0.3 epochs: 7

train accuracy: 1.0

test accuracy : 0.6

wrong test students: ['S16', 'S19']

new student (6 h, 80%): PASS

Your code (or attach the file)

Part E - Post-Lab Questions

E1. Write the perceptron learning rule and explain what happens when the prediction is correct.

Your answer

E2. Why can a single perceptron learn AND and OR but not XOR?

Your answer

E3. What is the role of the bias? What went wrong when the bias was not updated (Part C4)?

Your answer

E4. In Task 5 the training accuracy was 1.0 but test accuracy was lower. What does this teach us?

Your answer

E5. Starting from zero weights, what happens to the learned weights if the learning rate is doubled? (Try it.)

Your answer

Marking Rubric (applied to Part D)

Criterion

Full

Partial

None

Code runs without errors

Runs fully

Small fixable error

Does not run

Correct learning rule and predictions

All correct

Minor mistakes

Incorrect

Uses the Perceptron class / NumPy properly

Clean, reusable

Some repetition

Copy-paste everywhere

Plots and printed results labelled

All present

Some missing

None

Explanations (XOR, test accuracy)

Clear and correct

Partly correct

Missing

Submission

  • Make a folder RollNo_P02 with perceptron.py, task1.py ... task5.py (or one notebook P02.ipynb), cotton.csv, students.csv and task4.png.
  • Zip the folder and upload it to the LMS / Google Classroom by the end of the lab day.
  • Copy Task 3 and Task 5 code and output into your Lab Book (Practical 2 pages) for signature next week.
  • Do NOT use scikit-learn in this lab: the aim is to build the perceptron yourself.

← All CS-511 material