重复numpy数组的每个元素5次

时间:2021-06-28 21:23:48
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:

我想重复数据的每个元素5次,并按如下方式创建新数组:

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

How can I do it?

我该怎么做?

What about repeating the whole array 5 times?

怎么样重复整个阵列5次?

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 .......]

2 个解决方案

#1


31  

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

To repeat each element 5 times use np.repeat:

要重复每个元素5次,请使用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:

要重复数组5次,请使用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.

但请注意,有时您可以利用NumPy广播而不是使用重复元素创建更大的数组。

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:

相反,NumPy将为您广播阵列:

In [15]: z + v
Out[15]: 
array([[4, 5],
       [5, 6],
       [6, 7]])

#2


5  

Simply use np.repeat:

只需使用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])

#1


31  

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

To repeat each element 5 times use np.repeat:

要重复每个元素5次,请使用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:

要重复数组5次,请使用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.

但请注意,有时您可以利用NumPy广播而不是使用重复元素创建更大的数组。

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:

相反,NumPy将为您广播阵列:

In [15]: z + v
Out[15]: 
array([[4, 5],
       [5, 6],
       [6, 7]])

#2


5  

Simply use np.repeat:

只需使用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])