Error trying to calculate element with the highest value of an array

0

I have a problem with the following function:

def conjunmas():
    for (conjunto) in (arreglo):
        totalm = 0
        cma = 0        
        for (unidad) in (arreglo):
            totalm = (totalm + unidad)
            if (totalm > cma):
                cma = totalm

    print(cma)  

When doing the test I get the following error:

  

ValueError: The truth value of an array with more than one element is   ambiguous. Use a.any () or a.all ()

I am grateful if you indicate the problem, and if I am using the code well.

    
asked by Gabriel 10.05.2016 в 02:49
source

1 answer

1

The issue of numpy.all () or .any () tends to jump when we directly compare a numpy matrix against another matrix in a statement since it can not get a single answer for the iteration of all the elements of the matrix

What comes to say is that when comparing a matrix, the saying "greater than" or "less than" is ambiguous, since it is composed of a "list" of one or more dimensions, and for that ... numpy provides the .all () and .any (), which comes to say if you want to compare all the values of the indicated axis or that the statement is for any iterated element on the axis. To show a button:

import numpy as np
a = np.array([1,2,3,4,5])  # Vector 1d
b = np.array([[0,0,0,0,0],[6,6,6,6,6]])  # Matriz 2d
c = np.array([[1,1,1,1,1],[3,7,0,0,8]]) # Otra Matriz de 2d

If we compare directly with each other:

a < b
Out[4]: array([[False, False, False, False, False], [ True,  True,  True,  True,  True]], dtype=bool)
b < a
Out[5]: array([[ True,  True,  True,  True,  True], [False, False, False, False, False]], dtype=bool)
c > b
Out[7]:array([[ True,  True,  True,  True,  True], [False,  True, False, False,  True]], dtype=bool)

As we can see in the last comparison, it gives us a bool for each element since we have compared all of them, but, if we say if c > b waiting for a True or False numpy does not know what we are referring to since the statement is ambiguous.

if c > b:
    print('mayor')

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-9893a7236442>", line 1, in <module>
    if c > b:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

the .all () and .any () return True if all the values are True, in case of all or if any is True in case of any.

d = np.array([True,False,False,True,True])
d.all()
False
d.any()
True

That for the comparative cases at the beginning could be:

d.any()>c.all()
True
# que seria lo mismo que True > False

or

(c > b).any()
True
(c > b).all()
False

What would be the case of applying .all .any to the matrix resulting from the first comparisons of c> I hope I have been helpful. The documentation of numpy for these two methods where they explain much better than me what they do you have in:

numpy.any ()

numpy.all ()

    
answered by 17.03.2017 в 18:25