Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy absolute() Function



The NumPy absolute() function is used to calculate the absolute value element-wise. It returns an array containing the absolute value of each element in the input array. This function is identical to abs().

This function can handle both integer and floating-point arrays, and it also works with complex numbers by returning their magnitude.

Syntax

Following is the syntax of the NumPy absolute() function −

numpy.absolute(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Parameters

This function accepts the following parameters −

  • x: The input array whose absolute values are to be calculated.
  • out (optional): A location into which the result is 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 (optional): This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Otherwise, it will retain its original value.
  • casting (optional): Controls what kind of data casting may occur. Defaults to 'same_kind'.
  • order (optional): Controls the memory layout order of the result. 'C' means C-order, 'F' means Fortran-order, 'A' means 'F' if inputs are all F, 'C' otherwise, 'K' means match the layout of the inputs as closely as possible.
  • dtype (optional): The type of the returned array and of the accumulator in which the elements are summed. The dtype of x is used by default unless dtype is specified.
  • subok (optional): If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.

Return Value

This function returns an array containing the absolute value of each element in the input array x. If out is provided, it returns a reference to out.

Example: Basic Usage of absolute() Function

In the following example, we create a 1-dimensional array with both positive and negative numbers and use the absolute() function to calculate their absolute values −

import numpy as np

# Creating a 1-dimensional array
arr = np.array([-1, -2, 3, -4])

# Calculating the absolute values
result = np.absolute(arr)
print(result)

Following is the output obtained −

[1 2 3 4] 

Example: absolute() Function with Floating-Point Numbers

In this example, we use the absolute() function on an array of floating-point numbers to get their absolute values −

import numpy as np

# Creating an array of floating-point numbers
arr = np.array([-1.1, -2.2, 3.3, -4.4])

# Calculating the absolute values
result = np.absolute(arr)
print(result)

This will produce the following result −

[1.1 2.2 3.3 4.4]

Example: absolute() Function with Complex Numbers

In this example, we use the absolute() function on an array of complex numbers to get their magnitudes −

import numpy as np

# Creating an array of complex numbers
arr = np.array([1+2j, -3+4j, -5-6j])

# Calculating the magnitudes
result = np.absolute(arr)
print(result)

Following is the output of the above code −

[2.23606798 5.         7.81024968]

Example: absolute() Function with Broadcasting

In this example, we demonstrate the use of broadcasting with the absolute() function. We create a 2-dimensional array and calculate the absolute values element-wise −

import numpy as np

# Creating a 2-dimensional array
arr = np.array([[1, -2, 3], [-4, 5, -6]])

# Calculating the absolute values
result = np.absolute(arr)
print(result)

This will produce the following result −

[[1 2 3]
 [4 5 6]]
numpy_arithmetic_operations.htm
Advertisements