2016-01-11 8 views
6

Ich habe ein numpy Array in Python und ich muss zwischen einem Bereich von Wert (> = 2 bis < 5 = 100) zu klassifizieren. Ich habe eine Fehlermeldung und ich verstehe nicht die Verwendung von a.any() or a.all()reklassifizieren Sie ein numpy Array in Python zwischen einem Bereich

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() 

Antwort

6

Sie waren so nah dran.

>>> myarray[(myarray >= 2) & (myarray < 5)] = 100 
>>> myarray 
array([[ 1, 100, 100, 100, 5], 
     [ 1, 100, 100, 100, 5], 
     [ 1, 100, 100, 100, 5]]) 
Verwandte Themen