-
Notifications
You must be signed in to change notification settings - Fork 7
/
rf_transform_model.py
50 lines (33 loc) · 1.54 KB
/
rf_transform_model.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
from pyspark.sql import SparkSession, SQLContext
from pyspark.ml.classification import RandomForestClassifier, RandomForestClassificationModel
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
import sys
def main():
spark = SparkSession \
.builder \
.appName("RandomForest") \
.config("spark.executor.heartbeatInterval","60s")\
.getOrCreate()
sc = spark.sparkContext
sqlContext = SQLContext(sc)
sc.setLogLevel("INFO")
# Loading the test data
df_test= spark.read.parquet(sys.argv[1])
df_test, df_discard = df_test.randomSplit([0.2, 0.8])
# Load the model
rf_model=RandomForestClassificationModel.load(sys.argv[2])
# Make the predictions
predictions = rf_model.transform(df_test)
#predictionsRDD=predictions.rdd
#predictionsRDD.saveAsTextFile(sys.argv[3]+"output.text")
evaluator_acc = MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label", metricName="accuracy")
accuracy = evaluator_acc.evaluate(predictions)
print "accuracy *******************"
print accuracy
evaluator_pre = MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label", metricName="weightedPrecision")
print "precision *******************"
print evaluator_pre.evaluate(predictions)
print "recall **********************"
print MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="label", metricName="weightedRecall").evaluate(predictions)
if __name__ == '__main__':
main()