開始使用免費開始

在相同資料上比較 Tfidf 與 BOW

在這個練習中,你將同時使用 bag-of-words(BOW)與 tfidf 轉換來處理 Amazon 產品 reviewsreview 欄位。

建立兩個向量化器,僅指定最大特徵數為 100。在轉換後建立 DataFrame,並列印各自的前 5 列。

請留意你如何設定詞彙表中的最大特徵數。詞彙表過大可能會導致你的工作階段被中斷。

本練習屬於課程

Python 情感分析

檢視課程

練習說明

  • 匯入 BOW 與 Tfidf 的向量化器。
  • review 欄位建立並擬合 BOW 與 Tfidf 向量化器,並將產生的特徵數限制為 100。
  • 由轉換後的向量表示建立 DataFrame。

動手互動練習

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

# Import the required packages
____

# Build a BOW and tfidf vectorizers from the review column and with max of 100 features
vect1 = ____(____=100).____(____.____)
vect2 = ____(____=100).____(____.____) 

# Transform the vectorizers
X1 = vect1.transform(reviews.review)
X2 = vect2.transform(reviews.review)
# Create DataFrames from the vectorizers 
X_df1 = pd.DataFrame(X1.____, columns=____.____)
X_df2 = pd.DataFrame(X2.____, columns=____.____)
print('Top 5 rows using BOW: \n', X_df1.head())
print('Top 5 rows using tfidf: \n', X_df2.head())
編輯並執行程式碼