開始使用免費開始

產品回饋的 TF-IDF 表示法

你正與一家智慧家庭公司的客服團隊合作。他們蒐集了多種智慧裝置的使用者回饋,想找出每篇評論中最突出的字詞。你建議使用 TF-IDF 技術,來凸顯各則回饋中最具代表性的詞彙。讓我們一起幫他們開始吧!

已為你預先載入一個 preprocess() 函式,會接收文字並回傳處理後的結果。此函式會進行轉小寫、斷詞與移除標點符號。Pandas 已以 pd 匯入,且 TfidfVectorizer 類別可直接使用。

本練習屬於課程

Python 的 Natural Language Processing(NLP)

檢視課程

練習說明

  • 初始化一個 TF-IDF vectorizer
  • 將清理後的評論轉換為 tfidf_matrix
  • 建立一個以詞彙表為欄位的 DataFrame df,其內容來自 tfidf_matrix

動手互動練習

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

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())
編輯並執行程式碼