Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any function in python which can perform the inverse of numpy.repeat function?

For example

x = np.repeat(np.array([[1,2],[3,4]]), 2, axis=1)

gives you

x = array([[1, 1, 2, 2],
          [3, 3, 4, 4]])

but is there something which can perform

 x = np.*inverse_repeat*(np.array([[1, 1, 2, 2],[3, 3, 4, 4]]), axis=1)

and gives you

x = array([[1,2],[3,4]])
like image 613
Soumya Shubhra Ghosh Avatar asked Aug 02 '16 13:08

Soumya Shubhra Ghosh


1 Answers

Regular slicing should work. For the axis you want to inverse repeat, use ::number_of_repetitions

x = np.repeat(np.array([[1,2],[3,4]]), 4, axis=0)
x[::4, :]  # axis=0
Out: 
array([[1, 2],
       [3, 4]])

x = np.repeat(np.array([[1,2],[3,4]]), 3, axis=1)

x[:,::3]  # axis=1
Out: 
array([[1, 2],
       [3, 4]])


x = np.repeat(np.array([[[1],[2]],[[3],[4]]]), 5, axis=2)
x[:,:,::5]  # axis=2
Out: 
array([[[1],
        [2]],

       [[3],
        [4]]])
like image 84
ayhan Avatar answered Oct 06 '22 10:10

ayhan