python - 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
我错过了什么?
解决方案
推荐阅读
- python - Django makemigrations 找不到表
- python - 保存/加载具有大常数张量的 Tensorflow 模型的最佳方法?
- linux - 在使用 eclipse 进行远程调试 arm 期间找不到 glibc 源代码
- swift - 使用复选框按钮 RxCocoa/RxSwift 验证表单
- c - 如何编写与 null 语言匹配的弹性模式?
- azure-data-factory - 如何限制使用 Azure 数据工厂传输的字符数?
- python - 在一张图中绘制 2 个数据框
- optaplanner - Optaplanner 约束流加入 List 属性
- python - 在 Python3 中基于共享字典值创建分组列表
- node.js - 带脚手架的 NodeJS:如何设置开发环境?