首页 > 解决方案 > CSR 稀疏矩阵和截断SVD 拟合 - NLP / 主题建模

问题描述

我正在做一个主题建模项目。在这种情况下,我想在应用 KMeans 之前使用截断的 SVD 降低文档矩阵的维度。

我已经处理(即清理、标记化、词形化)并矢量化了原始文档。我应用了一个 Tf-Idf 矢量化器。作为一个仅供参考,我已经成功地使用 LDA 和 NMF 实现了 Tfidf 矢量化器。

我尝试了两种方法:

第一种方法

docs_raw = wallstreet['processed_text']
n_topics = 20
max_doc_freq = 0.7
min_doc_freq = 4
max_features = 10000
ngram_rng = [1, 2]
max_iterations = 25

docs_vectorized, vectorizer = nlp_topic_utils.tfidf_vectorizer(docs_raw, min_doc_freq, max_doc_freq, max_features, ngram_rng)

tsvd = TruncatedSVD(n_components=docs_vectorized.shape[1]-1)
docs_tsvd = tsvd.fit(docs_vectorized)

这种方法导致了内存错误:

MemoryError: Unable to allocate 3.38 GiB for an array with shape (45372, 10009) and data type float64

另外,我不清楚为什么docs_vectorized有 10,009 列而不是 10,000 列。

然后我尝试使用导致索引错误的 Dask 数组。

第二种方法

import dask.array as da

docs_vectorized, vectorizer = nlp_topic_utils.tfidf_vectorizer(docs_raw, min_doc_freq, max_doc_freq, max_features, ngram_rng)

docs_dask_arr = da.from_array(docs_vectorized, chunks='auto', asarray=True)

tsvd = TruncatedSVD(n_components=docs_dask_arr.shape[1]-1, random_state=random_state)
docs_tsvd = tsvd.fit(docs_dask_arr)

对应的错误:

IndexError: tuple index out of range

我错过了什么?

标签: pythonscikit-learnnlpsparse-matrixdask

解决方案


推荐阅读