# Compute the absolute values element-wise and store the result in a new location in Numpy

To compute the absolute values element-wise, use the numpy.fabs() method in Python Numpy. The new location where we will store the result is a new array.

This function returns the absolute values (positive magnitude) of the data in x. Complex values are not handled, use absolute to find the absolute values of complex data. The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

## Steps

At first, import the required library −

import numpy as np

Create an array with some NaT and date values −

arr = np.array([-1, 1, 10, 45, 67, 89, -97])


Display the array −

print("Array...", arr)

Get the type of the array −

print("Our Array type...", arr.dtype)


Get the dimensions of the Array −

print("Our Array Dimensions...",arr.ndim)

Get the number of elements in the Array −

print("Number of elements...", arr.size)


Create another array with the same shape to store the result −

arrRes = np.array([5.3, -3.4, 7.6, 5.1, 5.9, 6.8, 9.8])

To compute the absolute values element-wise, use the numpy.fabs() method in Python Numpy. The new location where we will store the result is arrRes −

print("Compute the absolute values element-wise...",np.fabs(arr, arrRes))


Check the value of the new array where our result is stored −

print("Result...",arrRes)

## Example

import numpy as np

# Create an array with some NaT and date values
arr = np.array([-1, 1, 10, 45, 67, 89, -97])

# Display the array
print("Array...", arr)

# Get the type of the array
print("Our Array type...", arr.dtype)

# Get the dimensions of the Array
print("Our Array Dimensions...",arr.ndim)

# Get the number of elements in the Array
print("Number of elements...", arr.size)

# Create another array with the same shape to store the result
arrRes = np.array([5.3, -3.4, 7.6, 5.1, 5.9, 6.8, 9.8])

# To compute the absolute values element-wise, use the numpy.fabs() method in Python Numpy
# The new location where we will store the result is arrRes
print("Compute the absolute values element-wise...",np.fabs(arr,
arrRes))

# Check the value of the new array where our result is stored
print("Result...",arrRes)

## Output

Array...
[ -1 1 10 45 67 89 -97]

Our Array type...
int64

Our Array Dimensions...
1

Number of elements...
7

Compute the absolute values element-wise...
[ 1. 1. 10. 45. 67. 89. 97.]

Result...
[ 1. 1. 10. 45. 67. 89. 97.]

Updated on: 08-Feb-2022

45 Views

Get certified by completing the course