I don’t understand what it does, and only saw it used by a sklearn-object.
On trying some testing like this
x = [(1, 2), (2, 3), (3, 4)]
y = [[1, 2], [2, 3], [3, 4]]
print(y[:, 0])
I got this error (with both x and y):
TypeError: list indices must be integers or slices, not tuple
My assumption was that the : before the comma tells Python to take all entries, while the 0 specifies which ‘subset’.
How does one use this expression properly and what does it do?
>Solution :
As explained here this is a numpy-specific notation to index arrays, not plain python. This is why it does not work on your code. In your (initial) case, the sklearn object probably wraps a numpy array that supports the numpy slicing syntax.
In your specific case, it would work as follows:
import numpy as np
y = np.array([[1, 2], [2, 3], [3, 4]])
print(y[:, 0])
# prints: [1 2 3]
This would yield all indexes along the first axis (i.e. use full column vectors), but use only index 0 in the second axis (i.e. use only the first column vector).