단어 빈도(Bag-of-Words)

Python으로 배우는 Sentiment Analysis

Violeta Misheva

Data Scientist

Bag-of-Words(BOW)란?

 

  • 문서 또는 문서 집합(코퍼스)에서 단어 출현을 기술합니다

  • 단어 사전과 존재 여부 측정값을 만듭니다

Python으로 배우는 Sentiment Analysis

아마존 상품 리뷰

아마존 상품 리뷰 데이터셋 상위 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...