開始使用免費開始

電影標語的 BoW 模型

在這個練習中,已提供一個包含超過 7000 則電影標語的 corpus。你的任務是為這些標語產生詞袋(bag of words)表示法 bow_matrix。為了簡化,本題會略過文字前處理,直接建立 bow_matrix

我們也會檢視產生後 bow_matrix 的形狀。主控台已列印出 corpus 中前 5 則標語供你查看。

本練習屬於課程

Python 中文本特徵工程

檢視課程

練習說明

  • sklearn 匯入 CountVectorizer 類別。
  • 建立一個 CountVectorizer 物件,命名為 vectorizer
  • 使用 fit_transform(),為 corpus 產生 bow_matrix

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import CountVectorizer
from sklearn.feature_extraction.text import ____

# Create CountVectorizer object
____ = ____

# Generate matrix of word vectors
bow_matrix = vectorizer.____(____)

# Print the shape of bow_matrix
print(bow_matrix.shape)
編輯並執行程式碼