1

I have an array A=[A0,A1], where A0 is a 4x3 matrix, A1 is a 3x2 matrix. I want to compare A with a float, say, 1.0, element-wise. The expected return B=(A>1.0) is an array with the same size as A. How to achieve this?

I can copy A to C and then reset all elements in C to be 1.0, then do a comparison, but I think python (numpy/scipy) must have a smarter way to do this... Thanks.

2
  • Your A, having a dtype of object, is little more than a list of arrays. Most of the numpy power is in working with multidimensional arrays of numbers. Commented Mar 24, 2015 at 5:08
  • Some operations, like basic math ones, do 'pass through' to the inner arrays. I don't know if there's a list of what works and what doesn't. Commented Mar 24, 2015 at 16:48

2 Answers 2

2

Suppose we have the same shape of a array of arrays you mention:

>>> A=np.array([np.random.random((4,3)), np.random.random((3,2))])
>>> A
array([ array([[ 0.20621572,  0.83799579,  0.11064094],
       [ 0.43473089,  0.68767982,  0.36339786],
       [ 0.91399729,  0.1408565 ,  0.76830952],
       [ 0.17096626,  0.49473758,  0.158627  ]]),
       array([[ 0.95823229,  0.75178047],
       [ 0.25873872,  0.67465796],
       [ 0.83685788,  0.21377079]])], dtype=object)

We can test each elements with a where clause:

>>> A[0]>.2
array([[ True,  True, False],
       [ True,  True,  True],
       [ True, False,  True],
       [False,  True, False]], dtype=bool)

But not the whole thing:

>>> A>.2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

So just rebuild the array B thus:

>>> B=np.array([a>.2 for a in A])
>>> B
array([ array([[ True,  True, False],
       [ True,  True,  True],
       [ True, False,  True],
       [False,  True, False]], dtype=bool),
       array([[ True,  True],
       [ True,  True],
       [ True,  True]], dtype=bool)], dtype=object)
Sign up to request clarification or add additional context in comments.

Comments

0

Using List Comprehensions for 1 matrix

def compare(matrix,flo):
    return  [[x>flo for x in y] for y in matrix]

Assuming I understood your question correctly and for example

matrix= [[0,1],[2,3]]
print(compare(matrix,1.5))

should print [[False, False], [True, True]]

For a list of matrices:

def compareList(listofmatrices,flo):
    return [[[x>flo for x in y] for y in matrix] for matrix in listofmatrices]

or

def compareList(listofmatrices,flo):
    return [compare(matrix,flo) for matrix in listofmatrices]

UPDATE: recursive function:

def compareList(listofmatrices,flo):
    if(isinstance(listofmatrices, (int, float))):
        return listofmatrices > flo
    return [compareList(matrix,flo) for matrix in listofmatrices]

2 Comments

Thanks. Yes, 3 for-loops definitely will work. But I wonder if python/numpy has some built-in features to perform comparison of a tensor with a scalar element-wise? What I mean is if later I have a rank 100 tensor, for example, the handcrafted code will be cumbersome (100 for-loops, certainly don't want that...)
@Void you can do that recursivly.

Your Answer

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.