我想在保留(2, *(x, y))
的值的同时将形状为(1, *(x,y), 2)
的数组重塑为(x, y)
吗?
(2, *(x,y))
,其中2代表游戏屏幕的帧,其中(x, y)
是具有像素值的数组。我希望将其转换为形状为(1, *(x, y), 2)
的数组,以便数字2仍代表帧索引,而保留(x,y)
数组的值。 1将用于索引批次以训练神经网络。
numpy.reshape(1, *(x,y), 2)
不保留(x,y)
数组。
答案 0 :(得分:0)
使用numpy.transpose()
,例如:
import numpy as np
arr = np.arange(2 * 3 * 4).reshape((2, 3, 4))
arr.shape
# (2, 3, 4)
arr.transpose(1, 2, 0).shape
# (3, 4, 2)
new_arr = arr.transpose(1, 2, 0)[None, ...]
new_arr.shape
# (1, 3, 4, 2)
# the `(3, 4)` array is preserved:
arr.transpose(1, 2, 0)[:, :, 0]
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])
arr[0, :, :]
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11]])