Skip to main content

Finding the Maximum, Minimum, and Sum of NumPy Array

In NumPy, we can find maximum, minimum elements in a Ndarray and also sum of Ndarray by using following function.

ndarray.max():

This function of ndarray.numpy returns the maximum value of ndarray object.

Syntax:

numpy.max(input_array)

Example:

# import numpy library
import numpy as np
  
# create numpy array 
a = np.array([10,20,30,40,50])
  
# find the maximum element in the array
b = np.max(a)
print(b)

Output: 50

ndarray.min():

This function of ndarray.numpy returns the minimum value of ndarray object.

Syntax:

numpy.min(input_array)

Example:

# import numpy library
import numpy as np
  
# create numpy array 
a=np.array([10,20,30,40,50])
  
# find the minimum element in the array
b=np.min(a)
print(b)

Output: 10

numpy.sum():

This function is used to return the sum of all elements of an array over specified axis. It is possible to add rows and column elements of an array.

Syntax:

numpy.sum(array, axis=None, dtype=None, out=None, keepdims=<no value>, initial=<no value>)

Parameters:

  1. array: It is the array whose elements are used as the source for sum.
  2. axis: It describe the axis along which a sum is performed.
  3. dtype: It is used to specify the type of return array.
  4. out: It is used to specify the array to place the result.
  5. keepdims: It defines the boolean value.If the parameter is set to True, the axis which are reduced are left t in the result as dimensions with size one.
  6. initial: It specifies the starting value of sum.

Example:

import numpy as np
a=np.array([1,2,3,4,5])
b=np.sum(a)
print(b)

Output: 15

Submitted by devanshi.srivastava on August 6, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.