開始使用免費開始

將 NMF 應用到 Wikipedia 文章

在影片中,你看到如何把 NMF 用於轉換一個玩具範例的詞頻陣列。現在換你來套用 NMF,這次使用 Wikipedia 文章的 tf-idf 詞頻陣列,已提供為 csr 矩陣 articles。在這裡先訓練模型並轉換這些文章。下一個練習中,你會進一步探索結果。

本練習屬於課程

Unsupervised Learning in Python

檢視課程

練習說明

  • sklearn.decomposition 匯入 NMF
  • 建立一個名為 model、具有 6 個成分的 NMF 實例。
  • 將模型擬合到詞數資料 articles
  • 使用 model.transform() 方法轉換 articles,並將結果指定給 nmf_features
  • 列印 nmf_features 以先大致看看長相(.round(2) 會把各項四捨五入到小數點後 2 位)。

動手互動練習

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

# Import NMF
____

# Create an NMF instance: model
model = ____

# Fit the model to articles
____

# Transform the articles: nmf_features
nmf_features = ____

# Print the NMF features
print(nmf_features.round(2))
編輯並執行程式碼