3

I have 2 numpy arrays from which I am trying to find the difference for each element pair and store the difference in a matrix.

Here is the code used by me:

for i in range(arr1):
    for j in range(arr2):
        data[i,j] = float(arr1[i])-float(arr2[j])

what can be done to optimize the speed of this loop?

8
  • 1
    you can use np.subtract.outer(arr1, arr2) or broadcasting or np.subtract(*np.ix_(arr1, arr2)); broadcasting: arr1[:, None] - arr2[None, :] Commented Apr 11, 2017 at 6:01
  • 1
    similar: arr1 - arr2.reshape((-1,1)) Commented Apr 11, 2017 at 6:05
  • arr1_arr2_transformation = numpy.dstack(-arr1, arr2) then do numpy.sum(arr1_arr2_transformation) Commented Apr 11, 2017 at 6:09
  • Why are you not posting your answers as answers? Commented Apr 11, 2017 at 6:10
  • @FancyDolphin are you sure? First of all numpy.dstack takes only one argument returns an array. Summin of it yields a scalar. I dont see how this is supposed to work. Commented Apr 11, 2017 at 6:58

2 Answers 2

1

As pointed out in the comments there are several ways to reach your goal.

In [1]: import numpy as np
In [6]: a = np.random.rand(1000)
In [7]: b = np.random.rand(1000)

In [9]: %timeit a - b.reshape((-1,1))
100 loops, best of 3: 2.46 ms per loop

In [10]: %timeit np.subtract.outer(a, b)
100 loops, best of 3: 2.52 ms per loop

It seems that the reshape and the subtract.outer are comparable in speed. However it looks like you need to transpose the result in order to have identical results for both methods

In [18]: a - b.reshape((-1,1)) == np.subtract.outer(a, b).T

array([[ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       ..., 
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True]], dtype=bool)

Edit The second method proposed by @PaulPanzer seems to be the slowest.

In [27]: %timeit np.subtract(*np.ix_(a, b)); a[:, None] - b[None, :]
100 loops, best of 3: 4.99 ms per loop
Sign up to request clarification or add additional context in comments.

Comments

0
np.subtract.outer(arr1, arr2) helped me solved the problem.

Thanks everyone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.