Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max value of a 3d array in python

I want to find the max value of a 3d array in python. I tried

image_file1 = open("lena256x256.bmp","rb")
img_i = PIL.Image.open(image_file1)
pix = numpy.array(img_i);
maxval= max(pix)

but i am getting an error

 File "test.py", line 31, in <module>
    maxval= max(pix)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I cannot catch my mistake here, please help me.

like image 720
Ajay Soman Avatar asked Apr 09 '13 10:04

Ajay Soman


People also ask

How do you find the max value in an array in Python?

The max() Function — Find the Largest Element of a List. In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.

Can you have a 3d array in Python?

In Python to initialize a 3-dimension array, we can easily use the np. array function for creating an array and once you will print the 'arr1' then the output will display a 3-dimensional array.

What is Max NumPy in Python?

maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned.


2 Answers

You are using the builtin max function that does not understand multidimensional NumPy arrays. You must instead use one of:

  • pix.max()
  • numpy.max(pix)
  • numpy.amax(pix)

These are also faster than the builtin in the case of 1D NumPy arrays.

like image 87
Janne Karila Avatar answered Sep 20 '22 15:09

Janne Karila


Max is expecting a single value, the error message should be quite clear, you want to use amax instead.

maxval = numpy.amax(pix)
like image 43
Bas Jansen Avatar answered Sep 23 '22 15:09

Bas Jansen