numpy.repeat的反义词是什么?

时间:2016-11-15 19:03:02

标签: python arrays numpy

给出的答案here解释了如何从

调整数组大小
[1,5,9]
[2,7,3]
[8,4,6]

[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]

使用np.repeat。鉴于较低的阵列,将它缩小到上层的最佳方法是什么?

2 个答案:

答案 0 :(得分:5)

两个轴上的

Slice具有适当的步长 -

a[::2,::2] # 2 being stepsize here

示例运行 -

In [23]: a
Out[23]: 
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])

In [24]: a[::2,::2]
Out[24]: 
array([[1, 5, 9],
       [2, 7, 3],
       [8, 4, 6]])

答案 1 :(得分:-1)

<块引用>

np.repeat(a, repeats, axis=None)


重复数组的元素。

输入:

a = 

np.array([[1,5,9],
[2,7,3],
[8,4,6]])
print (a)

输出:

Out[76]: 
array([[1, 5, 9],
       [2, 7, 3],
       [8, 4, 6]])

使用np.repeat

In [78]: b = np.repeat(a,[2],axis = 1)

In [79]: b
Out[79]: 
array([[1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6]])

In [80]: c = np.repeat(b,[2],axis = 0)

In [81]: c
Out[81]: 
array([[1, 1, 5, 5, 9, 9],
       [1, 1, 5, 5, 9, 9],
       [2, 2, 7, 7, 3, 3],
       [2, 2, 7, 7, 3, 3],
       [8, 8, 4, 4, 6, 6],
       [8, 8, 4, 4, 6, 6]])