關於scipy中的一些方法

純粹是因爲在看GCN的源碼,裏面涉及到很多scipy中的方法,對於這些不常用的方法感覺看一遍很快就忘記了,所以彙集在這兒吧。
一、比較 todense() 和 toarray()

import scipy.sparse as sp
a = sp.eye(5)
print(type(a))
print(a)
print("------------------------------------")
b = a.todense() #轉化爲numpy.matrix
c = a.toarray()#轉爲爲numpy.ndarray
print(type(b))
print(b)
print("-----------------------------------")
print(type(c))
print(c)

打印的結果是如下,上述代碼的目的是比較 todense() 和 toarray() 方法

<class 'scipy.sparse.dia.dia_matrix'>
  (0, 0)	1.0
  (1, 1)	1.0
  (2, 2)	1.0
  (3, 3)	1.0
  (4, 4)	1.0
------------------------------------
<class 'numpy.matrix'>
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
-----------------------------------
<class 'numpy.ndarray'>
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章