How to Use Numpy Round
This article is originally published at https://www.sharpsightlabs.com
In this tutorial I’ll explain how to use the Numpy round function (AKA, np.round).
I’ll explain how the function works, and I’ll also show you some step-by-step examples of how to use it.
Table of Contents:
You can click on any of the above links and it will take you to the appropriate section of the tutorial.
That said, you’re relatively new to Numpy, you might want to read the whole tutorial.
A quick introduction to Numpy Round
Let’s start off with a quick explanation of what the Numpy round function is and what it does.
Numpy round is a function that’s included in the Numpy module for the Python programming language.
Numpy is a module for working with numeric data. Specifically, Numpy works with data organized into a structure called a Numpy array.
A Numpy array has a row-and-column structure, and is filled with numeric data. Here’s an example of a 2-dimensional Numpy array.
So Numpy has a variety of functions for creating these arrays of Numeric data (like Numpy arange, Numpy ones, Numpy randint, etc)
… but it also has a variety of functions for manipulating these numeric arrays.
Numpy Round is a Math Function for Numpy
Numpy Round is one of the Numpy functions that we use to manipulate Numpy arrays.
The Numpy module has a variety of data manipulation functions.
Some of these functions (like Numpy reshape or Numpy concatenate) deal with reshaping arrays or combining Numpy arrays.
But many Numpy functions perform mathematical operations on Numpy arrays. So we have functions for summing Numpy arrays, calculating exponents on array values, and more.
The Numpy round function is one of these mathematical functions.
Numpy Round Rounds the Numbers in a Numpy Array
A little more specifically: Numpy round rounds numbers.
It can do this with single input numbers like this:
When it operates on a single input value, Numpy round rounds the number to the nearest integer value.
Numpy Round Rounds Values of Numpy Arrays, Element Wise
Although we can use np.round on single values, you can also use Numpy round on arrays of numbers.
For example, you could use Numpy round on a 1-dimensional array of numbers. When you do this, Numpy will apply the np.round() function to every element of the array.
In other words, it will round all of the numbers, and the output will be a new Numpy array of the same shape that contains the rounded numbers.
The Syntax of np.round
Now that we’ve looked at what Numpy round does at a high level, let’s take a look at the syntax.
A quick note
One quick note before we get started.
Before we use Numpy, we need to import the Numpy package.
How exactly we import Numpy will change the exact form of the syntax.
Among data scientists, the common convention is to import Numpy as “np
“, like this:
import numpy as np
When we import Numpy like this, we’ll be able to use np
as a prefix when we call our Numpy functions.
As I said, this is the common convention among most Python users, and we’ll use it in this tutorial.
np.round syntax
Ok. The syntax for the round function is fairly simple.
We call the function as np.round().
Then, inside the parenthesis, we provide an input.
This input can be a single number (i.e., a Python float) or a Numpy array.
Having said that, let’s take a closer look at the input parameters as well as the output.
The Parameters of np.round
The np.round function has two major input parameters, a
and decimals
.
Additionally, there is one other parameter called out
, which is somewhat less commonly used.
Let’s quickly take a look at those.
a [required]
The a
parameter enables you to specify the input value or values to the function.
As you’ll see in the examples section, the input value that you provide can be a single number (i.e., a float
) or a Numpy array. It can also be an array-like object like a Python list.
This is required, in the sense that you need to provide an input to the function.
Having said that, when you use this, you do not need to explicitly type the parameter as a=
. Python knows that you’re passing an argument to this parameter by position.
decimals
The decimals
parameter enables you to specify the number of decimals places that will be used when the input numbers are rounded.
By default, this parameter is set to decimals = 0
(i.e., the number is rounded to the nearest integer value).
Technically, you can also specify a negative value for decimals
. If you do this, it will control the number of positions to the left of the decimal point to which to round the numbers.
out
The out
parameter enables you to specify an output array in which to store the output of the function.
This is somewhat rarely used, so we’re not going to cover this in the examples.
The Output of np.round
The output of np.round is a Numpy array with the same shape as the input. The output will contain the rounded values of the input.
Keep in mind that np.round does not change the original array by default. The output is a new array that will be sent to the console (if you’re working in an IDE) or that can be stored with a specific name using the =
sign.
Examples of how to use Numpy Round
Ok. Now that we’ve looked at the syntax, let’s look at some examples of Numpy round.
You can click on any of the following links, and it will take you to the appropriate example.
Examples:
- Round a value downward [example 1]
- Round a value upward [example 2]
- Use np.round on a negative number [example 3]
- Round a number to a specific decimal place [example 4]
- Round the values of a Numpy array [example 5]
Run this code first
One thing before you run any of the examples.
Before you run the example code, you need to import Numpy properly. You can import Numpy by running the following:
import numpy as np
This will allow us to call Numpy round with the prefix np
.
EXAMPLE 1: Round a value downward
Ok, let’s start simple.
Here, we’re going to round a single floating point number (a decimal value).
np.round(1.1)
OUT:
1.0
Explanation:
This is really straight forward. Here, we’re calling np.round on the value 1.1.
It’s simply rounding the number to the nearest integer value, which is 1.0.
Note that any input with a decimal value less than .5 will round down to the nearest integer value. So 1.2, 1.3, and 1.4 will all round down to 1.0, just like this example here.
But keep in mind, even though it’s rounding to the nearest integer value, the data type of the output is actually a float
.
This is a subtle point, but it might be important!
EXAMPLE 2: Round a value upward
Just to make things clear, let’s round another single value.
In the previous example, we rounded the value 1.1, which rounded down to 1.0.
Now, let’s input a value that will round upward.
np.round(1.5)
OUT:
2.0
Explanation:
Here, we’re rounding the value 1.5, which rounds upward to 2.0.
Keep in mind that any value with a decimal greater than or equal to .5 will round upward.
EXAMPLE 3: Round a negative number
Things get slightly more complicated when we work with negative numbers, but it’s intuitive once you understand the principle at work.
Let’s take a look at the code, and then I’ll explain.
np.round(-1.333)
OUT:
-1.0
Explanation:
Here, np.round rounded to -1.0. Why?
Whenever we use np.round, the function will round the input to the nearest integer value.
In this case, the nearest integer is -1.0.
This is really simple if you just remember that Numpy round rounds to the nearest integer.
EXAMPLE 4: Round a number to a specific decimal place
Next, we’ll round a number to a specific decimal place.
Specifically, we’ll round the number e
(Euler’s number) to 3 decimal places.
np.round(np.e, decimals = 3)
OUT:
2.718
Explanation:
In Numpy, the value of the constant np.e
is 2.718281828459045.
When we use np.round with decimals = 3
, the function rounds the input value to 3 decimal places, which in this case is 2.718.
EXAMPLE 5: Round the values of a Numpy array
Let’s do one more example.
Here, we’ll round the values of a Numpy array.
Create 2D array
To do this, we’ll create a Numpy array with floating point Numbers using the Numpy random uniform function. Numpy random uniform generates floating point numbers randomly from a uniform distribution in a specific range. Here, we’ll draw 6 numbers from the range -10 to 10, and we’ll reshape that array into a 2×3 array using the Numpy reshape method. (Note that we’re also using Numpy random seed to set the seed for the random number generator.)
#CREATE 2D ARRAY np.random.seed(2) floats_2d = np.random.uniform(size = 6, low = -10, high = 10).reshape((2,3))
And let’s take a look at the numbers in the array:
print(floats_2d)
OUT:
[[-1.28010196 -9.48147536 0.99324956] [-1.29355215 -1.59264396 -3.39330358]]
So floats_2d
contains 6 decimal numbers between -10 and 10, arranged into a Numpy array with 2 rows and 3 columns.
Round Numbers in Array
Now, let’s round the numbers.
np.round(floats_2d)
OUT:
array([[-1., -9., 1.], [-1., -2., -3.]])
Explanation
Here, np.round simply rounded all of the numbers of the input array to the nearest integer.
Notice a few things.
First, the output array has the exact same shape as the input array.
Second, all of the rounded values in the output have the float
data type. So even though the inputs have been rounded to the nearest integer value, they’re actually being reported as floating point numbers.
Leave your other questions in the comments below
Do you have other questions about Numpy round?
Leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve shown you how to use one Numpy function: Numpy round.
The np.round function is pretty easy to understand, once you work with it a little bit.
But other parts of Numpy can be a lot more complicated.
If you’re serious about learning Numpy, and if you’re serious about data science in Python, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, 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. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.
Find out more here:
Learn More About Numpy Mastery
The post How to Use Numpy Round appeared first on Sharp Sight.
Thanks for visiting r-craft.org
This article is originally published at https://www.sharpsightlabs.com
Please visit source website for post related comments.