What is Numpy Arange function in Python
The Numpy Arange function is used to create a numpy array whose elements are evenly distributed within a given range. In this tutorial, we will understand the syntax of np.arange() and go through multiple examples by using its various parameters.
Numpy Arange : numpy.arange()
Syntax
numpy.arange(start=0, stop, step=1, dtype)
- start (optional) – It denotes the starting value of the range, if not specified the default value is 0.
- stop – It denotes the ending value of the interval, it should be noted that this is excluded from the range of values of the numpy array. However, in some situations where the step is a float number the round-off effect can result in the inclusion of this value.
- step (optional) – It denotes the steps or interval between the values of the numpy array, if not specified the default is 1.
- dtype (optional) – It denotes the data type of the numpy array, if not specified, then it will derive the data type from the other input arguments.
Examples of Numpy Arange
Import Numpy Library
Before starting the examples, we shall import the numpy library as below.
In [0]:
import numpy as np
Example 1 – Simple Example of Numpy Arange
We start with the very basic example of numpy array where we pass just the only mandatory argument stop with a value of 10. This creates a numpy array with the default start value of 0 and the default step of 1.
In [1]:
arr = np.arange(10); print(arr)
[0 1 2 3 4 5 6 7 8 9]
Example 2 – Numpy Arange with Both Start and Stop
Let us rewrite the first example by including the parameter start = 0 and it can be seen that the same numpy array is produced. This also confirms the fact that the default value of stop is 0.
In [2]:
arr = np.arange(start = 0, stop = 10); print(arr)
[0 1 2 3 4 5 6 7 8 9]
Example 3 – Numpy Arange with Step Interval
This time let us modify the previous example by including the step parameter as 2 which creates the numpy array with evenly spaced values between 0 and 10 at an interval of 2.
In [3]:
arr = np.arange(start = 0, stop = 10, step=2); print(arr)
Out[3]:
[0 2 4 6 8]
Example 4 – np.arange with dtype parameter
By default, the arange function infers the data type of the numpy array from input parameters, however, we can define the data type by using dtype parameter.
In the below example we have used dtype = float which produces the output array with all values as float type.
In [4]:
arr = np.arange(start = 0, stop = 10, step=2, dtype=float); print(arr)
Out[4]:
[0. 2. 4. 6. 8.]
Example 5 – Numpy Arange with Float Step Interval
In this example, we assign the step parameter a float value of 1.5 which creates the corresponding ndarray as shown below.
In[5]:
arr = np.arange(start = 0, stop = 10, step=1.5, dtype=float); print(arr)
Out[5]:
[0. 1.5 3. 4.5 6. 7.5 9. ]
Example 6 – Issues with Float Step Interval
Although we can use float value for the step size, however, the documentation of numpy gives a caution while using it that the “output might not be numerically stable“. This is due to how the array population calculation takes place internally.
Such an example can be seen below.
In[6]:
arr = np.arange(start = 0, stop = 5, step = 0.5, dtype=int) print(arr)
Out [6]:
[0 0 0 0 0 0 0 0 0 0]
As we can see above that numpy array generation can go haywire while using the float value of step. Hence the documentation recommends using numpy linspace function instead for the non-integer or float step values.
You can learn more about linspace function below –