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 in Python by ana1504.k
• 7,870 points
23 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 by SDeb
• 13,160 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
50 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
104 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,520 points
1,631 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
• 56,520 points
87 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
• 56,520 points
67 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
• 56,520 points
32 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 in Python by SDeb
• 13,160 points
45 views
0 votes
1 answer

Dictionary in NumPy array

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

answered Jan 18 in Python by SDeb
• 13,160 points
888 views
0 votes
1 answer

Shift all indices in NumPy array

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

answered Feb 26 in Python by SDeb
• 13,160 points
307 views