Abhishek Thakur
Abhishek Thakur

Reputation: 17005

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?

Upvotes: 1

Views: 1935

Answers (2)

Abhishek Thakur
Abhishek Thakur

Reputation: 17005

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

Upvotes: 4

Mujtaba Hasan
Mujtaba Hasan

Reputation: 112

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

Upvotes: 1

Related Questions