简体   繁体   中英

apply a function to each pair of rows of an array

I have a numpy array of size 4x400 . I want to apply a function to all pairs of rows of this numpy array. The function:

def func(vector1,vector2):
    ...
    ...
    return X

where X is a float value.

So in the end I will get a vector of length 10.

Is there any way to this efficiently (fast) without using loops?

import numpy
import itertools as it
arr=numpy.random.rand(4,400)
transposed=arr.T
values=[numpy.dot(i,j) for i, j in it.combinations(transposed, 2)]
print values

I think u will have to use loop. Use itertools in python to generate all combinations of rows This may help you https://docs.python.org/2/library/itertools.html .. Then apply your function on all generated pairs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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