Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is x[ : , 0]?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading