forked from eriklindernoren/ML-From-Scratch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
perceptron.py
executable file
·100 lines (77 loc) · 2.91 KB
/
perceptron.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import sys
import os
import math
#from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
# Import helper functions
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, dir_path + "/../utils")
from data_manipulation import train_test_split, categorical_to_binary, normalize, load_iris_dataset
from data_operation import accuracy_score
sys.path.insert(0, dir_path + "/../unsupervised_learning/")
from principal_component_analysis import PCA
# Activation function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Gradient of activation function
def sigmoid_gradient(x):
return sigmoid(x) * (1 - sigmoid(x))
class Perceptron():
def __init__(self):
self.W = None # Output layer weights
self.biasW = None # Bias weights
def fit(self, X, y, n_iterations=40000,
learning_rate=0.01, plot_errors=False):
y = categorical_to_binary(y)
n_samples, n_features = np.shape(X)
n_outputs = np.shape(y)[1]
# Initial weights between [-1/sqrt(N), 1/sqrt(N)]
a = -1 / math.sqrt(n_features)
b = -a
self.W = (b - a) * np.random.random((n_features, n_outputs)) + a
self.biasW = (b - a) * np.random.random((1, n_outputs)) + a
errors = []
for i in range(n_iterations):
# Calculate outputs
neuron_input = np.dot(X, self.W) + self.biasW
neuron_output = sigmoid(neuron_input)
# Training error
error = y - neuron_output
mse = np.mean(np.power(error, 2))
errors.append(mse)
# Calculate the loss gradient
w_gradient = -2 * (y - neuron_output) * \
sigmoid_gradient(neuron_input)
bias_gradient = w_gradient
# Update weights
self.W -= learning_rate * X.T.dot(w_gradient)
self.biasW -= learning_rate * \
np.ones((1, n_samples)).dot(bias_gradient)
# Plot the training error
if plot_errors:
plt.plot(range(n_iterations), errors)
plt.ylabel('Training Error')
plt.xlabel('Iterations')
plt.show()
# Use the trained model to predict labels of X
def predict(self, X):
# Set the class labels to the highest valued outputs
y_pred = np.argmax(sigmoid(np.dot(X, self.W) + self.biasW), axis=1)
return y_pred
def main():
data=load_iris_dataset(dir_path + r"/../data/iris.csv")
X=data['X']
y=data['target']
X = normalize(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
# Perceptron
clf = Perceptron()
clf.fit(X_train, y_train, plot_errors=True)
y_pred = clf.predict(X_test)
print "Accuracy:", accuracy_score(y_test, y_pred)
# Reduce dimension to two using PCA and plot the results
pca = PCA()
pca.plot_in_2d(X_test, y_pred)
if __name__ == "__main__":
main()