简体   繁体   English

切片/向量化表示法中的numpy数组聚合

[英]Numpy array aggregation in slice/vectorized notation

Using np, how to create a new numpy array that is a combination of 2 numpy arrays? 使用np,如何创建由2个numpy数组组合而成的新的numpy数组?

Here is the problem: 这是问题所在:

x = [[a1,a2],[b1,b2],...] # this is an ndarray
y = [a,b,c,...] # ditto

xnew = [[a1,a2,a],...]

or xnew = [([a1,a2],a), ...] xnew = [([a1,a2],a), ...]

Here is how I would solve it using lists and for loops: 这是我如何使用列表和for循环来解决它:

xnew = [(x[i],y[i]) for i in range(len(x))]

How do I do the same thing using numpy? 我如何使用numpy做同样的事情?

This is straight forward case of concatenation - except that y needs to be transposed: 这是串联的直接例子-除了y需要转置:

In [246]: x = np.array([[1,2],[3,4]])
In [247]: y= np.array([[5,6]])
In [248]: np.concatenate((x,y.T),axis=1)
Out[248]: 
array([[1, 2, 5],
       [3, 4, 6]])

That is, in one way or other y has to have as many rows as x . 也就是说,以某种方式y必须具有与x一样多的行。 column_stack and hstack require the same transpose. column_stackhstack需要相同的转置。

In numpy , tuple notation is used for structured array records. numpy ,元组符号用于结构化数组记录。 That requires defining a compound dtype . 这需要定义一个复合dtype If you outline the desired dtype , I can help you construct it. 如果您概述所需的dtype ,我可以帮助您构造它。

You comment: 您评论:

Y can be an arbitrary length list, as can X, so I need to keep them separate.. Y可以是任意长度的列表,X可以是任意长度的列表,因此我需要将它们分开。

Does that mean there can be different number of items in Y and X, and that some of those tuples will in complete? 这是否意味着Y和X中可以有不同数量的项,并且其中某些元组将完整? Having ax term but not y, or vv? 有斧头词但没有y或vv? If that's the case, then you'd be test of using list comprehension and one of the zip tools (regular zip or one from itertools ). 如果真是这样,那么您将测试使用列表理解和一种zip工具(常规zip或itertools )。 numpy arrays are for lists/arrays that match in size. numpy数组用于大小匹配的列表/数组。

zip examples: 压缩示例:

In [263]: x = [[1,2],[3,4]]
In [264]: y= [5,6,7]         # not nested

zip over the shortest, ignore the longest 拉链最短,忽略最长

In [266]: [(i,j) for i,j in zip(x,y)]
Out[266]: [([1, 2], 5), ([3, 4], 6)]

zip over the longest, pad the shortest 拉链最长,垫最短

In [267]: [(i,j) for i,j in itertools.zip_longest(x,y)]
Out[267]: [([1, 2], 5), ([3, 4], 6), (None, 7)]

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

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