2

The answer given here explains how to resize an array from

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

to

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

using np.repeat. Given the lower array, what is the best way to shrink it to the upper?

Community
  • 1
  • 1
John Crow
  • 927
  • 3
  • 13
  • 26

2 Answers2

7

Slice across both axes with appropriate stepsize -

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

Sample run -

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]])
Divakar
  • 218,885
  • 19
  • 262
  • 358
-1

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


Repeat elements of an array.

input :

a = 

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

output:

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

using 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]])
Almadani
  • 125
  • 1
  • 4