Mobile Price Classification

Machine Learning Project for Mobile Price Classification based on the available attributes

Problem Statement or Business Problem

Bob has started his own mobile company. He wants to give a tough fight to big companies like Apple, Samsung etc.

He does not know how to estimate the price of mobiles his company creates. In this competitive mobile phone market, you cannot simply assume things. To solve this problem he collects sales data of mobile phones of various companies.

Bob wants to find out some relation between features of a mobile phone(eg:- RAM, Internal Memory, etc) and its selling price. But he is not so good at Machine Learning. So he needs our help to solve this problem.

In this problem, you do not have to predict the actual price but a price range indicating how high the price is

Attribute Information or Dataset Details:

battery_power : Total energy a battery can store in one time measured in mAh
blue: Has bluetooth or not
clock_speed : speed at which microprocessor executes instructions
dual_sim : Has dual sim support or not
fc : Front Camera mega pixel
four_g :Has 4G or not
int_memory : Internal Memory in Gigabytes
m_dep : Mobile Depth in cm
mobile_wt : Weight of mobile phone
n_cores : Number of cores of processor
pc : Primary Camera mega pixels
px_height: Pixel Resolution Height
px_width: Pixel Resolution Width
ram : Random Access Memory in Megabyte
sc_h: Screen Height of mobile in cm
sc_w: Screen Width of mobile in cm
talk_time : longest time that a single battery charge will last when you are
three_g: Has 3G or not
touch_screen: Has touch screen or not
wifi: Has wifi or not
price_range: This is the target variable with value of 0(low cost), 1(medium cost), 2(high cost) and 3(very high cost).

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 Mobile Price Classification 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 Mobile Price Classification 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

val csv = spark.read.option("inferSchema","true").option("header", "true").csv("/FileStore/tables/train-1.csv")

csv.show()

Output:

+-------------+----+-----------+--------+---+------+----------+-----+---------+-------+---+---------+--------+----+----+----+---------+-------+------------+----+-----------+
|battery_power|blue|clock_speed|dual_sim| fc|four_g|int_memory|m_dep|mobile_wt|n_cores| pc|px_height|px_width| ram|sc_h|sc_w|talk_time|three_g|touch_screen|wifi|price_range|
+-------------+----+-----------+--------+---+------+----------+-----+---------+-------+---+---------+--------+----+----+----+---------+-------+------------+----+-----------+
|          842|   0|        2.2|       0|  1|     0|         7|  0.6|      188|      2|  2|       20|     756|2549|   9|   7|       19|      0|           0|   1|          1|
|         1021|   1|        0.5|       1|  0|     1|        53|  0.7|      136|      3|  6|      905|    1988|2631|  17|   3|        7|      1|           1|   0|          2|
|          563|   1|        0.5|       1|  2|     1|        41|  0.9|      145|      5|  6|     1263|    1716|2603|  11|   2|        9|      1|           1|   0|          2|
|          615|   1|        2.5|       0|  0|     0|        10|  0.8|      131|      6|  9|     1216|    1786|2769|  16|   8|       11|      1|           0|   0|          2|
|         1821|   1|        1.2|       0| 13|     1|        44|  0.6|      141|      2| 14|     1208|    1212|1411|   8|   2|       15|      1|           1|   0|          1|
|         1859|   0|        0.5|       1|  3|     0|        22|  0.7|      164|      1|  7|     1004|    1654|1067|  17|   1|       10|      1|           0|   0|          1|
|         1821|   0|        1.7|       0|  4|     1|        10|  0.8|      139|      8| 10|      381|    1018|3220|  13|   8|       18|      1|           0|   1|          3|
|         1954|   0|        0.5|       1|  0|     0|        24|  0.8|      187|      4|  0|      512|    1149| 700|  16|   3|        5|      1|           1|   1|          0|
|         1445|   1|        0.5|       0|  0|     0|        53|  0.7|      174|      7| 14|      386|     836|1099|  17|   1|       20|      1|           0|   0|          0|
|          509|   1|        0.6|       1|  2|     1|         9|  0.1|       93|      5| 15|     1137|    1224| 513|  19|  10|       12|      1|           0|   0|          0|
|          769|   1|        2.9|       1|  0|     0|         9|  0.1|      182|      5|  1|      248|     874|3946|   5|   2|        7|      0|           0|   0|          3|
|         1520|   1|        2.2|       0|  5|     1|        33|  0.5|      177|      8| 18|      151|    1005|3826|  14|   9|       13|      1|           1|   1|          3|
|         1815|   0|        2.8|       0|  2|     0|        33|  0.6|      159|      4| 17|      607|     748|1482|  18|   0|        2|      1|           0|   0|          1|
|          803|   1|        2.1|       0|  7|     0|        17|  1.0|      198|      4| 11|      344|    1440|2680|   7|   1|        4|      1|           0|   1|          2|
|         1866|   0|        0.5|       0| 13|     1|        52|  0.7|      185|      1| 17|      356|     563| 373|  14|   9|        3|      1|           0|   1|          0|
|          775|   0|        1.0|       0|  3|     0|        46|  0.7|      159|      2| 16|      862|    1864| 568|  17|  15|       11|      1|           1|   1|          0|
|          838|   0|        0.5|       0|  1|     1|        13|  0.1|      196|      8|  4|      984|    1850|3554|  10|   9|       19|      1|           0|   1|          3|
|          595|   0|        0.9|       1|  7|     1|        23|  0.1|      121|      3| 17|      441|     810|3752|  10|   2|       18|      1|           1|   0|          3|
|         1131|   1|        0.5|       1| 11|     0|        49|  0.6|      101|      5| 18|      658|     878|1835|  19|  13|       16|      1|           1|   0|          1|
|          682|   1|        0.5|       0|  4|     0|        19|  1.0|      121|      4| 11|      902|    1064|2337|  11|   1|       18|      0|           1|   1|          1|
+-------------+----+-----------+--------+---+------+----------+-----+---------+-------+---+---------+--------+----+----+----+---------+-------+------------+----+-----------+
only showing top 20 rows

Statistics of Data

%scala

csv.select("battery_power", "blue", "clock_speed", "dual_sim", "fc", "four_g", "int_memory", "m_dep", "mobile_wt", "n_cores", "pc", "px_height", "px_width", "ram", "sc_h", "sc_w", "talk_time", "three_g", "touch_screen", "wifi", "price_range").describe().show()

Output:

+-------+-----------------+------------------+------------------+------------------+-----------------+-------------------+------------------+------------------+-----------------+------------------+-----------------+------------------+-----------------+------------------+-----------------+-----------------+-----------------+-----------------+------------------+------------------+------------------+
|summary|    battery_power|              blue|       clock_speed|          dual_sim|               fc|             four_g|        int_memory|             m_dep|        mobile_wt|           n_cores|               pc|         px_height|         px_width|               ram|             sc_h|             sc_w|        talk_time|          three_g|      touch_screen|              wifi|       price_range|
+-------+-----------------+------------------+------------------+------------------+-----------------+-------------------+------------------+------------------+-----------------+------------------+-----------------+------------------+-----------------+------------------+-----------------+-----------------+-----------------+-----------------+------------------+------------------+------------------+
|  count|             2000|              2000|              2000|              2000|             2000|               2000|              2000|              2000|             2000|              2000|             2000|              2000|             2000|              2000|             2000|             2000|             2000|             2000|              2000|              2000|              2000|
|   mean|        1238.5185|             0.495|1.5222499999999983|            0.5095|           4.3095|             0.5215|           32.0465|0.5017500000000017|          140.249|            4.5205|           9.9165|           645.108|        1251.5155|          2124.213|          12.3065|            5.767|           11.011|           0.7615|             0.503|             0.507|               1.5|
| stddev|439.4182060835313|0.5001000400170073| 0.816004208895068|0.5000347661750049|4.341443747983898|0.49966246736236364|18.145714955206856|0.2884155496235117|35.39965489638834|2.2878367180426618|6.064314941347778|443.78081080643824|432.1994469463379|1084.7320436099492|4.213245004356303|4.356397605826408|5.463955197766688|0.426272922318731|0.5001160445626741|0.5000760322381088|1.1183136021064597|
|    min|              501|                 0|               0.5|                 0|                0|                  0|                 2|               0.1|               80|                 1|                0|                 0|              500|               256|                5|                0|                2|                0|                 0|                 0|                 0|
|    max|             1998|                 1|               3.0|                 1|               19|                  1|                64|               1.0|              200|                 8|               20|              1960|             1998|              3998|               19|               18|               20|                1|                 1|                 1|                 3|
+-------+-----------------+------------------+------------------+------------------+-----------------+-------------------+------------------+------------------+-----------------+------------------+-----------------+------------------+-----------------+------------------+-----------------+-----------------+-----------------+-----------------+------------------+------------------+------------------+

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 = csv.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 Regression 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 price_range 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, a 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("battery_power", "blue", "clock_speed", "dual_sim", "fc", "four_g", "int_memory", "m_dep", "mobile_wt", "n_cores", "pc", "px_height", "px_width", "ram", "sc_h", "sc_w", "talk_time", "three_g", "touch_screen", "wifi")).setOutputCol("features")

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

training.show(false)

Output:

+--------------------------------------------------------------------------------------------------+-----+
|features                                                                                          |label|
+--------------------------------------------------------------------------------------------------+-----+
|[501.0,0.0,2.3,0.0,12.0,1.0,54.0,0.3,131.0,4.0,19.0,504.0,1089.0,2346.0,13.0,12.0,2.0,1.0,0.0,1.0]|1    |
|[501.0,1.0,0.5,1.0,14.0,0.0,22.0,0.5,174.0,6.0,20.0,239.0,1636.0,3077.0,17.0,3.0,17.0,0.0,0.0,0.0]|2    |
|[502.0,0.0,0.8,0.0,7.0,0.0,52.0,1.0,82.0,6.0,8.0,281.0,1159.0,2666.0,5.0,4.0,20.0,1.0,1.0,0.0]    |2    |
|[502.0,0.0,1.5,1.0,7.0,0.0,37.0,0.2,199.0,2.0,12.0,705.0,1810.0,1649.0,6.0,1.0,14.0,0.0,1.0,0.0]  |1    |
|[503.0,0.0,1.2,1.0,5.0,1.0,8.0,0.4,111.0,3.0,13.0,201.0,1245.0,2583.0,11.0,0.0,12.0,1.0,0.0,0.0]  |1    |
|[503.0,0.0,2.5,0.0,3.0,0.0,57.0,0.6,185.0,6.0,11.0,778.0,1291.0,305.0,11.0,8.0,16.0,0.0,0.0,1.0]  |0    |
|[503.0,1.0,1.8,1.0,1.0,1.0,13.0,0.7,131.0,1.0,4.0,1495.0,1688.0,3117.0,19.0,6.0,9.0,1.0,0.0,1.0]  |3    |
|[504.0,0.0,2.8,1.0,0.0,0.0,40.0,0.5,178.0,3.0,0.0,626.0,1195.0,470.0,6.0,0.0,16.0,1.0,0.0,0.0]    |0    |
|[504.0,1.0,0.5,1.0,5.0,0.0,16.0,0.1,166.0,1.0,9.0,767.0,1665.0,701.0,17.0,15.0,15.0,1.0,0.0,1.0]  |0    |
|[504.0,1.0,1.0,0.0,8.0,0.0,14.0,0.5,189.0,7.0,9.0,881.0,1129.0,1607.0,15.0,0.0,10.0,1.0,1.0,1.0]  |0    |
|[506.0,0.0,1.6,0.0,6.0,1.0,41.0,0.8,159.0,1.0,7.0,875.0,1025.0,2965.0,13.0,10.0,15.0,1.0,0.0,0.0] |2    |
|[507.0,1.0,1.9,1.0,0.0,1.0,39.0,0.7,142.0,1.0,0.0,17.0,1084.0,2124.0,6.0,0.0,12.0,1.0,1.0,0.0]    |1    |
|[508.0,0.0,0.8,0.0,7.0,1.0,42.0,0.3,94.0,1.0,8.0,39.0,557.0,663.0,13.0,12.0,7.0,1.0,0.0,0.0]      |0    |
|[508.0,0.0,1.6,1.0,0.0,0.0,9.0,0.4,162.0,6.0,2.0,1419.0,1920.0,2616.0,18.0,8.0,10.0,1.0,1.0,1.0]  |2    |
|[509.0,1.0,0.6,1.0,2.0,1.0,9.0,0.1,93.0,5.0,15.0,1137.0,1224.0,513.0,19.0,10.0,12.0,1.0,0.0,0.0]  |0    |
|[510.0,1.0,2.0,1.0,5.0,1.0,45.0,0.9,168.0,6.0,16.0,483.0,754.0,3919.0,19.0,4.0,2.0,1.0,1.0,1.0]   |3    |
|[511.0,0.0,0.7,1.0,1.0,1.0,52.0,0.7,180.0,2.0,10.0,24.0,759.0,3865.0,8.0,3.0,7.0,1.0,0.0,1.0]     |2    |
|[511.0,0.0,0.9,1.0,15.0,1.0,24.0,0.6,136.0,3.0,18.0,367.0,1264.0,2378.0,18.0,3.0,4.0,1.0,0.0,0.0] |1    |
|[511.0,1.0,3.0,1.0,5.0,1.0,34.0,0.9,125.0,8.0,13.0,149.0,1285.0,3265.0,14.0,7.0,14.0,1.0,0.0,0.0] |2    |
|[512.0,1.0,0.5,1.0,7.0,0.0,15.0,0.9,83.0,3.0,15.0,249.0,1849.0,2610.0,18.0,14.0,15.0,0.0,1.0,1.0] |2    |
+--------------------------------------------------------------------------------------------------+-----+
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 Logistic Regression algorithm you want to use and use its fit method to train a model based on the training DataFrame. In this exercise, you will use a Logistic Regression 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 price_range column to trueLabel.

%scala

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

Output:

+--------------------------------------------------------------------------------------------------+---------+
|features                                                                                          |trueLabel|
+--------------------------------------------------------------------------------------------------+---------+
|[504.0,1.0,0.5,1.0,2.0,1.0,46.0,0.9,172.0,5.0,14.0,280.0,1795.0,2085.0,13.0,5.0,8.0,1.0,0.0,0.0]  |1        |
|[504.0,1.0,2.8,1.0,2.0,0.0,54.0,0.4,163.0,2.0,10.0,1207.0,1539.0,2378.0,17.0,11.0,2.0,1.0,0.0,0.0]|2        |
|[507.0,1.0,0.5,1.0,1.0,0.0,32.0,0.5,141.0,7.0,11.0,936.0,1398.0,1702.0,17.0,0.0,5.0,1.0,1.0,1.0]  |1        |
|[508.0,1.0,1.3,0.0,1.0,0.0,50.0,0.7,82.0,5.0,9.0,102.0,1195.0,2175.0,14.0,4.0,14.0,0.0,0.0,1.0]   |1        |
|[510.0,0.0,1.7,1.0,3.0,0.0,35.0,0.8,120.0,3.0,6.0,382.0,1228.0,2509.0,17.0,2.0,11.0,0.0,1.0,1.0]  |1        |
|[510.0,0.0,2.6,0.0,0.0,0.0,33.0,0.1,110.0,6.0,10.0,1052.0,1897.0,1693.0,6.0,2.0,5.0,0.0,1.0,0.0]  |1        |
|[511.0,1.0,0.6,1.0,12.0,0.0,50.0,0.1,175.0,3.0,16.0,140.0,622.0,1484.0,9.0,7.0,8.0,1.0,0.0,0.0]   |0        |
|[514.0,1.0,1.6,0.0,7.0,1.0,37.0,0.1,172.0,1.0,9.0,956.0,1723.0,3392.0,12.0,8.0,5.0,1.0,1.0,1.0]   |3        |
|[516.0,0.0,1.1,1.0,0.0,1.0,39.0,0.4,91.0,5.0,7.0,855.0,1401.0,819.0,8.0,0.0,10.0,1.0,1.0,1.0]     |0        |
|[516.0,1.0,0.7,1.0,1.0,0.0,30.0,0.9,138.0,1.0,12.0,126.0,698.0,3731.0,17.0,13.0,15.0,0.0,0.0,0.0] |2        |
|[517.0,0.0,1.4,1.0,3.0,1.0,33.0,0.8,183.0,4.0,8.0,660.0,974.0,3704.0,17.0,16.0,11.0,1.0,0.0,1.0]  |3        |
|[525.0,1.0,0.5,1.0,5.0,0.0,51.0,0.5,137.0,8.0,11.0,262.0,1587.0,1891.0,18.0,3.0,12.0,0.0,1.0,0.0] |1        |
|[528.0,0.0,1.7,0.0,12.0,1.0,6.0,0.8,142.0,2.0,15.0,574.0,637.0,3256.0,9.0,8.0,6.0,1.0,1.0,0.0]    |2        |
|[530.0,0.0,2.4,0.0,1.0,0.0,32.0,0.3,88.0,6.0,20.0,48.0,1012.0,959.0,17.0,7.0,6.0,0.0,1.0,0.0]     |0        |
|[531.0,0.0,1.1,0.0,10.0,0.0,63.0,0.7,189.0,7.0,14.0,145.0,1903.0,2958.0,17.0,1.0,19.0,0.0,1.0,0.0]|2        |
|[532.0,1.0,0.8,1.0,3.0,0.0,8.0,0.1,193.0,5.0,10.0,1213.0,1354.0,728.0,13.0,5.0,8.0,1.0,1.0,0.0]   |0        |
|[538.0,0.0,0.8,0.0,12.0,1.0,2.0,0.8,177.0,7.0,13.0,235.0,662.0,417.0,8.0,4.0,9.0,1.0,0.0,1.0]     |0        |
|[541.0,1.0,2.3,0.0,4.0,0.0,51.0,0.4,200.0,8.0,17.0,1012.0,1226.0,403.0,11.0,2.0,12.0,0.0,0.0,0.0] |0        |
|[543.0,0.0,0.5,0.0,0.0,0.0,57.0,0.7,192.0,5.0,4.0,391.0,984.0,2413.0,17.0,14.0,15.0,1.0,0.0,0.0]  |1        |
|[547.0,0.0,1.9,1.0,1.0,0.0,37.0,0.4,154.0,5.0,4.0,371.0,541.0,2705.0,17.0,3.0,10.0,1.0,1.0,0.0]   |1        |
+--------------------------------------------------------------------------------------------------+---------+
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 price_range where the label is unknown; but in this case, you are using the test data which includes a known true label value, so you can compare the predicted price_range.

%scala

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

Output:
+--------------------+----------+--------------------+---------+
|            features|prediction|         probability|trueLabel|
+--------------------+----------+--------------------+---------+
|[504.0,1.0,0.5,1....|       1.0|[0.27930483856825...|        1|
|[504.0,1.0,2.8,1....|       2.0|[0.21988968697952...|        2|
|[507.0,1.0,0.5,1....|       0.0|[0.32582400696642...|        1|
|[508.0,1.0,1.3,0....|       3.0|[0.21716018861018...|        1|
|[510.0,0.0,1.7,1....|       1.0|[0.21783557256168...|        1|
|[510.0,0.0,2.6,0....|       3.0|[0.22265784539270...|        1|
|[511.0,1.0,0.6,1....|       0.0|[0.33186968339861...|        0|
|[514.0,1.0,1.6,0....|       3.0|[0.10318662553012...|        3|
|[516.0,0.0,1.1,1....|       0.0|[0.37520161726592...|        0|
|[516.0,1.0,0.7,1....|       3.0|[0.09488022213165...|        2|
+--------------------+----------+--------------------+---------+                                  
By Bhavesh