Introduction
In this tutorial, you will learn how to create NumPy arrays using numerical ranges. NumPy provides various functions to create arrays from specified numerical ranges, such as arange, linspace, and logspace.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Using numpy.arange
numpy.arange is a function used to create an array by using evenly spaced values over any given interval.
Syntax
numpy.arange(start, stop, step, dtype)
Parameters
- start: This parameter indicates the starting point of the interval. The default value is 0.
- stop: This parameter represents the value at which the interval ends excluding this value.
- step: This parameter represents the number by which the interval's values change.
- dtype: This parameter indicates the data type of the NumPy array items.
Example
import numpy as np
arr = np.arange(0, 10, 2)
print(arr)
Output:
[0 2 4 6 8]
Using numpy.linspace
numpy.linspace is similar to arange() function, but instead of a step size, the number of evenly spaced values between the interval is specified using the num argument.
Syntax
numpy.linspace(start, stop, num, endpoint, retstep, dtype)
Parameters
- start: This parameter represents the starting value of the interval.
- stop: This parameter represents the stopping value of the interval.
- num: This parameter indicates the amount of evenly spaced samples over the interval to be generated. The default value is 50.
- endpoint: A Boolean parameter's value is used to indicate that the stopping value is included in the interval.
- retstep: The value of this parameter is a boolean value and is used to represent the steps and samples between the consecutive numbers.
- dtype: This parameter is used to represent the data type of the array items.
Example
import numpy as np
## start=20, end=30, num=5
arr = np.linspace(20, 30, 5)
print(arr)
Output:
[20. 22.5 25. 27.5 30.]
Using numpy.logspace
numpy.logspace is used to create an array by using the numbers that are evenly separated on a log scale.
Syntax
numpy.logspace(start, stop, num, endpoint, base, dtype)
Parameters
start: This parameter represents the starting value of the interval in the base.
stop: This parameter represents the stopping value of the interval in the base.
num: This parameter indicates the number of values between the range.
endpoint: The value of this Boolean parameter used to make the value represented by the stop as the last value of the interval.
base: A parameter is used to represent the base of the log space.
dtype: This parameter is used to represent the data type of the array items.
Example
import numpy as np
arr = np.logspace(5, 30, num=5, base=3, endpoint=True)
print("The array is: ", arr)
Output:
The array is: [2.43000000e+02 2.33138563e+05 2.23677324e+08 2.14600041e+11
2.05891132e+14]
Summary
In this tutorial, you learned that the NumPy arrays can be created using specified numerical ranges. We used three functions (numpy.arange, numpy.linspace, and numpy.logspace) to create arrays from numerical ranges. We covered those functions with their syntax, parameters, and related examples.