Prediction task is to determine whether a person makes over 50K a year Part 2

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 = IncomeFinalDF.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 rename the income_indexedcolumn 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 logistic regression and 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("age", "workclass_indexed", "fnlwgt", "education_indexed", "education-num", "marital-status_indexed", "occupation_indexed", "relationship_indexed", "race_indexed", "sex_indexed", "capital-gain", "capital-loss", "hours-per-week", "native-country_indexed")).setOutputCol("features")

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

training.show(false)

Output:

+-----------------------------------------------------------------+-----+
|features                                                         |label|
+-----------------------------------------------------------------+-----+
|[17.0,3.0,34019.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,20.0,0.0]  |0.0  |
|[17.0,3.0,41643.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,15.0,0.0]  |0.0  |
|[17.0,3.0,48703.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,30.0,0.0]  |0.0  |
|[17.0,3.0,48751.0,5.0,7.0,1.0,7.0,2.0,1.0,1.0,0.0,0.0,40.0,0.0]  |0.0  |
|[17.0,3.0,64785.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,30.0,0.0]  |0.0  |
|[17.0,3.0,86786.0,7.0,6.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,40.0,0.0]  |0.0  |
|[17.0,3.0,89870.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0]  |0.0  |
|[17.0,3.0,94366.0,7.0,6.0,1.0,7.0,5.0,0.0,0.0,0.0,0.0,6.0,0.0]   |0.0  |
|[17.0,3.0,110998.0,1.0,10.0,1.0,7.0,2.0,2.0,1.0,0.0,0.0,40.0,3.0]|0.0  |
|[17.0,3.0,112942.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0] |0.0  |
|[17.0,3.0,114798.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,18.0,0.0] |0.0  |
|[17.0,3.0,138507.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,20.0,0.0] |0.0  |
|[17.0,3.0,139183.0,7.0,6.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,15.0,0.0] |0.0  |
|[17.0,3.0,144114.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0] |0.0  |
|[17.0,3.0,145258.0,5.0,7.0,1.0,7.0,5.0,0.0,1.0,0.0,0.0,25.0,0.0] |0.0  |
|[17.0,3.0,145886.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,30.0,0.0] |0.0  |
|[17.0,3.0,148769.0,0.0,9.0,1.0,7.0,2.0,1.0,0.0,0.0,0.0,40.0,0.0] |0.0  |
|[17.0,3.0,151141.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,30.0,0.0] |0.0  |
|[17.0,3.0,161259.0,7.0,6.0,1.0,7.0,5.0,0.0,0.0,0.0,0.0,12.0,0.0] |0.0  |
|[17.0,3.0,166759.0,11.0,8.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,40.0,0.0]|0.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 LogisticRegression 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 Logistic Regression 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.LogisticRegression

val lr = new LogisticRegression().setLabelCol("label").setFeaturesCol("features").setMaxIter(10).setRegParam(0.3)

val model = lr.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 income_indexed column to trueLabel.

%scala

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

testing.show(false)

Output:

+-----------------------------------------------------------------+---------+
|features                                                         |trueLabel|
+-----------------------------------------------------------------+---------+
|[17.0,3.0,34088.0,11.0,8.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,25.0,0.0] |0.0      |
|[17.0,3.0,47407.0,5.0,7.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,10.0,0.0]  |0.0      |
|[17.0,3.0,67808.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0]  |0.0      |
|[17.0,3.0,80077.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,20.0,0.0]  |0.0      |
|[17.0,3.0,103810.0,11.0,8.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0]|0.0      |
|[17.0,3.0,104025.0,5.0,7.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,18.0,0.0] |0.0      |
|[17.0,3.0,127003.0,10.0,5.0,1.0,7.0,2.0,1.0,0.0,0.0,0.0,40.0,0.0]|0.0      |
|[17.0,3.0,158762.0,7.0,6.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,20.0,0.0] |0.0      |
|[17.0,3.0,161981.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0] |0.0      |
|[17.0,3.0,171461.0,7.0,6.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,20.0,0.0] |0.0      |
|[17.0,3.0,172145.0,7.0,6.0,1.0,7.0,2.0,1.0,1.0,0.0,0.0,40.0,0.0] |0.0      |
|[17.0,3.0,181337.0,7.0,6.0,1.0,7.0,2.0,4.0,1.0,0.0,0.0,20.0,0.0] |0.0      |
|[17.0,3.0,187539.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,10.0,0.0] |0.0      |
|[17.0,3.0,215743.0,5.0,7.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0] |0.0      |
|[17.0,3.0,241021.0,11.0,8.0,1.0,7.0,2.0,4.0,1.0,0.0,0.0,40.0,0.0]|0.0      |
|[17.0,3.0,275778.0,10.0,5.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,25.0,1.0]|0.0      |
|[17.0,3.0,297117.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,40.0,0.0] |0.0      |
|[17.0,3.0,406920.0,7.0,6.0,1.0,7.0,2.0,0.0,0.0,0.0,0.0,40.0,0.0] |0.0      |
|[17.0,3.0,806316.0,5.0,7.0,1.0,7.0,2.0,0.0,1.0,0.0,0.0,20.0,0.0] |0.0      |
|[17.0,2.0,39815.0,7.0,6.0,1.0,5.0,2.0,0.0,1.0,0.0,0.0,25.0,0.0]  |0.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 income_indexed; but in this case, you are using the test data which includes a known true label value, so you can compare the income_indexed

%scala

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

Output

+--------------------+----------+--------------------+---------+
|            features|prediction|         probability|trueLabel|
+--------------------+----------+--------------------+---------+
|[17.0,3.0,34088.0...|       0.0|[0.90194761316253...|      0.0|
|[17.0,3.0,47407.0...|       0.0|[0.91064463670565...|      0.0|
|[17.0,3.0,67808.0...|       0.0|[0.88423614372625...|      0.0|
|[17.0,3.0,80077.0...|       0.0|[0.91913907339023...|      0.0|
|[17.0,3.0,103810....|       0.0|[0.85811022115489...|      0.0|
|[17.0,3.0,104025....|       0.0|[0.90220443593173...|      0.0|
|[17.0,3.0,127003....|       0.0|[0.89818895831678...|      0.0|
|[17.0,3.0,158762....|       0.0|[0.92499659900428...|      0.0|
|[17.0,3.0,161981....|       0.0|[0.88411225055710...|      0.0|
|[17.0,3.0,171461....|       0.0|[0.92498528025178...|      0.0|
|[17.0,3.0,172145....|       0.0|[0.91179545948266...|      0.0|
|[17.0,3.0,181337....|       0.0|[0.94240932647075...|      0.0|
|[17.0,3.0,187539....|       0.0|[0.92776669090787...|      0.0|
|[17.0,3.0,215743....|       0.0|[0.87530598361569...|      0.0|
|[17.0,3.0,241021....|       0.0|[0.91004459320758...|      0.0|
|[17.0,3.0,275778....|       0.0|[0.92589095480737...|      0.0|
|[17.0,3.0,297117....|       0.0|[0.89851323194870...|      0.0|
|[17.0,3.0,406920....|       0.0|[0.88378947133124...|      0.0|
|[17.0,3.0,806316....|       0.0|[0.91844296678151...|      0.0|
|[17.0,2.0,39815.0...|       0.0|[0.91760743437592...|      0.0|
+--------------------+----------+--------------------+---------+

Evaluating a Model (We got 78% Accuracy)

%scala

import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator


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

Output:

import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
evaluator: org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator = MulticlassClassificationEvaluator: uid=mcEval_5ae6549e1586, metricName=accuracy, metricLabel=0.0, beta=1.0, eps=1.0E-15
accuracy: Double = 0.7831989384505461

By Bhavesh