非負矩陣分解(NMF)

Unsupervised Learning in Python

Benjamin Wilson

Director of Research at lateral.io

非負矩陣分解

  • NMF = 「non-negative matrix factorization」
  • 降維技術
  • NMF 模型可解釋(不像 PCA)
  • 好解讀就好說明!
  • 但所有樣本特徵需為非負(>= 0)
Unsupervised Learning in Python

可解釋的構成部件

  • NMF 將文件表示為主題(或「主題群」)的組合

 

文字方塊:DataCamp is the first and foremost leader in Data Science Education offering skill-based training, pioneering technical innovation... 約等於 0.6 倍含有 program、r、python、function、method 的文字方塊,加上 0.5 倍含有 data、analysis、cluster、statistics、mean 的文字方塊,加上 0.7 倍含有 teaching、learn、lesson、lessons、course 的文字方塊

Unsupervised Learning in Python

可解釋的構成部件

  • NMF 將影像表示為圖樣的組合

 

三條紋的方塊 約等於 0.98 倍單條紋方塊 加上 0.91 倍不同條紋的方塊 加上 0.95 倍另一種條紋的方塊

Unsupervised Learning in Python

使用 scikit-learn 的 NMF

  • 採用 fit()transform() 模式
  • 必須指定成分數,例如 NMF(n_components=2)
  • 可用於 NumPy 陣列與 csr_matrix
Unsupervised Learning in Python

詞頻陣列範例

  • 詞頻陣列,4 個詞,多份文件
  • 用「tf-idf」衡量每份文件中的詞出現情形
    • 「tf」=詞在文件中的出現頻率
    • 「idf」降低常見詞的影響

詞頻陣列

Unsupervised Learning in Python

NMF 使用範例

  • samples 是詞頻陣列
from sklearn.decomposition import NMF

model = NMF(n_components=2)
model.fit(samples)
NMF(n_components=2)
nmf_features = model.transform(samples)
Unsupervised Learning in Python

NMF 成分

  • NMF 具有成分(components)
  • …就像 PCA 有主成分
  • 成分的維度=樣本的維度
  • 各項皆為非負
print(model.components_)
[[ 0.01  0.    2.13  0.54]
 [ 0.99  1.47  0.    0.5 ]]
Unsupervised Learning in Python

NMF 特徵

  • NMF 特徵值為非負
  • 可用來重建樣本
  • …以特徵值結合成分
print(nmf_features)
[[ 0.    0.2 ]
 [ 0.19  0.  ]
  ...
 [ 0.15  0.12]]
Unsupervised Learning in Python

樣本重建

print(samples[i,:])
[ 0.12  0.18  0.32  0.14]
print(nmf_features[i,:])
[ 0.15  0.12]

將 NMF 成分乘以特徵值後相加

Unsupervised Learning in Python

樣本重建方式

  • 以成分乘上特徵值後相加
  • 也可表為矩陣相乘
  • 這就是「NMF」中的「Matrix Factorization」
Unsupervised Learning in Python

NMF 僅適用非負資料

  • 各文件的詞頻
  • 以陣列編碼的影像
  • 聲音頻譜圖
  • 電商網站的購買紀錄
  • …還有更多!
Unsupervised Learning in Python

一起來練習吧!

Unsupervised Learning in Python

Preparing Video For Download...