Bag-of-words

Sentiment Analysis con Python

Violeta Misheva

Data Scientist

Che cos'è un bag-of-words (BOW)?

 

  • Descrive la presenza delle parole in un documento o in una collezione (corpus)

  • Crea un vocabolario delle parole e una misura della loro presenza

Sentiment Analysis con Python

Recensioni prodotti Amazon

prime 10 righe del dataset di recensioni prodotti Amazon

Sentiment Analysis con Python

Analisi del sentiment con BOW: esempio

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 }
  • Si perdono l'ordine delle parole e la grammatica!
Sentiment Analysis con Python

Risultato finale BOW

  • L'output apparirà più o meno così:

prime 5 righe di un dataset, che mostrano l'output di un approccio BOW

Sentiment Analysis con Python

Funzione CountVectorizer

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 con Python

Output di CountVectorizer

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

Sentiment Analysis con Python

Trasformare il 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 con Python

Esercitiamoci!

Sentiment Analysis con Python

Preparing Video For Download...