from google.colab import drive
drive.mount('/data/')
data_dir = '/data/My Drive/Colab Notebooks/Experiment'
!ls '/data/My Drive/Colab Notebooks/Experiment'
!pip install matplotlib
Mounted at /data/ diamonds.csv m_data.csv TSLA.csv w_data.csv Iris.csv news_data.csv USvideos.csv Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (3.2.2) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (1.3.1) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (2.4.7) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (0.10.0) Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (2.8.1) Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (1.18.5) Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cycler>=0.10->matplotlib) (1.15.0)
import pandas as pd
df = pd.read_csv(data_dir + '/USvideos.csv')
print(df.shape)
(40949, 16)
df['text'] = df['title']+df['tags']+df['channel_title']
import nltk
from nltk.stem import *
nltk.download('punkt')
from nltk.tokenize import RegexpTokenizer
import pandas as pd
from tqdm.notebook import tqdm
tqdm.pandas()
from functools import reduce
import re
[nltk_data] Downloading package punkt to /root/nltk_data... [nltk_data] Unzipping tokenizers/punkt.zip.
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
stopwords.words('english')
en_stops = set(stopwords.words('english'))
[nltk_data] Downloading package stopwords to /root/nltk_data... [nltk_data] Unzipping corpora/stopwords.zip.
df.isnull().sum()
video_id 0 trending_date 0 title 0 channel_title 0 category_id 0 publish_time 0 tags 0 views 0 likes 0 dislikes 0 comment_count 0 thumbnail_link 0 comments_disabled 0 ratings_disabled 0 video_error_or_removed 0 description 570 text 0 dtype: int64
from nltk.corpus import stopwords
nltk.download('wordnet')
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
def process(line):
return([wordnet_lemmatizer.lemmatize(t) for t in tokenizer.tokenize(line) if t not in en_stops])
tokenizer = RegexpTokenizer(r'\w+')
df['tokens']=df['text'].str.lower().apply(process)
[nltk_data] Downloading package wordnet to /root/nltk_data... [nltk_data] Unzipping corpora/wordnet.zip.
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 40949 entries, 0 to 40948 Data columns (total 18 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 video_id 40949 non-null object 1 trending_date 40949 non-null object 2 title 40949 non-null object 3 channel_title 40949 non-null object 4 category_id 40949 non-null int64 5 publish_time 40949 non-null object 6 tags 40949 non-null object 7 views 40949 non-null int64 8 likes 40949 non-null int64 9 dislikes 40949 non-null int64 10 comment_count 40949 non-null int64 11 thumbnail_link 40949 non-null object 12 comments_disabled 40949 non-null bool 13 ratings_disabled 40949 non-null bool 14 video_error_or_removed 40949 non-null bool 15 description 40379 non-null object 16 text 40949 non-null object 17 tokens 40949 non-null object dtypes: bool(3), int64(5), object(10) memory usage: 4.8+ MB
from nltk import FreqDist
vectors = pd.DataFrame()
for row in df.head(250)['tokens']:
vectors = vectors.append(dict(FreqDist(row)),ignore_index=True)
vectors
marriageshantell | martincaseyneistat | talk | want | donald | hbo | john | last | lastweektonight | oliver | presidency | tonight | trump | week | alesso | anitta | anwar | bach | bear | black | brazil | driver | getting | hannahstocking | inanna | iphone | king | lele | lelepons | license | love | mancuso | music | official | pineapple | pons | ponsracist | poo | racist | rudy | ... | sacred | bezos | ceo | community | festival | gathering | growing | la17 | rare | richest | successsummit | summit | 17taylor | reputationswift | target | taylorswift | taylurking | cant | dunkfest | fuckin | guard | reed | saying | willie | 100 | breath | canbreath | hold | holding | watchcut | earl | grey | linecupcakes | macarons | makrides | pastry | pipe | scran | scranline | vanilla | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1.0 | 1.0 | 1.0 | 1.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | NaN | NaN | NaN | NaN | 2.0 | 1.0 | 2.0 | 3.0 | 1.0 | 2.0 | 2.0 | 3.0 | 5.0 | 3.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | 1.0 | 1.0 | 2.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 2.0 | 2.0 | 1.0 | 1.0 | 1.0 | 4.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 2.0 | 4.0 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
3 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 2.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
4 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
245 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
246 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | 3.0 | 1.0 | 2.0 | 2.0 | 1.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
247 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
248 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | 1.0 | 1.0 | 3.0 | 1.0 | 1.0 | 1.0 | 3.0 | 1.0 | 2.0 |
249 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
250 rows × 3135 columns
vectors.count()
marriageshantell 2 martincaseyneistat 2 talk 19 want 5 donald 3 .. pastry 1 pipe 1 scran 1 scranline 1 vanilla 1 Length: 3135, dtype: int64
from math import log
log_tf = pd.DataFrame()
for c in vectors.columns:
log_tf[c]=vectors[c].apply(lambda x: 1+log(x) if x>0 else 0)
count = vectors.count().iloc[0]
data = pd.DataFrame()
for c in vectors.columns:
data[c]=vectors[vectors[c]>0].count().apply(lambda x: 1+log(count/x) if x>0 else 0)
data = data.iloc[0]
data.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x7f9508b69da0>
tf_count = pd.DataFrame()
for c in vectors.columns:
tf_count=vectors[vectors[c]>0].sum()
tf_count.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x7f95085f89e8>
tf_weighed = pd.DataFrame()
for c in log_tf.columns:
tf_weighed[c] = log_tf[c]*data[c]
for c in vectors.columns:
tf_count=tf_weighed[tf_weighed[c]>0].sum()
tf_count.hist()
<matplotlib.axes._subplots.AxesSubplot at 0x7f9507bca9e8>
def most_frequent(List):
counter = 0
num = List[0]
for i in List:
curr_frequency = List.count(i)
if(curr_frequency> counter):
counter = curr_frequency
num = i
return num
df['most_common'] = df['tokens'].apply(most_frequent)
df.head()
video_id | trending_date | title | channel_title | category_id | publish_time | tags | views | likes | dislikes | comment_count | thumbnail_link | comments_disabled | ratings_disabled | video_error_or_removed | description | text | tokens | most_common | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2kyS6SvSYSE | 17.14.11 | WE WANT TO TALK ABOUT OUR MARRIAGE | CaseyNeistat | 22 | 2017-11-13T17:13:01.000Z | SHANtell martin | 748374 | 57527 | 2966 | 15954 | https://i.ytimg.com/vi/2kyS6SvSYSE/default.jpg | False | False | False | SHANTELL'S CHANNEL - https://www.youtube.com/s... | WE WANT TO TALK ABOUT OUR MARRIAGESHANtell mar... | [want, talk, marriageshantell, martincaseyneis... | want |
1 | 1ZAPwfrtAFY | 17.14.11 | The Trump Presidency: Last Week Tonight with J... | LastWeekTonight | 24 | 2017-11-13T07:30:00.000Z | last week tonight trump presidency|"last week ... | 2418783 | 97185 | 6146 | 12703 | https://i.ytimg.com/vi/1ZAPwfrtAFY/default.jpg | False | False | False | One year after the presidential election, John... | The Trump Presidency: Last Week Tonight with J... | [trump, presidency, last, week, tonight, john,... | trump |
2 | 5qpjK5DgCt4 | 17.14.11 | Racist Superman | Rudy Mancuso, King Bach & Le... | Rudy Mancuso | 23 | 2017-11-12T19:05:24.000Z | racist superman|"rudy"|"mancuso"|"king"|"bach"... | 3191434 | 146033 | 5339 | 8181 | https://i.ytimg.com/vi/5qpjK5DgCt4/default.jpg | False | False | False | WATCH MY PREVIOUS VIDEO ▶ \n\nSUBSCRIBE ► http... | Racist Superman | Rudy Mancuso, King Bach & Le... | [racist, superman, rudy, mancuso, king, bach, ... | rudy |
3 | puqaWrEC7tY | 17.14.11 | Nickelback Lyrics: Real or Fake? | Good Mythical Morning | 24 | 2017-11-13T11:00:04.000Z | rhett and link|"gmm"|"good mythical morning"|"... | 343168 | 10172 | 666 | 2146 | https://i.ytimg.com/vi/puqaWrEC7tY/default.jpg | False | False | False | Today we find out if Link is a Nickelback amat... | Nickelback Lyrics: Real or Fake?rhett and link... | [nickelback, lyric, real, fake, rhett, link, g... | nickelback |
4 | d380meD0W0M | 17.14.11 | I Dare You: GOING BALD!? | nigahiga | 24 | 2017-11-12T18:01:41.000Z | ryan|"higa"|"higatv"|"nigahiga"|"i dare you"|"... | 2095731 | 132235 | 1989 | 17518 | https://i.ytimg.com/vi/d380meD0W0M/default.jpg | False | False | False | I know it's been a while since we did this sho... | I Dare You: GOING BALD!?ryan|"higa"|"higatv"|"... | [dare, going, bald, ryan, higa, higatv, nigahi... | dare |
print(df.dtypes)
video_id object trending_date object title object channel_title object category_id int64 publish_time object tags object views int64 likes int64 dislikes int64 comment_count int64 thumbnail_link object comments_disabled bool ratings_disabled bool video_error_or_removed bool description object text object tokens object most_common object dtype: object
from collections import Counter
one = df['most_common'].to_list()
rslt = pd.DataFrame(Counter(one).most_common(10),
columns=['Word', 'Frequency']).set_index('Word')
print(rslt)
Frequency Word makeup 732 late 342 cat 316 trailer 257 show 219 news 206 movie 202 react 188 star 188 food 165
def popular(List_1):
first = rslt.index.to_list()
if List_1 in first:
return True
else:
return False
df['popular_word'] = df['most_common'].apply(lambda d: popular(d))
df['popular_word'].value_counts()
False 38134 True 2815 Name: popular_word, dtype: int64
df.head()
video_id | trending_date | title | channel_title | category_id | publish_time | tags | views | likes | dislikes | comment_count | thumbnail_link | comments_disabled | ratings_disabled | video_error_or_removed | description | text | tokens | most_common | popular_word | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2kyS6SvSYSE | 17.14.11 | WE WANT TO TALK ABOUT OUR MARRIAGE | CaseyNeistat | 22 | 2017-11-13T17:13:01.000Z | SHANtell martin | 748374 | 57527 | 2966 | 15954 | https://i.ytimg.com/vi/2kyS6SvSYSE/default.jpg | False | False | False | SHANTELL'S CHANNEL - https://www.youtube.com/s... | WE WANT TO TALK ABOUT OUR MARRIAGESHANtell mar... | [want, talk, marriageshantell, martincaseyneis... | want | False |
1 | 1ZAPwfrtAFY | 17.14.11 | The Trump Presidency: Last Week Tonight with J... | LastWeekTonight | 24 | 2017-11-13T07:30:00.000Z | last week tonight trump presidency|"last week ... | 2418783 | 97185 | 6146 | 12703 | https://i.ytimg.com/vi/1ZAPwfrtAFY/default.jpg | False | False | False | One year after the presidential election, John... | The Trump Presidency: Last Week Tonight with J... | [trump, presidency, last, week, tonight, john,... | trump | False |
2 | 5qpjK5DgCt4 | 17.14.11 | Racist Superman | Rudy Mancuso, King Bach & Le... | Rudy Mancuso | 23 | 2017-11-12T19:05:24.000Z | racist superman|"rudy"|"mancuso"|"king"|"bach"... | 3191434 | 146033 | 5339 | 8181 | https://i.ytimg.com/vi/5qpjK5DgCt4/default.jpg | False | False | False | WATCH MY PREVIOUS VIDEO ▶ \n\nSUBSCRIBE ► http... | Racist Superman | Rudy Mancuso, King Bach & Le... | [racist, superman, rudy, mancuso, king, bach, ... | rudy | False |
3 | puqaWrEC7tY | 17.14.11 | Nickelback Lyrics: Real or Fake? | Good Mythical Morning | 24 | 2017-11-13T11:00:04.000Z | rhett and link|"gmm"|"good mythical morning"|"... | 343168 | 10172 | 666 | 2146 | https://i.ytimg.com/vi/puqaWrEC7tY/default.jpg | False | False | False | Today we find out if Link is a Nickelback amat... | Nickelback Lyrics: Real or Fake?rhett and link... | [nickelback, lyric, real, fake, rhett, link, g... | nickelback | False |
4 | d380meD0W0M | 17.14.11 | I Dare You: GOING BALD!? | nigahiga | 24 | 2017-11-12T18:01:41.000Z | ryan|"higa"|"higatv"|"nigahiga"|"i dare you"|"... | 2095731 | 132235 | 1989 | 17518 | https://i.ytimg.com/vi/d380meD0W0M/default.jpg | False | False | False | I know it's been a while since we did this sho... | I Dare You: GOING BALD!?ryan|"higa"|"higatv"|"... | [dare, going, bald, ryan, higa, higatv, nigahi... | dare | False |