NMF 学习可解释的部分

Python 中的无监督学习

Benjamin Wilson

Director of Research at lateral.io

示例:NMF 学习可解释的部分

  • 文章词频数组(tf-idf)
  • 2 万篇科学文章(行)
  • 800 个词(列)

文章词频数组

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)
Python 中的无监督学习

NMF 组件即主题

 

nmf.components_

Python 中的无监督学习

NMF 组件即主题

 

选中 nmf.components_ 的一行,每个词有柱状图,柱高为 tfidf

Python 中的无监督学习

NMF 组件即主题

 

包含高频词及数值的框:species 2.95、plant 1.05、plants 0.78 等

Python 中的无监督学习

NMF 组件即主题

 

nmf.components_ 不同行的高频词新框:university 3.57、prof 1.19、college 0.88 等

Python 中的无监督学习

NMF 组件

  • 对文档:
    • NMF 组件表示主题
    • NMF 特征将主题组合成文档
  • 对图像:NMF 组件是图像的部件

 

三条纹方块 ≈ 0.98×单条纹方块 + 0.91×另一条纹方块 + 0.95×另一条纹方块

Python 中的无监督学习

灰度图像

  • "灰度"图像:无颜色,仅灰阶
  • 度量像素亮度
  • 用 0 到 1 表示(0 为黑)
  • 转为二维数组

2×3 像素矩形,黑白灰不同程度

Python 中的无监督学习

灰度图示例

  • 月亮的 8×8 灰度图像,以数组表示

 

像素化月亮图,箭头指向同维度、数值 0 到 1 的数组

Python 中的无监督学习

将灰度图展平成数组

  • 枚举各项
  • 按行
  • 自左至右,自上而下

2×3 像素框,指向数组数字

Python 中的无监督学习

将灰度图展平成数组

  • 枚举各项
  • 按行
  • 自左至右,自上而下

2×3 像素框 → 2×3 数组 → 1×6 数组

Python 中的无监督学习

编码图像集合

  • 同尺寸图像集合
  • 编码为二维数组
  • 每行对应一幅图
  • 每列对应一个像素
  • … 可应用 NMF!

nmf.components_ 的一行表示 2×3 像素图

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()
Python 中的无监督学习

让我们练习!

Python 中的无监督学习

Preparing Video For Download...