# np.absolute: How to Calculate Absolute Value in Numpy

Before learning the numpy absolute() method, we need to understand what absolute value is? Suppose we are given an integer -x; its absolute value will be (-x)2. Also, for any complex equation like a+ib, the absolute value will be √a²+√b². (Please read as under square root(a² + b²)).

## np.absolute

The np.absolute() is a mathematical function used to calculate the absolute value of each element of the array and returns that array. The numpy absolute() function takes three parameters and returns the absolute value of any given input.

### Syntax

`numpy.absolute(arr, out = None, ufunc ‘absolute’)`

### Parameters

The absolute() function can take up to three parameters:

1. arr: The input array or object whose absolute value needs to be calculated.
2. out: This is an optional field. A position the product is stored in. When given, it must have a form on which the inputs communicate. If not provided or None, it returns a freshly-allocated list. A tuple must have a length equal to the number of outputs (possible only as a keyword argument).

### Return Value

The absolute() function returns the array containing the absolute value of the given array or object.

### Program to find the absolute value of array elements in Python

See the following code.

```# app.py

import numpy as np

# Creating one 1D array
arr1 = [12, -5, -14, -100]

# printing its absolute values
print("Absolute values of arr1 are: ", np.absolute(arr1))

# Creating 2D array
arr2 = [[14, -4, 12], [-100, -3, 1]]

# printing its absolute values
print("Absolute values of arr1 are:\n ", np.absolute(arr2))
```

#### Output

```Absolute values of arr1 are:  [ 12   5  14 100]
Absolute values of arr1 are:
[[ 14   4  12]
[100   3   1]]```

#### Explanation

In this example, we have declared one 1D array with negative and positive values, then we have called absolute() to get absolute values of this array, and we can see all absolute values are returned.

In the second case, we have declared a 2D array, and we can see that all absolute values are printed when called.

### Program to find the absolute value of an object

See the following code.

```import numpy as np

# creating object

eq = (5-14)
# printing its absolute values
print("Absolute values of the equation is : ", np.absolute(eq))

eq2 = 10-100

# printing its absolute values
print("Absolute values of the equation is : ", np.absolute(eq2))
```

#### Output

```Absolute values of the equation is :  9
Absolute values of the equation is :  90```

#### Explanation

In this example, we have two equations (called object), usually, its value is negative, but when we called the equation in absolute(), the value we got is positive according to √a²+√b². (Please read as under square root(a² + b²)) equation.

### Program to show the graphical representation of absolute()

See the following code.

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

# Giving values of point to be plotted
a = np.linspace(start=-10, stop=10, num=100)

plt.title("Blue:with absolute\nGreen:without absolute")

# Ploting values with absolute values
plt.plot(a, np.absolute(a))

# Ploting values without absolute valuess
plt.plot(a, a, color='green')
plt.show()
```

#### Output #### Explanation

In this example, we have imported numpy and matplotlib libraries. In addition, we have given line space values 10 to -10, plotting these values in a graph.

We have plotted two types of values, one with a Blue line, which represents the graphical representation of absolute values, and another is without absolute value, described as a Green-line.

We can see that the green line lies between 10 and -10, but the blue line only lies between 10 to 10.

That’s it for np.absolute() function.

This site uses Akismet to reduce spam. Learn how your comment data is processed.