-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.py
More file actions
140 lines (109 loc) · 4.77 KB
/
perceptron.py
File metadata and controls
140 lines (109 loc) · 4.77 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import numpy as np
class MultiClassPerceptron(object):
def __init__(self,num_class,feature_dim):
"""Initialize a multi class perceptron model.
This function will initialize a feature_dim weight vector,
for each class.
The LAST index of feature_dim is assumed to be the bias term,
self.w[:,0] = [w1,w2,w3...,BIAS]
where wi corresponds to each feature dimension,
0 corresponds to class 0.
Args:
num_class(int): number of classes to classify
feature_dim(int): feature dimension for each example
"""
self.w = np.zeros((feature_dim+1,num_class))
def train(self,train_set,train_label):
""" Train perceptron model (self.w) with training dataset.
Args:
train_set(numpy.ndarray): training examples with a dimension of (# of examples, feature_dim)
train_label(numpy.ndarray): training labels with a dimension of (# of examples, )
"""
# YOUR CODE HERE
#print(train_set.shape)
#print(train_label.shape)
#print(self.w.shape)
lrate = 1/self.w.shape[0]
num_iter = 10000
self.__train(train_set, train_label, lrate, num_iter)
pass
def __train(self, train_set, train_label, lrate=0.1, num_iter=1000):
# array of 1's shape is (number of examples, 1)
# add this to training set, so that w and train_set have correct dims
# for matrix operations
padding = np.ones((train_set.shape[0], 1))
train_set = np.concatenate((padding, train_set), 1)
# print(train_label[0:10])
number_of_examples = train_set.shape[0]
number_of_features = train_set.shape[1]
number_of_classes = self.w.shape[1]
#print(train_set.shape)
#print(train_label.shape)
dot_product = np.zeros(number_of_classes)
train_label = np.reshape(train_label, (train_label.shape[0], 1))
#print(train_label.shape)
#shuffle_array = np.concatenate((train_set, train_label), 1)
#print(shuffle_array.shape)
for epochs in range(1,num_iter+1):
train_label = np.reshape(train_label, (train_label.shape[0], 1))
shuffle_array = np.concatenate((train_set, train_label), 1)
np.random.shuffle(shuffle_array)
train_set = shuffle_array[:,:train_set.shape[1]]
train_label = shuffle_array[:,train_set.shape[1]:]
train_label = train_label.astype(int)
train_label = np.reshape(train_label, train_label.shape[0])
#print(train_set.shape)
#print(train_label.shape)
for example in range(number_of_examples):
#print(example)
for classes in range(number_of_classes):
dot_product[classes] = np.dot(train_set[example], self.w[:,classes])
predicted_label_index = np.argmax(dot_product)
if predicted_label_index != train_label[example]:
#self.w[:,predicted_label_index] -= (1/((epochs*number_of_examples)+(example+1)))*train_set[example,:]
#self.w[:,train_label[example]] += (1/((epochs*number_of_examples)+(example+1)))*train_set[example,:]
#self.w[:,predicted_label_index] -= train_set[example,:]
#self.w[:,train_label[example]] += train_set[example,:]
#print(self.w.shape)
#print(train_set.shape)
#print(train_label.shape)
self.w[:,predicted_label_index] -= (1/((epochs*number_of_examples)+(example+1)))*train_set[example,:]
self.w[:,train_label[example]] += (1/((epochs*number_of_examples)+(example+1)))*train_set[example,:]
def test(self,test_set,test_label):
""" Test the trained perceptron model (self.w) using testing dataset.
The accuracy is computed as the average of correctness
by comparing between predicted label and true label.
Args:
test_set(numpy.ndarray): testing examples with a dimension of (# of examples, feature_dim)
test_label(numpy.ndarray): testing labels with a dimension of (# of examples, )
Returns:
accuracy(float): average accuracy value
pred_label(numpy.ndarray): predicted labels with a dimension of (# of examples, )
"""
#print(test_set.shape)
number_of_examples = test_set.shape[0]
number_of_classes = self.w.shape[1]
dot_product = np.zeros(number_of_classes)
padding = np.ones((test_set.shape[0], 1))
test_set = np.concatenate((padding, test_set), 1)
# YOUR CODE HERE
accuracy = 0
pred_label = np.zeros((len(test_set)))
for example in range(number_of_examples):
for classes in range(number_of_classes):
dot_product[classes] = np.dot(test_set[example], self.w[:,classes])
predicted_label_index = np.argmax(dot_product)
pred_label[example] = predicted_label_index
correct_prediction = 0
for example in range(number_of_examples):
if pred_label[example] == test_label[example]:
correct_prediction += 1
accuracy = correct_prediction / number_of_examples
print(accuracy)
return accuracy, pred_label
def guess(self,test_set):
for classes in range(number_of_classes):
dot_product[classes] = np.dot(test_set, self.w[:,classes])
predicted_label_index = np.argmax(dot_product)
print(dot_product)
return predicted_label_index