NMF 學出文件主題
在影片中你學到,當 NMF 應用在文件上時,各個組件會對應到文件的主題,而 NMF 特徵則是用這些主題來重建文件。現在請用你先前用維基百科文章建立的 NMF 模型,自己驗證看看。先前你看到,第 3 個 NMF 特徵在關於演員 Anne Hathaway 與 Denzel Washington 的文章上有高值。這個練習中,請找出對應 NMF 組件的主題。
你先前建立的 NMF 模型可由 model 取得;words 則是標示「詞頻陣列」欄名稱的單字清單。
完成後,花點時間想想,Anne Hathaway 與 Denzel Washington 的文章有哪些共同的主題!
本練習屬於課程
Unsupervised Learning in Python
練習說明
- 匯入
pandas並命名為pd。 - 由
model.components_建立 DataFramecomponents_df,並設定columns=words,讓欄位以單字標示。 - 列印
components_df.shape以檢查 DataFrame 的維度。 - 在 DataFrame
components_df上使用.iloc[]存取器選取第3列,並將結果指定給component。 - 呼叫
component的.nlargest()方法並列印結果。這會給出該組件中數值最高的 5 個單字。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import pandas
import pandas as pd
# Create a DataFrame: components_df
components_df = ____
# Print the shape of the DataFrame
print(components_df.shape)
# Select row 3: component
component = ____
# Print result of nlargest
print(component.nlargest())