I am trying to put all elements of rbs into a new array if the elements in var(another numpy array) is >=0 and <=.1 . However when I try the following code I get this error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
rbs = [ish[4] for ish in realbooks]
for book in realbooks:
var -= float(str(book[0]).replace(":", ""))
bidsred = rbs[(var <= .1) and (var >=0)]
any ideas on what I’m doing wrong?
The
andkeyword is used by Python to test between two booleans. How can an array be a boolean? If 75% of its items areTrue, is itTrueorFalse? Therefore, numpy refuses to compare the two.Therefore, use either
c[a & b]orc[np.logical_and(a, b)]. Either approach to combining theaandbarrays –a & bornp.logical_and(a, b)– will produce a boolean array with the same size as the input arraysaandb, which is necessary if the next step is to index into a same-sized arrayc.A
listof boolean values cannot be used for indexing instead. NumPy will interpret that as a list of index values (treatingTrueas1andFalseas0), so the output would contain multiple copies of the first two elements of the array, rather than a masked version.Similarly, to choose elements from
cwhere either of the corresponding elements fromaorbis true, usec[a | b]orc[np.logical_or(a,b)].