Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views7 pages

Tutorial 1 PYTHON AI

This tutorial outlines the steps to build a Perceptron from scratch, covering key concepts such as weight and bias updates, activation functions, and data loading. It provides a structured approach to creating a Perceptron class, training the model, and making predictions using the Iris dataset. Additionally, it includes tasks for further experimentation, such as modifying the activation function and adjusting output labels.

Uploaded by

Muhammad Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Tutorial 1 PYTHON AI

This tutorial outlines the steps to build a Perceptron from scratch, covering key concepts such as weight and bias updates, activation functions, and data loading. It provides a structured approach to creating a Perceptron class, training the model, and making predictions using the Iris dataset. Additionally, it includes tasks for further experimentation, such as modifying the activation function and adjusting output labels.

Uploaded by

Muhammad Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial 1:

Building a Perceptron

Objectives:
• Building Perceptron from Scratch
• How Weights and Bias get Updated
• Activation Function
• Data Loading
• Training & Testing

Lets build a Perceptron?

Step 1 — Perceptron Class


Follow these simple steps:

• Create a file called “Perceptron.py”

• Define a class called Perceptron inside


To build a perceptron, we need 3 attributes:

• η (eta): the learning rate is usually a small value between 0.0 and 1.0 which
defines how quickly the model learns.

• n_iter: the number of iterations.

• w_: the weights. A weight defines the importance of the corresponding


input value

Step 2 — Weighted sum

np.dot(X, w_) is simply:

Step 3 — Step function

The step function predict () returns:

• 1 — if the weighted sum is greater than 0.0

• -1 — Otherwise.
Step 4— Create the training method
An explanation will follow.

We initialize the weights to 0, then start our training.

For each input:

1. we calculate ŷ, the predicted output.

2. then we update the weights from W1 to Wn (remember Wo is the bias).

We repeat this process n_iter times (n_iter is the number of iterations).

Weights update in the perceptron


How are the weights updated?

Every weight Wi is updated by Δ(Wi).

Wi = Wi + Δ(Wi)

Where:

Δ(Wi) = η (y — ŷ) * Xi

Don’t panic, it’s easier than it seems:

• Wi is i-th weight.

• Δ(Wi) is the update. It determines if the weight increases or decreases.

• η is the learning rate.

• y is the actual value.

• ŷ is the predicted value.

• Xi is the i-th input.

Example

Get your pen, we are going to do some math.

Random data to calculate weights update

Δ(Wi) = η (y — ŷ) * Xi

When y is equal to ŷ, there is no update:

• Δ(W1) = 0.01 * ( 1 − 1 ) ∗ 0.5 = 0

• Δ(W2) = 0.01 * ( (−1) − (−1) ) ∗ 1 = 0

When y is different than ŷ, it means that there’s an error and the weights must be
updated by Δ(Wi):

• Δ(W3) = 0.01 * ( 1 − (−1) ) ∗ 4 = 0.08


• Δ(W4) = 0.01 * ( (−1) − 1) ∗ 2 = −0.04

Whew!

We made it through to the end, let’s test our Perceptron.

Step 5 : Loading Data:


The data is loaded from https://archive.ics.uci.edu/ml/machine-learning-
databases/iris/iris.data it represents the Iris dataset. It is a famous dataset used
for classification tasks. It contains 150 samples of iris flowers, each described by
four features (sepal length, sepal width, petal length, and petal width) and a
target label that indicates the species of the flower (either Iris-setosa, Iris-
versicolor, or Iris-virginica).

Output

Separating the data (X) from the labels (y)


Splitting the data into train and test set

Output: ( The output might Appear different on your screens )

Step 6: Training the perceptron:


Step 7 : Making predictions on test data

Step 8: Measuring Performances

Tasks:
1. Take manual input for the four features (sepal length, sepal width,
petal length, and petal width).
▪ Use the model to predict whether it's Iris-setosa or not based
on the manually provided values.
▪ Display the prediction result.

2. Replace the Step Function with Sigmoid.


3. Replace the labels of 1,-1 with 1 & 0.

You might also like