What are the efficient ways to loop over vectors along a specified axis in numpy ndarray?

前端 未结 2 770
广开言路
广开言路 2021-01-14 11:59

I\'m processing data by looping over vectors along an axis (could be any axis) of numpy ndarray (could be of any dimensions).

I didn\'t work on array directly becaus

2条回答
  •  生来不讨喜
    2021-01-14 12:43

    Have you considered numpy.nditer?

    See also Iterating over arrays.

    EDIT: maybe another solution would just be to either use:

    • flatten
    • ravel
    • the flat 1D iterator

    You can thus iterate 1D-like whatever the array's initial dim, and then reshape the array to its original shape.

提交回复
热议问题