In this article, we’ll learn Python Numpy linspace function. We’ll start with its definition and role, then we’ll jump to its syntax and explanation of its parameters. Finally, we’ll understand its usage practically with the help of Python code examples.
What is Python Numpy linspace Function?
In Python, the role of the Numpy linspace function is to create a sequence in which numbers are equally spaced and are within a specific range.
Syntax of linspace()
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
- The start parameter specifies the starting point of the range and is included in the resulting output.
- The stop parameter, as the name suggests, is used to show the ending point of the range and it’s also included in the output. But in case, the endpoint parameter is set to false, then the specified stop value will indeed be excluded from the output.
- The role of the num parameter is to specify the number of items in an equally spaced sequence. The value is by default set to 50.
- The parameter endpoint as we’ve discussed above that its role is to allow/disallow the end value in the resulting array. By default, the boolean value assigned to this parameter is True, which means the end value will be included in the output sequence. Making it false will throw out the ending value from the sequence.
- The parameter retstep has a default False Boolean value. If we make it True then the spacing will be returned between the values that are adjacent in the sequence along with the original sequence. For instance, if the space between values is .3 then .3 will also be included in the output at the end/right. The formula that’s used for it is (stop – start) / (num – 1).
- The dtype parameter takes a data type value which by default is None. If we set it to other datatypes like float etc. then the resulting sequence items will be of the specified data type. If it’s none, then the datatype will be generated using the type of other input parameters.
Examples of linspace NumPy Function
Example 1: Linspace Function with start and end range(start, end)
import numpy as np np.linspace(1,5)
array([1. , 1.08163265, 1.16326531, 1.24489796, 1.32653061, 1.40816327, 1.48979592, 1.57142857, 1.65306122, 1.73469388, 1.81632653, 1.89795918, 1.97959184, 2.06122449, 2.14285714, 2.2244898 , 2.30612245, 2.3877551 , 2.46938776, 2.55102041, 2.63265306, 2.71428571, 2.79591837, 2.87755102, 2.95918367, 3.04081633, 3.12244898, 3.20408163, 3.28571429, 3.36734694, 3.44897959, 3.53061224, 3.6122449 , 3.69387755, 3.7755102 , 3.85714286, 3.93877551, 4.02040816, 4.10204082, 4.18367347, 4.26530612, 4.34693878, 4.42857143, 4.51020408, 4.59183673, 4.67346939, 4.75510204, 4.83673469, 4.91836735, 5. ])
First, we imported the Python Numpy library. Then by using it, we’ve called the linspace function and have given it a specific start and end range. The output shows items between the specified range and the number of items are 50. We’ve learned in the parameters section that by default the number of items is 50. In the later example, we’ll customize this number as well. In the output, we can see that each item is at a constant distance from the other item.
Example 2: Not including the end value(endpoint=False)
array([6. , 6.06, 6.12, 6.18, 6.24, 6.3 , 6.36, 6.42, 6.48, 6.54, 6.6 , 6.66, 6.72, 6.78, 6.84, 6.9 , 6.96, 7.02, 7.08, 7.14, 7.2 , 7.26, 7.32, 7.38, 7.44, 7.5 , 7.56, 7.62, 7.68, 7.74, 7.8 , 7.86, 7.92, 7.98, 8.04, 8.1 , 8.16, 8.22, 8.28, 8.34, 8.4 , 8.46, 8.52, 8.58, 8.64, 8.7 , 8.76, 8.82, 8.88, 8.94])
We now cannot see the specified end value(9) in our resulting array. But still, we have 50 items in our sequence. So making the endpoint True will again include the ending value in the result. But, if you don’t want it then just specify it to False.
Example 3: Specifying items length(num Parameter)
array([6. , 6.3, 6.6, 6.9, 7.2, 7.5, 7.8, 8.1, 8.4, 8.7, 9. ])
For demonstration, we’ve used the same range but the output is different. The reason is that we’ve set the num parameter to 11 which will force the number of output items to be of length 11. By default, the length of items is set to 50. If you want it to be 50, then give it the value 50, or just don’t give it any value as by default, 50 is passed to the num parameter.
Example 4: Get the specified values space/distance (retstep=True)
(array([2. , 2.75, 3.5 , 4.25, 5. ]), 0.75)
We’ve passed the True boolean value to the parameter retstep and as a result, we can see that the space difference is also added to the end/right of the resulting output. If you don’t need it, then simply don’t specify this parameter. By doing that, it’ll be removed as False is passed by default to the retstep parameter.
If you want to store the array and the difference/space value in different variables, then use this:
array,step=np.linspace(1,7,num=6,retstep=True) print(array) print(step)
[1. 2.2 3.4 4.6 5.8 7. ] 1.2
The first output shows the array while the second output(1.2) shows the step size/space difference.
Example 5: Specify output Items Datatype(dtype parameter)
np.linspace(2,4,num=5) np.linspace(2,4,num=5,dtype=str) np.linspace(2,4,num=5,dtype=int)
array([2. , 2.5, 3. , 3.5, 4. ]) # default None so float is picked using items datatype array(['2.0', '2.5', '3.0', '3.5', '4.0'], dtype='<U32') # string is set array([2, 2, 3, 3, 4]) # integer is set
We’ve tried the dtype parameter with int, string, etc. We can see that through this parameter, we can easily change the datatype of items of the resulting array.
In conclusion, we’ve discussed the Python Numpy linspace function in detail by using multiple Python code examples. We’ve discussed all the parameters of the linspace function practically and with proper explanation. Hope you’ve enjoyed learning this concept.
Thank you for reading it.