Predict Ads Click – Practice Data Analysis and Logistic Regression Prediction

Machine Learning Project for Predict Ads Click based on the available attributes

Problem Statement or Business Problem

In this project we will be working with a data set, indicating whether or not a particular internet user clicked on an Advertisement. We will try to create a model that will predict whether or not they will click on an ad based off the features of that user.

Attribute Information or Dataset Details:

  1. ‘Daily Time Spent on Site’: consumer time on site in minutes
  2. ‘Age’: cutomer age in years
  3. ‘Area Income’: Avg. Income of geographical area of consumer
  4. ‘Daily Internet Usage’: Avg. minutes a day consumer is on the internet
  5. ‘Ad Topic Line’: Headline of the advertisement
  6. ‘City’: City of consumer
  7. ‘Male’: Whether or not consumer was male
  8. ‘Country’: Country of consumer
  9. ‘Timestamp’: Time at which consumer clicked on Ad or closed window
  10. ‘Clicked on Ad’: 0 or 1 indicated clicking on Ad

Technology Used

  1. Apache Spark
  2. Spark SQL
  3. Apache Spark MLLib
  4. Scala
  5. DataFrame-based API
  6. Databricks Notebook

Introduction

Welcome to this project on predict Ads Click in Apache Spark Machine Learning using Databricks platform community edition server which allows you to execute your spark code, free of cost on their server just by registering through email id.

In this project, we explore Apache Spark and Machine Learning on the Databricks platform.

I am a firm believer that the best way to learn is by doing. That’s why I haven’t included any purely theoretical lectures in this tutorial: you will learn everything on the way and be able to put it into practice straight away. Seeing the way each feature works will help you learn Apache Spark machine learning thoroughly by heart.

We’re going to look at how to set up a Spark Cluster and get started with that. And we’ll look at how we can then use that Spark Cluster to take data coming into that Spark Cluster, a process that data using a Machine Learning model, and generate some sort of output in the form of a prediction. That’s pretty much what we’re going to learn about the predictive model.

In this project, we will be performing predict Ads Click using a Logistic Regression algorithm.

We will learn:

  1. Preparing the Data for Processing.
  2. Basics flow of data in Apache Spark, loading data, and working with data, this course shows you how Apache Spark is perfect for a Machine Learning job.
  3. Learn the basics of Databricks notebook by enrolling in Free Community Edition Server
  4. Define the Machine Learning Pipeline
  5. Train a Machine Learning Model
  6. Testing a Machine Learning Model
  7. Evaluating a Machine Learning Model (i.e. Examine the Predicted and Actual Values)

The goal is to provide you with practical tools that will be beneficial for you in the future. While doing that, you’ll develop a model with a real use opportunity.

I am really excited you are here, I hope you are going to follow all the way to the end of the Project. It is fairly straight forward fairly easy to follow through the article we will show you step by step each line of code & we will explain what it does and why we are doing it.

Free Account creation in Databricks

Creating a Spark Cluster

Basics about Databricks notebook

Loading Data into Databricks Environment

Download Data

Load Data in Dataframe

Scala

Print Schema

Scala

Statistics of Data

Scala

Create Temporary View so we can perform Spark SQL on Data

Scala

Spark SQL

Scala

Exploratory Data Analysis or EDA​

One Visualization to Rule Them All​

Scala

Histogram for Age​

Scala

Scatter Plot for Age and Area Income

Scatter Plot for Age and Daily Time Spent on Site

Collecting all String Columns into an Array

Scala

StringIndexer encodes a string column of labels to a column of label indices.

Example of StringIndexer

Scala

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.

In this case, you will create a pipeline with stages:

  1. A StringIndexer estimator that converts string values to indexes for categorical features
  2. A VectorAssembler that combines categorical features into a single vector
Scala

Print Schema to view String Columns are converted in to equivalent Numerical Columns

Scala

Display Data

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 rename the ClickedonAd 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 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

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

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 ClickedonAd 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 ClickedonAd; but in this case, you are using the test data which includes a known true label value, so you can compare the ClickedonAd

Scala

Evaluating a Model (We got 93% Accuracy)

Scala
Scala
By Bhavesh