首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将不同大小的nd数组追加到单个数组中

将不同大小的nd数组追加到单个数组中
EN

Stack Overflow用户
提问于 2018-01-19 18:47:04
回答 1查看 38关注 0票数 -1

我有一个5000*3072的多维数组,我使用以下命令将其分成5个1000*3072的块

代码语言:javascript
复制
numpy.array_split() 

函数。现在对于迭代,我需要组合数组的不同组合

代码语言:javascript
复制
For example, 0th iteration: 1,2,3,4 chunks to combine
             1st iteration: 0,2,3,4 chunks to combine
             2nd iteration: 0,1,3,4 chunks to combine and so on

我尝试使用np.concatenate,但它给出了错误:

ValueError:所有输入数组必须具有相同的维数

有没有其他的组合方式呢?

EN

回答 1

Stack Overflow用户

发布于 2018-01-19 19:07:23

是的,这是可能的。您可以像这样使用np.concatenate

代码语言:javascript
复制
In [10]: arr = np.arange(20).reshape((5,4))

# split `arr` into 5 sub-arrays
In [11]: split_arrs = np.array_split(arr, 5)

# concatenate only last four sub-arrays
# for your case: 1,2,3,4 chunks to combine
In [12]: np.concatenate(split_arrs[1:], axis=0)
Out[12]: 
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])


# 0,2,3,4 chunks to combine
In [15]: np.concatenate((split_arrs[0], *split_arrs[2:]), axis=0)
Out[15]: 
array([[ 0,  1,  2,  3],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

# 0,1,3,4 chunks to combine
In [16]: np.concatenate((*split_arrs[0:2], *split_arrs[3:]), axis=0)
Out[16]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

我认为你得到了ValueError,因为你可能做过这样的事情:

代码语言:javascript
复制
In [17]: np.concatenate((split_arrs[0], split_arrs[2:]), axis=0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-938d5afdd06a> in <module>()
----> 1 np.concatenate((split_arrs[0], split_arrs[2:]), axis=0)

ValueError: all the input arrays must have same number of dimensions

请注意,如果您在元组中传递子数组,则应将其解包,以便维度匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48339315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档