1

What's that slicing doing here-

data = np.loadtxt(input_file, delimiter=',')
X, y = data[:, :-1], data[:, -1]

I know that -1 inverts the list and colon is start and stop signals.But what's the comma (,) doing in the middle?

Raheel Aslam
  • 444
  • 1
  • 9
  • 28
Almuntasir Abir
  • 320
  • 1
  • 5
  • 16
  • 1
    Let's clear one misunderstanding: that is not a Python list! It is a Python object which supports `[]` (`__getitem__`) operator, but it is not a list! – zvone Feb 11 '18 at 14:35

1 Answers1

4

NumPy supports multiple dimensions. In your case that's a 2D slice: the part before the comma slices the first dimension, and after the comma slices the second dimension. This implies the data is 2D or greater, and indeed loadtxt() does produce 2D arrays.

Ref: https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    `data[:][:-1]` would just be equivalent to `data[:-1]`; the first slicing operation is essentially a no-op. So I don't think it's helpful to say that you can think of `data[:, :-1]` as `data[:][:-1]` - the first takes all but the last column of a 2d array, while the second takes all but the last row of a 2d array. Maybe you meant `data[:, None][None, :-1]`, or `data[:][None, :-1]`, but that just introduces more stuff to explain :-(. – Mark Dickinson Feb 11 '18 at 16:15
  • @MarkDickinson: Good point, I deleted that part of the answer. – John Zwinck Feb 11 '18 at 16:22
  • So to be clear `data[:, :-1]` here -1 doesn't inverse the 2D matrix but excludes the last column? – Almuntasir Abir Feb 11 '18 at 17:34
  • @AlmuntasirAbir: Correct, because the commas divide the slices, so the second slice is `:-1` which is everything except the last. `[:, ::-1]` would flip the matrix. – John Zwinck Feb 12 '18 at 00:36