Python NumPy Array Indexing Explained - Learn with an Example

  • Context: Python 
  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
SUMMARY

The discussion focuses on Python's NumPy array indexing in the context of the Iris dataset from the scikit-learn library. The user queries how the labels array can contain 150 elements when the target_names array only has 3 elements. The explanation clarifies that the issue is not with NumPy indexing but rather with the way the scikit-learn library handles classification problems, where the target array indexes into the target_names array to create the labels. The user is encouraged to review the scikit-learn documentation for a deeper understanding.

PREREQUISITES
  • Understanding of Python programming
  • Familiarity with scikit-learn library (version 0.24 or later)
  • Basic knowledge of machine learning classification problems
  • Awareness of NumPy array structures and indexing
NEXT STEPS
  • Review the scikit-learn documentation on the Iris dataset
  • Learn about NumPy array indexing and slicing techniques
  • Explore classification algorithms in scikit-learn
  • Study the differences between NumPy and pandas for data manipulation
USEFUL FOR

Data scientists, machine learning practitioners, and Python developers looking to enhance their understanding of array indexing and classification in scikit-learn.

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
 

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
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 20 ·
Replies
20
Views
3K