如何拆分2D阵列,从而从“行到行"创建阵列.价值观 [英] How to split an 2D array, creating arrays from "row to row" values

查看:93
本文介绍了如何拆分2D阵列,从而从“行到行"创建阵列.价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以这种方式拆分2D数组:

I want to split an 2D array this way:

示例.

从此4x4 2D阵列:

From this 4x4 2D array:

np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])

创建这四个2x2 2D阵列:

Create these four 2x2 2D arrays:

np.array([[1,2],[3,4]])
np.array([[5,6],[7,8]])
np.array([[9,10],[11,12]])
np.array([[13,14],[15,16]])

通常,从NxN 2D数组(正方形数组)中创建尽可能多的KxK形状的2D数组.

In a general case, from a NxN 2D array (square arrays) create 2D arrays of KxK shape, as many as possible.

更准确地说:创建输出数组,不一定要由该行中的所有值组成.

Just to be more precise: to create the output array, not necessarily it will be made of all values from the row.

示例:

在2D 8x8数组中,值从1到64,如果我想将此数组拆分为2D 2x2数组,则8x8数组的第一行是1到8的行,并且第一个输出2D 2x2数组将是np.array([[1,2 ,, [3,4]]),第二个输出2D 2x2数组将是np.array([[5,6],[7,8]])...它一直持续到最后一个输出2D数组,即np.array([[61,62],[63,64]]).看看每个2D 2x2数组都没有用行(CORRECT)中的所有值填充.

From a 2D 8x8 array, with values from 1 to 64, if I want to split this array in 2D 2x2 arrays, the first row from 8x8 array is a row from 1 to 8, and the first output 2D 2x2 array will be np.array([[1,2],[3,4]]), and the second output 2D 2x2 array will be np.array([[5,6],[7,8]])... It continues until the last output 2D array, that will be np.array([[61,62],[63,64]]). Look that each 2D 2x2 array was not filled with all the values from the row (CORRECT).

有一个Numpy方法可以做到这一点吗?

There is a Numpy method that do this?

推荐答案

要获得所需的输出,您需要将其重塑为3D数组,然后解压缩第一个维度:

To get your desired output, you need to reshape to a 3D array and then unpack the first dimension:

>>> inp = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
>>> list(inp.reshape(-1, 2, 2))
[array([[1, 2],
        [3, 4]]), 
 array([[5, 6],
        [7, 8]]), 
 array([[ 9, 10],
        [11, 12]]), 
 array([[13, 14],
        [15, 16]])]

如果要将数组存储在不同的变量中,而不是在一个数组列表中,也可以使用=解压缩:

You can also unpack using = if you want to store the arrays in different variables instead of in one list of arrays:

>>> out1, out2, out3, out4 = inp.reshape(-1, 2, 2)
>>> out1
array([[1, 2],
       [3, 4]])

如果您对包含2D 2x2数组的3D数组没问题,则无需拆包或list()调用:

If you're okay with a 3D array containing your 2D 2x2 arrays you don't need unpacking or the list() call:

>>> inp.reshape(-1, 2, 2)
array([[[ 1,  2],
        [ 3,  4]],

       [[ 5,  6],
        [ 7,  8]],

       [[ 9, 10],
        [11, 12]],

       [[13, 14],
        [15, 16]]])

-1reshape的特殊值.正如文档所述:

The -1 is a special value for reshape. As the documentation states:

一个形状尺寸可以为-1.在这种情况下,该值是根据数组的长度和其余维来推断的.

One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.


如果您希望它更通用,只需取行长的平方根,然后将其用作reshape的参数即可:


If you want it more general, just take the square root of the row-length and use that as argument for reshape:

>>> inp = np.ones((8, 8))  # 8x8 array
>>> square_shape = 2
>>> inp.reshape(-1, square_shape, square_shape)  # 16 2x2 arrays

>>> square_shape = 4
>>> inp.reshape(-1, square_shape, square_shape)  # 4 4x4 arrays

这篇关于如何拆分2D阵列,从而从“行到行"创建阵列.价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆