简体   繁体   English

将2D阵列存储在更大的3D阵列中

[英]Storing 2D array inside a larger 3D array

I'm trying to store multiple 2D arrays (STAR) inside of a larger 3D array (called STACK) that can continue to grow in size. 我试图将多个2D数组(STAR)存储在可以继续增长的更大3D数组(称为STACK)中。 I need STACK to be a global parameter that can be accessed by multiple functions at any time. 我需要STACK是可以随时通过多个函数访问的全局参数。 To do this I tried using numpy.dstack() So far this is what my code looks like: 为此,我尝试使用numpy.dstack()到目前为止,这是我的代码如下所示:

box_size = np.shape(star)[0]
# The 2D array I'm trying to add to the STACK
STAR = [[1, 1, 1, 1, 1],\
        [1, 1, 2, 1, 1],\
        [1, 2, 3, 2, 1],\
        [1, 1, 2, 1, 1],\
        [1, 1, 1, 1, 1,]]
# Initialize STACK to be an empty array the same size as STAR
STACK = np.zeros((2*(box_size/2)+1,2*(box_size/2)+1))

# A function that appends STAR to STACK
def add_Star(STAR):
    np.dstack((STACK,STAR))

# Call the function
add_Star(STAR)

However when I try to print the updated STACK, I get 但是,当我尝试打印更新的堆栈时,我得到

[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]

I don't know if I'm missing something obvious or completely misusing the function. 我不知道我是否缺少明显的东西或完全滥用了该功能。

To be clear, I'd like STACK to be a memory of every STAR I add to it. 明确地说,我希望STACK是我添加到它的每个STAR的记忆。 That is, I'd like each STAR to be accessible in the STACK if I need, to say, remove it later. 也就是说,如果希望稍后再将其删除,我希望可以在堆栈中访问每个STAR。 In practice every STAR is going to be different, so simply adding STACK to STAR isn't going to work. 实际上,每个STAR都将有所不同,因此仅将STACK添加到STAR是行不通的。

There seem to be a few confusions here. 这里似乎有些混乱。

First, the following code should do what you want it to, with as few changes as possible: 首先,以下代码应做您想要的事,并尽可能减少更改:

# The 2D array I'm trying to add to the STACK
STAR = [[1, 1, 1, 1, 1],\
        [1, 1, 2, 1, 1],\
        [1, 2, 3, 2, 1],\
        [1, 1, 2, 1, 1],\
        [1, 1, 1, 1, 1,]]
STAR = np.asarray(STAR)


# Initialize STACK to be an empty array the same size as STAR
STACK = np.zeros(STAR.shape, np.float_)

# A function that appends STAR to STACK
def add_Star(star):
    out = np.dstack((STACK,star))

    return out

# Call the function
STACK = add_Star(STAR)

Now let's break down why. 现在让我们解释一下原因。 Most importantly, the variables in your function eg the star in 最重要的是,在你的函数中的变量,例如在star

def add_Star(star) def add_Star(star)

don't have to be the same names as variables elsewhere in your code (and in fact shouldn't, because it's confusing). 不必与代码中其他位置的变量具有相同的名称(并且实际上不应该如此,因为它令人困惑)。 Only in the function call 仅在函数调用中

STACK = add_Star(STAR) 堆栈= add_Star(STAR)

do you need to feed the function some variable defined elsewhere. 您是否需要向函数提供一些在其他地方定义的变量。

You'll notice that I also added return to the function, because I interpreted your question as wanting to be able to run add_Star repeatedly, and each time to output an expanded STACK. 您会注意到,我还向函数添加了return,因为我将您的问题解释为希望能够重复运行add_Star,并且每次都输出扩展的STACK。

Also, an easy way to see the dimensions of any given array is 另外,查看任何给定数组的尺寸的一种简单方法是

array.shape 数组形状

You'll see I used that to define the shape of STACK, rather than going through the extra step of defining a box_size. 您会看到我用它来定义堆栈的形状,而不是经过定义box_size的额外步骤。

Finally, STAR as you had it defined was not in array form. 最后,您定义的STAR不是数组形式。 When using numpy, it's easy to just use np.asarray to get a list like you had into array format. 当使用numpy时,很容易使用np.asarray来获得一个像数组格式一样的列表。

You could do this using lists by using the append function: 您可以通过使用append函数使用列表来执行此操作:

STAR_1 = [[1, 1, 1, 1, 1],\
         [1, 1, 2, 1, 1],\
         [1, 2, 3, 2, 1],\
         [1, 1, 2, 1, 1],\
         [1, 1, 1, 1, 1,]]

STACK = [STAR_1]

STAR_2 = [[2, 2, 2, 2, 2],\
         [1, 1, 2, 1, 1],\
         [1, 2, 3, 2, 1],\
         [1, 1, 2, 1, 1],\
         [1, 1, 1, 1, 1,]]

STACK.append(STAR_2)
print(STACK)

This will print: 这将打印:

[[[1, 1, 1, 1, 1],
  [1, 1, 2, 1, 1],
  [1, 2, 3, 2, 1],
  [1, 1, 2, 1, 1],
  [1, 1, 1, 1, 1]],
 [[2, 2, 2, 2, 2],
  [1, 1, 2, 1, 1],
  [1, 2, 3, 2, 1],
  [1, 1, 2, 1, 1],
  [1, 1, 1, 1, 1]]]

Let's streamline your action a bit, to better understand what is going on 让我们简化一下操作,以更好地了解发生了什么

In [125]: n=5

Make star an array, a 5x5 one using your values 使用值将star排列成一个5x5的阵列

In [126]: star=np.array([[1, 1, 1, 1, 1],\  # don't need the \
        [1, 1, 2, 1, 1],\
        [1, 2, 3, 2, 1],\
        [1, 1, 2, 1, 1],\
        [1, 1, 1, 1, 1,]])
In [127]: star
Out[127]: 
array([[1, 1, 1, 1, 1],
       [1, 1, 2, 1, 1],
       [1, 2, 3, 2, 1],
       [1, 1, 2, 1, 1],
       [1, 1, 1, 1, 1]])

initial stack to zeros of the same size. 初始stack为相同大小的零。 This is not the same as an 'empty' array. 这与“空”数组不同。

In [128]: stack=np.zeros((5,5))

In [130]: newstack=np.dstack((stack,star))
In [131]: newstack.shape
Out[131]: (5, 5, 2)

dstack is not like the list append. dstack不像列表追加。 It makes a new array. 它产生一个新的数组。 Note the shape - 3d. 注意形状-3d。

In [132]: newstack
Out[132]: 
array([[[ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.]],
....

Note, it has the zeros plus the star values. 注意,它具有零加星号。

In [133]: newstack=np.dstack((newstack,star))
In [134]: newstack.shape
Out[134]: (5, 5, 3)

Do you really want to do this? 您真的要这样做吗?

Here's a better way of building a 3d array incrementally: 这是一种增量构建3d数组的更好方法:

In [135]: alist=[]
In [136]: alist.append(star)
In [137]: alist.append(star)
In [138]: alist
Out[138]: 
[array([[1, 1, 1, 1, 1],
        [1, 1, 2, 1, 1],
        [1, 2, 3, 2, 1],
        [1, 1, 2, 1, 1],
        [1, 1, 1, 1, 1]]), array([[1, 1, 1, 1, 1],
        [1, 1, 2, 1, 1],
        [1, 2, 3, 2, 1],
        [1, 1, 2, 1, 1],
        [1, 1, 1, 1, 1]])]
In [139]: np.array(alist)
Out[139]: 
array([[[1, 1, 1, 1, 1],
        [1, 1, 2, 1, 1],
        [1, 2, 3, 2, 1],
        [1, 1, 2, 1, 1],
        [1, 1, 1, 1, 1]],

...)
In [140]: _.shape
Out[140]: (2, 5, 5)

Note that np.array joins the components along a new axis, at the front. 请注意, np.array在前面沿新轴连接组件。 This is the better place for doing that in numpy . 这是在numpy执行此操作的更好位置。

You mention removing a 'star' later; 您提到以后要删除“星号”; that will be easier if you leave it in the list form. 如果将其保留在列表中,将更加容易。 The 3d array form is useful if you need to do calculations across 'star', but otherwise might not be needed. 如果您需要跨“星号”进行计算,则3d数组形式很有用,但否则可能不需要。


Or if you know how many star you will have, you can initial the stack to the right size, and assign values: 或者,如果您知道将要拥有多少star ,则可以将stack初始化为正确的大小,然后分配值:

In [146]: stack=np.zeros((2,5,5),dtype=int)
In [147]: stack[0,:,:] = star
In [148]: stack[1,:,:] = star*2
In [149]: stack
Out[149]: 
array([[[1, 1, 1, 1, 1],
        [1, 1, 2, 1, 1],
        [1, 2, 3, 2, 1],
        [1, 1, 2, 1, 1],
        [1, 1, 1, 1, 1]],

       [[2, 2, 2, 2, 2],
        [2, 2, 4, 2, 2],
        [2, 4, 6, 4, 2],
        [2, 2, 4, 2, 2],
        [2, 2, 2, 2, 2]]])

This too is better than repeatedly using dstack (or some other concatenation). 这也比重复使用dstack (或其他串联)更好。

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

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