Skip to content

Instantly share code, notes, and snippets.

@arikaa
Last active July 17, 2018 02:50
Show Gist options
  • Save arikaa/e2d40caefc977cab0cfa093d0ea58287 to your computer and use it in GitHub Desktop.
Save arikaa/e2d40caefc977cab0cfa093d0ea58287 to your computer and use it in GitHub Desktop.
Perceptron classifier function
import numpy as np
def perceptron(data, label, iteration, y):
'''
The perceptron classifier
Arguments
data: This is the training data with a certain shape (x, y), meaning there are x samples and each sample has y features.
label: This is the training data's label with shape (x, 1). The 1 corresponds to the correct classification of the data.
iteration: This is the number of times the function will iterate.
Returns
w: This is the separator with the shape (1, y).
'''
w = np.zeros((1,y))
for i in range(iteration):
row = data[i] # getting the row from the data
row_tr = np.transpose(row) # transposing the row for using the dot product
if (np.sign(np.dot(w, row_tr)) != label[i]): # check if the sign is negative, not if update the separator
w = w + np.dot(row, label[i])
return w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment