NumPy Array Indexing

0 votes

I have a rec-array which holds ages in one space, and corresponding values in another. I also have an array which is my desired subset of ages. For example:

ages = np.arange(100)
values = np.random.uniform(low=0, high= 1, size = ages.shape)
data = np.core.rec.fromarrays([ages, values], names='ages,values')
desired_ages = np.array([1,4, 16, 29, 80])

I'm trying to do is something like this:

data.values[data.ages==desired_ages]

But, it's not working. Can anyone tell me the solution to this?

Jul 26, 2019 in Python by ana1504.k
• 7,910 points
707 views

1 answer to this question.

0 votes

If you want to create a subarray containing only the values whose indexes are in desired_ages. Python doesn't have any syntax that directly corresponds to this. But list comprehensions can do a pretty nice job:

result = [value for index, value in enumerate(data.values) if index in desired_ages]

However, doing it this way results in Python scanning through desired_ages for each element in data.values, which is slow. If you could insert

desired_ages = set(desired_ages)

This might help!

answered Jul 26, 2019 by SDeb
• 13,300 points

Related Questions In Python

+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
1,062 views
0 votes
1 answer
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,754 views
0 votes
1 answer

How to save Numpy array as image in python?

If you have matplotlib, you can do: import ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,560 points
8,981 views
0 votes
1 answer

Numpy: Multiplying large arrays with dtype=int8 is SLOW

Unfortunately, as correctly underlined in the comments, the ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 58,020 points
3,042 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,020 points
987 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 58,020 points
1,306 views
0 votes
1 answer

Python Pandas: selecting element in array column

pa.loc[row] selects the row with label row. pa.loc[row, ...READ MORE

answered May 13, 2019 in Python by SDeb
• 13,300 points
13,400 views
0 votes
1 answer

Dictionary in NumPy array

You have a 0-dimensional array of object ...READ MORE

answered Jan 18, 2019 in Python by SDeb
• 13,300 points
8,635 views
0 votes
1 answer

Shift all indices in NumPy array

You can use the following : y = ...READ MORE

answered Feb 26, 2019 in Python by SDeb
• 13,300 points
5,007 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP