scikit-learn有稀疏矩阵的NMF实现.但是,您将需要GitHub的最新版本,因为所有已发布的版本(最多包括0.14)都存在可伸缩性问题.随后是一个演示.
加载一些数据:二十个新闻组文本语料库.
>>> from sklearn.datasets import fetch_20newsgroups
>>> from sklearn.decomposition import NMF
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> from sklearn.preprocessing import normalize
>>> data = fetch_20newsgroups().data
>>> X = CountVectorizer(dtype=float).fit_transform(data)
>>> X = normalize(X)
>>> X
<11314x130107 sparse matrix of type '<type 'numpy.float64'>'
with 1787565 stored elements in Compressed Sparse Column format>
Run Code Online (Sandbox Code Playgroud)
现在适合具有10个组件的NMF模型.
>>> nmf = NMF(n_components=10, tol=.01)
>>> Xnmf = nmf.fit_transform(X)
Run Code Online (Sandbox Code Playgroud)
我调整了公差选项,以便在几秒钟内实现这种收敛.使用默认容差,需要更长的时间.此问题的内存使用量约为360MB.
免责声明:我是此实施的贡献者,因此这不是无偏见的建议.