-
Notifications
You must be signed in to change notification settings - Fork 8
/
DecisionTreeShapeExample.scala
109 lines (81 loc) · 2.86 KB
/
DecisionTreeShapeExample.scala
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
// Databricks notebook source
val data = Seq(
(9,"BabyChair"),
(10,"BabyChair"),
(11,"BabyChair"),
(12,"BabyChair"),
(16,"chair"),
(17,"chair"),
(18,"chair"),
(19,"chair"),
(18,"chair"),
(29,"table"),
(30,"table"),
(31,"table"),
(29,"table"),
(30,"table"),
(31,"table")).toDF("height","shape")
// COMMAND ----------
// Index labels, adding metadata to the label column.
// Fit on whole dataset to include all labels in index.
import org.apache.spark.ml.feature._
val labelIndexer = new StringIndexer()
.setInputCol("shape")
.setOutputCol("indexedLabel")
.fit(data)
// COMMAND ----------
//Continous Features
val continousFeatures = Seq("height")
// COMMAND ----------
val featureAssembler = new VectorAssembler()
.setInputCols(continousFeatures.toArray)
.setOutputCol("features")
// COMMAND ----------
// Split the data into training and test sets (30% held out for testing).
val Array(trainingData, testData) = data.randomSplit(Array(0.8, 0.2))
// COMMAND ----------
// Train a DecisionTree model.
import org.apache.spark.ml.classification.{DecisionTreeClassificationModel, DecisionTreeClassifier}
val dt = new DecisionTreeClassifier()
.setLabelCol("indexedLabel")
.setFeaturesCol("features")
// COMMAND ----------
// Convert indexed labels back to original labels.
val labelConverter = new IndexToString()
.setInputCol("prediction")
.setOutputCol("predictedLabel")
.setLabels(labelIndexer.labels)
// COMMAND ----------
// Chain indexers and tree in a Pipeline.
import org.apache.spark.ml.{Pipeline, PipelineModel}
val pipeline = new Pipeline()
.setStages(Array(labelIndexer,featureAssembler, dt, labelConverter))
// COMMAND ----------
// Train model. This also runs the indexers.
val model: PipelineModel = pipeline.fit(trainingData)
// COMMAND ----------
// Make predictions.
val predictions = model.transform(testData)
// Select example rows to display.
predictions.select("predictedLabel", "indexedLabel", "features").show(5)
// COMMAND ----------
// Select (prediction, true label) and compute test error.
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
val evaluator = new MulticlassClassificationEvaluator()
.setLabelCol("indexedLabel")
.setPredictionCol("prediction")
.setMetricName("accuracy")
val accuracy = evaluator.evaluate(predictions)
println(s"Test Error = ${(1.0 - accuracy)}")
val treeModel = model.stages(2).asInstanceOf[DecisionTreeClassificationModel]
println(s"Learned classification tree model:\n ${treeModel.toDebugString}")
// COMMAND ----------
import org.apache.spark.sql.types.DoubleType
val analysisDataDF = spark.range(0 , 40).toDF("height")
.withColumn("height" , 'height.cast(DoubleType))
// COMMAND ----------
val opDf = model.transform(analysisDataDF)
display(opDf)
// COMMAND ----------
val tree = model.stages(2).asInstanceOf[DecisionTreeClassificationModel]
display(tree)