Effortlessly Master the Python Range Function
NumPy arange(): How to Use np.arange()
Return Value and Parameters of np.arange()
NumPy arange()
is a function that creates an instance of the ndarray
array type with evenly spaced values. It returns a reference to the created array.
The arange()
function takes four parameters that define the range of values in the array, the spacing between them, and the data type of the elements:
start
(optional): This parameter specifies the first value in the array. It can be an integer or a decimal number.stop
: This parameter specifies the end of the array and is not included in the array.step
(optional): This parameter specifies the spacing (difference) between each two consecutive values in the array. The default value is 1.dtype
(optional): This parameter specifies the data type of the elements in the output array. The default value isNone
.
It is important to note that step
cannot be zero, otherwise a ZeroDivisionError
will occur. Additionally, if the increment or decrement is 0, the array will not move away from the start
value.
Range Arguments of np.arange()
The np.arange()
function can be used in different ways depending on the number of range arguments provided. Here are the various ways to use np.arange()
:
Providing All Range Arguments
One way to use np.arange()
is by providing all the range arguments: start
, stop
, step
, and dtype
. This allows you to create an array with evenly spaced values within the specified range. Here’s an example:
Output:
In this example, the array arr
contains the values 0, 2, 4, 6, and 8. The start value is 0, the stop value is 10 (not included in the array), the step value is 2, and the data type is integer (dtype=int
).
Providing Two Range Arguments
Another way to use np.arange()
is by providing only two range arguments: start
and stop
. In this case, the step value is set to the default of 1. Here’s an example:
Output:
In this example, the array arr
contains the values 0, 1, 2, 3, and 4. The start value is 0, the stop value is 5 (not included in the array), and the step value is 1 (default).
Providing One Range Argument
If only one range argument is provided, it is considered as the stop
value, and the start
value is set to 0, and the step value is set to 1. Here’s an example:
Output:
In this example, the array arr
contains the values 0, 1, 2, 3, and 4. The start value is 0 (default), the stop value is 5 (not included in the array), and the step value is 1 (default).
Providing Negative Arguments
The np.arange()
function also supports negative arguments for the range values. This allows you to create arrays with values descending from the start value. Here’s an example:
Output:
In this example, the array arr
contains the values 5, 4, 3, 2, and 1. The start value is 5, the stop value is 0 (not included in the array), and the step value is -1.
Counting Backwards
You can also use np.arange()
to count backwards by providing a negative step value. Here’s an example:
Output:
In this example, the array arr
contains the values 10, 8, 6, 4, and 2. The start value is 10, the stop value is 0 (not included in the array), and the step value is -2.
Getting Empty Arrays
If the stop value is less than the start value and the step value is positive, or if the stop value is greater than the start value and the step value is negative, the result is an empty array. Here’s an example:
Output:
In this example, the array arr
is empty because the stop value (0) is less than the start value (5), and the step value is positive (default).
Data Types of np.arange()
The np.arange()
function allows you to specify the data type of the elements in the output array using the dtype
parameter. By default, the dtype
parameter is set to None
, which means the data type of the elements will be determined automatically based on the inputs. However, you can specify a specific data type if needed.
Here’s an example that demonstrates how to specify the data type when using np.arange()
:
Output:
In this example, the array arr
contains the values 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, and 9.0. The data type of the elements is specified as float
using the dtype
parameter.
Beyond Simple Ranges With np.arange()
The np.arange()
function can be used to create more complex ranges by combining it with other NumPy functions. For example, you can use np.arange()
to create a range of dates or a range of values with a specific pattern.
Here’s an example that demonstrates how to create a range of dates using np.arange()
and the np.datetime64
data type:
Output:
In this example, the array dates
contains the dates ‘2020-01-01’, ‘2020-01-02’, ‘2020-01-03’, and ‘2020-01-04’.
Comparison of range and np.arange()
The np.arange()
function is similar to the Python built-in class range()
, but there are some differences in terms of the parameters and outputs.
Parameters and Outputs
range()
takes three parameters:start
,stop
, andstep
. Thestart
andstep
parameters are optional, and the default values are0
and1
, respectively. The output is an iterator.np.arange()
takes four parameters:start
,stop
,step
, anddtype
. Thestart
,step
, anddtype
parameters are optional, and the default values are0
,1
, andNone
, respectively. The output is a NumPy array.
Creating Sequences
range()
creates a sequence of integer values. It provides a lightweight way to loop over a range of values.np.arange()
creates a sequence of values with a specified step size. It is more versatile and allows you to create ranges with decimal values or non-integer step sizes.
Python for Loops
range()
is commonly used in Pythonfor
loops. It allows you to iterate over a range of values and perform certain operations.np.arange()
is often used in conjunction with other NumPy functions to create arrays for mathematical operations or data manipulation.
Other Routines Based on Numerical Ranges
NumPy offers many other routines for working with numerical ranges. Some of these routines include:
np.linspace()
: This function creates an array of evenly spaced values within a specified range.np.logspace()
: This function creates an array of logarithmically spaced values within a specified range.np.geomspace()
: This function creates an array of geometrically spaced values within a specified range.
These routines provide additional flexibility and options for creating arrays with specific spacing or distribution.
Quick Summary
In this article, we have learned about the np.arange()
function in NumPy, which allows us to create arrays with evenly spaced values based on numerical ranges. We have explored the different ways to use np.arange()
by providing different combinations of range arguments. We have also discussed the data types of the output arrays, as well as the comparison between np.arange()
and the Python built-in range()
class. Additionally, we have briefly mentioned other NumPy routines that are based on numerical ranges.
Using np.arange()
effectively can enhance your Python programming skills, especially when you are working with libraries like SciPy, Pandas, Matplotlib, and scikit-learn that rely on NumPy arrays.
Conclusion
NumPy’s arange()
function is a powerful tool for creating arrays with evenly spaced values. It allows you to define the range of values, the spacing between them, and the data type of the elements. By understanding the various ways to use np.arange()
and its parameters, you can create arrays that suit your specific needs in numerical computing and data analysis tasks.