Array Indexing

  • Python
  • Thread starter EngWiPy
  • Start date
  • #1
1,367
61
Hello all,

I have this piece of code in Python

Code:
from sklearn.datasets import load_iris

data = load_iris()

features = data['data']
feature_name = data['feature_names']
target = data['target']
target_names = data['target_names']
labels = target_names[target]

print(target.shape)#This outputs (150,)
print(target_names.shape)#This outputs (3,)
print(labels.shape)#This output (150,) but how?
target_names contains 3 elements, how does labels contain 150 elements? How does indexing work in Python's NumPy?

Thanks in advance
 

Answers and Replies

  • #2
StoneTemplePython
Science Advisor
Gold Member
2019 Award
1,167
569
Hello all,

I have this piece of code in Python

Code:
from sklearn.datasets import load_iris

data = load_iris()

features = data['data']
feature_name = data['feature_names']
target = data['target']
target_names = data['target_names']
labels = target_names[target]

print(target.shape)#This outputs (150,)
print(target_names.shape)#This outputs (3,)
print(labels.shape)#This output (150,) but how?
target_names contains 3 elements, how does labels contain 150 elements? How does indexing work in Python's NumPy?

Thanks in advance
This is not a numpy indexing issue.

One tell-tale sign is they did not import numpy anywhere. Another is that your 'data' object has excel style 'column names' being passed to it, which would cause a failure in numpy because numpy only allows numeric indexing.

How much time did you spend reading the sklearn docs? And how much do you know about classification problems in machine learning?

it seems pretty straightforward if you know a little about both and read this page:

http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html
 

Related Threads on Array Indexing

Replies
20
Views
1K
Replies
3
Views
9K
Replies
21
Views
1K
Replies
7
Views
7K
Replies
1
Views
1K
  • Last Post
Replies
10
Views
3K
  • Last Post
Replies
14
Views
4K
  • Last Post
Replies
4
Views
2K
Top