如何用邻接矩阵处理内存错误?

问题描述 投票:1回答:1

我在用python做图聚类。该算法要求从图传来的数据必须是 G 应该是邻接矩阵。然而,为了得到 adjacency-matrix 作为 numpy-array 像这样。

import networkx as nx
matrix = nx.to_numpy_matrix(G)

我得到一个内存错误。该信息是 MemoryError: Unable to allocate 2.70 TiB for an array with shape (609627, 609627) and data type float64

然而,我的设备是新的(联想E490),windows 64位,内存8Gb。

其他重要信息可以是。

Number of nodes: 609627
Number of edges: 915549

整个故事如下:

Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)

Markov Clustering

import markov_clustering as mc
import networkx as nx

matrix = nx.to_scipy_sparse_matrix(G) # build the matrix
result = mc.run_mcl(matrix)            # run MCL with default parameters

MemoryError

enter image description here

python pandas numpy cluster-analysis networkx
1个回答
2
投票

你要创建的矩阵大小为 609627x609627 的float64。每一个float64使用8个字节的内存,你将需要 609627*609627*8~3TB 内存。好吧,你的系统只有8GB,即使加上物理内存,3TB似乎也太大了,无法操作。假设你的节点id是整数,你可以用 dtype=unit4(以说明所有 609627 节点),但它仍然需要超过TB的内存,这听起来是不可访问的。你想做的是什么,似乎你有一个稀疏的矩阵,你可能有另一种可能的方法来实现你的目标。邻接矩阵(除非压缩)似乎很难实现。

也许你可以受益于这样的东西。

to_scipy_sparse_matrix(G, nodelist=None, dtype=None, weight='weight', format='csr')

在... networks 包,或者说使用edgelist来计算你想达到的任何目的。或者说使用edgelist来计算你想达到的任何目的。

© www.soinside.com 2019 - 2024. All rights reserved.