0

我有一个 numpy 数据结构如下:

[[['diaad'],
  ['iaadf'],
  ['aadfe'],
  ['hedbb'],
  ['edbbb'],
  ['dbbbb']],

 [['gegec'],
  ['ehecf'],
  ['gecfc'],
  ['gadff'],
  ['adfef'],
  ['dffgc']],

 [['ddddj'],
  ['dddjd'],
  ['ddjdd'],
  ['jfffd'],
  ['fgfdb'],
  ['ggdbb']]]

这是这样实例化的:

>>> a = np.array([[['diaad'], ['iaadf'],  ['aadfe'],  ['hedbb'],  ['edbbb'],  ['dbbbb']], [['gegec'],  ['ehecf'],  ['gecfc'],  ['gadff'],  ['adfef'],  ['dffgc']], [['ddddj'],  ['dddjd'],  ['ddjdd'],  ['jfffd'],  ['fgfdb'],  ['ggdbb']]])

有没有直接numpy的方法来计算成对元素上的自定义函数?

例如,我的自定义函数被称为processPair(a,b). 它应该计算沿列的所有成对元素的结果,即在('diaad', 'gegec')('gegec', 'ddddj')之间('diaad', 'ddddj')。有什么建议吗?我在想这个map功能可以实现这一点,但不完全确定如何实现。

4

1 回答 1

1

这是我的解决方案。我对此并不完全满意——我觉得应该可以更优雅地做到这一点——但它确实有效:

from itertools import combinations

def apply_pairwise(func, a):
    "For each row, call func with every possible combination of two values"

    stack = []
    for col_a, col_b in combinations(range(a.shape[0]), 2):
        stack.append(np.hstack([a[col_a], a[col_b]]))

    combined = np.vstack(stack)

    def unpack_row(row):
        "Calls func with the values of a given numpy array as arguments"
        return func(*row.tolist())

    return np.apply_along_axis(unpack_row, 1, combined)

像这样使用(假设您的示例数组a已定义):

>>> f = lambda x, y: x + y
>>> print apply_pairwise(f, a)
['diaadgegec' 'iaadfehecf' 'aadfegecfc' 'hedbbgadff' 'edbbbadfef'
'dbbbbdffgc' 'diaadddddj' 'iaadfdddjd' 'aadfeddjdd' 'hedbbjfffd'
'edbbbfgfdb' 'dbbbbggdbb' 'gegecddddj' 'ehecfdddjd' 'gecfcddjdd'
'gadffjfffd' 'adfeffgfdb' 'dffgcggdbb']
于 2012-06-28T10:13:53.243 回答