find a minimum value in an array of floats


Question

how would one go about finding the minimum value in an array of 100 floats in python? I have tried minindex=darr.argmin() and print darr[minindex] with import numpy (darr is the name of the array)

but i get: minindex=darr.argmin()

AttributeError: 'list' object has no attribute 'argmin'

what might be the problem? is there a better alternative?

thanks in advance

1
27
8/17/2010 3:02:08 AM

Accepted Answer

Python has a min() built-in function:

>>> darr = [1, 3.14159, 1e100, -2.71828]
>>> min(darr)
-2.71828
62
8/17/2010 3:03:48 AM

If you want to use numpy, you must define darr to be a numpy array, not a list:

import numpy as np
darr = np.array([1, 3.14159, 1e100, -2.71828])
print(darr.min())

darr.argmin() will give you the index corresponding to the minimum.

The reason you were getting an error is because argmin is a method understood by numpy arrays, but not by Python lists.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon