产品反馈的 TF-IDF 表示
您正在与一家智能家居公司的客户支持团队合作。他们收集了多款智能设备的用户反馈,想找出每条评价中最突出的词。您建议使用 TF-IDF 技术,突出各条反馈中最相关的术语。让我们一起开始吧!
已为您预加载 preprocess() 函数,该函数接收一段文本并返回处理后的文本。此函数会执行小写化、分词和去除标点。Pandas 已以 pd 导入,且 TfidfVectorizer 类可以直接使用。
本练习是课程的一部分
Python 中的自然语言处理(NLP)
练习说明
- 初始化一个 TF-IDF
vectorizer。 - 将清洗后的评价转换为
tfidf_matrix。 - 为
tfidf_matrix创建一个 DataFramedf,其列名为词汇表中的单词。
交互式实操练习
通过完成这段示例代码来试试这个练习。
reviews = ["The smart speaker is incredible. Clear sound and fast responses!",
"I am disappointed with the smart bulb. It stopped working in a week.",
"The thermostat is okay. Not too smart, but functional."]
cleaned_reviews = [preprocess(review) for review in reviews]
# Initialize the vectorizer
vectorizer = ____
# Transform the cleaned reviews
tfidf_matrix = ____
# Create a DataFrame for TF-IDF
df = pd.DataFrame(
tfidf_matrix.toarray(),
columns=vectorizer.____
)
print(df.head())