JavaScript
x
6
1
import numpy as np
2
n1=np.array([10,20,30,40,50,60])
3
n2=np.array([50,60,70,80])
4
5
np.sum([n1,n2])
6
JavaScript
1
24
24
1
ValueError Traceback (most recent call last)
2
<ipython-input-19-d22debd88ae6> in <module>
3
3 n2=np.array([50,60,70,80])
4
4
5
----> 5 np.sum([n1,n2])
6
7
<__array_function__ internals> in sum(*args, **kwargs)
8
9
~anaconda3libsite-packagesnumpycorefromnumeric.py in sum(a, axis, dtype, out, keepdims, initial, where)
10
2239 return res
11
2240
12
-> 2241 return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
13
2242 initial=initial, where=where)
14
2243
15
16
~anaconda3libsite-packagesnumpycorefromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
17
85 return reduction(axis=axis, out=out, **passkwargs)
18
86
19
---> 87 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
20
88
21
89
22
23
ValueError: operands could not be broadcast together with shapes (6,) (4,)
24
I am trying the sum the elements of these arrays…..but it is throwing this error in jupiter notebook
Advertisement
Answer
JavaScript
1
2
1
np.sum(np.append(n1,n2))
2
You first need to append the two arrays together, then you can take the total sum.