简体   繁体   English

如何切割 Numpy 二维数组并存储为 3d 数组?

[英]How to cut a Numpy 2d array and store as a 3d array?

suppose I have the following Numpy 2d array (3*9)假设我有以下 Numpy 二维数组 (3*9)

[[-2 -2 -2 -2 -2 -2 -2 -2 -2]
 [-2 -2 -2 -2 -2 -2 -2 -2 -2]
 [-2 -2 71 -1 -1 -1 71 -1 52]]

I would like to equally divide this whole array by 3 and get the target array like this, which means I should get a 3d array of shape(3 by 3 by3):我想将整个数组除以 3 并得到这样的目标数组,这意味着我应该得到一个形状为 3d 的数组(3 x 3 x3):

[[-2 -2 -2] [-2 -2 -2] [-2 -2 -2]
 [-2 -2 -2] [-2 -2 -2] [-2 -2 -2]
 [-2 -2 71] [-1 -1 -1] [71 -1 52]]

Anyone can explain how to do this?任何人都可以解释如何做到这一点?

If you already know the previous shape and target shape, you can simply use reshape如果你已经知道之前的形状和目标形状,你可以简单地使用reshape

arr = [[-2, -2, -2, -2, -2, -2, -2, -2, -2],
[-2, -2, -2, -2, -2, -2, -2, -2, -2,],
[-2, -2, 71, -1, -1, -1, 71, -1, 52]]
arr = np.array(arr)
arr.reshape(3,3,3)

you can use reshape method like the following您可以使用如下reshape方法

import numpy as np
array = np.array([[-2,-2,-2,-2,-2,-2,-2,-2,-2],
                [-2,-2,-2,-2,-2,-2,-2,-2,-2],
                [-2,-2,71,-1,-1,-1,71,-1,52]])

newarr = array.reshape(3,3,3)
print(newarr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM