0

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!

3
  • 1
    Check out np.stack and friends. Commented Apr 14, 2022 at 22:28
  • 3
    This is a pattern you should avoid in numpy. (and as written, using a = a + n, you should avoid it with regular list objects too, although a += n would be fine, since that would work efficiently in-place) Commented Apr 14, 2022 at 22:29
  • 1
    For an array a+n is element-wise addition. It is not join/concatenate (as it is for lists and strings). Commented Apr 14, 2022 at 23:10

2 Answers 2

2

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])
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

You should really never do this.
@juanpa.arrivillaga, why?!
It is still important to mention that concatenation is very slow and should be avoided. Better to first create an array of the full length and then fill parts.
@I'mahdi because this is a classic anti-pattern that results in quadratic time when you can do this for lists in linear time trivially and with numpy too (using an intermediate list and then one final call to concatenate).
@JohanC or in Python, using the built-in mutator methods for list, e.g. .append and .extend, or equivalently, += (which essentially jsut calls .extend)

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.