'How to implement '+=' for Numpy arrays?
I'm trying to figure out how to use concatenate arrays on Numpy with Python by using something similar to '+='. However I ran in to an operand error while running the program, which is quite confusing.
import numpy as np
a=np.array([])
for i in range(10):
n=np.random.normal(1, 1, 10)
a = a+n
print(a)
What should I change in order for the thing to work? It's a really simple issue, but since I don't know how Numpy works it's been bothering me. Thanks!
Solution 1:[1]
We really should ask for the error, with traceback, before suggesting fixes. Anyways here's your full error:
In [83]: a=np.array([])
In [84]: n = np.random.normal(1,1,10)
In [85]: a.shape
Out[85]: (0,)
In [86]: n.shape
Out[86]: (10,)
In [87]: a+n
Traceback (most recent call last):
Input In [87] in <cell line: 1>
a+n
ValueError: operands could not be broadcast together with shapes (0,) (10,)
This trying to do element-wise addition of a 0 element array and 10 element one. This isn't an operand error; it's a ValueError, a broadcasting one. It's important that you understand what's happening.
But if a starts as a list:
In [88]: a = []
In [89]: a.append(n)
In [90]: a
Out[90]:
[array([0.73866347, 0.68341855, 1.14853292, 0.96903861, 0.28691117,
1.20049352, 1.89670582, 0.92089883, 0.84876042, 0.79195955])]
In [91]: a.append(n)
In [92]: a
Out[92]:
[array([0.73866347, 0.68341855, 1.14853292, 0.96903861, 0.28691117,
1.20049352, 1.89670582, 0.92089883, 0.84876042, 0.79195955]),
array([0.73866347, 0.68341855, 1.14853292, 0.96903861, 0.28691117,
1.20049352, 1.89670582, 0.92089883, 0.84876042, 0.79195955])]
Now we get a list of arrays, which can be joined into one array with:
In [93]: np.stack(a)
Out[93]:
array([[0.73866347, 0.68341855, 1.14853292, 0.96903861, 0.28691117,
1.20049352, 1.89670582, 0.92089883, 0.84876042, 0.79195955],
[0.73866347, 0.68341855, 1.14853292, 0.96903861, 0.28691117,
1.20049352, 1.89670582, 0.92089883, 0.84876042, 0.79195955]])
Filling an array, row by row, is competative in speed to this list append approach:
In [94]: arr = np.zeros((3,10))
...: for i in range(3):
...: arr[i,:] = n
But creating all rows at once is even better:
In [97]: n = np.random.normal(1,1,(3,10))
Here's how += is used with arrays:
In [99]: a = np.arange(5)
In [100]: a = np.arange(5)
In [101]: a
Out[101]: array([0, 1, 2, 3, 4])
In [102]: a += 10
In [103]: a
Out[103]: array([10, 11, 12, 13, 14])
In [104]: a += [1,10,100,1000,10000]
In [105]: a
Out[105]: array([ 11, 21, 112, 1013, 10014])
Solution 2:[2]
You can create zero array then insert numbers in it, like below, or only write np.random.normal(1, 1, 100) in this problem:
import numpy as np
a=np.zeros(100)
for i in range(10):
n=np.random.normal(1, 1, 10)
a[i*10:(i+1)*10] = n
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
