
- 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 argmax() Function
The Numpy argmax() function is used to find the index of the maximum value in an array along a specified axis. It is particularly useful for tasks that require identifying the position of the highest value in data structures, such as arrays or matrices.
The Numpy argmax() function works on both one-dimensional and multi-dimensional arrays. When the funtion is used with multi-dimensional arrays, we need to specify the axis along which to find the maximum index.
In an array, if the maximum element appears multiple times, the numpy.argmax() function will return the index of the first occurrence of the maximum value.
Syntax
Following is the syntax of the Numpy argmax() function −
numpy.argmax(a, axis=None, out=None, *, keepdims=<no value>)
Parameters
Following are the parameters of the Numpy argmax() function −
- a: The input array from which the maximum value index is to be found.
- out (optional): If provided, the result will be placed in this output array. Must be of the same shape and type as the expected output.
- keepdims: If set to True, the reduced axes are retained in the result as dimensions of size 1, meaning they are not removed. This ensures the result has the same number of dimensions as the original array, which is useful for broadcasting.
- axis (optional): The axis along which to find the index of the maximum value.
- None (default): Finds the index of the maximum value in the flattened array.
- Axis can be an integer (e.g., 0 for rows, 1 for columns in a 2D array).
Return Type
This function returns an integer (for 1D arrays) or an array of integers (for multi-dimensional arrays), indicating the indices of the maximum values along the specified axis.
Example
Following is a basic example to find the index of the maximum value in a one-dimensional array using Numpy argmax() function −
import numpy as np array = np.array([10, 20, 5, 30, 15]) max_index = np.argmax(array) print("Array:", array) print("Index of Maximum Value:", max_index)
Output
Array: [10 20 5 30 15] Index of Maximum Value: 3
Example: Using axis Parameter
The axis parameter is used to find the maximum value indices along a specific axis in multi-dimensional arrays.
In the following example, we find the maximum element along rows and columns by setting the axis parameter to 1 (row-wise) and 0 (column-wise), respectively, in the numpy.argmax() function −
import numpy as np array_2d = np.array([[85, 3, 7], [2, 8, 6], [66, 50, 9]]) # Finding max indices along rows max_index_row = np.argmax(array_2d, axis=1) # Finding max indices along columns max_index_col = np.argmax(array_2d, axis=0) print("2D Array:") print(array_2d) print("Indices of Maximum Values Along Rows:", max_index_row) print("Indices of Maximum Values Along Columns:", max_index_col)
Output
2D Array: [[85 3 7] [ 2 8 6] [66 50 9]] Indices of Maximum Values Along Rows: [0 1 0] Indices of Maximum Values Along Columns: [0 2 2]
Example: Flattened Array
When axis=None, the array is flattened, and the index of the maximum value in the flattened array is returned.
Here is an example where we find the maximum element in a 2D array by flattening it first −
import numpy as np array_2d = np.array([[10, 20, 30], [5, 25, 15], [40, 35, 50]]) max_index_flat = np.argmax(array_2d) print("2D Array:") print(array_2d) print("Index of Maximum Value in Flattened Array:", max_index_flat)
Output
2D Array: [[10 20 30] [ 5 25 15] [40 35 50]] Index of Maximum Value in Flattened Array: 8
Example: Using out Parameter
The out parameter can be used to store the result in a pre-allocated array. The output array must have the appropriate shape and data type.
import numpy as np array = np.array([4, 12, 7, 18, 9]) out_array = np.empty((), dtype=int) np.argmax(array, out=out_array) print("Array:", array) print("Index of Maximum Value (using out):", out_array)
Output
Array: [ 4 12 7 18 9] Index of Maximum Value (using out): 3
Example: Mutiple Occurence of Maximum Value
The Numpy argmax() function returns the index of the first occurrence of the maximum value if it appears multiple times in the array.
In the following example, the value 100 occurs twice, but the numpy.argmax() function returns the index of its first occurrence −
import numpy as np array = np.array([4, 59, 100, 76, 19, 100]) max_Value = np.argmax(array,) print("Array:", array) print("Index of Maximum Value:", max_Value)
Output
Array: [ 4 59 100 76 19 100] Index of Maximum Value: 2