Phước Hữu Lưu - 2 years ago 225

Python Question

I am trying to calculate the sum of all

`1d matrices`

I cant explain the reason why?

Here is my Code:

`import numpy as np`

from functools import reduce

matrix = np.matrix([[1,2,3],[2,3,4],[1,1,1]])

my_sum_matrix = reduce(lambda x,y : x + y, matrix)

print (my_sum_matrix)

print (my_sum_matrix[0])

And this is output :

`[[4 6 8]]`

[[4 6 8]]

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The Numpy `matrix`

type should only be used when you *really* need it. For more general work, you should use the `array`

type. And you don't need `reduce`

for this task.

```
import numpy as np
matrix = np.array([[1,2,3], [2,3,4], [1,1,1]])
my_sum_matrix = np.sum(matrix, axis=0)
print(my_sum_matrix)
```

**output**

```
[4 6 8]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**