Numpy重塑阵列

时间:2014-04-02 22:47:16

标签: python arrays numpy

我有一个下面再现的数组,当我绘制它时,我得到锯齿波,我正在寻找方波输出,即当第二列中的第11个值的值应该减小时。我正在寻找一种方法来做到这一点,而无需每次都手动重塑。

例如我有这个:

[[ 0.   0. ]
 [ 0.   0.1]
 [ 0.   0.2]
 [ 0.   0.3]
 [ 0.   0.4]
 [ 0.   0.5]
 [ 0.   0.6]
 [ 0.   0.7]
 [ 0.   0.8]
 [ 0.   0.9]
 [ 0.1  0. ]
 [ 0.1  0.1]
 [ 0.1  0.2]
 [ 0.1  0.3]
 [ 0.1  0.4]
 [ 0.1  0.5]
 [ 0.1  0.6]
 [ 0.1  0.7]
 [ 0.1  0.8]
 [ 0.1  0.9]

我想要这个:

 [[ 0.   0. ]
 [ 0.   0.1]
 [ 0.   0.2]
 [ 0.   0.3]
 [ 0.   0.4]
 [ 0.   0.5]
 [ 0.   0.6]
 [ 0.   0.7]
 [ 0.   0.8]
 [ 0.   0.9]
 [ 0.1  0.9]
 [ 0.1  0.8]
 [ 0.1  0.7]
 [ 0.1  0.6]
 [ 0.1  0.5]
 [ 0.1  0.4]
 [ 0.1  0.3]
 [ 0.1  0.2]
 [ 0.1  0.1]
 [ 0.1  0.0]

2 个答案:

答案 0 :(得分:1)

如果第一列在数组的下半部分保持不变:

my_array[10:] = my_array[10:][::-1]

或者如果您的数组不是固定大小:

my_array[my_array.shape[0]/2:] = my_array[my_array.shape[0]/2:][::-1]

答案 1 :(得分:1)

如果您有指定的ymin, ymax, ystep,则可以执行以下操作:

import numpy as np
ymin, ymax, ystep = 0, 1, 0.1
z = np.arange(ymin, ymax, ystep)
x = np.repeat(z, len(z))
y = np.tile(np.tile((z, z[::-1]), (1, 1)).flatten(), len(z)/2)
arr = np.vstack((x, y)).T

>>>arr[:20]
array([[ 0. ,  0. ],
       [ 0. ,  0.1],
       [ 0. ,  0.2],
       [ 0. ,  0.3],
       [ 0. ,  0.4],
       [ 0. ,  0.5],
       [ 0. ,  0.6],
       [ 0. ,  0.7],
       [ 0. ,  0.8],
       [ 0. ,  0.9],
       [ 0.1,  0.9],
       [ 0.1,  0.8],
       [ 0.1,  0.7],
       [ 0.1,  0.6],
       [ 0.1,  0.5],
       [ 0.1,  0.4],
       [ 0.1,  0.3],
       [ 0.1,  0.2],
       [ 0.1,  0.1],
       [ 0.1,  0. ]])
       #keeps going
       #...
       #[0.9,  0.9],
       #...
       #[0.9,  0. ]]