×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

How to find element-wise maximum values from a NumPy array?

Learn, how to apply a function on NumPy arrays for finding maximum values that too element-wise?
By Pranit Sharma Last updated : December 26, 2023

Problem statement

Suppose that we are given multiple NumPy arrays and we need to apply a max function on these arrays together in such a way that it checks each element and returns an array of the max element of each array.

NumPy Array: Finding element-wise maximum values

To find element-wise maximum values from a NumPy array, we can use numpy.vstack() which will stack all the arrays vertically and then we will apply max() function with (axis = 0), since the arrays are stacked vertically. We need to extract max values from each column.

Let us understand with the help of an example,

Python code to find element-wise maximum values from a NumPy array

# Import numpy
import numpy as np

# Creating numpy arrays
A = np.array([0,1,2])
B = np.array([1,0,3])
C = np.array([3,0,4])

# Display Original arrays
print("Original array 1:\n",A,"\n")
print("Original array 2:\n",B,"\n")
print("Original array 3:\n",C,"\n")

# Stacking all the arrays vertically
res = np.vstack([A,B,C])

# Finding max value
max = res.max(axis=1)

# Display result
print("Result:\n",max)

Output

Example: How to find element-wise maximum values from a NumPy array?

In this example, we have used the following Python basic topics that you should learn:

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.