In this post, we will learn how to use arange(), one of the most useful functions in Numpy. With Numpy arange function we can create evenly spaced arrays.
For example, If you need to create an array of numbers within a given range, you can use the arange function.
To get started let us load numpy and check the Numpy version we are using.
import numpy as np np.__version__ 1.24.0
Numpy arange() Syntax
Basic syntax of using Numpy’s arange() function with default values is
np.arange(start=0, end, step=1)
Here, the argument start is optional and it can be integer or real. It defines the start of the interval including the value. By defulat, start is set to 0.
The next argument is stop and it defines the end of the interval. It can be integer or real. The interval we get does not include the end value (except due to some round off effects under certain condition).
The step argument is optional and it defines the spacing between the values in the interval. And the default value of step is 1. In addition to these arguments, we can also specify dtype for the type of output array. It is optional and arange() function infers the datatype from the inputs.
How to use Numpy arange() to generate evenly spaced numbers
For example, to create an array of numbers from 0 to 9, you can use the following code:
np.arange(10)
We get evenly spaced numbers from 0 to 9.
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
As we saw in the Numpy arange syntax, the code below is the same as the one in the above example to get evenly spaced numbers from 0 to 10 (not including 10) with an increment of 1.
np.arange(start=0, end=10, step=1)
To get a sequence of even numbers, we can change the step argument as shown below.
even_array = np.arange(0, 10, 2)
And we get even numbers.
even_array array([0, 2, 4, 6, 8])
Similarly, with start=1 and step=2, we get the list of odd numbers from 1 to 9.
odd_array = np.arange(1, 10, 2) odd_array array([1, 3, 5, 7, 9])
Numpy arange example with float step size
In the previous examples our step sizes were integers. Here is an example where the step size is 0.1 and we generate array from 0 to 1 with arange()
.
# Generate an array of 10 evenly spaced values from 0 to 1, with float64 data type np.arange(0, 1, 0.1, dtype=np.float64) [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
Numpy arange example with specific data type
In the example below, we set the datatype to be int8 and generate an array of 5 evenly spaced numbers from 1 to 10 using Numpy’s araange()
# Generate an array of 5 evenly spaced values from 1 to 10, with int8 data type np.arange(1, 11, 2, dtype=np.int8) [1 3 5 7 9]
A caveat to note with Numpy arange
Numpy’s arange() function is very similar to Python range() function. One big difference between arange() and range() function is that Python’s range() function can take arbitrary size integer.
However, Numpy’s arange() produces numpy.int32 or numpy.int64 numbers. And this can result in incorrect results for large integer values. Numpy’s help function for arange() nicely illustrates this problem with the following example
power = 40 modulo = 10000 x1 = [(n ** power) % modulo for n in range(8)] x2 = [(n ** power) % modulo for n in np.arange(8)] print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect