Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating each element of a numpy array 5 times

import numpy as np

data = np.arange(-50,50,10)
print data

[-50 -40 -30 -20 -10   0  10  20  30  40]

I want to repeat each element of data 5 times and make new array as follows:

ans = [-50 -50 -50 -50 -50 -40 -40 ... 40]

How can I do it?

What about repeating the whole array 5 times?

ans =  [-50 -40 -30 -20 -10   0  10  20  30  40 -50 -40 -30 -20 -10   0  10  20  30  40 -50 -40 -30 -20 -10   0  10  20  30  40 -50 -40 -30 -20 -10   0  10  20  30  40 -50 -40 -30 -20 -10   0  10  20  30  40 .......]
like image 712
puti Avatar asked Aug 24 '14 12:08

puti


People also ask

How do you repeat an element in a NumPy array?

NumPy: repeat() function The repeat() function is used to repeat elements of an array. Input array. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.

How do you repeat an element in an array?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How does NP repeat work?

The NumPy repeat function essentially repeats the numbers inside of an array. It repeats the individual elements of an array. Having said that, the behavior of NumPy repeat is a little hard to understand sometimes.


2 Answers

In [1]: data = np.arange(-50,50,10)

To repeat each element 5 times use np.repeat:

In [3]: np.repeat(data, 5)
Out[3]: 
array([-50, -50, -50, -50, -50, -40, -40, -40, -40, -40, -30, -30, -30,
       -30, -30, -20, -20, -20, -20, -20, -10, -10, -10, -10, -10,   0,
         0,   0,   0,   0,  10,  10,  10,  10,  10,  20,  20,  20,  20,
        20,  30,  30,  30,  30,  30,  40,  40,  40,  40,  40])

To repeat the array 5 times use np.tile:

In [2]: np.tile(data, 5)
Out[2]: 
array([-50, -40, -30, -20, -10,   0,  10,  20,  30,  40, -50, -40, -30,
       -20, -10,   0,  10,  20,  30,  40, -50, -40, -30, -20, -10,   0,
        10,  20,  30,  40, -50, -40, -30, -20, -10,   0,  10,  20,  30,
        40, -50, -40, -30, -20, -10,   0,  10,  20,  30,  40])

Note, however, that sometimes you can take advantage of NumPy broadcasting instead of creating a larger array with repeated elements.

For example, if

z = np.array([1, 2])
v = np.array([[3], [4], [5]])

then to add these arrays to produce

 [[4 5]
  [5 6]
  [6 7]]

you do not need to use tile:

In [12]: np.tile(z, (3,1))
Out[12]: 
array([[1, 2],
       [1, 2],
       [1, 2]])

In [13]: np.tile(v, (1,2))
Out[13]: 
array([[3, 3],
       [4, 4],
       [5, 5]])

In [14]: np.tile(z, (3,1)) + np.tile(v, (1,2))
Out[14]: 
array([[4, 5],
       [5, 6],
       [6, 7]])

Instead, NumPy will broadcast the arrays for you:

In [15]: z + v
Out[15]: 
array([[4, 5],
       [5, 6],
       [6, 7]])
like image 95
unutbu Avatar answered Oct 16 '22 21:10

unutbu


Simply use np.repeat:

In [5]: data.repeat(5)

Out[5]: 
array([-50, -50, -50, -50, -50, -40, -40, -40, -40, -40, -30, -30, -30,
       -30, -30, -20, -20, -20, -20, -20, -10, -10, -10, -10, -10,   0,
         0,   0,   0,   0,  10,  10,  10,  10,  10,  20,  20,  20,  20,
        20,  30,  30,  30,  30,  30,  40,  40,  40,  40,  40])
like image 29
Alex Riley Avatar answered Oct 16 '22 21:10

Alex Riley