Machine Learning Project YouTube Spam Comment Prediction. The study of the classification YouTube comment as spam based on the available attributes
Data Set Information
- COMMENT_ID: String
- AUTHOR: String
- DATE: String
- CONTENT: String
- CLASS: Double
Technology Used
- Apache Spark
- Spark SQL
- Apache Spark MLLib
- Scala
- DataFrame-based API
- Databricks Notebook
Challenges
Process Comma-separated values file (ie file with .csv as Extensions) with user define a schema for data
Convert String data to Numeric format so we can process the data in Apache Spark ML Library.
Introduction
Welcome to this project on creating prediction model to Identify spam comment 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, 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 predictive model.
In this project we will be Predict the YouTube Spam Comment using Logistic Regression algorithm.
We will learn:
- Preparing the Data for Processing.
- Basics flow of data in Apache Spark, loading data, and working with data, this course shows you how Apache Spark is perfect for Machine Learning job.
- Learn basics of Databricks notebook by enrolling into Free Community Edition Server
- Define the Machine Learning Pipeline
- Train a Machine Learning Model
- Testing a Machine Learning Model
- 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
Create a Spark Cluster
Basics about Databricks notebook
Loading Data into Databricks Environment
Download Data
Defining User Define Schema to Load Data in Dataframe
%scala import org.apache.spark.sql.Encoders import org.apache.spark.sql.functions.{col, to_date, to_timestamp} import org.apache.spark.sql.types.DateType case class Youtube(COMMENT_ID: String, AUTHOR: String, DATE: String, CONTENT: String, CLASS: Double ) val YoutubeSchema = Encoders.product[Youtube].schema val YoutubeDF = spark.read.schema(YoutubeSchema).option("header", "true").csv("/FileStore/tables/Youtube02_KatyPerry.csv", "/FileStore/tables/Youtube01_Psy.csv", "/FileStore/tables/Youtube03_LMFAO.csv", "/FileStore/tables/Youtube04_Eminem.csv", "/FileStore/tables/Youtube05_Shakira.csv") YoutubeDF.show(); Output: +--------------------+--------------------+--------------------+--------------------+-----+ | COMMENT_ID| AUTHOR| DATE| CONTENT|CLASS| +--------------------+--------------------+--------------------+--------------------+-----+ |z12rwfnyyrbsefonb...| Lisa Wellas| null|+447935454150 lov...| 1.0| |z130wpnwwnyuetxcn...| jason graham|2015-05-29T02:26:...|I always end up c...| 0.0| |z13vsfqirtavjvu0t...| Ajkal Khan| null|"my sister just r...| 1.0| |z12wjzc4eprnvja43...| Dakota Taylor|2015-05-29T02:13:...| Cool| 0.0| |z13xjfr42z3uxdz22...| Jihad Naser| null|Hello I'am fr...| 1.0| |z133yfmjdur4dvyjr...| Darrion Johnson|2015-05-29T01:27:...|Wow this video al...| 0.0| |z12zgrw5furdsn0sc...| kyeman13| null|Go check out my r...| 1.0| |z12vxdzzds2kzzrzq...| Damax|2015-05-29T00:41:...| Almost 1 billion| 0.0| |z12gxdortqzwhhqas...|Muhammad Asim Mansha| null|Aslamu Lykum... F...| 1.0| |z132wd4ywmicxj2gn...| JuanPa Rap|2015-05-28T23:23:...|Eminem is idol fo...| 0.0| |z13si1rx3nnbshdoj...| Mjt12| null|Help me get 50 su...| 1.0| |z13hwbshcnrhztsw2...| emily 13|2015-05-28T23:10:...| i love song :)| 0.0| |z13nsd141x24yjh2m...| TheJohnRage| null|Alright ladies, i...| 1.0| |z132ib3jvvqvzjj5t...| William Davis|2015-05-28T22:42:...|The perfect examp...| 0.0| |z13puxp4xp35shhfy...| Ashleigh_ Baise|2015-05-28T22:21:...|The boyfriend was...| 0.0| |z13kyh3gdnnzdvxjt...| Lauralyn Karoll| null|"<a href=""https:...| 1.0| |z12lhjtxlsisx55yu...| Shirley Lim| null|Take a look at th...| 1.0| |z13hhxajgrnldjmn5...|TMCB production (...| null|Check out our Cha...| 1.0| |z12xurdj0qznephwg...| Sylith|2015-05-28T21:50:...|Rihanna and Emine...| 0.0| |z13isdtoikvzjlkij...| nepaladventure| null|Check out this pl...| 1.0| +--------------------+--------------------+--------------------+--------------------+-----+ only showing top 20 rows
Collecting all String Columns into an Array
%scala var StringfeatureCol = Array("COMMENT_ID", "AUTHOR", "CONTENT");
StringIndexer encodes a string column of labels to a column of label indices
Example of StringIndexer
import org.apache.spark.ml.feature.StringIndexer val df = spark.createDataFrame( Seq((0, "a"), (1, "b"), (2, "c"), (3, "a"), (4, "a"), (5, "c")) ).toDF("id", "category") df.show() val indexer = new StringIndexer() .setInputCol("category") .setOutputCol("categoryIndex") val indexed = indexer.fit(df).transform(df) indexed.show() Output: +---+--------+ | id|category| +---+--------+ | 0| a| | 1| b| | 2| c| | 3| a| | 4| a| | 5| c| +---+--------+ +---+--------+-------------+ | id|category|categoryIndex| +---+--------+-------------+ | 0| a| 0.0| | 1| b| 2.0| | 2| c| 1.0| | 3| a| 0.0| | 4| a| 0.0| | 5| c| 1.0| +---+--------+-------------+
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:
- A StringIndexer estimator that converts string values to indexes for categorical features
- A VectorAssembler that combines categorical features into a single vector
%scala import org.apache.spark.ml.attribute.Attribute import org.apache.spark.ml.feature.{IndexToString, StringIndexer} import org.apache.spark.ml.{Pipeline, PipelineModel} val indexers = StringfeatureCol.map { colName => new StringIndexer().setInputCol(colName).setHandleInvalid("skip").setOutputCol(colName + "_indexed") } val pipeline = new Pipeline() .setStages(indexers) val YoutubeFinalDF = pipeline.fit(YoutubeDF).transform(YoutubeDF)
Print Schema to view String Columns are converted in to equivalent Numerical Columns
%scala YoutubeFinalDF.printSchema() Output: root |-- COMMENT_ID: string (nullable = true) |-- AUTHOR: string (nullable = true) |-- DATE: string (nullable = true) |-- CONTENT: string (nullable = true) |-- CLASS: double (nullable = true) |-- COMMENT_ID_indexed: double (nullable = false) |-- AUTHOR_indexed: double (nullable = false) |-- CONTENT_indexed: double (nullable = false)
Display Data
%scala YoutubeFinalDF.show() Output: +--------------------+--------------------+--------------------+--------------------+-----+------------------+--------------+---------------+ | COMMENT_ID| AUTHOR| DATE| CONTENT|CLASS|COMMENT_ID_indexed|AUTHOR_indexed|CONTENT_indexed| +--------------------+--------------------+--------------------+--------------------+-----+------------------+--------------+---------------+ |z12rwfnyyrbsefonb...| Lisa Wellas| null|+447935454150 lov...| 1.0| 956.0| 850.0| 141.0| |z130wpnwwnyuetxcn...| jason graham|2015-05-29T02:26:...|I always end up c...| 0.0| 1169.0| 1557.0| 598.0| |z13vsfqirtavjvu0t...| Ajkal Khan| null|"my sister just r...| 1.0| 1833.0| 139.0| 126.0| |z12wjzc4eprnvja43...| Dakota Taylor|2015-05-29T02:13:...| Cool| 0.0| 1064.0| 391.0| 25.0| |z13xjfr42z3uxdz22...| Jihad Naser| null|Hello I'am fr...| 1.0| 1885.0| 716.0| 481.0| |z133yfmjdur4dvyjr...| Darrion Johnson|2015-05-29T01:27:...|Wow this video al...| 0.0| 1242.0| 411.0| 1270.0| |z12zgrw5furdsn0sc...| kyeman13| null|Go check out my r...| 1.0| 1140.0| 1590.0| 450.0| |z12vxdzzds2kzzrzq...| Damax|2015-05-29T00:41:...| Almost 1 billion| 0.0| 1049.0| 393.0| 210.0| |z12gxdortqzwhhqas...|Muhammad Asim Mansha| null|Aslamu Lykum... F...| 1.0| 677.0| 985.0| 227.0| |z132wd4ywmicxj2gn...| JuanPa Rap|2015-05-28T23:23:...|Eminem is idol fo...| 0.0| 1214.0| 746.0| 409.0| |z13si1rx3nnbshdoj...| Mjt12| null|Help me get 50 su...| 1.0| 1738.0| 963.0| 495.0| |z13hwbshcnrhztsw2...| emily 13|2015-05-28T23:10:...| i love song :)| 0.0| 1472.0| 1512.0| 1519.0| |z13nsd141x24yjh2m...| TheJohnRage| null|Alright ladies, i...| 1.0| 1620.0| 1291.0| 213.0| |z132ib3jvvqvzjj5t...| William Davis|2015-05-28T22:42:...|The perfect examp...| 0.0| 1204.0| 1389.0| 1133.0| |z13puxp4xp35shhfy...| Ashleigh_ Baise|2015-05-28T22:21:...|The boyfriend was...| 0.0| 1679.0| 230.0| 1122.0| |z13kyh3gdnnzdvxjt...| Lauralyn Karoll| null|"<a href=""https:...| 1.0| 1548.0| 830.0| 87.0| |z12lhjtxlsisx55yu...| Shirley Lim| null|Take a look at th...| 1.0| 793.0| 1202.0| 46.0| |z13hhxajgrnldjmn5...|TMCB production (...| null|Check out our Cha...| 1.0| 1466.0| 1260.0| 323.0| |z12xurdj0qznephwg...| Sylith|2015-05-28T21:50:...|Rihanna and Emine...| 0.0| 1105.0| 1250.0| 992.0| |z13isdtoikvzjlkij...| nepaladventure| null|Check out this pl...| 1.0| 1491.0| 1648.0| 1.0| +--------------------+--------------------+--------------------+--------------------+-----+------------------+--------------+---------------+ only showing top 20 rows
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 = YoutubeFinal.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 class 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 import org.apache.spark.ml.feature.VectorAssembler val assembler = new VectorAssembler().setInputCols(Array("COMMENT_ID_indexed", "AUTHOR_indexed", "CONTENT_indexed")).setOutputCol("features") val training = assembler.transform(train).select($"features", $"CLASS".alias("label")) training.show() Output: +--------------------+-----+ | features|label| +--------------------+-----+ |[104.0,1516.0,186.0]| 0.0| | [156.0,986.0,441.0]| 0.0| | [191.0,559.0,897.0]| 0.0| |[433.0,1671.0,618.0]| 0.0| | [442.0,558.0,290.0]| 0.0| |[487.0,1735.0,171...| 0.0| |[498.0,1150.0,158...| 0.0| |[513.0,1342.0,128...| 0.0| | [515.0,190.0,632.0]| 0.0| | [532.0,654.0,198.0]| 0.0| |[534.0,1646.0,103...| 0.0| | [541.0,844.0,562.0]| 0.0| |[542.0,588.0,1337.0]| 0.0| |[543.0,1535.0,414.0]| 0.0| | [550.0,516.0,678.0]| 0.0| |[569.0,1146.0,745.0]| 0.0| | [572.0,126.0,994.0]| 0.0| |[596.0,1520.0,870.0]| 0.0| |[600.0,1722.0,170.0]| 0.0| |[619.0,1599.0,134...| 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 CLASS column to trueLabel.
%scala val testing = assembler.transform(test).select($"features", $"CLASS".alias("trueLabel")) testing.show() Output +--------------------+---------+ | features|trueLabel| +--------------------+---------+ | [63.0,166.0,375.0]| 0.0| | [403.0,988.0,287.0]| 0.0| |[422.0,454.0,1609.0]| 0.0| |[430.0,404.0,1651.0]| 0.0| |[502.0,1184.0,170...| 0.0| |[540.0,320.0,1238.0]| 0.0| |[559.0,1710.0,906.0]| 0.0| |[573.0,783.0,1533.0]| 0.0| | [636.0,235.0,677.0]| 0.0| | [639.0,666.0,670.0]| 0.0| | [657.0,329.0,853.0]| 0.0| |[680.0,623.0,1339.0]| 0.0| | [718.0,25.0,461.0]| 0.0| | [756.0,300.0,413.0]| 0.0| |[757.0,355.0,1176.0]| 0.0| |[809.0,482.0,1393.0]| 0.0| |[829.0,215.0,1712.0]| 0.0| | [833.0,975.0,405.0]| 0.0| |[863.0,1105.0,105...| 0.0| |[880.0,829.0,1173.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 Class; but in this case, you are using the test data which includes a known true label value, so you can compare the CLASS
%scala val prediction = model.transform(testing) val predicted = prediction.select("features", "prediction", "trueLabel") predicted.show(10000) Output: +--------------------+----------+---------+ | features|prediction|trueLabel| +--------------------+----------+---------+ | [63.0,166.0,375.0]| 1.0| 0.0| | [403.0,988.0,287.0]| 0.0| 0.0| |[422.0,454.0,1609.0]| 0.0| 0.0| |[430.0,404.0,1651.0]| 0.0| 0.0| |[502.0,1184.0,170...| 0.0| 0.0| |[540.0,320.0,1238.0]| 0.0| 0.0| |[559.0,1710.0,906.0]| 0.0| 0.0| |[573.0,783.0,1533.0]| 0.0| 0.0| | [636.0,235.0,677.0]| 0.0| 0.0| | [639.0,666.0,670.0]| 0.0| 0.0| | [657.0,329.0,853.0]| 0.0| 0.0| |[680.0,623.0,1339.0]| 0.0| 0.0| | [718.0,25.0,461.0]| 1.0| 0.0| | [756.0,300.0,413.0]| 1.0| 0.0| |[757.0,355.0,1176.0]| 0.0| 0.0| |[809.0,482.0,1393.0]| 0.0| 0.0| |[829.0,215.0,1712.0]| 0.0| 0.0| | [833.0,975.0,405.0]| 0.0| 0.0| |[863.0,1105.0,105...| 0.0| 0.0| |[880.0,829.0,1173.0]| 0.0| 0.0| |[888.0,392.0,1603.0]| 0.0| 0.0| | [910.0,181.0,767.0]| 0.0| 0.0| |[926.0,1344.0,447.0]| 0.0| 0.0| |[941.0,1742.0,151...| 0.0| 0.0| | [977.0,504.0,796.0]| 0.0| 0.0| |[991.0,1457.0,170...| 0.0| 0.0| |[1004.0,1728.0,67...| 0.0| 0.0| | [1064.0,391.0,25.0]| 1.0| 0.0| |[1075.0,510.0,257.0]| 0.0| 0.0| |[1083.0,1507.0,16...| 0.0| 0.0| |[1101.0,1455.0,41...| 0.0| 0.0| |[1103.0,529.0,492.0]| 0.0| 0.0|