Codistan
Codistan

Reputation: 1519

Unexpected results with functools reduce in python

While executing the below code I'm simply getting the first row(split) of the matrix back not the sum of the elements as what I'm expecting. Is my understanding incorrect or did I do something stupid?

My objective is to get the sum of all elements in each row.

import numpy as np
from functools import reduce

matrix = 100*np.random.rand(4,4)
matrix=matrix.astype(int)
print(matrix)
s_matrix = np.vsplit(matrix, 4)
sum_test = reduce((lambda a,b : a+b), list(s_matrix[0]))
print(sum_test)

Output:

[[79 75 33 26]
[49 45 16 19]
[58 33 83 55]
[40 14  2 93]]

[79 75 33 26]

Expected:

[213, 129, 229, 149]

Upvotes: 0

Views: 275

Answers (3)

Mark
Mark

Reputation: 92440

You can use reduce() for this by continually adding the results to a list in the accumulator:

import numpy as np
from functools import reduce

matrix = 100*np.random.rand(4,4)
matrix=matrix.astype(int)
sum_test = reduce(lambda a,b : a+[sum(b)], list(matrix), [])
print(sum_test)

...but you really shouldn't. One of the major points of Numpy is that you can avoid explicit python loops. Instead you should just use the array's sum function. You can pass it an axis to tell it to sum rows instead of the whole thing:

import numpy as np

matrix = np.random.randint(0, 100, [4,4])
print(matrix)
print(matrix.sum(axis = 1))

Result

[[64 89 97 15]
 [12 47 81 31]
 [52 81 37 78]
 [27 64 79 50]]

[265 171 248 220]

Upvotes: 1

Aly Zayn
Aly Zayn

Reputation: 71

sum_test = reduce((lambda a,b : a+b), list(s_matrix[0]))

above line is your problem,

you are only giving the first row of your matrix instead of giving the whole matrix

Upvotes: -1

Prune
Prune

Reputation: 77837

Check the expression you're using: print(list(s_matrix[0])). I think you'll find that it's a double-nested list

[[79 75 33 26]]

Thus, the "sum" is merely concatenation of a single list element.

Upvotes: 2

Related Questions