0

I want to incrementally add to an array of shape (2,N) in a for loop with arrays of size (2,1) at each step of the loop. This is how I'm doing it right now:

x = []
a = np.array([[0.5], [0.5]])
for i in range(0, N):
    x = np.append(x, a + (np.random.randn(2, 1)/np.sqrt(5))).reshape(i+1, 2)
x = x.T

Is there a better way to do it without reshaping at each iteration and transposing the whole array in the end?

6
  • Is it alright for you to define the final array before the loop? Commented Sep 20, 2017 at 6:43
  • 1
    np.column_stack([np.random.randn(2, 1)/np.sqrt(5) for i in range(0, N)]) this works? Commented Sep 20, 2017 at 6:49
  • @Chiel yes I can do it Commented Sep 20, 2017 at 6:50
  • @Zero Hmmm ... that looks good. Shouldn't it be row_stack? Commented Sep 20, 2017 at 6:51
  • 1
    Or simply : np.random.rand(2,N)/np.sqrt(5). Commented Sep 20, 2017 at 6:51

3 Answers 3

1

You can try by initializing the array at the beginning: x = np.zeros((2,N)) and then in the for loop fill it in with the np.random.randn(2) / np.sqrt(5).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but how I do I fill it in?
See COLDSPEES' s answer: x[:,i] = np.random.rand(2) / np.sqrt(5)
1

Unless you have an urgent need for filling the array incrementally, you could you NumPy's random module for creating it at once:

x = np.random.randn(2, N) / np.sqrt(5) + 0.5

Comments

0

np.append is not a good function. Better to use concatenate or one of the stacks. Better yet, do a list append, with a concatenate at the end. And collect (1,2) arrays:

In [400]: a = np.array([[0.5], [0.5]]).T
In [401]: x = []
In [402]: for i in range(0,6):
     ...:     x.append(a+np.random.randn(1,2)/np.sqrt(5))
     ...:        
In [403]: x
Out[403]: 
[array([[ 0.53535176,  0.43666667]]),
 array([[ 0.25599309,  1.48571245]]),
 array([[ 0.82575401,  0.90599888]]),
 array([[ 0.38033551,  0.54522437]]),
 array([[ 0.41806277,  0.86227303]]),
 array([[ 0.25251664,  0.36236433]])]
In [404]: X=np.concatenate(x, axis=0)
In [405]: X
Out[405]: 
array([[ 0.53535176,  0.43666667],
       [ 0.25599309,  1.48571245],
       [ 0.82575401,  0.90599888],
       [ 0.38033551,  0.54522437],
       [ 0.41806277,  0.86227303],
       [ 0.25251664,  0.36236433]])

The loop can also be written as a list comprehension (as suggested in a comment).

If you created (2,) elements rather than (1,2) you could use np.array(x) to assemble the 2d array.

In [406]: a = np.array([0.5,0.5])
In [407]: x = [a+np.random.randn(2)/np.sqrt(5) for _ in range(6)]
In [408]: x
Out[408]: 
[array([ 0.26945613,  0.90210773]),
 ....
 array([ 0.76008067,  0.83912968])]
In [409]: np.array(x)
Out[409]: 
array([[ 0.26945613,  0.90210773],
       [ 0.96109886, -0.69105254],
       [-0.01010202,  0.92225443],
       [ 1.01784239,  0.21049822],
       [ 0.47476442,  0.08274172],
       [ 0.76008067,  0.83912968]])

np.append is supposed to look like a list append, but does a poor job of it. It's better to understand right from the start how a list of arrays can be joined with concatenate.

Comments

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.