csr_matrix矩陣用法小節


from scipy.sparse import *
 
row =  [0,0,0,1,1,1,2,2,2]#行指標
col =  [0,1,2,0,1,2,0,1,2]#列指標
data = [1,0,1,0,1,1,1,1,0]#在行指標列指標下的數字
team = csr_matrix((data,(row,col)),shape=(3,3))
print(team)
print(team.todense())  # 返回稀疏矩陣的np.matrix形式
 
 
輸出結果:
  (0, 0)    1
  (0, 1)    0
  (0, 2)    1
  (1, 0)    0
  (1, 1)    1
  (1, 2)    1
  (2, 0)    1
  (2, 1)    1
  (2, 2)    0
[[1 0 1]
 [0 1 1]
 [1 1 0]]
 
Process finished with exit code 0

 

row =  [0,0,0,0,1,1,1,1,2,2,2,2]#行指標
col =  [0,1,2,3,0,1,2,3,0,1,2,3]#列指標
data = [1,0,1,1,0,1,1,1,1,0,1,1]#在行指標列指標下的數字
team = csr_matrix((data,(row,col)),shape=(3,4))
# print(team)
# print(team.todense())
team_dok = team.todok()  # 返回稀疏矩陣的dok_matrix形式
# print(team_dok)
 
team_coo = team_dok.tocoo()  # 返回稀疏矩陣的coo_matrix形式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM