Numpy提供了4种方法对数组对象进行转置。它们是rollaxis(),swapaxes(),transpose(), ndarray**.T**。本文将向你展示一些如何转置Numpy数组的例子。
1.numpy.rollaxis()
-
当输入的数组是一个多维数组时,那么你可以使用这个方法将指定的数组轴移动到指定的位置。
-
下面是该方法的格式:
numpy.rollaxis(arr, axis, start) -
arr:传入的多维Numpy数组。
-
axis:要移动的多维数组轴的索引。其他轴的相对位置不改变。
-
start:移动轴的新索引。
-
但是从Numpy 1.11.0版本开始,你应该使用moveaxis(array, source, destination)方法来代替rollaxis()方法。
-
下面是例子的源代码,你可以看到注释中的详细解释。
import numpy as np def transpose_numpy_array_rollaxis(): # create the original 3 dimensional array that has 5 rows (axis 0), 2 columns(axis 1), and each element is an array that has 3 values(axis 2). # there are 3 axis in the array axis_0 - 5 rows, axis_1 - 2 columns, axis_2 - 3 values. array = np.arange(30).reshape(5,2,3) print('********** original array **********\r') print(array) print('\r array.ndim = ', array.ndim) # move the third axis (axis_2) to the second axis ( axis_1), that means swap the 2 axis. array_rolled_1 = np.rollaxis(array, axis = 2, start = 1) print('\n********** after roll the array\'s third axis to the second axis **********\r') # after the above swap, the array has 5 rows (axis 0), 3 columns ( axis 1), each element array has 2 values(axis 2). print(array_rolled_1) print('\r array_rolled_1.ndim = ', array_rolled_1.ndim) # move the third axis (axis_2) to the first axis ( axis_0), that means swap the 2 axis. array_rolled_2 = np.rollaxis(array, axis = 2, start = 0) print('\n********** after roll the array\'s third axis to the first axis **********\r') # after the above swap, the array has 3 rows (axis 0), 5 columns ( axis 1), each element array has 2 values(axis 2). print(array_rolled_2) print('\r array_rolled_2.ndim = ', array_rolled_2.ndim) array1 = np.arange(10).reshape(5,2) print(array1) print('\n original array = ', array1) # move the axis 1 to the first axis that means swap the axis 1 and axis 0. array1_rolled = np.moveaxis(array1, source = 1, destination = 0) print('\n np.moveaxis(array1, source = 1, destination = 0) = ', array1_rolled) if __name__ == '__main__': numpy_ndarray_ravel_example() -
下面是上述例子的源代码执行结果:
********** original array ********** [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]] [[12 13 14] [15 16 17]] [[18 19 20] [21 22 23]] [[24 25 26] [27 28 29]]] array.ndim = 3 ********** after roll the array's third axis to the second axis ********** [[[ 0 3] [ 1 4] [ 2 5]] [[ 6 9] [ 7 10] [ 8 11]] [[12 15] [13 16] [14 17]] [[18 21] [19 22] [20 23]] [[24 27] [25 28] [26 29]]] array_rolled_1.ndim = 3 ********** after roll the array's third axis to the first axis ********** [[[ 0 3] [ 6 9] [12 15] [18 21] [24 27]] [[ 1 4] [ 7 10] [13 16] [19 22] [25 28]] [[ 2 5] [ 8 11] [14 17] [20 23] [26 29]]] array_rolled_2.ndim = 3 [[0 1] [2 3] [4 5] [6 7] [8 9]] original array = [[0 1] [2 3] [4 5] [6 7] [8 9]] np.moveaxis(array1, source = 1, destination = 0) = [[0 2 4 6 8] [1 3 5 7 9]]
2.numpy.swapaxes()
-
该方法用于交换一个数组的两个轴。
-
该方法的语法如下:
numpy.swapaxes(arr, axis1, axis2) -
下面是一个例子:
import numpy as np def transpose_numpy_array_swapaxes(): # create the original 2 dimensional array. array = np.arange(10).reshape(5,2) print('\n source array = ', array) # swap the 2 axis of the 2 dimensional array. array_1 = np.swapaxes(array,1,0) print('\n np.swapaxes(array,1,0) = ', array_1) if __name__ == '__main__': transpose_numpy_array_swapaxes() -
下面是例子的执行输出:
source array = [[0 1] [2 3] [4 5] [6 7] [8 9]] np.swapaxes(array,1,0) = [[0 2 4 6 8] [1 3 5 7 9]]
3.Numpy.transpose()
-
Numpy.transpose()方法用于交换多维数组的维数。
-
下面是Numpy.transpose()方法的语法。
numpy.transpose(arr, axes) -
arr:要操作的数组。
-
axes:可选参数,元组或整数列表,它将根据该参数对数组进行转置。
-
下面是例子的源代码。
import numpy as np def transpose_numpy_array_transpose(): # create the original 2 dimensional array. array = np.arange(10).reshape(5,2) print('\n source array = ', array) # swap the 2 axis of the 2 dimensional array. array_1 = np.transpose(array) print('\n np.transpose() = ', array_1) if __name__ == '__main__': transpose_numpy_array_transpose() -
下面是上述例子的执行输出:
source array = [[0 1] [2 3] [4 5] [6 7] [8 9]] np.transpose() = [[0 2 4 6 8] [1 3 5 7 9]]
4. ndarray.T.
-
这个属性类似于numpy.transpose()方法。
import numpy as np def transpose_numpy_array_t(): # create the original 2 dimensional array. array = np.arange(10).reshape(5,2) print('\n source array = ', array) # swap the 2 axis of the 2 dimensional array. array_1 = array.T print('\n array.T = ', array_1) if __name__ == '__main__': transpose_numpy_array_t() -
下面是上述例子的执行输出。
source array = [[0 1] [2 3] [4 5] [6 7] [8 9]] array.T = [[0 2 4 6 8] [1 3 5 7 9]]