scipy: eliminate_zeros()的

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NockinOnHeavensDoor/article/details/83684957

例子:

import numpy as np
from scipy.sparse import csr_matrix
M = csr_matrix(np.ones([2, 2],dtype=np.int32))
print(M)
print(M.data.shape)
for i in range(np.shape(M)[0]):
    for j in range(np.shape(M)[1]):
        if i==j:
            M[i,j] = 0
print(M)
print(M.data.shape)

结果:
(0, 0) 1
(0, 1) 1
(1, 0) 1
(1, 1) 1
(4,)
(0, 0) 0
(0, 1) 1
(1, 0) 1
(1, 1) 0
(4,)

打印M的数据:

print(M.data)
[0 1 1 0]

使用eliminate_zeros()

M.eliminate_zeros()

打印:

print(M)

 (0, 1)	1
 (1, 0)	1

可以看到没有存储0元素

print(M.data)

[1 1]

猜你喜欢

转载自blog.csdn.net/NockinOnHeavensDoor/article/details/83684957