I've been experimenting with vectorizing a function of mine and have hit a strange bug in my code that I haven't been able to figure out.
Here's the numpy array in question (https://filebin.net/c14dcklwakrv1hw8)
import numpy as np
example = np.load("example_array.npy") #Shape (2, 5, 5)
The problem I was trying to solve was to normalize the values in each row such that they summed to 1, except of course rows that are entirely 0's. Since numpy divide has an option to skip 0's when dividing the function I used was
f = lambda x: np.divide(x, np.sum(x, axis=1)[:, np.newaxis], where=np.sum(x, axis=1)[:, np.newaxis]!=0)
What happens however is that the value of f(example[1])
changes depending if example[1]
or example[0]
is run in the python terminal before it. So if you run example[0]
then do f(example[1])
the last row of example[0]
replaces the first row of the answer.
The execution of it can seen here https://i.stack.imgur.com/mC51R.jpg
Python version - 3.6.6, numpy - 1.15.3
Edit: - Adding 1 to all elements of the matrix and repeating the same operation without the where condition in np.divide works without issue. I guess that's the source of the error but I don't know why it occurs