
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Max
What is the Max?
In mathematics, the "max" (maximum) refers to the largest value in a set of numbers. It identifies the greatest element, providing a measure of the highest point in a data set.
For example, in the set {3, 1, 4, 2}, the maximum is 4. The maximum is useful for understanding the upper bound of a data set.
The NumPy max() Function
The max() function in NumPy returns the largest value in an array. It can be applied to the entire array or along a specified axis to find the maximum value in each row or column.
You can also use the amax() function, which is an alias for max() function. Following is the basic syntax of the max() function in NumPy −
numpy.max(a, axis=None, out=None, keepdims=False)
Where,
- a: The input array or dataset from which the maximum value is to be found.
- axis: Specifies the axis along which the maximum value is computed. If None (default), the maximum value is computed over the entire array.
- out: This allows you to specify a location where the result will be stored. If None (default), the result is returned as a new array.
- keepdims: If True, the reduced dimensions are retained in the result, making it easier for broadcasting. If False (default), the result is squeezed.
Understanding the Max Calculation
The calculation of the maximum value in a dataset is simple and easy. The function scans through all the elements in the array and identifies the largest value. This process can be applied to arrays of any shape or size.
Example
Let us understand this concept with an example −
import numpy as np data = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) # Calculating the maximum value max_value = np.max(data) print("Maximum value:", max_value)
This will produce the following result −
Maximum value: 9
Computing Max along Different Axes
In NumPy, the axis parameter allows you to compute the maximum value along specific axes of a multi-dimensional array. The axis parameter refers to the direction along which the maximum value should be calculated. For example, in a 2D array −
- axis=0: Calculate the maximum value along the columns (vertical axis).
- axis=1: Calculate the maximum value along the rows (horizontal axis).
Example
In the following example, we are computing the maximum value along both axes of the 2D array −
import numpy as np # Create a 2D array data_2d = np.array([[1, 3, 5], [2, 4, 6], [7, 8, 9]]) # Calculate the maximum value along axis 0 (columns) max_axis_0 = np.max(data_2d, axis=0) # Calculate the maximum value along axis 1 (rows) max_axis_1 = np.max(data_2d, axis=1) print("Maximum value along axis 0:", max_axis_0) print("Maximum value along axis 1:", max_axis_1)
In the output below, the maximum value along axis 0 is computed by finding the largest element in each column. The maximum value along axis 1 is calculated by finding the largest element in each row −
Maximum value along axis 0: [7 8 9] Maximum value along axis 1: [5 6 9]
Max for Higher-Dimensional Arrays
The numpy.max() function also works for arrays with more than two dimensions. You can specify the axis along which to calculate the maximum value, and the function will return the maximum value for that axis while retaining the other dimensions. If no axis is specified, the maximum value is calculated over the entire array.
Example
Following is an example to compute maximum value of a 3D array −
import numpy as np # Create a 3D array data_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Maximum value along axis 0 max_3d_axis_0 = np.max(data_3d, axis=0) # Maximum value along axis 1 max_3d_axis_1 = np.max(data_3d, axis=1) # Maximum value along axis 2 max_3d_axis_2 = np.max(data_3d, axis=2) print("Maximum value along axis 0:", max_3d_axis_0) print("Maximum value along axis 1:", max_3d_axis_1) print("Maximum value along axis 2:", max_3d_axis_2)
In this case, the maximum value is calculated along each of the axes (0, 1, and 2) for the 3D array. The function returns the maximum values for each of the specified axes while preserving the other dimensions −
Maximum value along axis 0: [[5 6] [7 8]] Maximum value along axis 1: [[3 4] [7 8]] Maximum value along axis 2: [[2 4] [6 8]]
Handling NaN (Not a Number) Values
Sometimes, arrays may contain NaN (Not a Number) values, which can interfere with the calculation of the maximum value. To handle NaN values, NumPy provides an option to ignore them during the max calculation.
You can use the numpy.nanmax() function, which computes the maximum value while ignoring NaN values.
Example
In this example, we are handling NaN values while computing maximum value in NumPy −
import numpy as np # Create an array with NaN values data_with_nan = np.array([1, 3, np.nan, 5, 7]) # Calculate the maximum value while ignoring NaN values max_without_nan = np.nanmax(data_with_nan) print("Maximum value without NaN:", max_without_nan)
After executing the above code, we get the following output −
Maximum value without NaN: 7.0
Using the Out Parameter
The out parameter in the numpy.max() function allows you to store the result of the maximum value computation in a pre-allocated array.
This can be useful for memory management and efficiency when working with large datasets. The result is stored in the array specified by the out parameter, which must have the same shape as the expected output.
Example
In this example, the maximum value of the array data is calculated and stored in the pre-allocated array out_array, which is then printed to show the result −
import numpy as np # Create an array data = np.array([5, 2, 9, 1, 5, 6]) # Create an output array out_array = np.empty((), dtype=np.int32) # Calculate the maximum value and store it in out_array np.max(data, out=out_array) print("Output array:", out_array)
The result produced is as follows −
Output array: 9