summaryrefslogtreecommitdiffstats
path: root/predPy/predictor.py
blob: 8e8f26ede79e242d42676401b5e6239b759e9db1 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/python3

# Copyright (c) 2016 Huawei
# All Rights Reserved.
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements.  See the NOTICE file distributed with
#   this work for additional information regarding copyright ownership.
#   The ASF licenses this file to You under the Apache License, Version 2.0
#   (the "License"); you may not use this file except in compliance with
#   the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

"""Summary of models here.

SVM
Logistic Regression with SGD
Logistic Regression with LBFGS
Multinomial Naive Bayes
Decision Tree
Random Forest
Gradient Boosted Trees
"""

from __future__ import print_function
from pyspark import SparkContext
import csv
import StringIO
# import tempfile
# from shutil import rmtree
from pyspark.mllib.linalg import Vectors
from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.classification import SVMWithSGD
# from pyspark.mllib.classification import SVMModel
from pyspark.mllib.classification import LogisticRegressionWithSGD
from pyspark.mllib.classification import LogisticRegressionWithLBFGS
# from pyspark.mllib.classification import LogisticRegressionModel
from pyspark.mllib.classification import NaiveBayes
# from pyspark.mllib.classification import NaiveBayesModel
from pyspark.mllib.tree import DecisionTree
# from pyspark.mllib.tree import DecisionTreeModel
from pyspark.mllib.tree import RandomForest
# from pyspark.mllib.tree import RandomForestModel
from pyspark.mllib.tree import GradientBoostedTrees
# from pyspark.mllib.tree import GradientBoostedTreesModel


def loadRecord(line):
    """Load a CSV line and select 26 indicative parameters"""
    inputLine = StringIO.StringIO(line)
    reader = csv.reader(inputLine)
    parameters = reader.next()
    # Instances that were collected within seven days before the failures
    # are used to train the failing model
    # if float(parameters[-1]) == 1 and float(parameters[3]) >= 360:
    #    parameters[-1] = 0
    selectedParameters = (
        parameters[12:17] + parameters[19:20] + parameters[23:26] +
        parameters[39:47] + parameters[54:61] + parameters[62:]
    )
    # selectedParameters = parameters
    return selectedParameters


def parseLine(line):
    """Parse a row """
    label = float(line[-1])
    features = Vectors.dense(map(float, line[:-1]))
    return LabeledPoint(label, features)


if __name__ == "__main__":

    sc = SparkContext(appName="HardDriveFailurePrediction")

    # $example on$
    data = (sc.textFile('hdd/harddrive1.csv').map(loadRecord).
            map(parseLine))

    print("===== Choose SVM model =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a SVM model
    model = SVMWithSGD.train(trainingData, iterations=200, regParam=7e-2,
                             intercept=True)

    # Make prediction and test accuracy.
#    labelsAndPredictions = (testData
#        .map(lambda p: (p.label, model.predict(p.features))))
#    accuracy = (labelsAndPredictions
#        .filter(lambda (x, v): x == v).count() / float(testData.count()))
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of SVM model is: %.4f" % accuracy)
    print("The test recall of SVM model is: %.4f" % recall)
    print("The test fprate of SVM model is: %.4f\n\n" % fprate)

    print("===== Choose Logistic Regression model with SGD algorithm =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a logistic regression model
    model = LogisticRegressionWithSGD.train(trainingData, iterations=200,
                                            regParam=8e-2, intercept=True)

    # Make prediction and test accuracy.
    print("The original threshold: %0.2f" % float(model.threshold))
    model.setThreshold(0.40)
    print("The current threshold: %0.2f" % float(model.threshold))
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of Logistic Regression model with"
          " SGD algorithm is: %.4f" % accuracy)
    print("The test recall of Logistic Regression model with"
          " SGD algorithm is: %.4f" % recall)
    print("The test fprate of Logistic Regression model with"
          " SGD algorithm is: %.4f\n\n" % fprate)

    print("===== Choose Logistic Regression model with LBFGS algorithm =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a logistic regression model
    model = LogisticRegressionWithLBFGS.train(trainingData, iterations=200,
                                              regParam=7e-2, intercept=True)

    # Make prediction and test accuracy.
    print("The original threshold: %0.2f" % float(model.threshold))
    model.setThreshold(0.45)
    print("The current threshold: %0.2f" % float(model.threshold))
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of Logistic Regression model with"
          " LBFGS algorithm is: %.4f" % accuracy)
    print("The test recall of Logistic Regression model with"
          " LBFGS algorithm is: %.4f" % recall)
    print("The test fprate of Logistic Regression model with"
          " LBFGS algorithm is: %.4f\n\n" % fprate)

    print("===== Choose Multinomial Naive Bayes model =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a multinomial naive Bayes model given an RDD of LabeledPoint.
    model = NaiveBayes.train(trainingData, 7e-1)

    # Make prediction and test accuracy.
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of Multinomial Naive Bayes "
          "is: %.4f" % accuracy)
    print("The test recall of Multinomial Naive Bayes "
          "is: %.4f" % recall)
    print("The test fprate of Multinomial Naive Bayes "
          "is: %.4f\n\n" % fprate)

    print("===== Choose Decision Tree  model =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a decision tree model.
    # Empty categoricalFeaturesInfo indicates all features are continuous.
    model = DecisionTree.trainClassifier(trainingData, numClasses=2,
                                         categoricalFeaturesInfo={},
                                         impurity='entropy', maxDepth=4,
                                         maxBins=32)
    # print('Learned classification tree model:')
    # print(model.toDebugString())

    # Make prediction and test accuracy.
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of decision tree model is: %.4f" % accuracy)
    print("The test recall of decision tree model is: %.4f" % recall)
    print("The test fprate of decision tree model is: %.4f\n\n" % fprate)

    print("===== Choose Random Forest model =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a Random Forest model.
    # Empty categoricalFeaturesInfo indicates all features are continuous.
    # Note: Use larger numTrees in practice.
    # Setting featureSubsetStrategy="auto" lets the algorithm choose.
    model = RandomForest.trainClassifier(trainingData, numClasses=2,
                                         categoricalFeaturesInfo={},
                                         numTrees=15,
                                         featureSubsetStrategy="auto",
                                         impurity='gini', maxDepth=12,
                                         maxBins=32)
    # print('Learned classification tree model:')
    # print(model.toDebugString())

    # Make prediction and test accuracy.
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of random forest model is: %.4f" % accuracy)
    print("The test recall of random forest model is: %.4f" % recall)
    print("The test fprate of random forest model is: %.4f\n\n" % fprate)

    print("===== Choose Gradient Boosted Trees model =====")
    # Split data aproximately into training (80%) and test (20%)
    trainingData, testData = data.randomSplit([0.8, 0.2], seed=0)

    # Train a GradientBoostedTrees model.
    # Empty categoricalFeaturesInfo indicates all features are continuous.
    model = GradientBoostedTrees.trainClassifier(trainingData,
                                                 categoricalFeaturesInfo={},
                                                 numIterations=20, maxDepth=8,
                                                 maxBins=32)
    # print('Learned classification tree model:')
    # print(model.toDebugString())

    # Make prediction and test accuracy.
    predictions = model.predict(testData.map(lambda x: x.features))
    labelsAndPredictions = testData.map(lambda p: p.label).zip(predictions)
    tp = labelsAndPredictions.filter(lambda (v, p): v == p and p == 1).count()
    tn = labelsAndPredictions.filter(lambda (v, p): v == p and p == 0).count()
    fp = labelsAndPredictions.filter(lambda (v, p): v != p and p == 1).count()
    fn = labelsAndPredictions.filter(lambda (v, p): v != p and p == 0).count()
    accuracy = (labelsAndPredictions.filter(lambda (v, p): v == p).
                count() / float(testData.count()))
    print("true positive number: %d, false positive number: %d" % (tp, fp))
    print("false negative number: %d, true negative number: %d" % (fn, tn))
    recall = tp / float(tp + fn)
    fprate = fp / float(fp + tn)
    print("The test accuracy of Gradient Boosted Trees "
          "model is: %.4f" % accuracy)
    print("The test recall of Gradient Boosted Trees "
          "model is: %.4f" % recall)
    print("The test fprate of Gradient Boosted Trees "
          "model is: %.4f" % fprate)


# Save and load model
#    path = tempfile.mkdtemp(dir='.')
#    model.save(sc, path)
#    sameModel = SVMModel.load(sc, path)
#    sameModel = LogisticRegressionModel.load(sc, path)
#    sameModel = NaiveBayesModel.load(sc, path)
#    sameModel = DecisionTreeModel.load(sc, path)
#    sameModel = RandomForestModel.load(sc, path)
#    sameModel = GradientBoostedTreesModel.load(sc, path)
#    try:
#        rmtree(path)
#    except OSError:
#        pass