Bag-of-words

Analiza sentimentelor în Python

Violeta Misheva

Data Scientist

Ce este bag-of-words (BOW)?

 

  • Descrie apariția cuvintelor într-un document sau o colecție de documente (corpus)

  • Construiește un vocabular al cuvintelor și o măsură a prezenței acestora

Analiza sentimentelor în Python

Recenzii de produse Amazon

Primele 10 rânduri din setul de date cu recenzii de produse Amazon

Analiza sentimentelor în Python

Analiza sentimentelor cu BOW: Exemplu

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 }
  • Se pierde ordinea cuvintelor și regulile gramaticale!
Analiza sentimentelor în Python

Rezultatul final BOW

  • Rezultatul va arăta astfel:

Primele 5 rânduri dintr-un set de date, afișând rezultatul abordării BOW

Analiza sentimentelor în Python

Funcția 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)
Analiza sentimentelor în Python

Rezultatul CountVectorizer

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

Analiza sentimentelor în Python

Transformarea vectorizatorului

# 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())
Analiza sentimentelor în Python

Să exersăm!

Analiza sentimentelor în Python

Preparing Video For Download...