numpy:将第三个维度上的两个数组连接起来

时间:2018-07-20 09:37:03

标签: python numpy

我想向现有数据多维数据集中添加另一个“切片”数据:

import numpy as np

a = np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]])
print(a.shape)  # (2, 3, 4)

b = np.array([[7, 7, 7], [8, 8, 8]])
print(b.shape)  # (2, 3)

c = np.concatenate([a, b], axis=2)  # ValueError: all the input arrays must have same number of dimensions 
print(c.shape)  # wanted result: (2, 3, 5)

因此,我基本上想通过将最后一个尺寸扩展为2x3x5将2x3数组添加到2x3x4数组中。值错误对我来说不太有意义,因为数组的形状不能相同?我在这里做什么错了?

1 个答案:

答案 0 :(得分:4)

onParentClick(index) { let items = this.items; Observable.of(items).subscribe((items: any) => { //<======= here you can set any this.el.nativeElement.dispatchEvent(new CustomEvent(items[index].eventName, {})); console.log( items[index].eventName ); }); } 的末尾添加一个新轴,然后进行串联-

b

所以,对于给定的样本-

np.concatenate((a, b[...,None]), axis=2)
相关问题