-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensemble_cifar.py
275 lines (201 loc) · 8.33 KB
/
ensemble_cifar.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Execution: ./bin/spark-submit --master local[8] --driver-memory 12g --executor-memory 12g ~/Desktop/spark_lib/ensemble_cifar.py > ~/Desktop/spark_lib/output/cifar.op
from __future__ import print_function
import os
import shutil
from pyspark import SparkContext
from pyspark.mllib.classification import LogisticRegressionWithLBFGS
from pyspark.mllib.classification import SVMWithSGD
from pyspark.mllib.classification import NaiveBayes
from pyspark.mllib.tree import RandomForest
from pyspark.mllib.tree import DecisionTree
from pyspark.mllib.regression import LabeledPoint
from numpy import array
# DEBUG environment
DEBUG = 0
# Parameters
train_file = "/home/ajit/Desktop/spark_lib/dataset/cifar/cifar.data.train"
test_file = "/home/ajit/Desktop/spark_lib/dataset/cifar/cifar.data.test"
ensemble_train_file = "/home/ajit/Desktop/spark_lib/intermediate_output/cifar.ens.train"
ensemble_test_file = "/home/ajit/Desktop/spark_lib/intermediate_output/cifar.ens.test"
# Configure the environment
if 'SPARK_HOME' not in os.environ:
os.environ['SPARK_HOME'] = '$SPARK_HOME'
# Load and parse data
def parsePoint(line):
values = [float(x) for x in line.split(' ')]
return LabeledPoint(values[0], values[1:])
def appendColumn(ensemble_data, label_data):
for i in range(0,len(ensemble_data),1):
ensemble_data[i].extend([label_data[i]])
def writeToFile(en_data, filename):
f = open(filename,'w+')
f.writelines(' '.join(str(j) for j in i) + '\n' for i in en_data)
f.close()
sc = SparkContext(appName="Ensemble_Classifier_CIFAR_Dataset")
data = sc.textFile(train_file)
train_data = data.map(parsePoint)
data = sc.textFile(test_file)
test_data = data.map(parsePoint)
# Debug data
#if DEBUG==1:
#print(train_data.take(train_data.count()))
# Extract Labels
test_labels_rdd = test_data.map(lambda p: p.label)
test_labels = test_labels_rdd.collect()
ensemble_test = []
for i in range(0, len(test_labels),1):
l1 = [test_labels[i]]
ensemble_test.append(l1)
train_labels_rdd = train_data.map(lambda p: p.label)
train_labels = train_labels_rdd.collect()
ensemble_train = []
for i in range(0, len(train_labels),1):
l1 = [train_labels[i]]
ensemble_train.append(l1)
# C1
# Build the Model
model = LogisticRegressionWithLBFGS.train(train_data, intercept=True, validateData=True, numClasses=10)
# Predict Labels
c1_predict_labels_test_rdd = test_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_train_rdd = train_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_test = c1_predict_labels_test_rdd.collect()
c1_predict_labels_train = c1_predict_labels_train_rdd.collect()
# Append Labels
appendColumn(ensemble_test, c1_predict_labels_test)
appendColumn(ensemble_train, c1_predict_labels_train)
# C2
# Build the Model
model = LogisticRegressionWithLBFGS.train(train_data, regParam=0.1, intercept=True, validateData=True, numClasses=10)
# Predict Labels
c1_predict_labels_test_rdd = test_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_train_rdd = train_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_test = c1_predict_labels_test_rdd.collect()
c1_predict_labels_train = c1_predict_labels_train_rdd.collect()
# Append Labels
appendColumn(ensemble_test, c1_predict_labels_test)
appendColumn(ensemble_train, c1_predict_labels_train)
# C3
# Build the Model
model = LogisticRegressionWithLBFGS.train(train_data, regParam=0.001, intercept=True, validateData=True, numClasses=10)
# Predict Labels
c1_predict_labels_test_rdd = test_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_train_rdd = train_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_test = c1_predict_labels_test_rdd.collect()
c1_predict_labels_train = c1_predict_labels_train_rdd.collect()
# Append Labels
appendColumn(ensemble_test, c1_predict_labels_test)
appendColumn(ensemble_train, c1_predict_labels_train)
# C4
# Build the Model
model = LogisticRegressionWithLBFGS.train(train_data, regParam=1, regType='l1', intercept=True, validateData=True, numClasses=10)
# Predict Labels
c1_predict_labels_test_rdd = test_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_train_rdd = train_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_test = c1_predict_labels_test_rdd.collect()
c1_predict_labels_train = c1_predict_labels_train_rdd.collect()
# Append Labels
appendColumn(ensemble_test, c1_predict_labels_test)
appendColumn(ensemble_train, c1_predict_labels_train)
# C5
# Build the Model
model = LogisticRegressionWithLBFGS.train(train_data, regParam=0.1, regType='l1', intercept=True, validateData=True, numClasses=10)
# Predict Labels
c1_predict_labels_test_rdd = test_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_train_rdd = train_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_test = c1_predict_labels_test_rdd.collect()
c1_predict_labels_train = c1_predict_labels_train_rdd.collect()
# Append Labels
appendColumn(ensemble_test, c1_predict_labels_test)
appendColumn(ensemble_train, c1_predict_labels_train)
# C6
# Build the Model
model = LogisticRegressionWithLBFGS.train(train_data, regParam=10, regType='none', intercept=True, validateData=True, numClasses=10)
# Predict Labels
c1_predict_labels_test_rdd = test_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_train_rdd = train_data.map(lambda p: ( model.predict(p.features)))
c1_predict_labels_test = c1_predict_labels_test_rdd.collect()
c1_predict_labels_train = c1_predict_labels_train_rdd.collect()
# Append Labels
appendColumn(ensemble_test, c1_predict_labels_test)
appendColumn(ensemble_train, c1_predict_labels_train)
# Feature Data for Random Forest and Boosted Trees (List of Lists)
# Read trainfile
f = open(train_file)
train_features = []
l = f.readline()
while(l!=''):
a = l.split()
l2 = []
# Note: Started with index 1 to avoid the label
for j in range(1,len(a),1):
l2.extend([float(a[j])])
train_features.append(l2)
l = f.readline()
f.close()
# Read testfile
f = open(test_file)
test_features = []
l = f.readline()
while(l!=''):
a = l.split()
l2 = []
for j in range(1,len(a),1):
l2.extend([float(a[j])])
test_features.append(l2)
l = f.readline()
f.close()
# Random Forest
# C13 - C21
# Build the Model
rf_trees = [25, 50, 100, 150, 200, 250, 300, 400]
for i in range(0, len(rf_trees), 1):
num_rf_trees = rf_trees[i]
# Build the Model
model = RandomForest.trainClassifier(train_data, 10, {}, num_rf_trees)
rf_train_predict_label = []
rf_test_predict_label = []
# Predict Labels
for j in range(0, len(test_features), 1):
p_l = model.predict(test_features[j])
rf_test_predict_label.extend([p_l])
for j in range(0, len(train_features), 1):
p_l = model.predict(train_features[j])
rf_train_predict_label.extend([p_l])
# Append Labels
appendColumn(ensemble_test, rf_test_predict_label)
appendColumn(ensemble_train, rf_train_predict_label)
# Decision Trees
# C13 - C21
# Build the Model
max_depth = [5, 10, 15, 20]
for i in range(0, len(max_depth), 1):
m_depth = max_depth[i]
# Build the Model
model = DecisionTree.trainClassifier(train_data, 10, {}, impurity='gini', maxDepth=m_depth)
rf_train_predict_label = []
rf_test_predict_label = []
# Predict Labels
for j in range(0, len(test_features), 1):
p_l = model.predict(test_features[j])
rf_test_predict_label.extend([p_l])
for j in range(0, len(train_features), 1):
p_l = model.predict(train_features[j])
rf_train_predict_label.extend([p_l])
# Append Labels
appendColumn(ensemble_test, rf_test_predict_label)
appendColumn(ensemble_train, rf_train_predict_label)
#----------
# Save Intermediate Ensemble Output
writeToFile(ensemble_train, ensemble_train_file)
writeToFile(ensemble_test, ensemble_test_file)
# Run Logistic Regression on intermediate ensemble data
data = sc.textFile(ensemble_train_file)
ens_train_data = data.map(parsePoint)
data = sc.textFile(ensemble_test_file)
ens_test_data = data.map(parsePoint)
# Build the Model & Predic labels for test data
model = LogisticRegressionWithLBFGS.train(ens_train_data, regParam=0.01, intercept=True, validateData=True, numClasses=10)
labelsAndPreds = ens_test_data.map(lambda p: (p.label, model.predict(p.features)))
testErr = labelsAndPreds.filter(lambda (v,p): v!=p).count()/float(ens_test_data.count())
print("Ensemble Classifier Error: " + str(testErr))
sc.stop()