如何转换"SciPy稀疏矩阵"?到"NumPy矩阵"? [英] How do I transform a "SciPy sparse matrix" to a "NumPy matrix"?

查看:596
本文介绍了如何转换"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() or a.A - Return a dense ndarray representation of this matrix. (numpy.array, recommended)
  • a.todense() or a.M - Return a dense matrix representation of this matrix. (numpy.matrix)

这篇关于如何转换"SciPy稀疏矩阵"?到"NumPy矩阵"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆