In this guest post, you’ll learn how to detect YouTube comment spam by reading this tutorial by Dr. Joshua Eckroth, a Ph.D. from Ohio State University in AI and cognitive science and an assistant professor of computer science at Stetson University, where he teaches big data mining and analytics, artificial intelligence (AI), and software engineering.

In this article, you’ll look at a technique for detecting YouTube comment spam using bags of words and random forests. The dataset is pretty straightforward. You’ll use a dataset that has about 2,000 comments from popular YouTube videos (https://archive.ics.uci.edu/ml/datasets/YouTube+Spam+Collection). The dataset is formatted in a way, where each row has a comment followed by a value marked as 1 or 0 for spam or not spam.

First, import a single dataset. This dataset is actually split into four different files. Your set of comments comes from the PSY-Gangnam Style video:

https://www.packtpub.com/graphics/9781789539462/graphics/2189e7a4-986a-4a7c-a3e2-6fcd00d40a8f.png

Then print a few comments as follows:

https://www.packtpub.com/graphics/9781789539462/graphics/2633203d-79be-4dea-b6cc-2682e95d3a66.png

Here, you’ll be able to see that there are more than two columns, but you only require the content and the class columns. The content column contains the comments and the class column contains the values 1 or 0 for spam or not.

For example, notice that the first two comments are marked as not spam, but then the comment subscribe to me for call of duty vids is spam and hi guys please my android photo editor download yada yada is spam as well. Before you start sorting comments, look at the count of how many rows in the dataset are spam and how many are not. The result acquired is 175 and 175 respectively, which sums up to 350 rows overall in this file:

https://www.packtpub.com/graphics/9781789539462/graphics/5b10527b-885e-4204-ab1b-7d0609acac71.png

 

 

In scikit-learn, the bag of words technique is actually called CountVectorizer, which means counting the number of times each word appears and inserting them in a vector. To create a vector, you need to make an object for CountVectorizer and then perform the fit and transform simultaneously:

https://www.packtpub.com/graphics/9781789539462/graphics/b08987bd-f076-41e5-b442-16dd78000b59.png

This is performed in two different steps.

  1. The fit step is where it discovers the words present in the dataset
  2. The second is the transform step, which gives you the bag of words matrix for those phrases. The result obtained in this matrix is 350 rows by 1,418 columns:

https://www.packtpub.com/graphics/9781789539462/graphics/50ae438b-dde0-4fb4-8997-4570aaa8893b.png

There are 350 rows, which mean that you have 350 different comments and 1,418 words. 1418 words apparently are words that appear across all of these phrases.

Now print a single comment and then run the analyzer on that comment so that you can see how well the phrases break it apart. As seen in the following screenshot, the comment has been printed first and then you’ll analyze it to see how it broke it into words:

https://www.packtpub.com/graphics/9781789539462/graphics/fd853bae-a2f3-493c-8ad0-83d9fb820666.png

You can use the vectorizer feature to find out the words the dataset found after vectorizing. The following is the result found after vectorizing, where it starts with numbers and ends with regular words:

https://www.packtpub.com/graphics/9781789539462/graphics/b322222c-6722-41fe-b2c1-e214a71919bb.png

Execute the following command to shuffle the dataset with fraction 100% that is adding frac=1:

https://www.packtpub.com/graphics/9781789539462/graphics/3ec5d33c-e01d-4236-9983-3bacc1201498.png

Now split the dataset into training and testing sets. Assume that the first 300 will be for training, while the latter 50 will be for testing:

https://www.packtpub.com/graphics/9781789539462/graphics/9612e6d4-b520-4e6d-8f92-ad805443896f.png

In the preceding code, vectorizer.fit_transform(d_train[‘CONTENT’]) is an important step. At this stage, you have a training set that you want to perform a fit transform on, which means that it will learn the words and also produce the matrix. However, for the testing set, you don’t perform a fit transform again, since you don’t want the model to learn different words for the testing data. You’ll use the same words that it learned on the training set.

Suppose the testing set has different words, out of which some are unique to the testing set that might have never appeared in the training set. That’s perfectly fine and anyhow you’re going to ignore it.

Since you’re using the training set to build a random forest or decision tree or whatever would be the case, you have to use a certain set of words. These words will have to be the same words used on the testing set. You cannot introduce new words to the testing set since the random forest or any other model would not be able to gauge them.

Now perform the transform on the dataset, and later you can use the answers for training and testing. The training set now has 300 rows and 1,287 different words or columns, and the testing set has 50 rows, but you have the same 1,287 columns:

https://www.packtpub.com/graphics/9781789539462/graphics/d97266a3-0a64-4efb-a583-f87a0bbe06b7.png

Even though the testing set has different words, you need to make sure that it is transformed in the same way as the training set with the same columns. Now begin with the building of the random forest classifier. You can convert this dataset into 80 different trees and fit the training set so that you can score its performance on the testing set:

https://www.packtpub.com/graphics/9781789539462/graphics/cd208e68-e23f-4e9a-86e0-ae51fd93cf21.png

The output of the score is 98%; that’s really good. Here it seems that it got confused between spam and not-spam. You need to be sure that the accuracy is high; for that you can perform a cross-validation with five different splits. To perform a cross-validation, you can use all the training data and let it split into two different groups: 20%, 80%, and 20% will be testing data, and 80% will be the training data:

https://www.packtpub.com/graphics/9781789539462/graphics/e7353e2d-c6ee-45c4-9e1f-15c4f750829e.png

Now perform an average of the scores that you just obtained, which comes to about 95% accuracy. Now print all the data as seen in the following screenshot:

https://www.packtpub.com/graphics/9781789539462/graphics/21538f77-f22c-4229-b452-8d2d12a65360.png

The entire dataset has five different videos with comments, which means that all together you have around 2,000 rows. On checking all the comments, notice that there are 1005 spam comments and 951 not-spam comments; this is quite close enough to split it in to even parts:

https://www.packtpub.com/graphics/9781789539462/graphics/fe97c943-e8ee-424a-a1df-e036b11aa948.png

Now shuffle the entire dataset and separate the comments and answers:

https://www.packtpub.com/graphics/9781789539462/graphics/085e6421-0fd1-4910-859e-2d43aaa550c6.png

You need to perform a couple of steps here with CountVectorizer followed by the random forest. For this, use a feature in scikit-learn called a pipeline. The pipeline is really convenient and will bring together two or more steps so that all the steps are treated as one.

Build a pipeline with the bag of words and then use countVectorizer followed by the random forest classifier. Now print the pipeline and steps required:

https://www.packtpub.com/graphics/9781789539462/graphics/0c15d5c5-6d67-4425-ae2a-73ac6d0da571.png

You can let the pipeline name each step by itself by adding CountVectorizer in your RandomForestClassifier and it will name them CountVectorizer and RandomForestclassifier:

https://www.packtpub.com/graphics/9781789539462/graphics/557e6642-582b-4274-a757-27e8a0c3eaa8.png

Once the pipeline is created, you can just call it fit, and it will perform the rest that is, first it will perform the fit and then transform with the CountVectorizer, followed by a fit with the RandomForest classifier. That’s the benefit of having a pipeline:

https://www.packtpub.com/graphics/9781789539462/graphics/cd7d3578-6773-4f63-a4f5-43531f052496.png

Now you can call score so that it knows when you are scoring. It will to run it through the bag of words countVectorizer followed by predicting with the RandomForestClassifier:

https://www.packtpub.com/graphics/9781789539462/graphics/0bddf4d4-ad1c-454c-972c-5954d6af6da6.png

This whole procedure will produce a score of about 94. You can only predict a single example with the pipeline. For example, imagine you have a new comment after the dataset has been trained and you want to know whether the user has just typed this comment or whether it’s spam:

https://www.packtpub.com/graphics/9781789539462/graphics/4c47c0a8-afba-40d1-bb0a-3db8755cddb4.png

It’s detected correctly; but what about the following comment:

https://www.packtpub.com/graphics/9781789539462/graphics/ee751842-4d94-4370-9d4f-85fd1d038bd5.png

To overcome this, deploy this classifier into an environment and predict whether it is a spm or not when someone types a new comment. You can use your pipeline to figure out how accurate your cross-validation was. In this case, the average accuracy was about 94:

https://www.packtpub.com/graphics/9781789539462/graphics/0d699697-643c-45fa-a335-511d9182112b.png

It’s pretty good. Now add TF-IDF to your model to make it more precise. This will be placed after countVectorizer.

https://www.packtpub.com/graphics/9781789539462/graphics/72342ccc-0368-43f2-a732-0416d3ee0d89.png

After you have produced the counts, you can then produce a TF-IDF score for these counts. Now add this to the pipeline and perform another cross-validation check with the same accuracy:

https://www.packtpub.com/graphics/9781789539462/graphics/e004b372-645a-4fc9-9134-25defaf01d6a.png

This show the steps required for the pipeline:

https://www.packtpub.com/graphics/9781789539462/graphics/b76ae065-1156-4fc1-bba4-2414654f7391.png

The following output got you CountVectorizer, a TF-IDF transformer, and RandomForestClassifier. Notice that countvectorizer can be lower case or upper case in the dataset; it is on you to decide how many words you want to have. You can either use single words or bigrams, which would be pairs of words, or trigrams, which can be triples words. You can also remove stop words, which are really common English words such as andor, and the. With TF-IDF, you can turn off the idf component and just keep the tf component, which would just be a log of the count. You can use idf as well. With random forests, you’ve got a choice of how many trees you use, which is the number of estimators.

There’s another feature of scikit-learn available that allows you to search all of these parameters. It finds out what the best parameters are:

https://www.packtpub.com/graphics/9781789539462/graphics/77d2fd84-f85c-4bc6-82b0-981f67bb1310.png

You can make a little dictionary where you say the name of the pipeline step and then mention what the parameter name would be and this gives you your options. For demonstration, try a maximum number of words or maybe just a maximum of 1,000 or 2,000 words.

Using ngrams, you can mention just single words or pairs of words that are stop words; use the English dictionary of stop words; or don’t use stop words, which means that in the first case you need to get rid of common words, and in the second case you do not get rid of common words. Using TF-IDF, you can use idf to state whether it’s yes or no. The random forest created uses 20, 50, or 100 trees. Using this, you can perform a grid search, which runs through all of the combinations of parameters and finds out what the best combination is. So, give your pipeline number 2, which has the TF-IDF along with it. You can use fit to perform the search and the outcome can be seen in the following screenshot:

https://www.packtpub.com/graphics/9781789539462/graphics/2a6a55a5-6e75-4e39-a7c6-9179ec5fb3ce.png

Since there are a large number of words, it takes a little while, around 40 seconds and ultimately finds the best parameters. You can get the best parameters out of the grid search and print them to see what the score is:

https://www.packtpub.com/graphics/9781789539462/graphics/4c121155-4dbf-4aea-a4bd-6f2ad23185aa.png

So, you got an accuracy of around 96%. You used around 1,000 words, only single words, used yes to get rid of stop words, had 100 trees in the random forest, and used yes and the IDF and the TF-IDF computation. Here you’ve demonstrated not only bag of words, TF-IDF, and random forest, but also the pipeline feature and the parameter search feature known as grid search.

If you found this article interesting, you can explore Dr. Joshua Eckroth’s Python Artificial Intelligence Projects for Beginners to build smart applications by implementing real-world artificial intelligence projects. This book demonstrates AI projects in Python, covering modern techniques that make up the world of artificial intelligence.