Abdellah Didi - 1 month ago 7

Python Question

I have this dictionary:

`import numpy as np`

dct={'W1': np.array([[ 1.62434536, -0.61175641, -0.52817175],

[-1.07296862, 0.86540763, -2.3015387 ]]),

'b1': np.array([[ 1.74481176],

[-0.7612069 ]]),

'W2': np.array([[ 0.3190391 , -0.24937038],

[ 1.46210794, -2.06014071],

[-0.3224172 , -0.38405435]]),

'b2': np.array([[ 1.13376944],

[-1.09989127],

[-0.17242821]]),

'W3': np.array([[-0.87785842, 0.04221375, 0.58281521]]),

'b3': np.array([[-1.10061918]])}

I need to sum all the elements of W1, W2, W3 after squared them, each one at a time then all of the three.

I used this to extract a list with the keys W(i)

`l=[v for k, v in dict.items() if 'W' in k]`

How could I get the sum of the squared elements in each array?

When taken each array separately I do:

`np.sum(np.square(l[0]) to get 10.4889815722 for l[0]`

I don't know though how to them all in one shot

Answer Source

I'm not sure I got you, but here's what I think you're looking for:

```
>>> import numpy as np
>>> data = [np.array([1.62434536, -0.61175641, -0.52817175]), np.array([-1.07296862, 0.86540763, -2.3015387])]
>>> [np.sum(arr) for arr in data]
[0.48441719999999999, -2.5090996900000002]
```