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

时间:2016-03-08 23:41:57

标签: python arrays numpy data-structures

我正在尝试将多个2D数组(STAR)存储在一个可以继续增大的3D阵列(称为STACK)中。我需要STACK作为一个全局参数,可以随时由多个函数访问。为此,我尝试使用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)

然而,当我尝试打印更新的STACK时,我得到了

[[ 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.]]

我不知道我是否遗漏了一些明显或完全滥用该功能的东西。

要清楚,我希望STACK成为我添加的每一个STAR的记忆。也就是说,如果我需要,我希望每个STAR都能在STACK中访问,比如说以后删除它。在实践中,每个STAR都会有所不同,所以简单地将STACK添加到STAR是行不通的。

3 个答案:

答案 0 :(得分:1)

这里似乎有一些混淆。

首先,以下代码应该按照您的意愿执行,并尽可能少地进行更改:

# 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)

现在让我们分解为什么。最重要的是,您的函数中的变量,例如

中的star
  

def add_Star(明星)

不要在代码中的其他地方使用相同的名称(实际上不应该这样,因为它令人困惑)。仅在函数调用中

  

STACK = add_Star(STAR)

你需要为函数提供一些在别处定义的变量吗?

您会注意到我还添加了返回函数,因为我将您的问题解释为希望能够重复运行add_Star,并且每次都输出扩展的STACK。

此外,查看任何给定数组的尺寸的简单方法是

  

array.shape

您将看到我使用它来定义STACK的形状,而不是通过定义box_size的额外步骤。

最后,你所定义的STAR不是数组形式。使用numpy时,只需使用np.asarray即可获得类似于数组格式的列表。

答案 1 :(得分:0)

您可以使用@ModelAttribute("entity") public Entity getEntity(@RequestParam(value="id", required=false) Long id) { if (id == null) { return new Entity(); } else { Optional<Entity> entity = entityRepo.findById(id); // TODO: Throw exception if not found (probably concurrently deleted). return entity.orElse(null); } } @RequestMapping(value="/entity/save", method=RequestMethod.POST) public String postEdit(@Valid @ModelAttribute("entity") Entity entity, BindingResult bindingResult, @RequestParam(value = "version", required=false) Long version) { if (entity.getVersion() != version) { bindingResult.reject("concurrency", "The data was modified concurrently."); } if (bindingResult.hasErrors()) { // TODO: Redisplay edit form } else { // TODO: Proceed with saving. } } 函数使用列表执行此操作:

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)

答案 2 :(得分:0)

让我们简化您的行动,以便更好地了解正在发生的事情

In [125]: n=5

使用您的值

制作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]])

初始stack为相同大小的零。这与“空”数组不同。

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

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

dstack与列表追加不一样。它创建了一个新阵列。注意形状 - 3d。

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

注意,它有零加星值。

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

你真的想这样做吗?

这是一种以增量方式构建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)

请注意np.array沿着新轴在前面连接组件。这是numpy中更好的地方。

你提到以后删除'明星';如果将其保留在列表表单中会更容易。如果您需要跨'star'进行计算,则3d数组表单非常有用,但可能不需要。

或者,如果您知道自己拥有starstack,则可以将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]]]) 初始化为正确的尺寸,并指定值:

dstack

这也比重复使用setFetchMode()(或其他一些连接)更好。

相关问题