According to the documentation, ndarray.flatiterates over the array, and ndarray.ravelreturns a flattened array (when possible). Therefore, my question is: when should we use this or that? Which one would be preferable as rvalue in assignment, for example, in the code below?
import numpy as np
x = np.arange(2).reshape((2,1,1))
y = np.arange(3).reshape((1,3,1))
z = np.arange(5).reshape((1,1,5))
mask = np.random.choice([True, False], size=(2,3,5))
nc4slice = tuple(mask.any(axis=axis) for axis in ((1,2),(2,0),(0,1)))
indices = np.ix_(*nc4slice)
ncrds = 3
npnts = (np.broadcast(*indices)).size
points = np.empty((npnts, ncrds))
for i,crd in enumerate(np.broadcast_arrays(x,y,z)):
points[:,i] = crd[indices].flat
points[:,i] = crd[indices].ravel()
source
share