Python Python NumPy Array Indexing Explained - Learn with an Example

  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
The discussion centers around a Python code snippet using the scikit-learn library to load the Iris dataset. The user queries why the `labels` array has 150 elements while `target_names` only contains 3 elements. The response clarifies that this is not a NumPy indexing issue, as the user did not import NumPy and the data object uses a structure that allows for categorical indexing. It emphasizes the importance of understanding the scikit-learn documentation and basic concepts of classification in machine learning to grasp how the indexing works in this context. The conversation highlights the need for familiarity with the library and its functionalities to avoid confusion regarding data shapes and indexing.
EngWiPy
Messages
1,361
Reaction score
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
 
Technology news on Phys.org
S_David said:
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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 20 ·
Replies
20
Views
3K