Daniel Slätt
Daniel Slätt

Reputation: 771

Can I make numpy.sum return 0 when array is 0 rows?

I have a sum function that, in a simplified version, looks like this: The row arrays used as indicers change dynamically within my program, but this is a heavily reduced version to demonstrate the issue:

This runs perfectly fine if I throw a few integers into the Row1 array, which is obviously intended, but since there are instances where one of the row arrays will be empty in my program, I was thinking about whether I can make numpy execute this task without throwing an error. Let's

arr = np.array([1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1])
Row1 = np.array([])
Row2 = np.array([1,2])
Col = np.array([0,2])

Result = np.sum(arr[Row1[:, None], Col]) + np.sum(arr[Row2[:, None], Col]

This could obviously be interpreted to return 4 (0 + 4) but numpy will obviously throw an error as I'm trying to indice a 0 dimension array. I could solve this by doing:

arr = np.array([1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1])
Row1 = np.array([])
Row2 = np.array([1,2])
Col = np.array([0,2])

sum1 = np.sum(arr[Row1[:, None], Col]) if len(Row1) > 0 else 0
sum2 = np.sum(arr[Row2[:, None], Col]) if len(Row2) > 0 else 0

Result = sum1 + sum2

This would work just fine but would require hundreds of extra lines in my code that feel a bit unnecessary, so I was just wondering if anyone has a more efficient way around this issue. Thank you!

Upvotes: 2

Views: 246

Answers (2)

Paul Panzer
Paul Panzer

Reputation: 53029

"This could obviously be interpreted to return 4 (0 + 4) but numpy will obviously throw an error as I'm trying to indice a 0 dimension array. I could solve this by doing:"

Nope, as long as you make sure the empty array has dtype int it works just fine.

arr = np.array(([1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]))
Row1 = np.array([], dtype=int)
Row2 = np.array([1,2])
Col = np.array([0,2])

Result = np.sum(arr[Row1[:, None], Col]) + np.sum(arr[Row2[:, None], Col])

Result
# 4

Upvotes: 2

EdChum
EdChum

Reputation: 394041

I'd define your own sum func to wrap this logic, then you wouldn't need to modify so many lines:

In [235]:
def mySum(a, row, col):
    if len (row) > 0:
        return np.sum(a[row[:, None], col])
    else:
        return 0

Result = mySum(arr,Row1, Col) + mySum(arr,Row2,Col)
Result

Out[235]: 4

The thing here is to think if I need to write the same bit of code a few times, turn it into a func

Upvotes: 1

Related Questions