本文介绍了如何转换"SciPy稀疏矩阵"?到"NumPy矩阵"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个名为"incidence_matrix(G)"的python函数,该函数返回图的入射矩阵.它来自Networkx软件包.我面临的问题是此函数的返回类型为"Scipy稀疏矩阵".我需要以numpy矩阵或数组的格式获取事件矩阵.我想知道是否有任何简便的方法?还是有任何内置函数可以为我做这种转换?
I am using a python function called "incidence_matrix(G)", which returns the incident matrix of graph. It is from Networkx package. The problem that I am facing is the return type of this function is "Scipy Sparse Matrix". I need to have the Incident matrix in the format of numpy matrix or array. I was wondering if there is any easy way of doing that or not? Or is there any built-in function that can do this transformation for me or not?
谢谢
推荐答案
scipy.sparse.*_matrix
有几种有用的方法,例如,如果a
是例如scipy.sparse.csr_matrix
:
-
a.toarray()
或a.A
-返回此矩阵的密集ndarray表示形式. (numpy.array
,推荐) -
a.todense()
或a.M
-返回此矩阵的密集矩阵表示形式. (numpy.matrix
)
a.toarray()
ora.A
- Return a dense ndarray representation of this matrix. (numpy.array
, recommended)a.todense()
ora.M
- Return a dense matrix representation of this matrix. (numpy.matrix
)
这篇关于如何转换"SciPy稀疏矩阵"?到"NumPy矩阵"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!