Python 在 NumPy 中重塑数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14476415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:36:23  来源:igfitidea点击:

Reshape an array in NumPy

pythonarraysnumpyreshape

提问by user1876864

Consider an array of the following form (just an example):

考虑以下形式的数组(只是一个例子):

[[ 0  1]
 [ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]
 [12 13]
 [14 15]
 [16 17]]

It's shape is [9,2]. Now I want to transform the array so that each column becomes a shape [3,3], like this:

它的形状是 [9,2]。现在我想转换数组,使每一列变成一个形状 [3,3],如下所示:

[[ 0  6 12]
 [ 2  8 14]
 [ 4 10 16]]
[[ 1  7 13]
 [ 3  9 15]
 [ 5 11 17]]

The most obvious (and surely "non-pythonic") solution is to initialise an array of zeroes with the proper dimension and run two for-loops where it will be filled with data. I'm interested in a solution that is language-conform...

最明显的(当然也是“非 Pythonic”)解决方案是用适当的维度初始化一个零数组,并运行两个 for 循环,其中将填充数据。我对符合语言的解决方案感兴趣......

采纳答案by eumiro

a = np.arange(18).reshape(9,2)
b = a.reshape(3,3,2).swapaxes(0,2)

# a: 
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15],
       [16, 17]])


# b:
array([[[ 0,  6, 12],
        [ 2,  8, 14],
        [ 4, 10, 16]],

       [[ 1,  7, 13],
        [ 3,  9, 15],
        [ 5, 11, 17]]])

回答by Or_K

numpy has a great tool for this task ("numpy.reshape") link to reshape documentation

numpy 有一个很好的工具来完成这个任务(“numpy.reshape”)链接到重塑文档

a = [[ 0  1]
 [ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]
 [12 13]
 [14 15]
 [16 17]]

`numpy.reshape(a,(3,3))`

you can also use the "-1" trick

你也可以使用“-1”技巧

`a = a.reshape(-1,3)`

the "-1" is a wild card that will let the numpy algorithm decide on the number to input when the second dimension is 3

“-1”是一个通配符,当第二维为 3 时,它将让 numpy 算法决定要输入的数字

so yes.. this would also work: a = a.reshape(3,-1)

所以是的..这也可以: a = a.reshape(3,-1)

and this: a = a.reshape(-1,2)would do nothing

而这: a = a.reshape(-1,2)什么都不做

and this: a = a.reshape(-1,9)would change the shape to (2,9)

这: a = a.reshape(-1,9)将形状更改为 (2,9)

回答by Alleo

There are two possible result rearrangements (following example by @eumiro). Einopspackage provides a powerful notation to describe such operations non-ambigously

有两种可能的结果重新排列(以下示例来自@eumiro)。Einops包提供了一种强大的符号来明确地描述此类操作

>> a = np.arange(18).reshape(9,2)

# this version corresponds to eumiro's answer
>> einops.rearrange(a, '(x y) z -> z y x', x=3)

array([[[ 0,  6, 12],
        [ 2,  8, 14],
        [ 4, 10, 16]],

       [[ 1,  7, 13],
        [ 3,  9, 15],
        [ 5, 11, 17]]])

# this has the same shape, but order of elements is different (note that each paer was trasnposed)
>> einops.rearrange(a, '(x y) z -> z x y', x=3)

array([[[ 0,  2,  4],
        [ 6,  8, 10],
        [12, 14, 16]],

       [[ 1,  3,  5],
        [ 7,  9, 11],
        [13, 15, 17]]])