0

Can I use numpy broadcasting to achieve one column (col + 1) subtraction with other column (col -1) and dividing them by constant (n).

arr = [[2, 4, 6, 5, 8],
       [5, 3, 7, 4, 4],
       [3, 9, 5, 2, 1]]

result = [
          [ 2, # (6 - 2)/2
           .5, # (5 - 4)/2
            1, # (8 - 6)/2
          ],
          [ 1, # (7 - 5)/2
           .5, # (4 - 3)/2
           -1.5 # (4 - 7)/2
          ],
          [ 1, # (5 - 3)/2
           -3.5, # (2 - 9)/2
           -2 # (1 - 5)/2
          ]
        ]

Or what is the efficient way to achieve it on GBs of data?

2

2 Answers 2

3

By considering a single column col, the operation can be performed simply with a numpy's array.

col = 1
0.5 * (arr[:, col+1] - arr[:, col-1])
[Out]: array([2., 1., 1.])

And finally, to do the whole operation:

0.5 * (arr[:, 2:] - arr[:, :-2])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for help.
1

I believe you have some misconception about what broadcasting is, you should get clear about it. You can use slices to do what you want, slices will give you access to views which will not take memory, but subtraction product will need to be stored in memory. If you want it to be more efficient than this, you should probably write it in c with multithreading.

https://repl.it/repls/YawningAcceptableLivecd

take a look that b and c have their base as a.

import numpy as np
a = np.array([[2, 4, 6, 5, 8],
       [5, 3, 7, 4, 4],
       [3, 9, 5, 2, 1]])
b = a[:,2::]
c = a[:,:-2:]
print b
print b.base is a
print c
print c.base is a
res = (b-c)/2.0
print res

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.