電影評論的詞彙表大小
在本練習中,你會使用 movies 評論資料集的抽樣資料,練習幾種限制詞彙表大小的方法。第一欄是 review,型別為 object;第二欄是 label,其中 0 代表負評,1 代表好評。
你將使用的三種方法,會把文字欄位轉換成新的數值欄位,記錄每則評論中某個單字或片語出現的次數。每種方法最後都會產生不同數量的新特徵。
本練習屬於課程
Python 情感分析
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from sklearn.feature_extraction.text import CountVectorizer
# Build the vectorizer, specify size of vocabulary and fit
vect = CountVectorizer(____=____)
vect.fit(movies.review)
# Transform the review column
X_review = vect.transform(movies.review)
# Create the bow representation
X_df = pd.DataFrame(X_review.toarray(), columns=vect.get_feature_names())
print(X_df.head())