# Python NumPy absolute value with examples

In this Python tutorial, we will discuss Python NumPy absolute value with a few examples like below:

Python numpy absolute value sum
Python numpy absolute value sort
Python numpy absolute value without function
Python numpy absolute value tuple
Python numpy absolute value pandas
Python numpy absolute value complex number
Python numpy element-wise absolute value
Python numpy absolute value difference
Python numpy absolute value of column in pandas

## Python numpy absolute value

In this section, we will discuss about Python numpy absolute value.
It is a mathematical function that helps the user to calculate the absolute value of each element in the Python NumPy array.
In an array some values are negative some are positive.
If we apply numpy absolute value, it will calculate the absolute value of every value in the array.

Syntax:

Here is the syntax of numpy absolute

numpy.absolute
(
arr,
out=None,
where=True,
casting=’same_kind’,
dtype=None
)

It consists of few parameters.

arr: input array

out: 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: It is an optional parameter.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.

Returns: An array with absolute value of each element.

Example:

import numpy as np

arr = np.array([4, 5,-6,-7, 3])
result = np.absolute(arr)
print(result)

In the above example first, we will import a numpy library after that create an array using the np. array function and assign the values in an arguments .
Create a variable and assign the function np.absolute and print the result.

Here is the Screenshot of the following given code

Python numpy absolute value

Another method to check numpy absolute value is numpy.abs() function

It has only used one argument that is X. The X argument enables us to specify the input array.
We will compute the absolute values of an array of values, to do this first we need to create an array and then we can use np.abs() on that array.

Example:

import numpy as np

arr = np.array([5, 6,-6,-7, 3])
result = np.abs(arr)
print(result)

Here is the Screenshot of the following given code

Python numpy absolute value abs method

## Python numpy absolute value sum

In this section, we will discuss the numpy absolute value sum.
The absolute value of the sum of two arrays is always equal to the sum of their absolute values is only true if the signs of both numbers are same; that is either both numbers are positive or both numbers are negative.
In this method we can easily use the function numpy.absolute() to get the absolute value of given array.

Syntax:

Here is the Syntax of numpy absolute

numpy.absolute
(
arr,
out=None,
where=True,
casting=’same_kind’,
dtype=None
)

Example:

import numpy as np
arr1= np.array([2,-3,4])
arr2= np.array([4,-8,6])
def l1(arr1, arr2):

return np.absolute(arr1 + arr2).sum()
print(l1(arr1,arr2))

In the above example first, we import a numpy library after that create an array using the np. array function and assign the values in an arguments .
Then create a function l1 in which assign the arguments arr1 and arr2 then compute the numpy absolute function and print the result.

Here is the Screenshot of the following given code

Python numpy array absolute value sum

Another method to check the numpy absolute value sum is numpy.abs() function and map function.

The abs() method returns the absolute value of the given number, if the number is a negative number, abs() return its positive.

Map() function applies a given function to each item of an iterable (list,arrays)etc.
In this example we will use both the function to get the absolute value of sum.

Example:

import numpy as np

arr1 = ([4, 5, -6, -8, 9, 1])
result = sum(map(abs, arr1))
print(result)

Here is the Screenshot of the following given code

Python numpy absolute value sum abs method

## Python numpy absolute value sort

In this section, we will discuss the numpy absolute value sort in python.
In this method we can easily use the function numpy sort() and numpy abs() to get the sorted and absolute values.
Numpy sort() function returns a sorted copy of an array.
In this example we will use the both function to get the sorted and absolute values.

Example:

Let’s take an example to check how to sort and absolute the values

import numpy as np

x = np.array([4,2,-1,3])
y = np.sort(x)
z= np.abs(y)
print(z)

In the above example first, we will import a numpy library and then create an array using the np. array function.
Then create a variable and use a function np.sort() and np.abs().

Here is the Screenshot of the following given code

Python numpy absolute value sort

## Python numpy absolute value without function

In this section, we will discuss the numpy absolute value without function in Python.
In this method first we compute the square of the number.
Then calculate the square root of the calculated value.

Example:

num = -8
value = (num**2)**0.5

print(num)
print(value)

Here is the Screenshot of the following given code

Python numpy absolute value without function

## Python numpy absolute value tuple

In this section, we will discuss the Python numpy absolute value tuple.
In this method we can easily use the function numpy.absolute() to get the absolute value tuple.
If we apply numpy absolute value, it will calculate the absolute value of every value in the array.
It is basically used to store multiple items in a single variable.
It cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Example:

import numpy as np
tup = ([1,-2,-3,-4,-5])
res = np.absolute(tup)
print(res)

Here is the Screenshot of the following given code

Python numpy absolute value tuple

## Python numpy absolute value Pandas

In this section, we will discuss the Python numpy absolute pandas.
In this method we can use pandas packages to analysis of absolute value.

Dataframe.abs() is one of the simplest pandas dataframe function. It returns an object with absolute value taken and it is only applicable to objects that are all numeric.
This function will return the positive absolute value of a specific number or an expression.
To understand the abs()  method we will solve examples and get the absolute value of the DataFrame.
In order to find the absolute value, we also need to have negative values in the dataframe.

Syntax:

Dataframe.abs()

This function only applies to elements that are all numeric.
It will return series containing the absolute value of each element.

Example:

import pandas as pd
a = pd.Series([1,14,-19,-15,6])
res = a.abs()
print(res)

In the above example first, we will import a pandas library and create a variable and assign the values in series and print the result.

Here is the Screenshot of the following given code

Python numpy absolute value pandas

## Python numpy absolute value complex number

In this section, we will discuss the numpy absolute value complex number in Python.
In Python complex number can be created either using direct assignment statement .
Complex numbers which are mostly used where we are using two real numbers.
In this method we can easily use np.abs() function to get the absolute values.
In case of complex numbersabs() function returns the magnitude part only.
The absolute value of a complex number is defined as the distance between the points in the complex plane.
To understand the abs()  method we will solve examples and get the absolute value of the Complex number.

Syntax:

numpy.abs
(
arr,
out=None,
where=True,
casting=’same_kind’,
dtype=None
)

Example:

import numpy as np
compl = ([2+3j,2-4j,-4-5j])
res = np.abs(compl)
print(res)

Here is the Screenshot of the following given code

Python numpy absolute value complex number

An alternative method to check the numpy absolute value complex number traditionally.

We would ask the user to enter a complex number of form a+bj and it gets stored in variable x.
To perform mathematical functions on complex numbers, we would have to import cmath module.
Then, we would use x.real and x.imag to access real and imaginary parts of the complex number. ** is used for exponentiation. cmath.sqrt for finding out the square root and its result gets stored in variable y.

Example:

x=complex(input(“Enter complex number in form a+bj: “))

import cmath
y=cmath.sqrt((x.real)**2+(x.imag)**2)

print(“The modulus of “,x,” is”, y.real)

Here is the Screenshot of the following given code

Python numpy absolute value complex number alternative method

## Python numpy element-wise absolute value

In this section, we will discuss the numpy element-wise absolute value in Python.
To find the element wise absolute value of numpy array we are using numpy.absolute() function.
It is a mathematical function that helps the user to calculate the absolute value of each element in the array.
Put simply, Numpy absolute value calculates the absolute value of the values in a Numpy array.
we have some numbers in an array, some negative and some positive and the output will return in the form of positive numbers.

Syntax:

numpy.absolute
(
arr,
out=None,
where=True,
casting=’same_kind’,
dtype=None
)

Example:

import numpy as np

arr1 = np.array([[4, -5, 6],
[-1, 2, -9]])

# find element-wise
# absolute value
result = np.absolute(arr1)
print(result)

Here is the Screenshot of the following given code

Python numpy element-wise absolute value

An alternative method to check the numpy element-wise absolute value

In this method, we can easily use the function numpy fabs().
It is used to compute the absolute values element-wise.
It will return the absolute values in positive magnitude.
It always returns float data type number.

Syntax:

Here is the Syntax of numpy fabs()

numpy.fabs
(
arr,
out=None,
where=True,
casting=’same_kind’,
dtype=None
)

Example:

import numpy as np

arr1 = np.array([[3, -4, 6],
[-1, 2, -9]])

# find element-wise
# absolute value
result = np.fabs(arr1)
print(result)

Here is the Screenshot of the following given code

Python numpy element-wise absolute value fabs method

## Python numpy absolute value difference

In this section, we will discuss the Python numpy absolute value difference.
In this method we can easily use the function numpy.absolute().
If you want the absolute element wise difference between array, you can easily subtract them with numpy and use numpy.absolute() function.
In this example, First we will import a numpy library and create two matrices .
Take a variable and assign a numpy absolute function and display the result.

Syntax:

numpy.absolute
(
arr,
out=None,
where=True,
casting=’same_kind’,
dtype=None
)

Example:

import numpy as np

X = [[-4,-6,3],
[4 ,-5,7],
[3 ,-8,7]]

Y = [[4,2,-1],
[6,-9,-3],
[4,-5,9]]

result = np.absolute(np.array(X) – np.array(Y))
print(result)

Here is the Screenshot of the following given code

Python numpy absolute value difference

An alternative method to check the numpy absolute value difference

if you were required to do so in native Python you could zip the dimensions together in a nested list comprehension.
In this method we can easily use the function abs() and iterative loop for absolute value.

Example:

import numpy as np

X = [[-4,-6,3],
[4 ,-5,7],
[3 ,-8,7]]

Y = [[4,2,-1],
[6,-9,-3],
[4,-5,9]]
result = [[abs(a-b) for a, b in zip(xrow, yrow)]
for xrow, yrow in zip(X,Y)]
print(result)

Here is the Screenshot of the following given code

Python numpy absolute value difference abs

## Python numpy absolute value of column in pandas

In this section, we will discuss the numpy absolute value of column in pandas.
we will see on how to get absolute value of column in pandas dataframe. and absolute value of the series in pandas by using the function abs().
First we will create a dataframe and assign the values.

Example:

import pandas as pd
a = pd.Series([2,6,-29,-15,6])
res = a.abs()
print(res)

Here is the Screenshot of the following given code

Python numpy absolute value of a column in pandas

You may like the following python tutorials:

In this Python tutorial, we will discuss Python NumPy absolute value and also cover the below examples:

Python numpy absolute value sum
Python numpy absolute value sort
Python numpy absolute value without function
Python numpy absolute value tuple
Python numpy absolute value pandas
Python numpy absolute value complex number
Python numpy element-wise absolute value
Python numpy absolute value difference
Python numpy absolute value of column in pandas 