1

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?

1
  • I didnt downvote ... but you are likely going to give us a small self contained code example if you want an answer ... Commented Oct 28, 2014 at 16:18

2 Answers 2

4
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
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Certainly need a loop or comprehension list. Loops are probably the most basic and needed programming skill. If your not efficient in loops I would seriously brush up on them, or else you will never be able to handle data.
did it. without loops. see my own answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.