HW3
Implement the perceptron training rule described at pg. 88 (and shown below for your convenience) into a function called perceptron
.
The function receives the training data as a matrix and returns the weights of the perceptron. The last column of the data matrix is the target (or class label). Below I am including a possible skeleton of your entire program. You may do try-outs in a notebook, but the upload should be a perceptron.py
Python file.
# S. Visa (inlcude your own name as comments at the top of your program)
import sklearn
import numpy as np
# this data set has 4 examples as 4 rows; in each row, last element is the class label. For instance, -1 in example [0, 0, -1] is the class label.
data = np.array([[0, 0, -1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
def perceptron(data):
rows, cols = data.shape
# ...
# here comes student code
# while total_error > 0.02 and step < 100:
#...
return w
w = perceptron(data)
print("The perceptron weights are:", w)
Start with randomly generated weights. To check your work, I am offering that for the data set above, with starting weights w = [0.25, -0.1, 0.5], and eta = 0.1, your program should print:
The perceptron weights are: [-0.15 0.3 0.5 ]
Optionally, your function can print the steps as illustrated bellow.
___________________________________
step = 1 err=2.0
w = [0.25, -0.1, 0.5]
___________________________________
step = 2 err=4.0
w = [0.05 -0.1 0.5 ]
___________________________________
step = 3 err=2.0
w = [0.05 0.1 0.5 ]
___________________________________
step = 4 err=2.0
w = [-0.15 0.1 0.5 ]
___________________________________
step = 5 err=2.0
w = [0.05 0.3 0.5 ]
___________________________________
step = 6 err=0.0
w = [-0.15 0.3 0.5 ]
___________________________________
The perceptron weights are: [-0.15 0.3 0.5 ]
I will test your percetron with several sets. Here is another training set (3-attribute data set, last column is the target or class label) for you to try out.
1 | 2 | 3 | 1 |
0 | 1 | -2 | -1 |
4 | 8 | 2 | 1 |
-3 | 1 | -5 | -1 |
2 | 5 | 6 | 1 |
Hints:
- The following code flattens a 2D into a 1D vector.
aVect = np.array([[0, 0, -1]]) # this is a 2D array [[0, 0, -1]] aVect = aVect.flatten() # this is a 1D array [0, 0, -1]
- This creates a column of 5 “ones.”
shape = (5, 1) # shape of the column vector, five rows and one column col_of_ones = np.ones(shape, dtype = int)