This section of the tutorial illustrates how the numpy arrays can be created using some given specified range.
Numpy.arrange
It creates an array by using the evenly spaced values over the given interval. The syntax to use the function is given below.
snippet
numpy.arrange(start, stop, step, dtype)
It accepts the following parameters.
- start: The starting of an interval. The default is 0.
- stop: represents the value at which the interval ends excluding this value.
- step: The number by which the interval values change.
- dtype: the data type of the numpy array items.
group
Example
snippet
import numpy as np
arr = np.arange(0,10,2,float)
print(arr)
Output:
Example
snippet
import numpy as np
arr = np.arange(10,100,5,int)
print("The array over the given range is ",arr)
Output:
Output
The array over the given range is [10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]
NumPy.linspace
It is similar to the arrange function. However, it doesn?t allow us to specify the step size in the syntax.
Instead of that, it only returns evenly separated values over a specified period. The system implicitly calculates the step size.
The syntax is given below.
snippet
numpy.linspace(start, stop, num, endpoint, retstep, dtype)
It accepts the following parameters.
- start: It represents the starting value of the interval.
- stop: It represents the stopping value of the interval.
- num: The amount of evenly spaced samples over the interval to be generated. The default is 50.
- endpoint: Its true value indicates that the stopping value is included in the interval.
- rettstep: This has to be a boolean value. Represents the steps and samples between the consecutive numbers.
- dtype: It represents the data type of the array items.
Example
snippet
import numpy as np
arr = np.linspace(10, 20, 5)
print("The array over the given range is ",arr)
Output:
Output
The array over the given range is [10. 12.5 15. 17.5 20.]
Example
snippet
import numpy as np
arr = np.linspace(10, 20, 5, endpoint = False)
print("The array over the given range is ",arr)
Output:
Output
The array over the given range is [10. 12. 14. 16. 18.]
numpy.logspace
It creates an array by using the numbers that are evenly separated on a log scale.
The syntax is given below.
snippet
numpy.logspace(start, stop, num, endpoint, base, dtype)
It accepts the following parameters.
- start: It represents the starting value of the interval in the base.
- stop: It represents the stopping value of the interval in the base.
- num: The number of values between the range.
- endpoint: It is a boolean type value. It makes the value represented by stop as the last value of the interval.
- base: It represents the base of the log space.
- dtype: It represents the data type of the array items.
Example
snippet
import numpy as np
arr = np.logspace(10, 20, num = 5, endpoint = True)
print("The array over the given range is ",arr)
Output:
Output
The array over the given range is [1.00000000e+10 3.16227766e+12 1.00000000e+15 3.16227766e+17
1.00000000e+20]
Example
snippet
import numpy as np
arr = np.logspace(10, 20, num = 5,base = 2, endpoint = True)
print("The array over the given range is ",arr)
Output:
Output
The array over the given range is [1.02400000e+03 5.79261875e+03 3.27680000e+04 1.85363800e+05
1.04857600e+06]