For a minimal working example, let’s digitize a 2D array. numpy.digitize requires a 1D array:
import numpy as np
N = 200
A = np.random.random((N, N))
X = np.linspace(0, 1, 20)
print np.digitize(A.ravel(), X).reshape((N, N))
Now the documentation says:
… A copy is made only if needed.
How do I know if the ravel copy it is “needed” in this case? In general – is there a way I can determine if a particular operation creates a copy or a view?
This question is very similar to a question that I asked a while back:
You can check the
baseattribute.However, that’s not perfect. You can also check to see if they share memory using
np.may_share_memory.There’s also the flags attribute that you can check:
But this last one seems a little fishy to me, although I can’t quite put my finger on why…