0

I want to write this multidimensional array as soft code

adjust = np.array([[0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],
               [0.,1.,0.],   
               [0.,1.,0.]])

I tried np.repeat(x, 10, axis=0) for x = [0.,1.,0.], it repeated them in the same bracket [ ]. Is it possible to use np.repeat here? or other numpy?

Also, is it possible to write soft code for

adjust = np.array([[0.,0.,1.,0.,0.],
               [0.,0.,1.,0.,0.],
               [0.,0.,1.,0.,0.]])

which I may have to expand both left and right 0 in various numbers in future?

Jan
  • 1,389
  • 4
  • 17
  • 43

2 Answers2

4

You can add another axis to the array before repeating (note we're repeating n = 1000 times)

n = 1000
%timeit adjust = np.repeat(np.array([0., 1., 0.])[None, :], n, axis=0)
# 7.67 µs ± 940 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

or reshape and transpose the result of repeat()

%timeit adjust = np.repeat([0., 1., 0.], n, axis=0).reshape(3, -1).T
# 22.5 µs ± 1.13 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

or use broadcasting

%timeit adjust = np.array([0., 1., 0.]) * np.ones(n)[:, None]
# 26.8 µs ± 880 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

And for sake of performance comparison, Allen's suggestion:

%timeit adjust = np.asarray([ 0.,  1.,  0.] * n).reshape(n,-1)
# 93.5 µs ± 7.87 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

and Divakars suggestion

%timeit adjust = np.tile(np.array([0., 1., 0.]), (n, 1))
# 11.1 µs ± 686 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Conclusion: np.repeat() after adding another axis is the fastest.

Nils Werner
  • 34,832
  • 7
  • 76
  • 98
1

Repeat your list n times, put it in a numpy array and then reshape it to n rows.

np.asarray([ 0.,  1.,  0.]*10).reshape(10,-1)
Out[139]: 
array([[ 0.,  1.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  1.,  0.],
       ..., 
       [ 0.,  1.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  1.,  0.]])

Similarly for your second array:

np.asarray([0.,0.,1.,0.,0.]*3).reshape(3,-1)
Out[140]: 
array([[ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.]])

Timings:

%timeit np.asarray([ 0.,  1.,  0.]*10).reshape(10,-1)
The slowest run took 14.97 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 4.51 µs per loop

%timeit np.repeat([0., 1., 0.], 10, axis=0).reshape(3, -1).T
The slowest run took 4.44 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.3 µs per loop

%timeit np.array([0., 1., 0.]) * np.ones(10)[:, None]
The slowest run took 10.28 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.3 µs per loop
Allen Qin
  • 19,507
  • 8
  • 51
  • 67
  • While `asarray` is useful to be able to deal with heterogenous input (lists, tuples, ndarrays), in this case we know that [0.,1.,0.] is a list and should use `array`, which will execute slightly faster. That said this answer is clearly the most readable and clear one. – Jan Christoph Terasa May 24 '17 at 07:38