np.rollaxis
时间: 2023-11-08 17:55:27 浏览: 21
np.rollaxis函数是一个用于对多维数组进行轴滚动操作的函数。它可以将数组的某个轴滚动到指定的位置。函数的参数包括被操作的数组a、需要滚动的轴axis和滚动到的位置start,默认情况下start为0。
举个例子,假设我们有一个形状为(50000, 3072)的数组trainset_x。我们可以使用np.rollaxis函数对其进行操作,将第二个轴(轴索引为1)滚动到第四个轴(轴索引为3)的位置,即将数组从shape为(50000, 3, 32, 32)的形式变为shape为(50000, 32, 32, 3)的形式。
下面是示例代码:
import numpy as np
trainset_x = np.zeros(shape=[50000, 3072])
print('raw trainset_x.shape:', trainset_x.shape)
trainset_x = trainset_x.reshape(-1, 3, 32, 32)
print('after reshape trainset_x.shape:', trainset_x.shape)
trainset_x = np.rollaxis(trainset_x, 1, 4)
print('after rollaxis trainset_x.shape:', trainset_x.shape)
运行上述代码,我们可以看到最后一行输出的结果为after rollaxis trainset_x.shape: (50000, 32, 32, 3),说明数组trainset_x已经完成了轴滚动操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)