Bag-of-words

Pythonで学ぶSentiment Analysis

Violeta Misheva

Data Scientist

Bag-of-words(BOW)とは?

 

  • 文書または文書集合(コーパス)内の単語出現を表す

  • 語彙を作り、出現の度合いを数値化する

Pythonで学ぶSentiment Analysis

Amazon 製品レビュー

Amazon 製品レビューの上位 10 行

Pythonで学ぶSentiment Analysis

BOW による感情分析:例

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 }
  • 語順と文法は失われる!
Pythonで学ぶSentiment Analysis

BOW の最終結果

  • 出力は次のようになります:

BOW の出力を示すデータセット上位 5 行

Pythonで学ぶSentiment Analysis

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)
Pythonで学ぶSentiment Analysis

CountVectorizer の出力

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

Pythonで学ぶSentiment Analysis

ベクトライザの変換

# 配列に変換
my_array = X.toarray()
# DataFrame に戻し、列名を付与
X_df = pd.DataFrame(my_array, columns=vect.get_feature_names())
Pythonで学ぶSentiment Analysis

演習に進みましょう

Pythonで学ぶSentiment Analysis

Preparing Video For Download...