Our project uses the official Youtube Data API to form datasets on videos in the past 3 days.
Based on the datasets, our objective was to use machine learning models to predict:
- Whether a video will trend or not
- What Youtube video category (e.g. Gaming, Sports, Music) a video belongs to based on its title
These problem statements can give us insight into maximising Trending potential of Youtube videos, as well as allow us to explore natural language processing potential using titles and categories of video hosting platforms.
We cover the following topics in the following sections:
- Members
- Dataset Information
- Machine Learning Models Used
- Explanatory Data Analysis (EDA)
- Pre-Processing for Classification
- Comparing Models
- Youtube Category Prediction Process
- Conclusion
- References
- Chin Jun Hao, Mark
- Tan Kuan Kiat
- Chelson Chong
The YT_Dataset_Creation.ipynb file generates a dataset.
First, we get Youtube to list the videos currently on trending.
We then obtain the video ID of these trending videos and store them in a trending_id variable.
Next, we do a general search for videos.
We do not want to search for videos from too long ago, as trending videos are those that are released more recently. Hence, we set a start_date variable of datetime 72 hours before our program runs.
Then, we do a search of videos published after this start_date variable.
Our searches give us a list of videos. But the information given is quite lacking. We currently only get videoID, channelID, and publishTiming. We would want more factors that could possibly influence if a video trends.
To accomplish this, we make an API request for each individual videoID. This gives us more information about each video.
Now, we have access to information like categoryId, video duration, viewcounts, likecounts, and commentcounts.
We add all these videoIds to a video_id variable (unless they are in trending_id too as we do not want to double-count).
Giving the videos in trending_id a trending value of 1 and videos in video_id a trending value of 0, we get a compiled dataset below.
-
id: A string for the Youtube id of the video.
-
publishedAt: A datetime (in UTC) the video was published at.
-
channelId: A string for the Youtube id of the publishing channel.
-
title: A string for the title of the video.
-
description: A string for the description of the video.
-
channelTitle: A string for the title of the publishing channel.
-
categoryId: A categorial integer denoting what video category a video belongs to (e.g. Gaming, Sports, Music).
-
liveBroadcastContent: integer 0 if it is NOT an active/upcoming live broadcast, and 1 if it is.
-
duration: A string denoting hour-minute-second duration format of the video, after "PT".
-
dimension: A string denoting if a video is in "2d" or "3d".
-
definition: A string denoting if a video is in "hd" (high definition) or "sd" (standard definition).
-
caption: A boolean denoting if a video has captions.
-
viewCount: An integer representing number of views the video has. (contains a string "None" if the video has a paywall, e.g. paid movies)
-
likeCount: An integer representing number of likes the video has. (contains a string "None" if the video has likes disabled)
-
commentCount: An integer representing number of comments the video has. (contains a string "None" if the video has comments disabled)
-
Trending: integer 0 if the video is NOT trending at search time, and 1 if it is.
The YT_Dataset_Creation.py file does the same as the YT_Dataset_Creation.ipynb file, but generates a dataset once every 12 hours.
We then compiled these csv files into one YT_dataset.csv file.
- Decision Tree
- The Decision Tree classifier is a Supervised Machine Learning Algorithm that uses a set of rules to make decisions, similarly to how humans make decisions. The intuition behind Decision Trees is that you use the dataset features to create yes/no questions and continually split the dataset until you isolate all data points belonging to each class. We decided to use this model to compare with our Random Forest model in predicting Trending.
- Random Forest
- The Random Forest classifier consists of many individual decision trees that make use of different sets of data and different features to classify them. The model chooses the class most predicted by the trees as its prediction. We decided to use this model to compare with our Decision Tree model in predicting Trending.
- Naïve Bayes
- The Naïve Bayes classifier is a supervised machine learning algorithm, which is used for classification tasks, like text classification. It is also part of a family of generative learning algorithms, meaning that it seeks to model the distribution of inputs of a given class or category. We decided to use this model to predict video categories from a title.
A countplot for the categories of ALL videos Most common categories are Entertainment, News & Politics and Sport. However, for TRENDING videos, the top categories are Gaming, Entertainment and Music.
We believe that this could be because of their higher replay value.
Hours are in UTC, the timezone for the united states is UTC minus 5, thus,for ALL videos, the most popular time to post videos is at 11am.
For TRENDING videos, most of them are instead posted at 9am. It is interesting to note that the hours of 5 to 11, 12am to 6am, have the lowest number of videos posted. It coincides with the average time Americans sleep and wake up, affirming the data’s validity.
Saturday and Sunday are the most popular days for ALL videos. While Friday, Sunday then Saturday are the most popular days for TRENDING videos.
We inferred that it is because more videos are watched at the end of the people's work weeks.
Before our classifier can determine which variables are best in predicting Trending. We had to pre-process several variables in order for the classifier to be able to read them.
We omitted out data which have ‘None’s as the value of the viewCount as those videos are paid movies in youtube. These are not desirable data as they are not traditional videos and do not get trended.
Does trended videos usually have a longer title or description? We converted titles and descriptions to their length to answer that question.
As some videos have disabled comments or likes, we modified data with ‘None’s to a readable value of 0 for the classifier.
We converted it from a string with hours, minutes and seconds to a numeric value of seconds as the unit.
The inputs for both the Decision Tree and Random forest involved values of likeCount, commentCount, viewCount, titleCount, descriptionCount, duration and caption. The output was a prediction of whether that video was trending or not.

The inputs for both the Decision Tree and Random forest involved values of likeCount, commentCount, viewCount, titleCount, descriptionCount, duration and caption. The output was a prediction of whether that video was trending or not. The random forest classifier combines multiple decision trees to make a prediction. Each tree in the Random Forest is constructed using a subset of the features and training samples. Each tree's prediction is treated as a vote, and the majority vote is taken as the final prediction. It’s algorithm can rank the importance of each feature by it’s contribution to the classification accuracy of the model.
The models were trained and tested in a ratio of 80:20 (80 being the training set and 20 being the test set).
For predicting the training dataset, Random Forest had a classification accuracy of 1 while the decision tree’s accuracy is 0.85
For predicting the testing dataset, Random Forest had a classification accuracy of 0.91 while the decision tree’s accuracy is 0.85
In this Youtube Category Prediction Model, we made use of Naïve Bayes Classifier. By inputing in hypothetical video titles, the Prediction Model will produce for us the predicted category to place this vidoes in. There are 3 dataset that we have tested on, firstly a Pre-Dataset which is a smaller dataset which we used to work on while we collect more data from the YouTube API, secondly, the Full-Dataset and lastly the cleaned version of the Full-Dataset.
The Model was trained and tested in a ratio of 80:20 (80 being the training set and 20 being the test set). Here we provide hypothetical video titles, in which the model will then provide us with the predicted category these video titles will be categorised in. Some of the process that was done to clean the data included removing unnecessary spaces, cleaning the numbers, correction of the misspelled words, correction of rare words, cleaning bad case words, cleaning the repeat words, cleaning the emojis, unnecessary, punctuations, characters as seen in our preprocess function. These uncleaned data would affect the models efficency even if it is by the slightest margin. As seen in the chart above, there are 140k texts after cleaning the data in comparison to what it was which was at 167k texts.Our project has answered both of our problem statements, we have compared models determining the best predictor of Trending videos and trained a model to predict a video's category based on its title. However, perhaps we need larger datasets for greater accuracy.
Especially for the video category predictor model, our datasets may still be too small to make accurate classifications due to the huge number of video categories.
One possible extension is to examine data across other regions. We only observed Youtube data in the US region for this dataset. Maybe regions like Singapore and Europe will provide different results.
A second possible extension is to examine the data across more days. Maybe on days like public holidays, the data may differ.
- https://www.ibm.com/topics/naive-bayes#:~:text=The%20Na%C3%AFve%20Bayes%20classifier%20is,a%20given%20class%20or%20category
- https://towardsdatascience.com/understanding-random-forest-58381e0602d2
- https://developers.google.com/youtube/v3
- https://www.sleepcycle.com/sleep-science/what-we-know-about-americas-healthiest-happiest-best-rested/
- https://towardsdatascience.com/decision-tree-classifier-explained-in-real-life-picking-a-vacation-destination-6226b2b60575























