Home > OS >  Numpy python - calculating sum of columns from irregular dimension
Numpy python - calculating sum of columns from irregular dimension

Time:12-09

I have a multi-dimensional array for scores, and for which, I need to get sum of each columns at 3rd level in Python. I am using Numpy to achieve this.

import numpy as np

Data is something like:

score_list = [
    [[1,1,3], [1,2,5]],
    [[2,7,5], [4,1,3]]
]

This should return:

[[3 8 8] [5 3 8]]

Which is happening correctly using this:

sum_array = np_array.sum(axis=0)
print(sum_array)

However, if I have irregular shape like this:

score_list = [
    [[1,1], [1,2,5]],
    [[2,7], [4,1,3]]
]

I expect it to return:

[[3 8] [5 3 8]]

However, it comes up with warning and the return value is:

[list([1, 1, 2, 7]) list([1, 2, 5, 4, 1, 3])]

How can I get expected result?

CodePudding user response:

numpy will try to cast it into an nd array which will fail, instead consider passing each sublist individually using zip.

score_list = [
    [[1,1], [1,2,5]],
    [[2,7], [4,1,3]]
]

import numpy as np
res = [np.sum(x,axis=0) for x in zip(*score_list)]
print(res)
[array([3, 8]), array([5, 3, 8])]

CodePudding user response:

Here is one solution for doing this, keep in mind that it doesn't use numpy and will be very inefficient for larger matrices (but for smaller matrices runs just fine).

# Create matrix
score_list = [
[[1,1,3], [1,2,5]],
[[2,7,5], [4,1,3]]
]

# Get each row
for i in range(1, len(score_list)):

  # Get each list within the row
  for j in range(len(score_list[i])):

    # Get each value in each list
    for k in range(len(score_list[i][j])):

        # Add current value to the same index 
        # on the first row 
        score_list[0][j][k]  = score_list[i][j][k]

print(score_list[0])

There is bound to be a better solution but this is a temporary fix for you :)

Edit. Made more efficient

CodePudding user response:

A possible solution:

a = np.vstack([np.array(score_list[x], dtype='object')
              for x in range(len(score_list))])
[np.add(*[x for x in a[:, i]]) for i in range(a.shape[1])]

Another possible solution:

a = sum(score_list, [])
b = [a[x] for x in range(0,len(a),2)]
c = [a[x] for x in range(1,len(a),2)]
[np.add(x[0], x[1]) for x in [b, c]]

Output:

[array([3, 8]), array([5, 3, 8])]
  • Related