Bag-of-words

Sentiment Analysis in Python

Violeta Misheva

Data Scientist

What is a bag-of-words (BOW) ?

 

  • Describes the occurrence of words within a document or a collection of documents (corpus)

  • Builds a vocabulary of the words and a measure of their presence

Sentiment Analysis in Python

Amazon product reviews

top 10 rows of the Amazon product reviews dataset

Sentiment Analysis in Python

Sentiment analysis with BOW: Example

This is the best book ever. I loved the book and highly recommend it!!!

{'This': 1, 'is': 1, 'the': 2 , 'best': 1 , 'book': 2, 
'ever': 1, 'I':1 , 'loved':1 , 'and': 1  , 'highly': 1,
'recommend': 1 , 'it': 1 }
  • Lose word order and grammar rules!
Sentiment Analysis in Python

BOW end result

  • The output will look something like this:

top 5 rows of a dataset, displaying the output of a BOW approach

Sentiment Analysis in Python

CountVectorizer function

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
vect = CountVectorizer(max_features=1000) 
vect.fit(data.review)
X = vect.transform(data.review)
Sentiment Analysis in Python

CountVectorizer output

X
<10000x1000 sparse matrix of type '<class 'numpy.int64'>' 
  with 406668 stored elements in Compressed Sparse Row format>

Sentiment Analysis in Python

Transforming the vectorizer

# Transform to an array
my_array = X.toarray()
# Transform back to a DataFrame, assign column names
X_df = pd.DataFrame(my_array, columns=vect.get_feature_names())
Sentiment Analysis in Python

Let's practice!

Sentiment Analysis in Python

Preparing Video For Download...