NMF 學得可解釋的部件

Unsupervised Learning in Python

Benjamin Wilson

Director of Research at lateral.io

範例:NMF 學得可解釋的部件

  • 詞頻陣列(tf-idf)文章
  • 20,000 篇科學文章(列)
  • 800 個單字(欄)

文章的詞頻陣列

Unsupervised Learning in Python

將 NMF 套用到文章

print(articles.shape)
(20000, 800)
from sklearn.decomposition import NMF
nmf = NMF(n_components=10)
nmf.fit(articles)
NMF(n_components=10)
print(nmf.components_.shape)
(10, 800)
Unsupervised Learning in Python

NMF 元件即主題

 

nmf.components_

Unsupervised Learning in Python

NMF 元件即主題

 

選取 nmf.components_ 的一列,並為每個單字繪製長條圖,高度代表 tfidf

Unsupervised Learning in Python

NMF 元件即主題

 

方框內為最高權重單字與數值:species 2.95、plant 1.05、plants 0.78 等

Unsupervised Learning in Python

NMF 元件即主題

 

新的方框:來自 nmf.components_ 不同列的高權重單字:university 3.57、prof 1.19、college 0.88 等

Unsupervised Learning in Python

NMF 元件

  • 對文件:
    • NMF 元件表示主題
    • NMF 特徵將主題組合成文件
  • 對影像:NMF 元件是影像的部件

 

3 條紋的方框 約等於 0.98 × 單一條紋方框 + 0.91 × 另一條紋方框 + 0.95 × 另一條紋方框

Unsupervised Learning in Python

灰階影像

  • 「Grayscale」影像=無色彩,只有灰階
  • 以像素亮度衡量
  • 以 0 到 1 表示(0 為黑)
  • 轉成 2D 陣列

2×3 的像素矩形,黑白灰深淺不同

Unsupervised Learning in Python

灰階影像範例

  • 以陣列表示的 8×8 月球灰階影像

 

像素化月球示意圖,箭頭指向相同維度、值介於 0 到 1 的數字陣列

Unsupervised Learning in Python

將灰階影像攤平成陣列

  • 逐一列舉元素
  • 逐列展開
  • 由左到右、由上到下

2×3 像素方塊指向數值陣列

Unsupervised Learning in Python

將灰階影像攤平成陣列

  • 逐一列舉元素
  • 逐列展開
  • 由左到右、由上到下

2×3 像素方塊 → 2×3 數值陣列 → 1×6 數值陣列

Unsupervised Learning in Python

影像集合的編碼

  • 相同尺寸的影像集合
  • 編碼為 2D 陣列
  • 每列對應一張影像
  • 每欄對應一個像素
  • … 可套用 NMF!

代表 2×3 像素影像的 nmf.components_ 其中一列

Unsupervised Learning in Python

視覺化樣本

print(sample)
[ 0.   1.   0.5  1.   0.   1. ]
bitmap = sample.reshape((2, 3))
print(bitmap)
[[ 0.   1.   0.5]
 [ 1.   0.   1. ]]
from matplotlib import pyplot as plt
plt.imshow(bitmap, cmap='gray', interpolation='nearest')
plt.show()
Unsupervised Learning in Python

一起來練習吧!

Unsupervised Learning in Python

Preparing Video For Download...