開始使用免費開始

使用商品評論的 BOW

你已經在小型資料集上練習過 BOW。現在要把它應用到一組 Amazon 商品評論樣本。資料已為你載入為 reviews,包含兩個欄位。第一個欄位為 score,當評論為負面時為 0,正面時為 1。第二個欄位為 review,包含顧客撰寫的評論文字。你可以在 IPython Shell 中自由探索資料。

你的任務是使用 review 欄位建立一個 BOW 詞彙表。

記得可以在向量化器上呼叫 .get_feature_names() 方法,取得所有詞彙元素的清單。

本練習屬於課程

Python 情感分析

檢視課程

練習說明

  • 建立一個 CountVectorizer 物件,並指定最大特徵數。
  • 對向量化器執行 fit。
  • 對已 fit 的向量化器進行 transform。
  • 建立一個 DataFrame,將稀疏矩陣轉換為稠密陣列,並確實正確指定欄位名稱。

動手互動練習

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

from sklearn.feature_extraction.text import CountVectorizer 

# Build the vectorizer, specify max features 
vect = ____(____=100)
# Fit the vectorizer
vect.____(reviews.review)

# Transform the review column
X_review = vect.____(reviews.review)

# Create the bow representation
X_df=pd.DataFrame(X_review._____, columns=___.____)
print(X_df.head())
編輯並執行程式碼