/
analysis2.py
244 lines (200 loc) · 10 KB
/
analysis2.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# source: https://github.com/joshuamorton/Machine-Learning
import numpy as np
from StringIO import StringIO
from pprint import pprint
import argparse
from matplotlib import pyplot as plt
from collections import Counter
from sklearn.decomposition.pca import PCA as PCA
from sklearn.decomposition import FastICA as ICA
from sklearn.random_projection import GaussianRandomProjection as RandomProjection
from sklearn.cluster import KMeans as KM
from sklearn.mixture import GaussianMixture as EM
from sklearn.feature_selection import SelectKBest as best
from sklearn.feature_selection import chi2
from pybrain.datasets.classification import ClassificationDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
# first map things to things
def create_mapper(l):
return {l[n] : n for n in xrange(len(l))}
workclass = create_mapper(["Private", "Self-emp-not-inc", "Self-emp-inc", "Federal-gov", "Local-gov", "State-gov", "Without-pay", "Never-worked"])
education = create_mapper(["Bachelors", "Some-college", "11th", "HS-grad", "Prof-school", "Assoc-acdm", "Assoc-voc", "9th", "7th-8th", "12th", "Masters", "1st-4th", "10th", "Doctorate", "5th-6th", "Preschool"])
marriage = create_mapper(["Married-civ-spouse", "Divorced", "Never-married", "Separated", "Widowed", "Married-spouse-absent", "Married-AF-spouse"])
occupation = create_mapper(["Tech-support", "Craft-repair", "Other-service", "Sales", "Exec-managerial", "Prof-specialty", "Handlers-cleaners", "Machine-op-inspct", "Adm-clerical", "Farming-fishing", "Transport-moving", "Priv-house-serv", "Protective-serv", "Armed-Forces"])
relationship = create_mapper(["Wife", "Own-child", "Husband", "Not-in-family", "Other-relative", "Unmarried"])
race = create_mapper(["White", "Asian-Pac-Islander", "Amer-Indian-Eskimo", "Other", "Black"])
sex = create_mapper(["Female", "Male"])
country = create_mapper(["United-States", "Cambodia", "England", "Puerto-Rico", "Canada", "Germany", "Outlying-US(Guam-USVI-etc)", "India", "Japan", "Greece", "South", "China", "Cuba", "Iran", "Honduras", "Philippines", "Italy", "Poland", "Jamaica", "Vietnam", "Mexico", "Portugal", "Ireland", "France", "Dominican-Republic", "Laos", "Ecuador", "Taiwan", "Haiti", "Columbia", "Hungary", "Guatemala", "Nicaragua", "Scotland", "Thailand", "Yugoslavia", "El-Salvador", "Trinadad&Tobago", "Peru", "Hong", "Holand-Netherlands"])
income = create_mapper(["<=50K", ">50K"])
adultDataSetConverters = {
1: lambda x: workclass[x],
3: lambda x: education[x],
5: lambda x: marriage[x],
6: lambda x: occupation[x],
7: lambda x: relationship[x],
8: lambda x: race[x],
9: lambda x: sex[x],
13: lambda x: country[x],
14: lambda x: income[x]
}
spamDataSetConverters = {}
converters = {"adult": adultDataSetConverters, "spam": spamDataSetConverters}
def load(filename, converter):
with open(filename) as data:
instances = [line for line in data if "?" not in line] # remove lines with unknown data
return np.loadtxt(instances,
delimiter=',',
converters=converter,
dtype='u4',
skiprows=1)
def create_dataset(name, test, train):
training_set = load(train, converters[name])
testing_set = load(test, converters[name])
train_x, train_y = np.hsplit(training_set, [training_set[0].size-1])
test_x, test_y = np.hsplit(testing_set, [testing_set[0].size-1])
# this splits the dataset on the last instance, so your label must
# be the last instance in the dataset
return train_x, train_y, test_x, test_y
def plot(axes, values, x_label, y_label, title, name):
plt.clf()
plt.plot(*values)
plt.axis(axes)
plt.title(title)
plt.ylabel(y_label)
plt.xlabel(x_label)
plt.savefig(name+".png", dpi=500)
# plt.show()
plt.clf()
def pca(tx, ty, rx, ry):
compressor = PCA(n_components = tx[1].size/2)
compressor.fit(tx, y=ty)
newtx = compressor.transform(tx)
newrx = compressor.transform(rx)
em(newtx, ty, newrx, ry, add="wPCAtr", times=10)
km(newtx, ty, newrx, ry, add="wPCAtr", times=10)
nn(newtx, ty, newrx, ry, add="wPCAtr")
def ica(tx, ty, rx, ry):
compressor = ICA(whiten=False) # for some people, whiten needs to be off
compressor.fit(tx, y=ty)
newtx = compressor.transform(tx)
newrx = compressor.transform(rx)
em(newtx, ty, newrx, ry, add="wICAtr", times=10)
km(newtx, ty, newrx, ry, add="wICAtr", times=10)
nn(newtx, ty, newrx, ry, add="wICAtr")
def randproj(tx, ty, rx, ry):
compressor = RandomProjection(tx[1].size)
compressor.fit(tx, y=ty)
newtx = compressor.transform(tx)
# compressor = RandomProjection(tx[1].size)
newrx = compressor.transform(rx)
em(newtx, ty, newrx, ry, add="wRPtr", times=10)
km(newtx, ty, newrx, ry, add="wRPtr", times=10)
nn(newtx, ty, newrx, ry, add="wRPtr")
def kbest(tx, ty, rx, ry):
compressor = best(chi2)
compressor.fit(tx, y=ty)
newtx = compressor.transform(tx)
newrx = compressor.transform(rx)
em(newtx, ty, newrx, ry, add="wKBtr", times=10)
km(newtx, ty, newrx, ry, add="wKBtr", times=10)
nn(newtx, ty, newrx, ry, add="wKBtr")
def em(tx, ty, rx, ry, add="", times=5):
errs = []
# this is what we will compare to
checker = EM(n_components=2)
checker.fit(ry)
truth = checker.predict(ry)
# so we do this a bunch of times
for i in range(2,times):
clusters = {x:[] for x in range(i)}
# create a clusterer
clf = EM(n_components=i)
clf.fit(tx) #fit it to our data
test = clf.predict(tx)
result = clf.predict(rx) # and test it on the testing set
# here we make the arguably awful assumption that for a given cluster,
# all values in tha cluster "should" in a perfect world, belong in one
# class or the other, meaning that say, cluster "3" should really be
# all 0s in our truth, or all 1s there
#
# So clusters is a dict of lists, where each list contains all items
# in a single cluster
for index, val in enumerate(result):
clusters[val].append(index)
# then we take each cluster, find the sum of that clusters counterparts
# in our "truth" and round that to find out if that cluster should be
# a 1 or a 0
mapper = {x: round(sum(truth[v] for v in clusters[x])/float(len(clusters[x]))) if clusters[x] else 0 for x in range(i)}
# the processed list holds the results of this, so if cluster 3 was
# found to be of value 1,
# for each value in clusters[3], processed[value] == 1 would hold
processed = [mapper[val] for val in result]
errs.append(sum((processed-truth)**2) / float(len(ry)))
plot([0, times, min(errs)-.1, max(errs)+.1],[range(2, times), errs, "ro"], "Number of Clusters", "Error Rate", "Expectation Maximization Error", "EM"+add)
# dank magic, wrap an array cuz reasons
td = np.reshape(test, (test.size, 1))
rd = np.reshape(result, (result.size, 1))
newtx = np.append(tx, td, 1)
newrx = np.append(rx, rd, 1)
nn(newtx, ty, newrx, ry, add="onEM"+add)
def km(tx, ty, rx, ry, add="", times=5):
#this does the exact same thing as the above
errs = []
checker = KM(n_clusters=2)
checker.fit(ry)
truth = checker.predict(ry)
# so we do this a bunch of times
for i in range(2,times):
clusters = {x:[] for x in range(i)}
clf = KM(n_clusters=i)
clf.fit(tx) #fit it to our data
test = clf.predict(tx)
result = clf.predict(rx) # and test it on the testing set
for index, val in enumerate(result):
clusters[val].append(index)
mapper = {x: round(sum(truth[v] for v in clusters[x])/float(len(clusters[x]))) if clusters[x] else 0 for x in range(i)}
processed = [mapper[val] for val in result]
errs.append(sum((processed-truth)**2) / float(len(ry)))
plot([0, times, min(errs)-.1, max(errs)+.1],[range(2, times), errs, "ro"], "Number of Clusters", "Error Rate", "KMeans clustering error", "KM"+add)
td = np.reshape(test, (test.size, 1))
rd = np.reshape(result, (result.size, 1))
newtx = np.append(tx, td, 1)
newrx = np.append(rx, rd, 1)
nn(newtx, ty, newrx, ry, add="onKM"+add)
def nn(tx, ty, rx, ry, add="", iterations=250):
"""
trains and plots a neural network on the data we have
"""
resultst = []
resultsr = []
positions = range(iterations)
network = buildNetwork(tx[1].size, 5, 1, bias=True)
ds = ClassificationDataSet(tx[1].size, 1)
for i in xrange(len(tx)):
ds.addSample(tx[i], [ty[i]])
trainer = BackpropTrainer(network, ds, learningrate=0.01)
train = zip(tx, ty)
test = zip(rx, ry)
for i in positions:
trainer.train()
resultst.append(sum(np.array([(round(network.activate(t_x)) - t_y)**2 for t_x, t_y in train])/float(len(train))))
resultsr.append(sum(np.array([(round(network.activate(t_x)) - t_y)**2 for t_x, t_y in test])/float(len(test))))
# resultsr.append(sum((np.array([round(network.activate(test)) for test in rx]) - ry)**2)/float(len(ry)))
print i, resultst[-1], resultsr[-1]
plot([0, iterations, 0, 1], (positions, resultst, "ro", positions, resultsr, "bo"), "Network Epoch", "Percent Error", "Neural Network Error", "NN"+add)
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Run clustering algorithms on stuff')
parser.add_argument("name")
args = parser.parse_args()
name = args.name
train = name+".data"
test = name+".test"
train_x, train_y, test_x, test_y = create_dataset(name, test, train)
# nn(train_x, train_y, test_x, test_y); print 'nn done'
# em(train_x, train_y, test_x, test_y, times = 10); print 'em done'
# km(train_x, train_y, test_x, test_y, times = 10); print 'km done'
# pca(train_x, train_y, test_x, test_y); print 'pca done'
ica(train_x, train_y, test_x, test_y); print 'ica done'
# randproj(train_x, train_y, test_x, test_y); print 'randproj done'
# kbest(train_x, train_y, test_x, test_y); print 'kbest done'