Lin Ma Lin Ma - 3 months ago 8
Python Question

confused about Python list syntax

Here is the code and related document (http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html#sklearn.datasets.load_iris), I am confused by this line,

data.target[[10, 25, 50]]
, confused why using double
[[]]
, if anyone could clarify, it will be great.

from sklearn.datasets import load_iris
data = load_iris()
print data.target[[10, 25, 50]]
print list(data.target_names)


thanks in advance,
Lin

Answer

Your confusion is understandable: this isn't "standard" Python by any means.

data.target in this case is an ndarray from numpy:

In [1]: from sklearn.datasets import load_iris
   ...: data = load_iris()
   ...: print data.target[[10, 25, 50]]
   ...: print list(data.target_names)
[0 0 1]
['setosa', 'versicolor', 'virginica']

In [2]: print type(data.target)
<type 'numpy.ndarray'>

numpy's ndarray implementation allows you to create a new array by providing a list of indices of the items you want. For example:

In [13]: data.target
Out[13]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

In [14]: data.target[1]
Out[14]: 0

In [15]: data.target[[1,2,3]]
Out[15]: array([0, 0, 0])

In [16]: print type(data.target[[1,2,3]])
<type 'numpy.ndarray'>

and it likely does this by overriding __getitem__.

For more information, see Indexing in the NumPy array documentation:

Comments