NumPy Манипуляции с массивами: функция moveaxis ()
функция numpy.moveaxis ()
Функция moveaxis () используется для перемещения осей массива на новые позиции.
Другие оси остаются в исходном порядке.
Синтаксис:
numpy.moveaxis (a, источник, пункт назначения)
Версия: 1.15.0
Параметр:
название | Описание | Необходимые / Необязательный |
---|---|---|
Массив, оси которого должны быть переупорядочены. | необходимые | |
источник | Исходные позиции осей для перемещения. Они должны быть уникальными. | необходимые |
место назначения | Позиции назначения для каждой из исходных осей. Они также должны быть уникальными. | необходимые |
Возвращаемое значение:
result [np.ndarray] Массив с перемещенными осями. Этот массив является представлением входного массива
Пример-1: numpy.moveaxis ()
>> import numpy as np
>>> y = np.zeros((2,3,4))
>>> print("Initial Shape:",y.shape)
Initial Shape: (2, 3, 4)
>>> print(y)
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
>>> z = np.moveaxis(y, 0, -2)
>>> print("\nNew Shape:",z.shape)
New Shape: (3, 2, 4)
>>> print(z)
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
Иллюстрированная презентация:
Пример 2: numpy.moveaxis ()
>> import numpy as np
>>> y = np.zeros((2,3,4))
>>> print("Initial Shape:",y.shape)
Initial Shape: (2, 3, 4)
>>> print(y)
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
>>> z = np.moveaxis(y, -1, 0)
>>> print("\nNew Shape:",z.shape)
New Shape: (4, 2, 3)
>>> print(z)
[[[ 0. 0. 0.]
[ 0. 0. 0.]]
[[ 0. 0. 0.]
[ 0. 0. 0.]]
[[ 0. 0. 0.]
[ 0. 0. 0.]]
[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0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]]
Python - NumPy Code Editor:
Предыдущая: ndarray.flatten ()
Далее: rollaxis ()
Новый контент: Composer: менеджер зависимостей для PHP , R программирования
disqus2code