c00*_*ter 2 python arrays numpy repeat
我有一个我想重复的数组.
test = numpy.array([(1, 11,), (2, 22), (3, 33)])
现在
numpy.repeat(test, 2, 0)
numpy.repeat(test, 2, 1)
Run Code Online (Sandbox Code Playgroud)
结果是
array([[ 1, 11],
[ 1, 11],
[ 2, 22],
[ 2, 22],
[ 3, 33],
[ 3, 33]])
array([[ 1, 1, 11, 11],
[ 2, 2, 22, 22],
[ 3, 3, 33, 33]]).
Run Code Online (Sandbox Code Playgroud)
而
numpy.tile(test, 2)
Run Code Online (Sandbox Code Playgroud)
结果是
array([[ 1, 11, 1, 11],
[ 2, 22, 2, 22],
[ 3, 33, 3, 33]]).
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到这个结果呢?
array([[ 1, 11],
[ 2, 22],
[ 3, 33],
[ 1, 11],
[ 2, 22],
[ 3, 33]])
Run Code Online (Sandbox Code Playgroud)
或者,对于我的用例,我只使用重复值一次.为了避免内存分配,有没有办法让某个重复序列的生成器以某种方式?
np.tile 允许您为每个轴指定重复(作为元组)
In [370]: np.tile(test,(2,1))
Out[370]:
array([[ 1, 11],
[ 2, 22],
[ 3, 33],
[ 1, 11],
[ 2, 22],
[ 3, 33]])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |