In this tutorial, I’ll explain how to use the Numpy absolute value function, which is also known as np.abs or np.absolute.
Ultimately, you’ll learn how to compute absolute values with Numpy.
We’ll start with a brief overview of the function, and then work with some examples.
I recommend that you read the whole tutorial, but if you’re looking for something specific, you can click on any of the following links to navigate to a specific section.
Table of Contents:
Let’s start with a quick overview of what Numpy absolute value does.
A Quick Introduction to Numpy Absolute Value
Numpy absolute value calculates absolute values in Python.
It can do this with single values, but it can also operate on Numpy arrays.
Let’s quickly review what Numpy arrays are, just for context.
A Quick Review of Numpy
A Numpy array is a data structure in Python that contains numeric data.
These arrays can be 1 dimensional, like this:
Or they can be two dimensional:
Numpy arrays can also be multi-dimensional.
We often use Numpy arrays for anything involving computations involving strictly numeric data, like scientific computing and some forms of machine learning.
(For a more complete explanation, see our tutorial on Numpy arrays.)
The np.abs function calculates absolute values
So what does Numpy absolute value do?
Put simply, Numpy absolute value calculates the absolute value of the values in a Numpy array.
So let’s say we have some numbers in an array, some negative and some positive.
If we apply Numpy absolute value, it will calculate the absolute value of every value in the array. The output of the function will be a new array with the absolute values.
The syntax of Numpy Absolute Value
So now that you’ve learned about what Numpy absolute value does, let’s take a look at the syntax.
The syntax is actually very simple, but before we get into the syntax, there’s an important thing that I need to point out.
A quick note
Whenever we use Numpy, we need to import the module.
How we import the module will have an impact on the syntax, so I need to specify how we will import it.
The common way of importing Numpy is with the following code:
import numpy as np
When we import Numpy with the alias
np, we can use this prefix when we call our Numpy functions like Numpy absolute value.
This is by far the most common way to import Numpy, and it’s the syntactical convention that we’ll be using going forward.
The syntax of Numpy absolute value is extremely simple. It’s possibly one of the most simple Numpy functions.
We can call the function as
np.abs(). As noted above, this assumes that we’ve imported Numpy as
Alternatively, we can call the function as
np.absolute(). The functions
np.abs() are essentially the same. The
np.abs() function is essentially a shorthand version
np.absolute(). You can choose which ever one you like.
Inside of the function, we need to provide an input on which to operate.
Let’s quickly talk about that.
The parameters of np.abs
The numpy.abs function really only has one commonly used argument, the
x argument enables us to specify the input array.
The argument that we provide here can be a Numpy array, but also an array-like object, such as a Python list.
Keep in mind that you are required to provide something here. You need to provide some type of input.
Also note that you do not use
x this explicitly as a parameter.
What I mean is that you do not explicitly include
x in your syntax; the code
np.abs(x = myarray) will produce an error. It is implied that any input you provide to the function will serve as an argument to the
Other parameters of np.absolute
The np.absolute function does have some other parameters, including:
These parameters are somewhat uncommonly used, so we will not discuss them in this tutorial.
Examples of how to use Numpy Absolute Value
Okay. Now that we’ve looked at the syntax, let’s take a look at some examples.
- Example 1: Calculate absolute value for a single value
- Example 2: Calculate absolute values for a 1D array
- Example 3: Calculate absolute values for a 2D array
Run this code first
Before you run any of the examples below, you’ll need to import Numpy properly.
You can use the following code to import Numpy:
import numpy as np
This will enable us to use the alias “
np” in our code for the Numpy module.
EXAMPLE 1: Calculate absolute values for a single value
Ok, let’s start with a very simple example.
Here, we’re going to compute the absolute value of a single number.
This is extremely simple.
Here, the input to the Numpy absolute value function is a negative number,
-5. When we provide this value as the argument to the function, np.abs() simply computes the absolute value, which is 5.
To be clear, you could also run this code as
np.absolute(-5). It’s effectively the same as
Example 2: Calculate absolute values for a 1D array
Next, we’ll do a slightly more complicated example.
Here, we’ll compute the absolute values of an array of values.
To do this, we first need to create an array, and then we can use
np.abs() on that array.
Create 1D array
First, we’ll create a 1-dimensional array that contains the values from -2 to 2.
To do this, we’ll use the Numpy arange function.
array_1D = np.arange(start = -2, stop = 3)
And we can print it out to see the values.
[-2 -1 0 1 2]
Obviously, this array contains two negative values.
Next, we’ll apply the
np.abs() function to compute the absolute values.
array([2, 1, 0, 1, 2])
When we use np.abs() on the array
array_1D, it computes the absolute value of every value in the array.
The output is a new array that contains those absolute values.
EXAMPLE 3: Use Numpy Absolute value on a 2D array
Finally, let’s use Numpy absolute value on a 2-dimensional array.
This is very similar to example 2, where we operated on a 1D array.
Create 2D array
First, we’ll create a 2-dimensional array with the Numpy arrange function, and the Numpy reshape method:
array_2D = np.arange(start = -4, stop = 5).reshape((3,3))
And let’s print it out:
[[-4 -3 -2] [-1 0 1] [ 2 3 4]]
This is a simple 2D array that contains the values from -4 to 4, arranged in a 3 by 3 shape.
Next, we can apply np.abs to the 2D array.
array([[4, 3, 2], [1, 0, 1], [2, 3, 4]])
This is really simple.
The numpy absolute value function simply operates on every element of the 2D array, element wise.
The output is a new Numpy array that has the same shape. The values of the new Numpy are the absolute values of the values of the input array.
Frequently asked questions about np absolute value
Now that we’ve looked at a few examples, let me answer a common question about np absolute value.
Frequently asked questions:
Should I use np.abs or np.absolute
The most common question about Numpy absolute value is whether you should use
The short answer is: it doesn’t matter.
np.abs() is a shorthand version of
np.absolute(). They are effectively the same function, and work the same way.
Pick one version, and move on.
Leave your other questions in the comments below
Do you have other questions about Numpy absolute value?
Leave your questions in the comments section at the bottom of the page.
Join our course to learn more about Numpy
The examples you’ve seen in this tutorial should be enough to get you started, but if you’re serious about learning Numpy, you should enroll in our premium course called Numpy Mastery.
There’s a lot more to learn about Numpy, and Numpy Mastery will teach you everything, including:
- How to create Numpy arrays
- How to use the Numpy random functions
- What the “Numpy random seed” function does
- How to reshape, split, and combine your Numpy arrays
- How to perform mathematical operations on Numpy arrays
- and more …
Moreover, it will help you completely master the syntax within a few weeks. You’ll discover how to become “fluent” in writing Numpy code.
Find out more here: