0

For example, if I define my array as my_array = np.array([1,2,3]),

What is the difference between max(my_array)and my_array.max() ?

Is this one of those cases where one is 'syntactic sugar' for the other? Also, why does the first one work with a Python list, but the second doesn't?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    The first one is [`__builtin__.max()`](https://docs.python.org/3/library/functions.html#max) and the other one is [`numpy.ndarray.max`](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.ndarray.max.html) – pault Oct 26 '18 at 20:08
  • See also: [numpy max vs amax vs maximum](https://stackoverflow.com/questions/33569668/numpy-max-vs-amax-vs-maximum) – pault Oct 26 '18 at 20:13
  • `[1,2,3].max()` gives an error that explains why it doesn't work. `max` method is not defined for lists. An `AttributeError` like this is basic Python, so you need to learn its significance well. – hpaulj Oct 27 '18 at 00:16

1 Answers1

1

As people have stated in the comments of your question, they are referencing two different functions.

max(my_array) is a built in Python function available to any sequence data in python.

Whereas, my_array.max() is referencing a function within the object. In this case my_array is from the Numpy array class. Numpy does not recognize the list data type for this function. However, this method will provide a speed improvement over the max(my_array) whenever you are using numpy data sequences.

As a rule of thumb if the variable has a variable.someMethod() the function is a method and is specific to the class of object. If the function is called as function(variable) then the function is either a part of the python distribution or the file/class you are working with.

Jordan
  • 83
  • 2
  • 10