Search code examples
pythonnumpyvectorizationcumsum

Python Numpy: np.cumsum but subtraction, subtract all array values from single value, -=


I'm looking to do something like cumsum but with subtraction. I need to do this with vectorization because my dataset length is really 100,000 rather than 3.

I have two arrays:

a = np.array([-7.021, -1.322, 3.07])
b = np.array([[-1.592, -1.495, -1.415, -1.363, -0.408, -0.36, -0.308],
              [-0.287, -0.249, -0.226, -0.206, -0.197, -0.165, -0.075],
              [-0.389, -0.237, 0.144, 0.198, 0.539, 0.921, 0.932]])

I'd like to subtract each row value of array B from array A. For the example above, the answer would look like (apologies if typo in manual math):

result = np.array([[-5.429, 3.934, 2.519, 1.156, 0.748, -0.388, 0.08 ],
                   [-1.035, 0.786, 0.56, 0.354, 0.157, 0.008, 0.083],
                   [3.459, 3.696, 3.552, 3.354, 2.815, 1.894, 0.962]])

I've tried to clarify by using indicies below:

results = np.array([[a[0] - b[0][0], results[0][0] - b[0][1], results[0][1] - b[0][2] ...],
                    [a[1] - b[1][0], results[1][0] - b[1][1], results[1][1] - b[1][2] ...],
                    [a[2] - b[2][0], results[2][0] - b[2][1], results[2][1] - b[2][2] ...]])

Solution

  • You can run:

    result = a.reshape(-1, 1) - b.cumsum(axis=1)
    

    The result is:

    [[-5.429 -3.934 -2.519 -1.156 -0.748 -0.388 -0.08 ]
     [-1.035 -0.786 -0.56  -0.354 -0.157  0.008  0.083]
     [ 3.459  3.696  3.552  3.354  2.815  1.894  0.962]]
    

    a.reshape(-1, 1) changes the original (1 row) vector into a 1-column array. Then you subtract the cumulative sum of each row from b.