按元素组合 2 个 2D numpy 数组,形状相同,同时保留其形状

问题描述 投票:0回答:3

组合第一个数组和第二个数组中的每个元素并创建一个新数组,同时保留相同的形状。

#Numpy Array 1 -- shape 7 X 5 
#X coordinates
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
#Numpy Array 2 -- shape 7 X 5
#Y coordinates
 array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6]])

所需的输出应该是元组数组。

#Desired Output -- shape 7 X 5
#(X,Y) combination
array([[(0,0), (1,0), (2,0), (3,0), (4,0)],
       [(0,1), (1,1), (2,1), (3,1), (4,1)],
       [(0,2), (1,2), (2,2), (3,2), (4,2)],
       [(0,3), (1,3), (2,3), (3,3), (4,3)],
       [(0,4), (1,4), (2,4), (3,4), (4,4)],
       [(0,5), (1,5), (2,5), (3,5), (4,5)],
       [(0,6), (1,6), (2,6), (3,6), (4,6)]])
python python-3.x numpy numpy-ndarray
3个回答
1
投票

按如下方式使用

dstack
:(其中
x
y
是您的第一个和第二个数组)

np.dstack((x,y))

1
投票

您可以执行以下操作:

import numpy as np

arr1 = np.array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
#Numpy Array 2 -- shape 7 X 5
#Y coordinates
arr2 = np.array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6]])

comb = np.vstack(([arr1.T], [arr2.T])).T
print(comb)

虽然它不是元组数组,但给出了几乎相同的结果。

您可以随后添加步骤以通过以下方式获取元组数组:

import numpy as np

arr1 = np.array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
#Numpy Array 2 -- shape 7 X 5
#Y coordinates
arr2 = np.array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6]])

comb = np.vstack(([arr1.T], [arr2.T])).T

f = np.vectorize(lambda x:tuple(*x.items()), otypes=[np.ndarray])
res = np.apply_along_axis(lambda x:dict([tuple(x)]), 2, comb)
mat2 = np.vstack(f(res))

这将给出以下内容:

[[(0, 0) (1, 0) (2, 0) (3, 0) (4, 0)]
 [(0, 1) (1, 1) (2, 1) (3, 1) (4, 1)]
 [(0, 2) (1, 2) (2, 2) (3, 2) (4, 2)]
 [(0, 3) (1, 3) (2, 3) (3, 3) (4, 3)]
 [(0, 4) (1, 4) (2, 4) (3, 4) (4, 4)]
 [(0, 5) (1, 5) (2, 5) (3, 5) (4, 5)]
 [(0, 6) (1, 6) (2, 6) (3, 6) (4, 6)]]

0
投票

窃取 将 3d numpy 数组转换为 2d numpy 数组(其中内容是元组) :

import numpy as np

#Numpy Array 1 -- shape 7 X 5 
#X coordinates
ar1 = np.array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
#Numpy Array 2 -- shape 7 X 5
#Y coordinates
ar2 = np.array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6]])
       

comb = np.stack((ar1,ar2), axis = 2)

print(comb, comb.shape , comb.dtype,'\n\n')


import numpy.lib.recfunctions as nlr

comb2 = nlr.unstructured_to_structured(comb).astype('O')

print(comb2, comb2.shape, comb2.dtype)

print(type(comb2[0,0]))

输出:

[[[0 0]
  [1 0]
  [2 0]
  [3 0]
  [4 0]]

 .....
 .....
 [[0 6]
  [1 6]
  [2 6]
  [3 6]
  [4 6]]] (7, 5, 2) int64


[[(0, 0) (1, 0) (2, 0) (3, 0) (4, 0)]
 [(0, 1) (1, 1) (2, 1) (3, 1) (4, 1)]
 [(0, 2) (1, 2) (2, 2) (3, 2) (4, 2)]
 [(0, 3) (1, 3) (2, 3) (3, 3) (4, 3)]
 [(0, 4) (1, 4) (2, 4) (3, 4) (4, 4)]
 [(0, 5) (1, 5) (2, 5) (3, 5) (4, 5)]
 [(0, 6) (1, 6) (2, 6) (3, 6) (4, 6)]] (7, 5) object
<class 'tuple'>

相关部分是

import numpy.lib.recfunctions as nlr

comb2 = nlr.unstructured_to_structured(array).astype('O')

来自:

numpy.lib.recfunctions.unstructured_to_structd(arr,dtype =无,名称=无,align = False,复制= False,铸造='不安全')

numpy.lib.recfunctions.unstructured_to_structured(arr, dtype=None, names=None, align=False, copy=False, casting='unsafe')[source]

Converts an n-D unstructured array into an (n-1)-D structured array.

The last dimension of the input array is converted into a structure, with number of field-elements equal to the size of the last dimension of the input array. By default all output fields have the input array’s dtype, but an output structured dtype with an equal number of fields-elements can be supplied instead.

Nested fields, as well as each element of any subarray fields, all count towards the number of field-elements.

Parameters:

    arr
    ndarray

        Unstructured array or dtype to convert.
    dtype
    dtype, optional

        The structured dtype of the output array
    names
    list of strings, optional

        If dtype is not supplied, this specifies the field names for the output dtype, in order. The field dtypes will be the same as the input array.
    align
    boolean, optional

        Whether to create an aligned memory layout.
    copy
    bool, optional

        See copy argument to numpy.ndarray.astype. If true, always return a copy. If false, and dtype requirements are satisfied, a view is returned.
    casting
    {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional

        See casting argument of numpy.ndarray.astype. Controls what kind of data casting may occur.

Returns:

    structured
    ndarray

        Structured array with fewer dimensions.

© www.soinside.com 2019 - 2024. All rights reserved.