Numpy arange() Function



The Numpy arange() function is used to create a numpy array with evenly spaced elements as per the interval. The function takes the start, stop, and step parameters to define the sequence. It returns an array of elements that are generated within the specified interval.

The primary use of arange() function is to create sequences of numbers for iteration, data plotting, or as input for matrix operations. It's optimized for performance and integrates seamlessly with other NumPy operations. It is similar to the built-in range() function but it returns a numpy array instead of a list.

Syntax

Following is the syntax of the Numpy arange() function −

numpy.arange([start, ]stop, [step, ]dtype=None, like=None)

Parameters

Following are the parameters of the Numpy arange() function −

  • start(optional): It is the start of interval range. By default start = 0
  • stop: It is the end of interval range
  • step(optional): It is the spacing(difference) between each two consecutive values in the array. By default step = 1.
  • dtype(optional): This is the type of output array
  • like (optional): It allows the creation of an array which is like the input object but uses an existing array-like object (like another NumPy array).

Return Values

This function returns a numpy array of evenly spaced values.

Example

Following is a basic example to create a evenly spaced numpy array using Numpy arange() function −

import numpy as np
my_Array = np.arange(10)
print("Numpy Array",my_Array)
#type of array
print(type(my_Array))

Output

Following is the output of the above code −

Numpy Array [0 1 2 3 4 5 6 7 8 9]
<class 'numpy.ndarray'>

Example : Specified Start, Stop, Step

Using numpy.arange() function, we can generate a numpy array from start value to the end value with a spacing between two consecutive numbers by specifing step value.

In the following example, we have generated a NumPy array of multiples of 2 less than 10 using the numpy.arange() function, with the start value set to 2, the stop value set to 10, and the step value set to 2 −

import numpy as np
my_Array = np.arange(2,10,2)
print("Numpy Array -",my_Array)

Output

Following is the output of the above code −

Numpy Array - [2 4 6 8]

Example : Using 'step=0' in 'arange()'

The step parameter provides spacing between the two values. By default step values is 1. When we assign step value to zero it will raise ZeroDivisionError.

In the following example, we have assigned step parameter to zero in numpy.arange() function −

import numpy as np
my_Array=np.arange(10,20,0)
print(my_Array)

Output

Following is the output of the above code −

Traceback (most recent call last):
  File "/home/cg/root/32073/main.py", line 2, in <module>
    my_Array=np.arange(10,20,0)
ZeroDivisionError: division by zero

Example : N-dimensional Array Using 'arange()'

The numpy.arange() function, when combined with the reshape() method, allows for the creation of multi-dimensional arrays.

Here, we have generated 2-D numpy array of shape (3,4) using numpy.arange(). Where the start = 1, stop = 10 and step = 2 −

import numpy as np
my_Array = np.arange(0,8,2).reshape(2,2)
print("2D Numpy Array \n",my_Array)

Output

Following is the output of the above code −

2D Numpy Array 
 [[0 2]
 [4 6]]

Example : Creating a Float Data-type

We can create a NumPy array of floating-point numbers using numpy.arange() by specifying the dtype as float.

In the following example, we have generated a float point numpy array using numpy.arange()

import numpy as np
# create an array with elements from 0 to 1 with stepsize 0.2
array1 = np.arange(0, 10,3,dtype='float')
print("Float Numpy Array-",array1)

Output

Following is the output of the above code −

Float Numpy Array- [0. 3. 6. 9.]
numpy_array_creation_routines.htm
Advertisements