I have a vector D of length N and a matrix A of shape N*M. Vector D has some zero elements. I'm doing this operation:
D = D.reshape(-1,1)
A / D
However I'm getting a division by zero error because of some elements in D that are zero. What I need is to put zero when there's a division by zero instead of raising an error. How to do this?
E.g. my try:
A = [ [0,1,0,0,0,0],
[0,0,1,1,0,0],
[1,0,0,1,1,0],
[0,0,0,0,1,0],
[0,0,0,0,0,0],
[0,0,0,0,1,0]
]
A = np.array(A, dtype='float')
D = np.sum(A, axis=1)
D = D.reshape(-1, 1)
A = np.where(D != 0, A / D, 0)
RuntimeWarning: invalid value encountered in divide
A = np.where(D != 0, A / D, 0)