Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reclassify a numpy array in python between a range

I have an numpy array in Python and i need to classify between a range of value (>= 2 to < 5 = 100). I got an error message and I don't understand the use of a.any() or a.all()

    import numpy as np
    myarray = np.array([[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]])
    myarray[myarray >= 2 and myarray < 5] = 100

    Traceback (most recent call last):
          File "<input>", line 1, in <module>
        ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
like image 594
Gianni Spear Avatar asked Jan 11 '16 23:01

Gianni Spear


People also ask

What is __ Array_interface __?

__array_interface__ A dictionary of items (3 required and 5 optional). The optional keys in the dictionary have implied defaults if they are not provided. The keys are: shape (required) Tuple whose elements are the array size in each dimension.

What does .values do in NumPy?

values returns a numpy array with the underlying data of the DataFrame, without any index or columns names.


1 Answers

You were so close.

>>> myarray[(myarray >= 2) & (myarray < 5)] = 100
>>> myarray
array([[  1, 100, 100, 100,   5],
       [  1, 100, 100, 100,   5],
       [  1, 100, 100, 100,   5]])
like image 197
wim Avatar answered Nov 05 '22 05:11

wim