2

I have this dictionary:

import numpy as np
dict={'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 sum them all in one shot

3
  • 1
    I'm a bit confused how you get these numbers. Which elements add up to 10.4889815722 for example? Commented Aug 20, 2017 at 15:16
  • 1
    Sorry, my bad, I forget to say that I do square the elements before adding them up for 10.4889815722 that's np.sum(np.square(l[0]). Question edited to reflect that. Commented Aug 20, 2017 at 15:22
  • Ah, that makes sense. Thanks for the clarification. Commented Aug 20, 2017 at 15:29

2 Answers 2

1

You could simply extract the sum of all values with a dictionary comprehension:

>>> res = {key: np.square(arr).sum() for key, arr in dct.items()}  # you could also use if 'W' in key here too.
>>> res
{'W1': 10.48898156439229,
 'W2': 6.7973615015702658,
 'W3': 1.1120909752613031,
 'b1': 3.6238040224419072,
 'b2': 2.5249254365039309,
 'b3': 1.2113625793838725}

Given that dictionaries are unordered having the result as a dictionary is probably better (accessible with res['W1'] for example) because otherwise the list elements would be in arbitrary order (or you need to sort the keys before putting them in a list).

To sum all W* values:

>>> sum(v for k, v in res.items() if 'W' in k)  # normal sum this time but would also work with np.sum!
18.398434041223858
Sign up to request clarification or add additional context in comments.

Comments

0

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]

3 Comments

That answers my question. I needed only to add np.square to get the desired result. Thank you !
why not just np.sum(data, axis=1) in this case? this avoids the list comprehension...
@F.M.F. I thought the same. But I think there is a problem when the sub-arrays, that are within the array, have different lengths. E.g. np.sum([array([ 1, 10]), array([ 1, 20])], axis=1) works well, but np.sum([array([ 1, 10, 15]), array([ 1, 20])], axis=1) throws an error, because the one subarray has 3 and the other 2 elements. Pls, let me know if you have an idea how to avoid the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.