Index order in a numpy multidimensional array

For example, let's say that I mimic a bunch of particles doing something with time, and I have a multidimensional array with a name particleswith these indices:

  • Coordinates x / y / z particles (length a, which 3for three-dimensional space)
  • Single particle index (length b)
  • Time step index (length c)

Is it better to build an array such that particles.shape == (a, b, c)or particles.shape == (c, b, a)?

I'm more interested in the convention than efficiency: Numpy arrays can be configured either in the C-style (the last index changes most quickly) or in the Fortran style (first index), so it can effectively support any tuning. I also understand what I can use transposeto place indexes in any order I need, but I would like to minimize this.

I started researching this myself and found support for both ways:

Pro- (c, b, a):

  • By default, Numpy uses C-style arrays, where the last index is the fastest.
  • Most vector algebra functions ( inner, crossetc.) act on the last index. ( dotacts on the last of the one and the second to the last of the other.)
  • matplotlib (LineCollection, PolyCollection) .

Pro- (, , ):

  • meshgrid mgrid , . , np.mgrid[0:5,0:5,0:5].shape == (3,5,5,5). , , .
  • matplotlib scatter plot , , ax.plot3d(particles[0], particles[1], particles[2]) , particles[..., 0]

, (, - C Fortran), , Numpy, , .

+4
2

- , , . , , , , , :

, , , ? , :

# a first
for dimension in particles:
    ...

# b first
for particle in particles:
    ...

# c first
for timestep in particles:
    ...

, C-, . python numpy C- . ( , "" .)

, , numpy . , , . /.

, , .

+4

, numpy , . x[None,...]

np.array([x,y,z])   # produces a (3,...) array

np.ones((3,2)) + np.ones((1,2,10)) # error
np.ones((3,2,1)) + np.ones((2,10))  # (3,2,10)

, x/y/z.

np.dot last/2nd to last, np.tensordot np.einsum .


Apocheir , newaxis ,

 x / np.linalg.norm(x,axis=0)   # automatic newaxis at beginning
 x / np.linalg.norm(x,axis=-1)[...,np.newaxis]  # explicit newaxis

x, newaxis . x . , , - ( order='C').

keepdims (, sum, mean).

+2

Source: https://habr.com/ru/post/1568641/


All Articles