-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf.py
53 lines (39 loc) · 2.03 KB
/
pf.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
# Model training and testing
from __future__ import print_function
import sys
from pyspark import SparkContext
from pyspark.mllib.tree import RandomForest, RandomForestModel
from pyspark.mllib.util import MLUtils
def predict(sc, data):
#sc = SparkContext(appName="PythonRandomForestClassificationExample")
# $example on$
# Load and parse the data file into an RDD of LabeledPoint.
#data = MLUtils.loadLibSVMFile(sc, 'deathproject/test1.txt')
#data = [1, 0, 5, 1, 1, 65, 1, 2, 0, 1, 0, 6, 3450]
# Split the data into training and test sets (30% held out for testing)
# (trainingData, testData) = data.randomSplit([0.7, 0.3])
# Train a RandomForest model.
# Empty categoricalFeaturesInfo indicates all features are continuous.
# Note: Use larger numTrees in practice.
# Setting featureSubsetStrategy="auto" lets the algorithm choose.
print('Starting...')
#model = RandomForest.trainClassifier(trainingData, numClasses=8, categoricalFeaturesInfo={},
# numTrees=5, featureSubsetStrategy="auto",
# impurity='gini', maxDepth=4, maxBins=32)
sameModel = RandomForestModel.load(sc, "target/tmp/myRandomForestClassificationModel2")
print('Predicting...')
# Evaluate model on test instances and compute test error
#predictions = sameModel.predict(data.map(lambda x: x.features))
predictions = sameModel.predict(data)
#labelsAndPredictions = data.map(lambda lp: lp.label).zip(predictions)
#testErr = labelsAndPredictions.filter(lambda (v, p): v != p).count() / float(testData.count())
#print('Test Error = ' + str(testErr))
#print('Learned classification forest model:')
#print(sameModel.toDebugString())
print(predictions)
return int(predictions)
#print(labelsAndPredictions)
# Save and load model
# model.save(sc, "target/tmp/myRandomForestClassificationModel")
# sameModel = RandomForestModel.load(sc, "target/tmp/myRandomForestClassificationModel")
# $example off$