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:
[10, 25, 50]
are being indexed from thedata.target
array – OneCricketeerprint type(data.target)
, return is<type 'numpy.ndarray'>
. Confused what do you mean list containing a single element, could you help to elaborate a bit more? Thanks. – Lin Ma[[]]
? – Lin Ma[[]]
in[[10, 25, 50]]
– Lin Ma