【问题标题】:MemoryError: Unable to allocate 7.74 TiB for an array with shape (287318, 3704243) and data type float64MemoryError:无法为形状(287318、3704243)和数据类型 float64 的数组分配 7.74 TiB
【发布时间】:2020-12-02 09:03:06
【问题描述】:

我正在研究一个形状为 (287318, 3704243) 的矩阵 tfidf_matrix,我试图将其重用于以后的计算。这是我的完整代码

tfidf_vectorizer = TfidfVectorizer()                                    
# text shape is (287318,)
tfidf_matrix  = tfidf_vectorizer.fit_transform(text)
X = tfidf_matrix.todense()  # error here

pca_num_components = 2
reduced_data = PCA(n_components=pca_num_components).fit_transform(X)

我正试图通过 PCA 减少 tfidf_matrix 以用于绘图目的,但我在行 X = tfidf_matrix.todense() 说时遇到内存错误问题

MemoryError: Unable to allocate 7.74 TiB for an array with shape (287318, 3704243) and data type float64

请问有什么办法可以解决吗?

【问题讨论】:

  • 这是一个巨大的数据量......你可能必须找到一种方法来处理你的数据块。
  • 谢谢,这确实是唯一的办法

标签: python-3.x out-of-memory tokenize


【解决方案1】:

一种可能的解决方案(尽管并不完美)是随机选择特定数量的行并对其执行 PCA,如下所示。

max_items = np.random.choice(range(tfidf_matrix.shape[0]), size=3000, replace=False)
X=tfidf_matrix[max_items,:].todense()   
pca = PCA(n_components=2).fit_transform(X)

如果需要,我们可以更改size 参数

【讨论】:

    猜你喜欢
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2021-01-04
    • 2020-01-14
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多