# Python NumPy Absolute Value

Python NumPy `absolute()` function is used to return the element-wise absolute values of the input array. In other words, this mathematical function helps the user to calculate the absolute value element in the given array. An absolute value is a positive value of a given negative value, so it just removes the negative sign from the input value.

The NumPy `absolute()` function takes four parameters `arr`, `out`, `where`, and `dtype`, and returns a new array with an argument value as the absolute of the source array elements. In this article, I will explain how to use `numpy.absolute()` function on single, multi-dimension arrays of float elements, and complex numbers.

## 1. Quick Examples of Python NumPy Absolute Value

If you are in a hurry, below are some quick examples of how to use Python NumPy absolute value.

``````
# Below are the quick examples

# Example 1: Use numpy.absolute() to get the absolute value
arr = np.array([-46])
arr2 = np.absolute(arr)

# Example 2: get the absolute values of multiple elements of 1-d array
arr = [4, -9, 14, -23, 32, -56]
arr2 = np.absolute(arr)

# Example 3: Use numpy.absolute() function to get
# the absolute values of floating point
arr = [-15.7, 8.6, -7.1, 8.7, -19.2, 43.8]
arr2 = np.absolute(arr)

# Example 4: Use numpy.absolute() get the absolute values
# of 2-D array elements
arr = np.array([[-12, 8, -23, 32], [49, -74, -92, 106]])
arr2 = np.absolute(arr)

# Example 5: Use numpy.absolute() function with complex numbers
arr = [4+7j, -5-9j, 16+13j, -8+17j]
arr2 = np.absolute(arr)

# Example 6: Use np.absolute function to graphical representation
arr = np.linspace(start = -18, stop = 13, num = 12, endpoint = True)
arr2 = np.absolute(arr)
print(arr2)
plt.plot(arr, np.absolute(arr))
plt.plot(arr, arr, color = 'orange')
plt.show()
``````

## 2. Syntax of NumPy absolute()

Following is the syntax of the `numpy.absolute()` function.

``````
# Syntax of numpy.absolute()
numpy.absolute(arr, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None)
``````

### 2.1 Parameters of absolute()

The absolute() function allows the following parameters.

• `arr -` Input array.
• `out` – It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
• `where` – This condition is broadcast over the input.
• `dtype` â€“ It is an optional parameter that specifies the type of the returned array.

### 2.2 Return Value of absolute()

It returns an array with the absolute value for each element of an array.

## 3. Usage of NumPy absolute() Function

This mathematical Python NumPy in-built function `absolute()` takes the input array as param and returns the absolute value of each element in the input array. An absolute value is a positive value of a given negative value, so it just removes the negative sign from the input value. For positive value, it just returns the same value.

### 3.1 Get the Absolute Value of Single Element

Use `numpy.absolute()` function to get the absolute value of a single element of an input NumPy array.

``````
import numpy as np
arr = np.array([-46])

# Use numpy.absolute() to get the absolute value
arr2 = np.absolute(arr)
print(arr2)

# Output
# 46
``````

### 3.2 Get the Absolute Values of Multiple Elements of 1-D Array

Letâ€™s create a one-dimensional NumPy array using `numpy.array()` and use this function to calculate the absolute value for each element in the NumPy array.

``````
import numpy as np

# Create an 1D input array
arr = [4, -9, 14, -23, 32, -56]

# get the absolute values of multiple elements of 1-d array
arr2 = np.absolute(arr)
print(arr2)

# Output
[ 4  9 14 23 32 56]
``````

## 4. Get the absolute Values of Float Elements of 1-D NumPy Array

Similaly, lets calculate the absolute value of the float elements of an array by using the `numpy.absolute()` function. For float values, the absolute returns the positive float value.

``````
import numpy as np

# creating an 1D input array
arr = [-15.7, 8.6, -7.1, 8.7, -19.2, 43.8]

# Use numpy.absolute() function to get
# the absolute values of floating point
arr2 = np.absolute(arr)
print(arr2)

# Output
# [15.7  8.6  7.1  8.7 19.2 43.8]
``````

## 5. Get the Absolute Values of 2-D NumPy Array Elements

To get absolute values of Two- Dimensional array elements, just pass the two dimensional array tot he `absolute()` function. It returns the array in same shape as input.

``````
import numpy as np

# creating an 2D input array
arr = np.array([[-12, 8, -23, 32], [49, -74, -92, 106]])

# Use numpy.absolute() get the absolute values
# of 2-D array elements
arr2 = np.absolute(arr)
print(arr2)

# Output
# [[ 12   8  23  32]
#  [ 49  74  92 106]]
``````

## 6. Get the Absolute Values of Complex Numbers

We can also get the absolute value of complex elements of an array. For example

``````
import numpy as np

# Create an array with complex values
arr = [4+7j, -5-9j, 16+13j, -8+17j]

# Use numpy.absolute() function with complex numbers
arr2 = np.absolute(arr)
print(arr2)

# Output
# [ 8.06225775 10.29563014 20.61552813 18.78829423]
``````

## 7. Get the Absolute Values on Graphical Representation

By using the matplotlib.pyplot, we can represent the absolute values in a graphical representation.

``````
import numpy as np
import matplotlib.pyplot as plt

# Use np.absolute function to graphical representation
arr = np.linspace(start = -18, stop = 13,
num = 12, endpoint = True)

arr2 = np.absolute(arr)
print(arr2)

plt.plot(arr, np.absolute(arr))
plt.plot(arr, arr, color = 'orange')
plt.show()

# Output
# [18.         15.18181818 12.36363636  9.54545455  6.72727273  3.90909091
#   1.09090909  1.72727273  4.54545455  7.36363636 10.18181818 13.        ]
``````

Yields below output.

You can see the Parabolic graph of the absolute() method in Numpy.

## 8. Conclusion

In this article, I have explained how to use Python `numpy.absolute()` function and how to calculate the absolute value of every element in the given array with examples.

Happy Learning!!