-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron.py
167 lines (139 loc) · 4.79 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import collections
import numpy.matlib
import numpy as np
f1 = open("spam_train.txt", "r")
contents =f1.readlines()
training = []
validating = []
isSpam = []
for i in range(len(contents)):
if i < 4000:
training.append(contents[i])
else:
validating.append(contents[i])
def createDictionaries(): # returns all email dictionaries and vocab dictionary
allDict = list() # list of dictionaries for each email
vocab = collections.Counter() # total dictionary for all words
for email in training:
emailDict = dict()
split = email.split()
for i in range(len(split)):
if i == 0:
if split[i] == "0": # not spam
isSpam.append(-1)
else:
isSpam.append(1)
continue
if split[i] not in emailDict: #if we have not already seen it before
vocab[split[i]] += 1
emailDict[split[i]] = 1
else:
emailDict[split[i]] += 1
allDict.append(emailDict)
return allDict, vocab
def getNotIgnoredVocab():
notIgnoredVocab = set()
vocab = createDictionaries()[1]
for word in vocab:
if vocab[word] >= 30:
notIgnoredVocab.add(word)
return notIgnoredVocab
#build vocabulary list
def buildVocabularyVectors():
#ignore all words that appear in fewer than 30 emails
allDict = createDictionaries()[0]
notIgnoredVocab = getNotIgnoredVocab()
#make the dictionaries for each email
featureVectors = []
for email in allDict:
emailVector = []
for word in notIgnoredVocab:
if word in email:
emailVector.append(1)
else:
emailVector.append(0)
# print(emailVector)
featureVectors.append(np.array(emailVector))
return notIgnoredVocab, np.array(featureVectors)
# def emailVector(notIgnoredSet):
# make dictionary of each word in notIgnoredSet and then check if its in the individual email
#for each
def perceptron_train():
notIgnoredVocab, featureVectors = buildVocabularyVectors()
w = np.zeros(len(notIgnoredVocab))
print(len(featureVectors))
# keep updating until the weight does not need to be modified
numMistakes = 0
numUpdates = 0
numIterations = 0
while (True):
numIterations += 1
numUpdates = 0
for i in range(len(featureVectors)):
dot = np.dot(w, featureVectors[i])
if dot == 0:
dot = 1
# check for sign agreement
sign = isSpam[i] * dot
if sign <= 0:
numMistakes += 1
numUpdates += 1
#modify weight accordingly
w = np.add(w, np.multiply(isSpam[i],featureVectors[i]))
if numUpdates == 0:
break
# return the number of mistakes, the number of iterations and the weight array
return w, numMistakes, numIterations
def perceptron_test(w, data):
print("hiiii")
dataIsSpam = [] # saves the spam values for the data given as parameter
dataDict = list()
for email in data:
emailDict = dict()
split = email.split()
for i in range(len(split)): #iterating over each word
if i == 0:
if split[i] == "0": # not spam
dataIsSpam.append(-1)
else:
dataIsSpam.append(1)
continue
if split[i] not in emailDict: #if we have not already seen it before
emailDict[split[i]] = 1
else:
emailDict[split[i]] += 1
dataDict.append(emailDict)
notIgnoredVocab = buildVocabularyVectors()[0]
featureVectors = []
print("wop")
for email in dataDict:
emailVector = []
for word in notIgnoredVocab:
if word in email:
emailVector.append(1)
else:
emailVector.append(0)
# print(emailVector)
featureVectors.append(np.array(emailVector))
numMistakes = 0
numIterations = 0
print("oh hi")
for i in range(len(featureVectors)):
dot = np.dot(w, featureVectors[i])
if dot == 0:
dot = 1
# check for sign agreement
sign = isSpam[i] * dot
if sign <= 0:
numMistakes += 1
numIterations += 1
print(numMistakes)
print(numIterations)
return numMistakes/numIterations
#now i have the feature vectors, the notignoredvocab, and I need w
w, numMistakes, numIterations = perceptron_train()
print(w, numMistakes, numIterations)
print(perceptron_test(w, training))
print(perceptron_test(w, validating))
#contents is 5000 emails long
#split training (4000) and validating data(1000)