Python: Compare array elements with float, getting a boolean list - python

I want to compare all elements of a two-dimensional list
array with a float
x.
The result should be a list:
b = [[True, False,...],...].
I tried it like that:
import numpy as np
array = [[a1,a2,...], [a3,a4,...],...,]
x = 2.0
b = np.array([a >= x for a in array])`
"TypeError: '>=' not supported between instances of 'list' and 'float'"
When I use a one-dimensional list it works fine.
Thanks in advance!!

b = np.array([[a >= x for a in row] for row in array])

Related

Error then trying to multiply two arrays by using '#' [duplicate]

This question already has answers here:
Matrix Multiplication in pure Python?
(12 answers)
Closed 2 months ago.
I am trying to multiply two arrays by using '#' but this is the error I get
a#b
unsupported operand type(s) for #: 'list' and 'list'
I read that '#' is used for matrix multiplication, which is what I need
From the error message it appears as though you are using lists. You need to use arrays for which you can use the numpy module like this:
import numpy as np
x = [1,2,3,4]
y = [10,20,30,40]
x = np.array(x)
y = np.array(y)
z = x#y
print(z)
which returns this:
300
If you want to multiply lists without numpy you can do this:
# method 1
z = 0
for i, v in enumerate(x):
z = z + (x[i]*y[i])
print(z)
# method 2
z = sum([x*y for x,y in zip(x,y)])
print(z)
Both would return the same value as above...

only integer scalar arrays can be converted to a scalar index error

I'm trying to make a bucket sort and I keep getting the error 'only integer scalar arrays can be converted to a scalar index' but I'm not sure where the problem is. Any help would be appreciated!
def bucketSort(self, array):
n = len(array)
B = [[]for i in range(n)]
for j in range(array):
index = math.floor(array[j]*n)
B[int(index)].append(array[j])
for k in range(len(B)):
B[k] = self.insertionSort(B[k])
m = 0
for i in range(len(B)):
for j in range(len(B[i])):
array[m] = B[i][j]
m+=1
return array
traceback is:
<ipython-input-109-fe41639833f2> in bucketSort(self, array)
90 B = [[]for i in range(n)]
91
---> 92 for j in range(array):
93 index = math.floor(array[j]*n)
94 B[int(index)].append(array[j])
TypeError: only integer scalar arrays can be converted to a scalar index```
tl;dr: range() expects integers as arguments, while array is, well, an array and not an integer. From your use case it seems that you should use range(len(array)) instead of range(array).
Long version:
The Python 3 range() built-in method actually constructs an iterator object that, when iterated, returns a sequence of integers. From the documentations, the arguments passed to range() must either int values, or of some type that implements the __index__ method. The __index__ method is a special dunder method that allows using a user-defined object as an index in array indexing. The method must return an int.
NumPy arrays do implement the __index__ method, however, it only returns an int when the array object is indeed a scalar, i.e., when the array has a shape of (). When the array is not a scalar, it throws this TypeError exception that you see. For example:
import numpy as np
l = [1, 2, 3]
np.array(1).shape # == ()
l[np.array(1)] # == l[1]
np.array([1]).shape # == (1,)
l[np.array([1])] # TypeError: only integer scalar arrays can be converted to a scalar index
l['string'] # TypeError: list indices must be integers or slices, not str

Find indices of 2D numpy arrays that meet a condition

I have a large 2D numpy array and want to find the indices of the 1D arrays inside it that meet a condition: e.g., have at least a value greater than a given threshold x.
I already can do it the following way but is there a shorter, more efficient way to do it?
import numpy
a = numpy.array([[1,2,3,4,5], [1,2,3,4,20], [1,2,2,4,5]])
indices = []
i = 0
x = 10
for item in a:
if any(j > x for j in item):
indices.append(i)
i += 1
print(indices) # gives [1]
You could use numpy's built-in boolean operations:
import numpy as np
a = np.array([[1,2,3,4,5], [1,2,3,4,20], [1,2,2,4,5]])
indices = np.argwhere(np.any(a > 10, axis=1))

Get days as float from an array of Timedelta and NaN

I have a simple problem that causes me a lot of troubles: I have a a big 2D array which is a mixture of datetime.Timedelta objects and np.nan, simplified looking like this:
tdarray = np.array([dt.timedelta(days=5), np.nan])
Now I want to get the days and float/integer from the timedelta object, while leaving the np.nan as it is, i.e. the result should be np.array([ 5., nan]).
Getting the days from the timedelta object is easy with .days, and applying the function the array should work e.g. with np.fromiter and then reshaping. But how do I catch the error that occurs when trying to get the days from NaN? I tried masking, but this also fails with the AttributeError that the MaskedArray does not have the attribute days. Is there any simple solution?
Make use of the fact that np.nan is the only object not equals to itself. Note that if your array contains other objects, they should have the equality operator defined, else this will throw an error.
tdarray = np.asarray([dt.timedelta(days=5), np.nan])
mask = tdarray == tdarray # This gives array([True, False])
tdarray[mask] = [x.days for x in tdarray[mask]]
# Optionally cast to float
tdarray = tdarray.astype(np.float64)
Or you can simply rebuild the array
tdarray = np.asarray([x.days if x == x else x for x in tdarray],
dtype=np.float64)
And if tdarray is a ND array (N > 1) then
shape = tdarray.shape
tdarray = np.asarray([x.days if x == x else x
for x in tdarray.ravel()],
dtype=np.float64).reshape(shape)

How can I check if each row in a matrix is equal to an array and return a Boolean array containing the result?

How can I check if each row in a matrix is equal to an array and return a Boolean array containing the result using NumPy? e.g.
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([4,5,6])
# Expected Result: [False,True,False]
The neatest way I've found of doing this, is:
result = np.all(a==b, axis=1)

Categories

Resources