0

Can somebody help me to understand this code. How are we able to get the sum in this one?

import numpy as np two_d = np.array([[1,2,3,4], [5,6,7,8]]) # 2 x 4 one_d = np.array([[10]]) # 1 three_d = np.ones((3, 2)) # 3 x 2 print(np.add(two_d,one_d)) # I didn't get this addition part print(np.add(one_d, three_d)) #here also

18th Jul 2018, 12:53 PM
sagar
sagar - avatar
3 Answers
+ 3
It's adding the arrays together. Unlike lists, arrays (or matrices) are added algebraically element-wise. So in the first case, an element 10 is added to all elements of the two_d array and in the second case, 10 is added to all elements of three_d array (which is a matrix of ones). np.sum method will give you the sum of elements, if that's what you are looking for.
18th Jul 2018, 1:01 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 5
I have played around a bit with it. Read the comments in the code. https://code.sololearn.com/cFk244NX598J/?ref=app
18th Jul 2018, 1:38 PM
cyk
cyk - avatar
0
thank you for the help. I got it. Kuba SiekierzyƄski and cyk .
18th Jul 2018, 1:43 PM
sagar
sagar - avatar