将函数应用于元组数组 [英] Apply function to an array of tuples

查看:101
本文介绍了将函数应用于元组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要应用于元组数组的函数,我想知道是否存在一种干净的方法.

I have a function that I would like to apply to an array of tuples and I am wondering if there is a clean way to do it.

通常,我可以使用np.vectorize将该函数应用于数组中的每个项目,但是,在这种情况下,每个项目"是一个元组,因此numpy将该数组解释为3d数组并将该函数应用于每个项目在元组中.

Normally, I could use np.vectorize to apply the function to each item in the array, however, in this case "each item" is a tuple so numpy interprets the array as a 3d array and applies the function to each item within the tuple.

所以我可以假设传入数组是以下之一:

So I can assume that the incoming array is one of:

  1. 元组
  2. 元组的一维数组
  3. 元组的二维数组

我可能可以编写一些循环逻辑,但是numpy似乎最有可能具有更有效地执行此操作的功能,并且我不想重蹈覆辙.

I can probably write some looping logic but it seems like numpy most likely has something that does this more efficiently and I don't want to reinvent the wheel.

这是一个示例.我试图将tuple_converter函数应用于数组中的每个元组.

This is an example. I am trying to apply the tuple_converter function to each tuple in the array.

array_of_tuples1 = np.array([
        [(1,2,3),(2,3,4),(5,6,7)],
        [(7,2,3),(2,6,4),(5,6,6)],
        [(8,2,3),(2,5,4),(7,6,7)],
    ])

array_of_tuples2 = np.array([
        (1,2,3),(2,3,4),(5,6,7),
    ])

plain_tuple = (1,2,3)



# Convert each set of tuples
def tuple_converter(tup):
    return tup[0]**2 + tup[1] + tup[2]

# Vectorizing applies the formula to each integer rather than each tuple
tuple_converter_vectorized = np.vectorize(tuple_converter)

print(tuple_converter_vectorized(array_of_tuples1))
print(tuple_converter_vectorized(array_of_tuples2))
print(tuple_converter_vectorized(plain_tuple))

array_of_tuples1的所需输出:

[[ 6 11 38]
 [54 14 37]
 [69 13 62]]

array_of_tuples2的所需输出:

[ 6 11 38]

plain_tuple的所需输出:

6

但是上面的代码会产生此错误(因为它试图将函数应用于整数而不是元组.)

But the code above produces this error (because it is trying to apply the function to an integer rather than a tuple.)

<ipython-input-209-fdf78c6f4b13> in tuple_converter(tup)
     10 
     11 def tuple_converter(tup):
---> 12     return tup[0]**2 + tup[1] + tup[2]
     13 
     14 

IndexError: invalid index to scalar variable.

推荐答案

array_of_tuples1 array_of_tuples2 实际上不是元组数组,而只是3维和2维数组整数:

array_of_tuples1 and array_of_tuples2 are not actually arrays of tuples, but just 3- and 2-dimensional arrays of integers:

In [1]: array_of_tuples1 = np.array([
   ...:         [(1,2,3),(2,3,4),(5,6,7)],
   ...:         [(7,2,3),(2,6,4),(5,6,6)],
   ...:         [(8,2,3),(2,5,4),(7,6,7)],
   ...:     ])

In [2]: array_of_tuples1
Out[2]: 
array([[[1, 2, 3],
        [2, 3, 4],
        [5, 6, 7]],

       [[7, 2, 3],
        [2, 6, 4],
        [5, 6, 6]],

       [[8, 2, 3],
        [2, 5, 4],
        [7, 6, 7]]])

因此,与其对向量进行矢量化处理,还不如对其进行矢量化处理,因为它随后将基本上遍历数组的各个元素(整数),而应

So, instead of vectorizing your function, because it then will basically for-loop through the elements of the array (integers), you should apply it on the suitable axis (the axis of the "tuples") and not care about the type of the sequence:

In [6]: np.apply_along_axis(tuple_converter, 2, array_of_tuples1)
Out[6]: 
array([[ 6, 11, 38],
       [54, 14, 37],
       [69, 13, 62]])

In [9]: np.apply_along_axis(tuple_converter, 1, array_of_tuples2)
Out[9]: array([ 6, 11, 38])

这篇关于将函数应用于元组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆