Python For Data Science — How To Get More Views On Youtube | by Nicholas Leong | Towards Data Science

Greetings data practitioners,Welcome back to your weekly read of solving real-world problems with data science. Right on queue, one of the highest priority first world problems that Millenials face nowadays is —
Not knowing how to blow up on Youtube.
No worries, data can solve everything, and we are going to solve the YouTube Problem. Today.
The world-famous video-sharing website. It is no secret that YouTubers make big bucks posting videos on YouTube. If you’re living under a rock, big YouTubers literally make millions per year, while only having to post 1 video every week.
This had further caused everybody to want a piece of the pie.
Everyone and their dog is now a YouTuber, hoping to make lots of money.However, these people have arrived late to the game, and are struggling to get traffic onto their videos since the platform is already saturated.
In YouTube, more views equal more money.Hence, people start to ask questions like—
What kind of video title attracts more views?
Which video category attracts more views?
How to optimize my video tags to attract more views?
You’re in luck, we will be answering them today.In this article, we will be doing some exploratory data analysis on some YouTube video data from the US, extracting whatever insights we can get.
The EDA we will be performing today uses majorly Plotly. It’s important that you understand the fundamentals of building simple Plotly Graphs. I wrote about building the basic Plotly graphs here, covering the concepts you need to understand while building any sort of graph.
towardsdatascience.com
In order to work with Plotly, you will often need to transform data using Pandas. I got you covered. I happen to have written A Guide to Pandas as well.
towardsdatascience.com
Once you understand both Plotly and Pandas, you are well-prepared to perform almost any type of exploratory data analysis. I would call both these libraries my bread and butter of data analysis and visualization.
Working with Jupyter Notebooks...
# To manipulate data, NLP and visualizationsimport pandas as pdimport numpy as npimport seaborn as snsimport matplotlib.pyplot as pltimport osfrom subprocess import check_output# Standard plotly importsimport plotly as pyimport plotly.tools as tlsfrom plotly.offline import iplot, init_notebook_modeimport plotly.graph_objs as goimport cufflinks# Using plotly + cufflinks in offline modeinit_notebook_mode(connected=True)cufflinks.go_offline(connected=True)# To interactive buttonsimport ipywidgets as widgetsfrom ipywidgets import interact, interact_manualimport warningswarnings.filterwarnings("ignore")#nlpimport stringimport re    #for regeximport nltkfrom nltk.corpus import stopwordsimport spacyfrom nltk import pos_tagfrom nltk.stem.wordnet import WordNetLemmatizer from nltk.tokenize import word_tokenizefrom nltk.tokenize import TweetTokenizerfrom wordcloud import WordCloud, STOPWORDS
Now that's a lot of imports, but I promise they are necessary.
The dataset is imported from Kaggle.com. It is named Trending YouTube Video Statistics and it has data from many countries. For the purpose of this pet project, we will be only analyzing data from the US.
Note: The data is being extracted through the YouTube API. Hence, you can extract your own data for specific countries and different timeframes as well. All information in the source.
df = pd.read_csv(filepath)
In this exploratory data analysis, we aim to —
Better understand the dataset
Find out if the views differ by video categories
Find out if the quality of the video title affects its views
Let’s start off by understanding the variables in our data.
#Looking for Null values and the types of our datadf.info()df.head()
Looking at the columns, we will be focusing on views in this project.Let's find out the distribution for views.
df[['views']].iplot(kind="histogram",                 bins=50, theme="white",                 histnorm='probability',                 title="Distribuition of Views",                xTitle='Distribution',                yTitle='Probability')
Purely on this graph alone, we can tell that most videos get less than 5 million views. There are also a lot of outliers, these are probably the videos that go viral. Let’s take a look at the distribution without outliers.
def RemoveOutliers(df_num):     # calculating mean and std of the array    data_mean, data_std = np.mean(df_num), np.std(df_num)    # setting the threshold value    threshold = data_std * 3    #setting lower and upper limit    lower, upper = data_mean - threshold, data_mean + threshold    # array without outlier values    outliers_removed = [x for x in df_num if x > lower and x < upper]    return outliers_removeddf['removed_views_outliers'] = RemoveOutliers(df['views'])df['removed_views_outliers'].iplot(kind="histogram",                         bins=100, theme="white",                         histnorm='probability',                  title= "Distribution of Views Without Outliers",                        xTitle='Distribution',                        yTitle='Probability')
Almost 40% of the videos have less than 200k views, and it only gets increasingly hard to obtain views after the 200k mark.
It is no secret that certain categories get more views than others.Let’s explore the video categories available, as well as how often videos get posted in each of them.
rename_channels = {1:'Film/Animation', 2:'Cars/Vehicles', 10:'Music', 15:'Pets/Animals',                   17:'Sport',19:'Travel/Events',20: 'Gaming',22:'People/Blogs',23:'Comedy',                   24:'Entertainment',25:'News/Politics',26:'How to/Style',27:'Education',                   28:'Science/Technology',29:'Non Profits/Activism'}df['category_name'] = np.nandf['category_name'] = df['category_id'].map(rename_channels)percent_category = round(df["category_name"].value_counts(), 2)categories = round(df["category_name"].value_counts() / len(df["category_name"]) * 100,2)labels = list(categories.index)values = list(categories.values)trace1 = go.Pie(labels=labels,                 values=values,                 marker=dict(colors=['red']),                 text=(percent_category.index.map(str)),                hovertext=(percent_category.values.astype(str)))layout = go.Layout(title="Views by Category",                    legend=dict(orientation="h"));fig = go.Figure(data=[trace1], layout=layout)iplot(fig)
Looks like the top 3 categories with the most videos are —
Entertainment
Music
How to/Style
This is important knowledge for us to understand our competition if we were to post in any of these categories. In the Music and Entertainment category, majority of videos are posted by celebrities or entertainment companies rather than ‘YouTubers’. Hence, it would make sense that the quality and quantity of their video outputs exceed any of us.
We know these categories have the most videos, but do they have the most views?
trace1 = go.Bar(x=df.groupby(['category_name'])['views'].sum().index,                y=df.groupby(['category_name'])['views'].sum().values,                name='Total Views by Category', visible=True)trace2 = go.Bar(x=df.groupby(['category_name'])['channel_title'].nunique().index,                y=df.groupby(['category_name'])['channel_title'].nunique().values,                 name='Unique Channels by Category', visible=False)data = [trace1, trace2]updatemenus = list([    dict(active=-1,         showactive=True,         buttons=list([              dict(                label = 'Total Views by Category',                 method = 'update',                 args = [{'visible': [True, False, False]},                      {'title': 'Sum of views by Category'}]),                          dict(                  label = 'Total Channels by Category',                 method = 'update',                 args = [{'visible': [False, True, False]},                     {'title': 'Total Channels by Category'}]),]),    )])layout = dict(title='Different Metrics by each category <br> Sum of views by Category',               showlegend=False,              updatemenus=updatemenus)fig = dict(data=data, layout=layout)iplot(fig)
The Music and Entertainment categories do hold the most views and channels. An interesting fact is that even with more videos being posted in the Music category, there are more channels in the Entertainment category than Music. This means that the Entertainment category is slightly more saturated than Music.
On the topic on saturation, let’s explore further.
#saturation metrics by categoryviews_list = np.array(df.groupby(['category_name'])['views'].sum().tolist())channels_list = np.array(df.groupby(['category_name'])['channel_title'].nunique().tolist())videos_list = np.array(df.groupby(['category_name'])['title'].nunique().tolist())views_by_videos = views_list/videos_listviews_by_channels = views_list/channels_list
We create new metrics —
views_by_videos
views_by_channels
to see how many views each video and channel get by category. By doing this, we can roughly gauge how saturated a certain category is.
When starting off as a YouTuber, we don’t want to enter a saturated video category. Unless you have an extremely unfair advantage, otherwise it would be wise to enter categories that are more welcoming to newbies. Imagine competiting with the likes of ‘JustinBieber’ in Music, not a good ending.
Let’s display the Saturation Metrics on a plot.
trace1 = go.Bar(x=df.groupby(['category_name'])['title'].nunique().index,                y=views_by_videos,                name='Saturation - Views per Video', visible=True)trace2 = go.Bar(x=df.groupby(['category_name'])['title'].nunique().index,                y=views_by_channels,                name='Saturation - Views per Channel', visible=True)data = [trace1,trace2]updatemenus = list([    dict(active=-1,         showactive=True,         buttons=list([              dict(                label = 'Saturation - Views per Video',                 method = 'update',                 args = [{'visible': [True, False]},                      {'title': 'Saturation - Views per Video'}]),                          dict(                  label = 'Saturation - Views per Channel',                 method = 'update',                 args = [{'visible': [False, True]},                     {'title': 'Saturation - Views per Channel'}]),        ]),    )])layout = dict(title='*(Select from Dropdown)* Saturation Metrics by Category',               showlegend=False,              updatemenus=updatemenus)fig = dict(data=data, layout=layout)iplot(fig)
From the plots, we can see that music is still the leader in views per video and views per channel. However, trailing second is the Non Profits/Activism category which holds the second highest number in both views per video and views per channel. Looking at the previous chart, the video and channel count in the Non Profits/Activism holds one of the lowest number in both count.
This means that the category has the least videos and channels, but have the one of the highest views per video and channel. Hence, this would be an ideal category to enter.
If I were to pick one, I would pick the Film/Animation category. Looking at the data, it does not have too many videos and channels, but the views per video and views per channel numbers are extremely high. In addition, animation is in my area of interest so that’s a plus.
Now that we have chosen a category, its time to explore the video title.As many YouTubers have mentioned, the video title and thumbnail matters.
You can have an extremely well made video. Without a good video title and thumbnail, no one is going to click into it — every YouTuber ever
Hence, let’s make sure our video title is optimized to gain more views.Let’s first look at the word count in titles and tags, and how it affects our views.
#Unique word countdf['count_unique_word']=df["title"].apply(lambda x: len(set(str(x).split())))df['count_unique_word_tags']=df["tags"].apply(lambda x: len(set(str(x).split())))df[['count_unique_word_tags','count_unique_word']].iplot(kind="histogram",bins=50, theme="white", histnorm='probability', title="Distribuitions of Word Count in Title and Tags",xTitle='Distribuition',yTitle='Probability')
Looking at the distribution of the word count.For video titles, it looks like the majority of video titles have between 5 to 10 words. For video tags, majority of the video tags have less than 30 words.
We can take this as a reference. Let’s look how it affects views.
# Dataframe for unique word count for video titlesdf3 = df[['count_unique_word','views']]df3 = df3.groupby('count_unique_word').mean().reset_index()# Dataframe for unique word count for video tagsdf4 = df[['count_unique_word_tags','views']]df4 = df4.groupby('count_unique_word_tags').mean().reset_index()trace1 = go.Bar(x=df3['count_unique_word'],y=df3['views'],name = 'Views vs Video Title Word Count',visible = True)trace2 = go.Bar(x=df4['count_unique_word_tags'],y=df4['views'],name = 'Views vs Video Tags Word Count',visible = True)data=[trace1,trace2]updatemenus = list([    dict(active=-1,         showactive=True,         buttons=list([              dict(                label = 'Views vs Video Title Word Count',                 method = 'update',                 args = [{'visible': [True, False]},                      {'title': 'Views vs Video Title Word Count'}]),                          dict(                  label = 'Views vs Video Tags Word Count',                 method = 'update',                 args = [{'visible': [False, True]},                     {'title': 'Views vs Video Tags Word Count'}]),        ]),    )])layout = dict(title="*(Select from Dropdown)* Views vs Word Count",               showlegend=False,              updatemenus=updatemenus)fig = dict(data=data, layout=layout)iplot(fig)
Interesting. For video titles word count, titles with 2 words get the most views. After that, for each word count increase, the views increase as well until 11 words where the views start to flatline. This would suggest our video title length to be either 2 words, or anywhere between 5 to 10 words.
For video tags word count, videos with 25 word tags or less have pretty much the same view count. The views start to peak at between 26 to 34 word tags, after that the views start to decline.
Pretty interesting stuff.
Now that we know roughly how long our video titles and tags are, we want to know what kind of words to put in there. By exploring words content, we can roughly see what kind of words other Youtubers put in their videos. We can use the word cloud plot to clearly visualize these words.
plt.rcParams['font.size']= 15              plt.rcParams['savefig.dpi']= 100         plt.rcParams['figure.subplot.bottom']= .1 stopwords = set(stopwords.words("english"))plt.figure(figsize = (15,15))wordcloud = WordCloud(                          background_color='black',                          stopwords=stopwords,                          max_words=1000,                          max_font_size=120,                           random_state=42                         ).generate(str(df['title']))print(wordcloud)fig = plt.figure(1)plt.imshow(wordcloud)plt.title("WORD CLOUD - VIDEO TITLE")plt.axis('off')plt.show()
plt.figure(figsize = (15,15))wordcloud = WordCloud(                          background_color='black',                          stopwords=stopwords,                          max_words=1000,                          max_font_size=120,                           random_state=42                         ).generate(str(df['tags']))print(wordcloud)fig = plt.figure(1)plt.imshow(wordcloud)plt.title("WORD CLOUD - TAGS")plt.axis('off')plt.show()
For video titles, we can see that the most brought up word is ‘WANT’ and ‘TALK’. There’s a reason that these words appear the most, we will have to explore further to better understand the data.
For video tags, we can see that the most brought up word is ‘AMAZING’, ‘LAST’, ‘LIFE’, and ‘WEEK’. These are pretty general words. Again, we will have to analyze deeper in order to understand this data.
If you have read this far, you must really want to get that Youtube Money.If that’s the case, you simply have to follow these instructions on your Youtube Journey, and you’ll be set for life.
Create videos in the Film/Animation category
Have video titles of either 2 words or between 5 to 10 words
Have between 26 to 34 video tags
WANT and TALK were included the most in video titles — find out why
AMAZING, LAST, LIFE and WEEK were included the most in video tags — find out why
If you’re not on the same page yet, this data obviously only serves as a reference and have no proof of ever bringing you more views. This was an exploratory analysis on a small portion of YouTube data from the US, which means that it will not accurately display the behavior of views anywhere including the US.
I hope this has at least been fun for you, as for me.See you in the next one!
We are not done yet with our data journey. I am working on more stories, writings, and guides on the data industry. You can absolutely expect more posts like this. In the meantime, feel free to check out my other articles to temporarily fill your hunger for data.
As usual, I end with a quote.
Data is the new science. Big data holds the answers. ~ Pat Gelsinger, CEO, VMware
Thanks for reading! If you want to get in touch with me, feel free to reach me on nickmydata@gmail.com or my LinkedIn Profile. You can also view the code for previous write-ups in my Github.