# Solved: Finding Maximum Elements along columns using Python numpy.argmax%28%29

In the world of programming and data manipulation, Python has become a highly popular language due to its flexibility and abundance of libraries. One such powerful library is NumPy, which greatly simplifies the handling and manipulation of arrays and matrices. In this article, we are going to discuss a commonly encountered problem: finding the maximum element along the columns of a 2D array or matrix. We will be using the **numpy.argmax()** function to achieve this. So, sit back and relax as we dive deep into this amazing journey of array manipulation and maximum detection through Python and NumPy.

## Problem Statement and Solution using numpy.argmax()

The problem we are addressing is to find the maximum element along the columns of a 2D array or matrix. To accomplish this, we will be using the `numpy.argmax()` function from NumPy library.

```import numpy as np

# Sample 2D array
array = np.array([[1, 7, 3],
[4, 2, 9],
[5, 6, 8]])

# Finding maximum elements along columns
max_indices = np.argmax(array, axis=0)

print("Maximum elements along columns:", array[max_indices, range(len(max_indices))])
```

First, let’s import the NumPy library with the alias `np`. Then, we create a sample 2D array using `np.array()` and pass in a nested list containing the row values. After that, we utilize the `numpy.argmax()` function, passing the array along with `axis=0`, which specifies that we want to perform the operation along the columns. This function returns the indices of the maximum elements along the specified axis. Finally, we print the maximum elements along the columns using fancy indexing.

## Step-by-Step Explanation of the Code

In this section, we are going to discuss the step-by-step working of the code.

1. Import NumPy with `import numpy as np`.

2. Create a sample 2D array with `array = np.array([[1, 7, 3], [4, 2, 9], [5, 6, 8]])`.

3. Find the index positions of the maximum elements along the columns using `np.argmax()` with `axis=0`. This returns an array of indices, e.g., `[2, 0, 1]`, indicating the positions of maximum elements along each column.

4. Fancy indexing is applied to extract maximum elements along columns using `array[max_indices, range(len(max_indices))]`. Here, `max_indices` represents rows and `range(len(max_indices))` represents columns. This returns the maximum elements array, e.g., `[5, 7, 9]`.

5. Print the result using `print()` function.

```# Finding maximum elements along rows