Machine Learning Project – Predict Forest Cover Part 2

Define the Pipeline​

A predictive model often requires multiple stages of feature preparation.

A pipeline consists of a series of transformer and estimator stages that typically prepare a DataFrame for modeling and then train a predictive model.

Split the Data

It is common practice when building machine learning models to split the source data, using some of it to train the model and reserving some to test the trained model. In this project, you will use 70% of the data for training, and reserve 30% for testing.

%%scala

val splits = ForestDF.randomSplit(Array(0.7, 0.3))
val train = splits(0)
val test = splits(1)
val train_rows = train.count()
val test_rows = test.count()
println("Training Rows: " + train_rows + " Testing Rows: " + test_rows)

Prepare the Training Data

To train the Classification model, you need a training data set that includes a vector of numeric features, and a label column. In this project, you will use the VectorAssembler class to transform the feature columns into a vector, and then Cover_Type column to the label.

VectorAssembler()

VectorAssembler(): is a transformer that combines a given list of columns into a single vector column. It is useful for combining raw features and features generated by different feature transformers into a single feature vector, in order to train ML models like decision trees.

VectorAssembler accepts the following input column types: all numeric types, boolean type, and vector type.

In each row, the values of the input columns will be concatenated into a vector in the specified order.

%%scala

import org.apache.spark.ml.feature.VectorAssembler

val assembler = new VectorAssembler().setInputCols(Array("Elevation", "Aspect", "Slope", "Horizontal_Distance_To_Hydrology", "Vertical_Distance_To_Hydrology", "Horizontal_Distance_To_Roadways", "Hillshade_9am", "Hillshade_Noon", "Hillshade_3pm", "Horizontal_Distance_To_Fire_Points", "Wilderness_Area_0", "Wilderness_Area_1", "Wilderness_Area_2", "Wilderness_Area_3", "Soil_Type_0", "Soil_Type_1", "Soil_Type_2", "Soil_Type_3", "Soil_Type_4", "Soil_Type_5", "Soil_Type_6", "Soil_Type_7", "Soil_Type_8", "Soil_Type_9", "Soil_Type_10", "Soil_Type_11", "Soil_Type_12", "Soil_Type_13", "Soil_Type_14", "Soil_Type_15", "Soil_Type_16", "Soil_Type_17", "Soil_Type_18", "Soil_Type_19", "Soil_Type_20", "Soil_Type_21", "Soil_Type_22", "Soil_Type_23", "Soil_Type_24", "Soil_Type_25", "Soil_Type_26", "Soil_Type_27", "Soil_Type_28", "Soil_Type_29", "Soil_Type_30", "Soil_Type_31", "Soil_Type_32", "Soil_Type_33", "Soil_Type_34", "Soil_Type_35", "Soil_Type_36", "Soil_Type_37", "Soil_Type_38", "Soil_Type_39")).setOutputCol("features")

val training = assembler.transform(train).select($"features", $"Cover_Type".alias("label"))

training.show(false)

Output:

+-----------------------------------------------------------------------------------------------------+-----+
|features                                                                                             |label|
+-----------------------------------------------------------------------------------------------------+-----+
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1879.0,28.0,19.0,30.0,12.0,95.0,209.0,196.0,117.0,778.0,1.0,1.0])   |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,15],[1888.0,33.0,22.0,150.0,46.0,108.0,209.0,185.0,103.0,735.0,1.0,1.0]) |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1889.0,28.0,22.0,150.0,23.0,120.0,205.0,185.0,108.0,759.0,1.0,1.0]) |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1889.0,353.0,30.0,95.0,39.0,67.0,153.0,172.0,146.0,600.0,1.0,1.0])  |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1896.0,337.0,12.0,30.0,6.0,175.0,195.0,224.0,168.0,732.0,1.0,1.0])  |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1901.0,311.0,9.0,30.0,2.0,190.0,195.0,234.0,179.0,726.0,1.0,1.0])   |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1903.0,5.0,13.0,42.0,4.0,201.0,203.0,214.0,148.0,708.0,1.0,1.0])    |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1904.0,51.0,26.0,67.0,30.0,162.0,222.0,175.0,72.0,711.0,1.0,1.0])   |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1905.0,19.0,27.0,134.0,58.0,120.0,188.0,171.0,108.0,636.0,1.0,1.0]) |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1905.0,33.0,27.0,90.0,46.0,150.0,204.0,171.0,89.0,725.0,1.0,1.0])   |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,16],[1905.0,77.0,21.0,90.0,38.0,120.0,241.0,196.0,75.0,1025.0,1.0,1.0])  |3.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,15],[1916.0,24.0,25.0,212.0,74.0,175.0,197.0,177.0,105.0,789.0,1.0,1.0]) |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1916.0,320.0,24.0,190.0,60.0,162.0,151.0,210.0,195.0,832.0,1.0,1.0])|6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,23],[1918.0,321.0,28.0,42.0,17.0,85.0,139.0,201.0,196.0,402.0,1.0,1.0])  |3.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1919.0,44.0,26.0,162.0,68.0,150.0,216.0,173.0,77.0,706.0,1.0,1.0])  |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1922.0,50.0,29.0,108.0,43.0,218.0,219.0,165.0,62.0,659.0,1.0,1.0])  |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1927.0,4.0,36.0,120.0,78.0,95.0,149.0,147.0,116.0,666.0,1.0,1.0])   |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1927.0,54.0,25.0,190.0,76.0,175.0,225.0,177.0,71.0,735.0,1.0,1.0])  |6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,18],[1927.0,333.0,27.0,218.0,72.0,190.0,149.0,195.0,180.0,859.0,1.0,1.0])|6.0  |
|(54,[0,1,2,3,4,5,6,7,8,9,13,14],[1928.0,252.0,31.0,30.0,14.0,180.0,138.0,243.0,233.0,765.0,1.0,1.0]) |6.0  |
+-----------------------------------------------------------------------------------------------------+-----+
only showing top 20 rows

Train a Classification Model

Next, you need to train a Classification model using the training data. To do this, create an instance of the Decision Tree algorithm you want to use and use its fit method to train a model based on the training DataFrame. In this project, you will use a Decision Tree Classifier algorithm – though you can use the same technique for any of the regression algorithms supported in the spark.ml API

%%scala

import org.apache.spark.ml.classification.DecisionTreeClassificationModel
import org.apache.spark.ml.classification.DecisionTreeClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator

val dt = new DecisionTreeClassifier().setLabelCol("label").setFeaturesCol("features")

val model = dt.fit(training)

println("Model Trained!")

Prepare the Testing Data

Now that you have a trained model, you can test it using the testing data you reserved previously. First, you need to prepare the testing data in the same way as you did the training data by transforming the feature columns into a vector. This time you’ll rename the Cover_Type column to trueLabel.

%%scala

val testing = assembler.transform(test).select($"features", $"Cover_Type".alias("trueLabel"))
testing.show()

Output:

+--------------------+---------+
|            features|trueLabel|
+--------------------+---------+
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,5,6,7,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      3.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      3.0|
|(54,[0,1,2,3,4,5,...|      3.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,5,6,7,...|      6.0|
|(54,[0,1,2,5,6,7,...|      3.0|
|(54,[0,1,2,3,4,5,...|      3.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
|(54,[0,1,2,3,4,5,...|      6.0|
+--------------------+---------+
only showing top 20 rows

Test the Model

Now you’re ready to use the transform method of the model to generate some predictions. You can use this approach to predict the Cover_Type; but in this case, you are using the test data which includes a known true label value, so you can compare the Cover_Type

%%scala

val prediction = model.transform(testing)
val predicted = prediction.select("features", "prediction", "trueLabel")
display(predicted)

Evaluating a Model (We got 70% Accuracy)

%%scala

val evaluator = new MulticlassClassificationEvaluator()
  .setLabelCol("trueLabel")
  .setPredictionCol("prediction")
  .setMetricName("accuracy")
val accuracy = evaluator.evaluate(prediction)

Output:

evaluator: org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator = MulticlassClassificationEvaluator: uid=mcEval_357a96124b80, metricName=accuracy, metricLabel=0.0, beta=1.0, eps=1.0E-15
accuracy: Double = 0.7000927229643875
By Bhavesh