-
Notifications
You must be signed in to change notification settings - Fork 7
/
gb_clasiifier.py
59 lines (43 loc) · 2.14 KB
/
gb_clasiifier.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
from pyspark.sql import SparkSession, SQLContext
from pyspark.ml.classification import RandomForestClassifier, RandomForestClassificationModel
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.classification import GBTClassifier
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_train = df_test.randomSplit([0.3, 0.7])
df_train_indexed=df_train.selectExpr("label as indexedLabel","features as indexedFeatures")
df_test_indexed=df_test.selectExpr("label as indexedLabel","features as indexedFeatures")
# # Load the model
# rf_model = RandomForestClassificationModel.load(sys.argv[2])
#
# # Make the predictions
# predictions = rf_model.transform(df_test)
gbt = GBTClassifier(labelCol="indexedLabel", featuresCol="indexedFeatures", maxIter=100,maxBins=24000000)
model=gbt.fit(df_train_indexed)
predictions = model.transform(df_test_indexed)
# predictionsRDD=predictions.rdd
# predictionsRDD.saveAsTextFile(sys.argv[3]+"output.text")
evaluator_acc = MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="indexedLabel",
metricName="accuracy")
accuracy = evaluator_acc.evaluate(predictions)
print "accuracy *******************"
print accuracy
evaluator_pre = MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="indexedLabel",
metricName="weightedPrecision")
print "precision *******************"
print evaluator_pre.evaluate(predictions)
print "recall **********************"
print MulticlassClassificationEvaluator(predictionCol="prediction", labelCol="indexedLabel",
metricName="weightedRecall").evaluate(predictions)
if __name__ == '__main__':
main()