-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq1_part_B_C.py
191 lines (152 loc) · 7.42 KB
/
q1_part_B_C.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
def decisionTreeClassifier(trainingData, testData, ncolumns, schemaNames):
from pyspark.ml import Pipeline
from pyspark.ml.classification import DecisionTreeClassifier
from pyspark.ml.tuning import ParamGridBuilder
from pyspark.ml.feature import StringIndexer, VectorIndexer
from pyspark.ml.tuning import CrossValidator
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
import numpy as np
from pyspark.ml.evaluation import BinaryClassificationEvaluator
import time
dt = DecisionTreeClassifier(labelCol="label", featuresCol="features", maxDepth=15, maxBins=15, impurity='entropy')
timer = ''
start = time.time()
cvModelDT = dt.fit(trainingData)
end = time.time()
timer = ((end - start)/60)
prediction = cvModelDT.transform(testData)
# Select (prediction, true label) and compute test error
evaluator = MulticlassClassificationEvaluator(
labelCol="label", predictionCol="prediction", metricName="accuracy")
accuracy = evaluator.evaluate(prediction)
# Evaluate model
evaluator = BinaryClassificationEvaluator(rawPredictionCol="prediction")
areaUC = evaluator.evaluate(prediction)
fi = cvModelDT.featureImportances
imp_feat = np.zeros(ncolumns-1)
imp_feat[fi.indices] = fi.values
x = np.arange(ncolumns-1)
idx = (-imp_feat).argsort()[:3]
feat = []
for i in idx:
feat.append(schemaNames[i])
return feat, accuracy, areaUC, timer
def LogisticRegression(trainingData, testData, schemaNames):
from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression
from pyspark.ml.tuning import ParamGridBuilder
from pyspark.ml.feature import StringIndexer, VectorIndexer
from pyspark.ml.tuning import CrossValidator
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
import numpy as np
import time
lr = LogisticRegression(featuresCol='features', labelCol='label', regParam=0.1, maxIter=7)
timer = ''
start = time.time()
cvModel = lr.fit(trainingData)
end = time.time()
timer = ((end - start)/60)
prediction = cvModel.transform(testData)
evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
accuracy = evaluator.evaluate(prediction)
evaluator = BinaryClassificationEvaluator(rawPredictionCol="prediction")
areaUC = evaluator.evaluate(prediction)
w_r = cvModel.coefficients
w_r = w_r.tolist()
feat = []
for i in (w_r)[-3:][::-1]:
feat.append(schemaNames[(w_r.index(i))])
return feat, accuracy, areaUC, timer
def decisionTreeRegressor(data, ncolumns, schemaNames):
from pyspark.ml import Pipeline
from pyspark.ml.regression import DecisionTreeRegressor
from pyspark.ml.tuning import ParamGridBuilder
from pyspark.ml.feature import StringIndexer, VectorIndexer
from pyspark.ml.tuning import CrossValidator
from pyspark.ml.evaluation import RegressionEvaluator
from pyspark.ml.feature import Binarizer
from pyspark.ml.evaluation import BinaryClassificationEvaluator
import numpy as np
import time
binarizer = Binarizer(threshold=0.00001, inputCol="features", outputCol="binarized_features", )
binarizedDataFrame = binarizer.transform(data)
(trainingData, testData) = binarizedDataFrame.randomSplit([0.9, 0.1], 50)
dtr = DecisionTreeRegressor(labelCol="label", featuresCol="binarized_features", maxDepth=10, maxBins=10, impurity='Variance')
timer = ''
start = time.time()
cvModel = dtr.fit(trainingData)
end = time.time()
timer = ((end - start)/60)
prediction = cvModel.transform(testData)
evaluator = RegressionEvaluator\
(labelCol="label", predictionCol="prediction", metricName="rmse")
rmse = evaluator.evaluate(prediction)
evaluator = BinaryClassificationEvaluator(rawPredictionCol="prediction")
areaUC = evaluator.evaluate(prediction)
fi = cvModel.featureImportances
imp_feat = np.zeros(ncolumns-1)
imp_feat[fi.indices] = fi.values
x = np.arange(ncolumns-1)
idx = (-imp_feat).argsort()[:3]
feat = []
for i in idx:
feat.append(schemaNames[i])
return feat, rmse, areaUC, timer
def main():
import time
import pyspark
from pyspark.sql import SparkSession
import numpy as np
import functools
spark = SparkSession.builder.master("local[20]").appName("COM6012 Decision Trees Regression").getOrCreate()
sc = spark.sparkContext
rawdata = spark.read.csv('/home/acp18sak/.conda/envs/jupyter-spark/Assignment2/Data/HIGGS.csv.gz')
# rawdata.cache()
sc.setLogLevel("WARN")
newNames = ('label','lepton pT', 'lepton eta', 'lepton phi', 'missing energy magnitude', 'missing energy phi', 'jet 1 pt', 'jet 1 eta', 'jet 1 phi', 'jet 1 b-tag', 'jet 2 pt', 'jet 2 eta', 'jet 2 phi', 'jet 2 b-tag', 'jet 3 pt', 'jet 3 eta', 'jet 3 phi', 'jet 3 b-tag', 'jet 4 pt', 'jet 4 eta', 'jet 4 phi', 'jet 4 b-tag', 'm_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb')
oldColumns = rawdata.schema.names
df = functools.reduce(lambda rawdata, idx: rawdata.withColumnRenamed(oldColumns[idx], newNames[idx]), range(len(oldColumns)), rawdata)
schemaNames = df.schema.names
ncolumns = len(df.columns)
from pyspark.sql.types import DoubleType
for i in range(ncolumns):
df = df.withColumn(schemaNames[i], df[schemaNames[i]].cast(DoubleType()))
from pyspark.ml.feature import VectorAssembler
assembler = VectorAssembler(inputCols = schemaNames[1:ncolumns], outputCol = 'features')
raw_plus_vector = assembler.transform(df)
data = raw_plus_vector.select('features','label')
(trainingData, testData) = data.randomSplit([0.9, 0.1], 50)
feat, accuracy, areaUC, training_time = decisionTreeClassifier(trainingData, testData, ncolumns, schemaNames)
print('\n\n\n ----------------------------------------------------------------------------')
print('\t-------------- Results for DecisionTreeClassifier --------------\n')
print("Training Time in minutes: ", training_time)
print("\nAccuracy for DecisionTreeClassifier = %g " % (accuracy))
print("AreaUndertheCurve for DecisionTreeClassifier = %g " % areaUC)
print('\n Top Three Features for Decision Tree Classifier\n')
for i in range(len(feat)):
print(i+1, ' ->' ,feat[i])
print('\n ----------------------------------------------------------------------------\n\n')
feat, accuracy, areaUC, training_time = LogisticRegression(trainingData, testData, schemaNames)
print('\n\n\n ----------------------------------------------------------------------------')
print('\t-------------- Results for LogisticRegression --------------\n')
print("Training Time in minutes: ", training_time)
print("\nAccuracy for LogisticRegression = %g " % (accuracy))
print("AreaUndertheCurve for LogisticRegression = %g " % areaUC)
print('\n Top Three Features for LogisticRegression\n')
for i in range(len(feat)):
print(i+1, ' ->' ,feat[i])
print('\n ----------------------------------------------------------------------------\n\n')
feat, rmse, areaUC, training_time = decisionTreeRegressor(data, ncolumns, schemaNames)
print('\n\n\n ----------------------------------------------------------------------------')
print('\t-------------- Results for DecisionTreeRegressor --------------\n')
print("Training Time in minutes: ", training_time)
print("\nRMSE for DecisionTreeRegressor = %g " % (rmse))
print("AreaUndertheCurve for DecisionTreeRegressor = %g " % areaUC)
print('\n Top Three Features for Decision Tree Regressor\n')
for i in range(len(feat)):
print(i+1, ' ->' ,feat[i])
print('\n ----------------------------------------------------------------------------\n\n')
spark.close()
if __name__ == '__main__':
main()