# 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 method, we will learn and discuss Python numpy absolute value.
• It is a statistical function that helps the user to measure the absolute value of each element in NumPy array.
• In a numpy array, some elements are negative some are positive.
• If we apply the numpy absolute value, it will calculate the absolute value of every value in the array.

Syntax:

Here is the syntax of NumPy absolute value

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

Example:

``````import numpy as np

arr = np.array([4, 5,-6,-7, 3])
result = np.absolute(arr)
print(result)``````
• In the above code, we will import a numpy library and create an array using the numpy. array function and assign the values in an argument.
• Create a variable and assign the function np. absolute and print the result.

Here is the Screenshot of the following given code

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

• It has only one parameter that is X. The X argument enables us to provide the input array.
• We will generate the absolute values of an array of values, to do this first we will create a numpy array and then we can use an np. abs() function on that numpy 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 sum

• In this method, we will learn and 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 the same that is used for both the numbers positive or negative.
• In this method, we can easily use the function numpy.absolute() to get the absolute value of the 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 code, we import a numpy library and create an array using the numpy. array function and assign the values in an argument.
• Then declare a method l1 in which assign the parameters arr1 and arr2 then generate the np. absolute function and print the result.

Here is the Screenshot of the following given code

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

• The abs() function always returns the abs value of the given number, if the no is a negative number, numpy.abs() convert into positive.
• Map() function applies a given function to each item of an iterable sequence list and arrays.
• IIn this example, we will use both the function to get the absolute value of the 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 sort

• In this method, we will learn and discuss the numpy absolute value sort in python.
• In this method, we can use the function numpy.sort() and numpy.abs() to get the sorted elements and positive values.
• This function always returns a sorted copy of an array.
• In this example, we will use both functions to get them 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 code, we will import a numpy library and create an array using the numpy. array function.
• Then create a variable and use a method numpy.sort() and numpy.abs().

Here is the Screenshot of the following given code

## Python numpy absolute value without function

• In this method, we will learn and discuss the numpy absolute value without function in Python.
• In this method first, we will declare the square of the number.
• Then measure 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 tuple

• In this method, we will learn and discuss the numpy absolute value tuple.
• In this method, we can easily use the function np.absolute() to get the positive value tuple.
• If we apply the numpy absolute value, it will define the absolute value of every value in the array.
• It is used to contain multiple objects in a single variable.
• This method cannot be changed until lists and tuples use open brackets, 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 Pandas

• In this method, we will learn and discuss the numpy absolute pandas.
• In this method, we can use pandas packages for the analysis of absolute value.
• Dataframe.abs() is one of the easiest ways to use the pandas dataframe function. It always returns items with positive values and if it is only applicable to items that are all numeric.
• This function will return the positive absolute value of a specified number of a given expression.
• To understand the np.abs() method we will solve examples and get the absolute value of the DataFrame.
• To find the positive values, we also need to have negative values in the dataframe.

Syntax:

``Dataframe.abs()``
• This method only applies to values that are all numeric.
• It will return series containing the positive value of each element.

Example:

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

Here is the Screenshot of the following given code

## Python numpy absolute value complex number

• In this method, we will learn and discuss the numpy absolute value complex number in Python.
• In Python, complex numbers can be declared either using an assignment statement.
• Complex numbers are commonly used when we are using two real numbers.
• In this method, we can easily use the numpy.abs() function to get the positive values.
• In the case of complex numbers, the numpy.abs() function returns the magnitude part only.
• The positive value of a complex number is generated as the distance between the points in the complex plane.
• To understand the numpy.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

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

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 element-wise absolute value

• In this method, we will learn and discuss the numpy element-wise positive value in Python.
• To find the element-wise absolute value of numpy array we are using numpy.absolute() function.
• It is a statistical function that helps the user to calculate the absolute value of each element in the array.
• Put simply, Numpy positive value calculates the absolute value of the values in an 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

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

• In this method, we have to use the function numpy.fabs().
• It is used to generate the absolute values element-wise.
• It will always return the positive values in positive magnitude.
• It always returns float decimal data type numbers.

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 absolute value difference

• In this method, we will learn to discuss the Python numpy absolute value difference.
• In this method, we have to use the function numpy.absolute().
• If you want the absolute element-wise difference between arrays, 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

An alternative method to check the numpy absolute value difference

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 of column in pandas

• In this section, we will discuss the numpy absolute value of columns in pandas.
• we will see how to get the absolute value of a column in the pandas dataframe. and the absolute value of the series in pandas by using the function abs().
• First, we will create a dataframe module 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

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