Home > other >  What is x[ : , 0]?
What is x[ : , 0]?

Time:12-28

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?

CodePudding user response:

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).

CodePudding user response:

What you express would work with more complex objects, like numpy (and its famous slicing). In the case of vanilla Python, this is not possible. To access a specific number (in your case) you would have to do x[2][1] (yielding 4 in your example).

To achieve what you want (take the first item of each tuple), you would do [item[0] for item in y]. This is list comprehension: iterate through y, take the object at the first index of each item, and make a list out of that.

  • Related