# numpy.argmax() and tensorflow.argmax() - Are They Same

**Numpy** is a Python library used for working with arrays. It provides multi-dimensional array objects, masked arrays, and matrices. To work with these arrays, NumPy provides some methods to carry out operations like linear algebra, statistical operations, shape manipulation, mathematical and logical operations, and much more.

The array object in NumPy is called `ndarray`, and dimensions are called `axes`.

Whereas **TensorFlow** is an open-source library developed by Google primarily to build deep learning and machine learning neural networks.

However, TensorFlow has some modules and methods to handle arrays or tensors in TensorFlow's language.

In this article, we will see the `argmax()` function in both NumPy and TensorFlow, and ultimately we'll discover if they provide the same functionality to handle arrays.

## Definition

First, we'll decipher the definition of the `argmax()` function provided in the docs of both the libraries NumPy and TensorFlow. 

### numpy.argmax

NumPy provides a function called ***argmax()*** that **returns the indices of the maximum element of the array along an axis**.

`numpy.argmax()` can be used to get the indices of the maximum values from the 1-dimensional and multi-dimensional arrays.

### tensorflow.math.argmax or tensorflow.argmax

TensorFlow has a module named `math` that provides the `argmax()` function, or we can call the ***argmax()*** function directly from the TensorFlow is also used to **return the index of the largest value across the axes of the tensor or array**.

## Syntax

Let's look at the syntax since they both are used for similar actions.

### numpy.argmax

> numpy.argmax(a, axis=None, out=None)

Parameters:

- `a` - The array you will work on

- `axis` - It is optional. We can specify an axis like 0 or 1 to find the maximum value index horizontally or vertically.

- `out` - By default, it is None. It provides a feature to insert the output to the out area, but the array should be of appropriate shape and dtype.

> Return value

>An array is returned with the indices of max values with the same shape as `a.shape` with the dimension along the ***axis*** removed.

### tensorflow.argmax

> tensorflow.argmax(
    input,
    axis=None,
    output_type=tf.dtypes.int64,
    name=None
)

Parameters:

- `input` - It is a tensor from which we'll get the index of the highest values.

- `axis` - It is for specifying the axes to reduce the tensor. In the case of vector, we always use `axis=0`.

- `output_type` - It defines the dtype in which the returned result should be. The default value is **int64**.

- `name` - By default it is None. It is used to specify a name for the operation. 

> Return value

>A tensor is returned with the indices of the highest values along the axes with the type of ***output_type***.

## Code Example

Let's see some examples.

### numpy.argmax

```python
# Importing numpy as np
import numpy as np

# Creating a 2D array of shape 3, 6
arr = np.arange(18).reshape(3, 6)

# Printing out an input array
print("INPUT ARRAY: \n", arr)

# Applying argmax to the input array
max_elem_index = np.argmax(arr)

# Printing the max element from the input array
print("\nMAX ELEMENT INDEX:", max_elem_index)
```
**Output**

```python
INPUT ARRAY: 
 [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]]

MAX ELEMENT INDEX: 17
```
Since we didn't specify the ***axis*** argument in the ***argmax()***, the array is flattened or considered a 1D array, so we got the ***index*** of the highest value from the input array.

**Finding indices of the highest values along the axis**

```python
import numpy as np

# Creating a 2D array of random integers
input_arr = np.random.randint(16, size=(4, 4))
print("INPUT ARRAY: \n", input_arr)

# Printing the indices of the max element along axis 0
print("\nIndices of MAX ELEMENT", np.argmax(input_arr, axis=0))

# Printing the indices of the max element along axis 1
print("\nIndices of MAX ELEMENT", np.argmax(input_arr, axis=1))
```
**Output**

```python
INPUT ARRAY: 
 [[13  3  8  1]
 [13  2  8 15]
 [ 7  8  7  7]
 [ 1  9 11  8]]

Indices of MAX ELEMENT [0 3 3 1]

Indices of MAX ELEMENT [0 3 1 2]
```
**Get the detailed explanation of numpy.argmax() function**.

%[https://geekpython.in/numpy-argmax-function-in-python]

### tensorflow.argmax

```python
import tensorflow as tf

inp_tensor = tf.constant([3, 1, 5, 10, 45, 21])

out = tf.argmax(inp_tensor)
print(f"INDEX of highest value: {out}")
print("Tensor:", out)
```
**Output**

```python
INDEX of highest value: 4
Tensor: tf.Tensor(4, shape=(), dtype=int64)
```
Unlike NumPy, TensorFlow provides additional information like the shape and dtype of the output.

**Finding the indices of the highest values in a tensor along the specified axis**

```python
import tensorflow as tf

inp_tensor = tf.constant([[7, 13, 9, 10, 13.5, 21], [9, 2, 8, 14, 45, 21], [92, 3, 14, 92.1, 19, 8]])

# Printing the indices of highest values along axis 0
print(f"INDEX of highest value: {tf.argmax(inp_tensor, 0)}")
print("Tensor:", tf.argmax(inp_tensor, 0), "\n")

# Printing the indices of highest values along axis 1
print(f"INDEX of highest value: {tf.argmax(inp_tensor, 1)}")
print("Tensor:", tf.argmax(inp_tensor, 1))
```
**Output**

```python
INDEX of highest value: [2 0 2 2 1 0]
Tensor: tf.Tensor([2 0 2 2 1 0], shape=(6,), dtype=int64) 

INDEX of highest value: [5 4 3]
Tensor: tf.Tensor([5 4 3], shape=(3,), dtype=int64)
```

## Conclusion

We already discussed that NumPy is primarily for numerical computation, or we often use it for handling complex array operations. In contrast, TensorFlow is used to build machine learning and deep learning neural networks for developing AI-based applications.

Both libraries have the `argmax()` function, and by this article, we meant to look at the function.

After seeing the definition, syntax, and code, we can conclude that there was a slight difference in syntax, but the function's fundamentals were the same in both libraries.

Another minor difference was that tensorflow provides additional info like the output tensor's shape and dtype.

Now we got a pretty good idea about the ***argmax()*** function provided by both libraries.

---

**That's all for now**

**Keep Coding✌✌**
