ndarray1维数组为0的地方改为1

import numpy as np
from scipy.sparse import csr_matrix

row = np.array([0, 0, 1, 2, 2])
col = np.array([0, 2, 2, 0, 2])
data = np.array([1, 2, 3, 4, 6])
sparseA=csr_matrix((data, (row, col)), shape=(3, 3))
print(“sparseA=”)
print(sparseA)
arrA=sparseA.toarray()
print(“arrA=”)
print(arrA)
sumA=sparseA.sum(axis=0)
print(“sumA=”)
print(sumA)
print(type(sumA))
d=sumA.A1
print(“d=”)
print(d)
print(type(d))
d[d == 0] = 1
print(“d=”)
print(d)

运行结果:
sparseA=
(0, 0) 1
(0, 2) 2
(1, 2) 3
(2, 0) 4
(2, 2) 6
arrA=
[[1 0 2]
[0 0 3]
[4 0 6]]
sumA=
[[ 5 0 11]]

d=
[ 5 0 11]

d=
[ 5 1 11]

你可能感兴趣的:(机器学习python,python,机器学习,深度学习)