• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:18 mins read
You are currently viewing 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 Absolute Value

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


# Quick examples of absolute value

# 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 or object whose elements’ absolute values are to be computed.
  • / – Indicates the end of positional arguments
  • out – optional) Output array where the result will be placed. If not provided, a new array will be created.
  • * – Indicates the start of keyword-only arguments.
  • where -(optional) This parameter is used to define a condition that selects which elements to calculate. By default, it is set to True, meaning that all elements will be considered.
  • casting – (optional) Controls what kind of data casting may occur during the operation. Default is ‘same_kind’.
  • order – (optional) Controls the memory layout of the result. Default is ‘K’.
  • dtype – (optional) Data type of the output array. If not specified, the data type is inferred from the input 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 a 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 a Single Element

To get the absolute value of a single element rather than an entire array, you can use the numpy.absolute() function. For instance, it creates a NumPy array with a single element ([-46]), and then uses the numpy.absolute() function to obtain the absolute value of that element.


# Import numpy
import numpy as np

# Create array
arr = np.array([-46])
print("Original array:\n", arr)

# Use numpy.absolute() 
# To get the absolute value
arr2 = np.absolute(arr)
print("Absolute value:\n", arr2)

Yields below output.

numpy absolute value

It shows that the absolute value of -46 is 46. Your usage of numpy.absolute() is correct for getting the absolute value of a single element in a NumPy array.

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

If you have a 1-dimensional NumPy array and you want to get the absolute values of all its elements, you can use the numpy.absolute() function.

The below example defines a 1-dimensional NumPy array arr with multiple elements, and then it is used numpy.absolute() to get the absolute values of all elements. The resulting arr2 will contain the absolute values.


import numpy as np

# Create an 1D input array
arr = [4, -9, 14, -23, 32, -56]
print("Original array:", arr)

# Get the absolute values 
# Of multiple elements of 1-d array
arr2 = np.absolute(arr)
print("Absolute values:", arr2)

Yields below output.

numpy absolute value

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

Similarly, let’s 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.

The below example arr is a 1-dimensional NumPy array with float elements and numpy.absolute() is used to calculate the absolute values of those elements. The results arr2 will contain the absolute values.


import numpy as np

# Create an 1D input array
arr = [-15.7, 8.6, -7.1, 8.7, -19.2, 43.8]
print("Original array:", arr)

# Use numpy.absolute() function to get 
# The absolute values of floating point
arr2 = np.absolute(arr)
print("Absolute float values:", arr2)

# Output:
# Original array: [-15.7, 8.6, -7.1, 8.7, -19.2, 43.8]
# Absolute float values: [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 to the absolute() function. It returns the array in the same shape as the input.

In this example, arr is a 2-dimensional NumPy array, and numpy.absolute() is used to calculate the absolute values of all elements. The resulting arr2 will contain the absolute values.


import numpy as np

# Creating an 2D input array
arr = np.array([[-12, 8, -23, 32], [49, -74, -92, 106]])
print("Original 2-D array:\n", arr)

# Use numpy.absolute() function
# Get the absolute values of 2-D array elements
arr2 = np.absolute(arr)
print("Absolute values of 2-D array:\n", arr2)

# Output:
# Original 2-D array:
#  [[-12   8 -23  32]
#  [ 49 -74 -92 106]]
# Absolute values of 2-D array:
#  [[ 12   8  23  32]
#  [ 49  74  92 106]]

6. Get the Absolute Values of Complex Numbers

You can use the numpy.absolute() function to get the absolute values of complex elements in a NumPy array. For instance, it creates a list of complex numbers and then uses the numpy.absolute() function to obtain the absolute values of each complex number in the list. However, if you want to work with NumPy arrays instead of lists, you should create a NumPy array using np.array().


import numpy as np

# Create an array with complex values
arr = [4+7j, -5-9j, 16+13j, -8+17j]
print("Original array with complex numbers:\n", arr)

# Use numpy.absolute() function 
# With complex numbers
arr2 = np.absolute(arr)
print("Absolute values of complex numbers:\n", arr2)

# Output:
# Original array with complex numbers:
#  [(4+7j), (-5-9j), (16+13j), (-8+17j)]
# Absolute values of complex numbers:
#  [ 8.06225775 10.29563014 20.61552813 18.78829423]

7. Get the Absolute Values on Graphical Representation

By using the matplotlib.pyplot, you 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.

numpy absolute

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

Frequently Asked Questions

How do I calculate the absolute value of elements in a NumPy array?

To calculate the absolute value of elements in a NumPy array, you can use the numpy.absolute() function or its equivalent numpy.abs() function.

Can I get the absolute values of a NumPy array containing complex numbers?

You can get the absolute values of a NumPy array containing complex numbers using the numpy.absolute() function or its equivalent numpy.abs() function.

Can I calculate the absolute values of elements in a 2D NumPy array?

You can calculate the absolute values of elements in a 2D NumPy array using the np.abs() function.

How do I get the absolute values of float elements in a NumPy array?

To get the absolute values of float elements in a NumPy array, you can use the numpy.absolute() function or its equivalent numpy.abs() function

What is the difference between numpy.absolute() and numpy.abs()?

There is no functional difference between numpy.absolute() and numpy.abs(). They are two different names for the same function. Both functions are used to compute the absolute values of elements in a NumPy array, whether they are real, complex, or of any other numeric type.

Can I compute the absolute values along a specific axis in a multi-dimensional array?

You can compute the absolute values along a specific axis in a multi-dimensional array using the axis parameter with the np.abs() function. The axis parameter allows you to specify along which axis the absolute values should be calculated.

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!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.