Python NumPy Array Indexing Explained - Learn with an Example

  • Python
  • Thread starter EngWiPy
  • Start date
  • Tags
    Array
In summary, this conversation discusses a piece of code in Python that uses the sklearn library to load data and perform classification. The code includes features, feature names, target, target names, and labels. There is a question about how labels contains 150 elements when target names only has 3 elements, and the conversation suggests that this is not a numpy indexing issue but rather a result of sklearn's data processing. The conversation also mentions the importance of reading the sklearn documentation and having knowledge about classification problems in machine learning.
  • #1
EngWiPy
1,368
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
  • #2
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
 

FAQ: Python NumPy Array Indexing Explained - Learn with an Example

1. What is NumPy array indexing in Python?

NumPy array indexing in Python is a way to access specific elements or groups of elements within a NumPy array. It allows you to retrieve and manipulate data in an organized and efficient manner.

2. How does NumPy array indexing work?

NumPy array indexing uses a syntax similar to regular Python lists, where square brackets are used to access elements by their index. However, NumPy also allows for more advanced indexing techniques, such as using boolean arrays or even other arrays as indices.

3. What is the difference between basic and advanced indexing in NumPy?

Basic indexing in NumPy refers to using integers or slices to access specific elements or ranges of elements within an array. Advanced indexing, on the other hand, allows for more complex methods of indexing, such as using boolean arrays or other arrays as indices.

4. Can you provide an example of NumPy array indexing?

Yes, for example, if we have an array called "arr" with the values [1, 2, 3, 4, 5], we can use basic indexing to access the third element by using arr[2], which would return the value 3. We can also use advanced indexing to access elements based on a condition, such as arr[arr > 3], which would return an array with the values [4, 5].

5. How can I use NumPy array indexing to manipulate data?

NumPy array indexing allows you to not only retrieve data, but also manipulate it in various ways. For example, you can use indexing to assign new values to specific elements or ranges of elements within an array, or to perform mathematical operations on specific elements.

Similar threads

Replies
5
Views
1K
Replies
2
Views
950
Replies
29
Views
2K
Replies
2
Views
1K
Replies
5
Views
3K
Replies
3
Views
2K
Back
Top