-2

While going through Sklearn Tutorial I came accross this piece of code

order_centroids = original_space_centroids.argsort()[:, ::-1]

I do not understand what :, does.

The data stored in order_centroids is

array([[28060, 36086, 36087, ..., 29380, 28915, 28914],
       [28060, 33378, 33379, ...,  9698, 26784, 15313],
       [28060, 36209, 36211, ..., 15303, 22350, 48197],
       ...,
       [28060, 36664, 36665, ..., 47821, 32892, 37525],
       [56120, 31887, 31888, ...,  9603, 51250, 30224],
       [56120, 33902, 33903, ..., 20843, 14948, 30316]])
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ashish Cherian
  • 367
  • 1
  • 3
  • 15

2 Answers2

2

The comma separates slicing for each dimension in an array. So [:,::-1] is getting all items from the first dimension and all items from the second dimension but reversing the order on the second dimension with the “step” set to -1.

Ethan
  • 1,363
  • 1
  • 6
  • 8
0

It's just a normal comma operator that defines a tuple. Your code

order_centroids = original_space_centroids.argsort()[:, ::-1]

is equivalent to

order_centroids = original_space_centroids.argsort().__getitem__(
    (slice(None, None, None), slice(None, None, -1)
)
chepner
  • 497,756
  • 71
  • 530
  • 681