Skip to main content
  1. About
  2. For Teams
Asked
Viewed 75 times
0

I have an array of numbers with shape (1220,) called x

I'm looking at numbers greater than 1.0,

mask1 =  [i for i in x if i>1.0 ]

returning

[1.2958354, 1.0839227, 1.1919032]

My question now is then how am able to determine the index location of these values in my initial array x?

I've tried each individually, but an error occurs

list(x).index(1.2958354)

ValueError: 1.2958354 is not in list

7 Answers 7

4

You've tagged this as numpy, and describe a shape (not len). Which leads me to think you have a numpy array.

In [665]: x=np.random.rand(10)
In [666]: x
Out[666]: 
array([ 0.6708692 ,  0.2856505 ,  0.19186508,  0.59411697,  0.1188686 ,
        0.54921919,  0.77402055,  0.12569494,  0.08807101,  0.11623299])
In [667]: x>.5
Out[667]: array([ True, False, False,  True, False,  True,  True, False, False, False], dtype=bool)
In [668]: list(x).index(.6708692)
ValueError: 0.6708692 is not in list

The reason for the ValueError is that you are looking for a float, and those often don't exactly match. If the array was of ints, then such an index would work.

In [669]: list(np.arange(10)).index(5)
Out[669]: 5

This reasoning applies whether x was an array or a list.

numpy has a where that returns the index of boolean true values in an array

In [670]: np.where(x>.5)
Out[670]: (array([0, 3, 5, 6], dtype=int32),)

x>.5 is the boolean array as shown above, and [0,3,5,6] the index values where this is true.

In [671]: x[np.where(x>.5)]
Out[671]: array([ 0.6708692 ,  0.59411697,  0.54921919,  0.77402055])

The equality test doesn't work any better

In [672]: x[np.where(x==0.6708692)]
Out[672]: array([], dtype=float64)

For floats there is the concept of close - the difference is within some tolerance (np.allclose is particularly useful):

In [679]: np.where(np.isclose(x,0.59411697))
Out[679]: (array([3], dtype=int32),)

For lists, one of the enumerate solutions is great, and also works with 1d arrays. But it is already an array, use the where.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use enumerate function, for example:

mask1 =  [(i, value) for i, value in enumerate(x) if value>1.0 ]
print mask1

Comments

1

Try:

mask1 =  [i for i in range(len(x)) if x[i]>1.0]

Comments

1

You can use the index function on x, which will return the first index of each value. To get a list of all the indices from mask1 try:

map(x.index, mask1)

Comments

1

mask_1 = [index for index, value in enumerate(x) if value > 1.0]

Comments

1

Use enumerate to create tuple pairs of the index and the filtered value, then use zip with the * operator to unpack the variables into separate lists.

a = np.array([0, 1, 2, 3])

idx, vals = zip(*[(i, v) for i, v in enumerate(a) if v > 1])

>>> idx
(2, 3)

>>> vals
(2, 3)

Comments

1

Try using enumerate() to get the index and value together:

mask1 = [(i,v) for i,v in enumerate(x) if v > 1.0]

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.