Let’s learn How to generate evenly spaced sample in Numpy Python library. We will use Numpy linspace method for that purpose.
To generate evenly spaced sample within given interval you may need linpsace function. It generates the array and takes starting point, end point and the number of needed elements.
The simpliest example
Let’s see how to generate even sample from 0 to 100. I will need 5 elements in my sample array.
import numpy as np cities = ["New York", "London", "Austin", "Chicago", "Quebec"] my_linspace_array = np.linspace(0, 100, len(cities)) print(f"This is my sample array generated using linspace method: " f"\n {my_linspace_array}")
More advanced example
You can even add more parameters to your code:
import numpy as np my_array = ([0, 20, 25, 40, 100]) my_linspace_array = np.linspace(min(my_array), max(my_array), len(my_array)) print(f"This is my sample array generated using linspace method: " f"\n {my_linspace_array}")
The result is the same. Linspace interval is fully parametrized by my_array list.