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

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

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

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

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

Evaluating a Model (We got 70% Accuracy)

Scala
By Bhavesh