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.
Syntax
snippet
numpy.linspace(start, stop, num, endpoint, retstep, dtype)
Parameters
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.
Return
An array within the specified range is returned.
Example 1
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 2
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.]