Python NumPy arange() Tutorial
NumPy library offers a wide range of functions and, arange function is one of the most used methods. The arange function is used to create evenly spaced values and then returns the reference to them. NumPy provides four parameters for the NumPy arange function:
Start: The Start parameter takes an integer value which denotes the first value of the array.
Stop: The Stop parameter takes the value which denotes the end of the array.
Step: This parameter is used for the spacing between the consecutive values in the array and it is also a number.
dtype (DataType): This parameter takes the data type for the elements of the resulting array.
Benefits of NumPy arange
The NumPy arrays come with some major speed benefits when it comes to execution times, size, memory, etc. The NumPy functions take less memory and perform much better than regular lists in terms of functionality.
The arange function itself provides us the ability to create a ndarray with equally spaced values as well as references to it.
The major benefit of using arange is its ability to control the interval of values contained in a list. arange function requires much less memory as compared to conventional lists.
The NumPy returns the ndarrays instead of lists which allows the developer to pass any data type.
Earlier, the built-in Python functions such as range and xrange restricted the developers to only use/pass integers. Another benefit of using NumPy arange is that it provides the ability to be used by other NumPy methods such as np.where which allows us to find the values generate by np.arange function.
Python Step Size
In Python, the step size is a part of an indexing trick that retrieves a specific element in indexing, according to size. If the step size is set to 1, it means every element will be selected.
However, step size 2 means that every alternative element will be chosen, and it continues based on the step size. Let’s go over an actual example:
# A list of 10 numbers my_list = [1,2,3,4,5,6,7,8,9,10] # Printing the list with Step Size of 2 # It will print the odd numbers according to our list print(my_list[::2])
The above code snippet shows that we provided the list with only the step size of 2. It implies that it will space the elements equally with the interval of 2 from start to end of the list.
# Getting the even numbers between 1 to 9 with a step size of 2 print(my_list[1:9:2])
In the above example, we provided the list with all the arguments such as Start, Stop, and Step size, so the list will start from 1 and will end at 9 with an interval of 2 consecutive values among all the values in the list.
We can reshape the array by using the “reshape” function with “np.arange”. The purpose of reshaping is to edit the dimensions of the array.
As we can add and delete the number of dimensions of the array as well as change the values of the array.
The following snippet is the code example of how to use it. The example code illustrates how we can change the dimensions of the array from one to many.
b = np.arange(2,8).reshape(3,2) print(b)
Python range VS NumPy arange
The range() is a built-in Python function that provides the functionality of generating integer numbers from a defined range. The numbers are fetched from the list; so it works on it.
The range has the functionality of Start, Stop & Step as well.
# range with Start, Stop & Step l = range(1, 10, 2) for i in l: print(i)
Although it looks the same as arange, there are two differences between range and NumPy arange.
The first difference is that the Python range function only generates integers, whereas, arange generates numbers of different data types available in a NumPy array.
The other difference is that range only takes the list as input whereas arange accepts other formats too.
import numpy as np a = np.arange(4) print(a)
arange Data Types
NumPy arange uses the integer data type by default and the Data Type argument is represented by dtype. Some of the commonly used data types with np.arange are int, np.int32, np.int64, np.csingle. The following code is an example for np.csingle data type.
# Data Type example with csingle data type result_array = np.arange(start=10, stop=30, step=1, dtype=np.csingle) print(result_array)
Float data type is also very commonly used as a shorthand method with np.arange:
# Float data type example result_array = np.arange(start=10, stop=30, step=1, dtype=float) print(result_array)
NumPy arange takes the three main Range Arguments which are start, stop, and step.
The start argument specifies the starting range i.e., from which number it will start, the stop argument specifies the ending number i.e., until which number it shall stop, and the step argument specifies the spacing between the consecutive values, the following snippet is an example of the usage of all range arguments:
# example of the usage of all range elements a = np.arange(start=2, stop=10, step=2) print(a)
In the above example, the start argument is set to 2, which specifies the array starting number will be 2. The stop argument is set to 10 which specifies that the array will end at 10.
The step argument is set to 2 which specifies that it shall add a spacing of 2 elements i.e., only fetching alternative values.
Hence, the output starts at 2 and ends at 8, adding the spacing of 2 among all the consecutive values.
We can also provide only one range argument to the np.arange function and it will automatically assume that the provided argument is the stop argument without mentioning it.
arange of datetime
Among other functionalities, it is also possible to generate date times data with the np.arange method by providing the starting and ending date range along with the number of days. Check the following code example:
# example of datetime generation with np.arange function dt = np.arange(datetime(1985,7,1), datetime(2015,7,1), timedelta(days=1)).astype(datetime) print(dt)
The above code provides the idea of how np.arange can be used to generate dates on the basis of given range of dates as in the above example, we provided the start date as 1985 and end date as 2015 with a step size of 1 day interval which means the resultant dates would be equally spaced on 1 day interval for each date.
Passing negative arguments
As this is the case where we can use the negative values for start, stop and step arguments. It would seem that it may not work but it works normally.
The only difference between negative and positive values is that the negative arguments will generate negative values whereas positive arguments generate positive values.
a=np.arange(-10, -1) print("The output is:") print(a)
What is np.linspace?
NumPy linspace function is used to return the evenly spaced values over a specified interval. It is quite similar to the NumPy arange function, but it does not use step argument to specify the interval.
Instead of doing that, it uses the sample number to provide the evenly spaced values, according to the sample number. Check the following snippet to understand it in more detail:
# Code example for linspace print("B\n", np.linspace(2.0, 3.0, num=5, retstep=True), "\n") # To evaluate sin() in long range x = np.linspace(0, 2, 10) print("A\n", np.sin(x))
The above code example shows the usage of np.linspace as it takes a number of arguments such as start, stop, restep, num, and dtype. These arguments are used in the above example to demonstrate its usage in detail and delineate the differences from the np.arange.
arange vs linspace (when to use each)
As we went over both np.arange and np.linspace, we understand that both have their own functionalities, and both provide the ability to add equal spacing.
However, the major difference between np.arange and np.linspace is that np.arange lets us define the step size and infers the number of values we get.
On the other hand, the np.linspace allows us to define that how many values we will get including the maximum and minimum number as it infers with the step size, not the actual values. Check the following example of both np.arange and np.linspace:
# Example of np.linspace print(np.linspace(0,1,11)) # Example of np.arange print(np.arange(0,1,.1))
The above example shows the usage of both linspace and np.arange as both of them seems to be identical but are much different in terms of functionality.
NumPy arange VS xrange
xrange: xrange was a built-in Python function that was discontinued in Python 3.x. It was used by loop i.e., xrange(0,10) to generate a number for the provided range.
It was also used as a replacement to the range function, but it did not add much value in terms of execution times and memory consumption.
Furthermore, the arguments can be a float as well whereas xrange doesn’t provide float support.
Setting Float or Decimal Step Size in Python range
In Python range function, the step size can strictly be an integer only. In case of decimal or float values, range function returns an error of non-integer value in the output.
The following code snippet demonstrates what would happen if we tried to pass a float to range function step size argument:
for i in range(0, 1, 0.1): print(i)
Module NumPy has no attribute ‘arrange’
NumPy is written in C & Python to provide more speed, most of the functions are located somewhere in the depths of those files, and in order to use them, one has to call them by their exact spellings, and no typos can be tolerated.
So, when someone calls the np.arange function with np.arrange instead of np.arange, it will not work as it is not correct and requires the exact name of the function to call.
Memory Error or Empty Array when using arange
Memory errors mostly occur when the system runs out of RAM for the code to execute further.
The major reason behind this is that sometimes people tend to load the whole datasets in the np.arange instead of using batch processing or fetching the data in batches from the hard disk.
The RAM can handle the amount of data to certain limits as it is stored in a temporary heap and does not have enough memory to store Gigabytes of data in one execution.
Empty arrays on the other hand come when the floating-point spacing is more than the actual allocated space for the array as the allocated space is determined through the intermediate floating-point calculations.
As NumPy arange does not accept floating point values so this could be resolved by using ceil((stop – start)/step) as we are facing the floating point overflow, this could help in outing the last element rather than being greater than the stop.
start=2e9 end=start+321 step=0.066833171999 x=np.arange(start,end,step=step) print(x[-1]>end) # Prints "True" print(x[-1]-end) # Prints 0.00013661384582519531
Inconsistent Runtime Warning
The problem behind getting this runtime warning is the data type that is being passed to the array as this problem can also be solved by using range instead of arange.
To use it with arange, we will have to change the datatype which is being inferred to np.arange being “np.int32”.
total = 0 k = 10**6 arr = np.arange(k, 0, -1) for i in arr: total += 1/(i**2) print(total)
As np.arange accepts np.int64 or np.float64 in case of a float value, by using one of these, the problem can be resolved.
# Solution arr = np.arange(k, 0, -1, dtype=np.float64)
I hope you find the tutorial useful. Keep coming back.
Mokhtar is the founder of LikeGeeks.com. He works as a Linux system administrator since 2010. He is responsible for maintaining, securing, and troubleshooting Linux servers for multiple clients around the world. He loves writing shell and Python scripts to automate his work.