35

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?

starball
  • 20,030
  • 7
  • 43
  • 238
pjehyun
  • 911
  • 2
  • 9
  • 11

4 Answers4

84

Python has a min() built-in function:

>>> darr = [1, 3.14159, 1e100, -2.71828]
>>> min(darr)
-2.71828
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    If your (numpy) array is more than one dimensional then you must use `darr.min()`, rather than `min(darr)`. – Mead Mar 17 '22 at 18:51
25

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 3
    True, though under the hood `np.amin(darr)` ends up calling `np.asarray(darr).min()`, and is about 50% slower due to extra `__array_wrap__` code. – unutbu May 16 '13 at 03:11
0

You need to iterate the 2d array in order to get the min value of each row, then you have to push any gotten min value to another array and finally you need to get the min value of the array where each min row value was pushed

def get_min_value(self, table):
    min_values = []
    for i in range(0, len(table)):
        min_value = min(table[i])
        min_values.append(min_value)

    return min(min_values)
Pedro Machado
  • 158
  • 2
  • 10
0

If min value in array, you can try like:

>>> mydict = {"a": -1.5, "b": -1000.44, "c": -3}
>>> min(mydict.values())
-1000.44
Artem Baranov
  • 863
  • 11
  • 11