在同一数据上比较 Tfidf 与 BOW
在本练习中,您将对 Amazon 商品 reviews 的 review 列分别进行词袋(bag-of-words,BOW)和 tfidf 转换。
构建这两种向量化器,仅指定最大特征数为 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())