Updated April 3, 2023
Introduction to numpy.linspace()
numpy.linspace() is a function that is used for creating numeric sequences over a specified interval. The output of the function is a ndarray containing the numeric sequence. This function is similar to np.arange() and np.geomspace() in the numpy library.
Syntax and parameters:
The standard syntax for writing this function is as follows :
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
The different parameters used in the function are :
1. start: array_like object
The starting value from where the numeric sequence has to be started.
2. stop: array_like object
The last value of the numeric sequence.
3. num: non- negative integer
The number of numbers or samples to be generated. If you do not mention anything for this parameter then the function uses a default value of 50. The value of this parameter must be non-negative.
4. endpoint: boolean {True or False}
If you mention True for this parameter, then “stop” is treated as the last sample. Otherwise, “stop” is not included in the sequence. If you do not mention anything for this parameter then the function uses True as default. Also, note that the step size changes when the endpoint is False.
5. retstep: boolean {True or False}
If retstep has been set to True, the function returns samples with the mentioned step as the spacing between samples.
6. dtype : dtype, optional
The data type of the output array. If you do not mention anything for this parameter then the function infers the data type from the other input arguments.
7. axis: integer {0,-1, none}
The axis along which the created samples have to be stored. If you do not mention anything for this parameter then by default 0 will be used as the axis, the samples will be along a new axis inserted at the beginning. If you want the samples at the end then use axis = -1.
The first two parameters are compulsory, other parameters are optional and can be used on the requirement basis.
np.linspace() returns an ndarray.
Examples of numpy.linspace()
Given below are the examples mentioned:
Example #1
Program to illustrate np.linspace() function with start and stop parameters.
Code:
import numpy as np
#illustrating linspace function using start and stop parameters only
#By default 50 samples will be generated
np.linspace(3.0, 7.0)
Output:
Example #2
Program to illustrate np.linspace() function with start, stop and num parameters.
Code:
import numpy as np
#illustrating linspace function using start, stop and num parameters
#We can notice that the step size changes with the sample size.
np.linspace(3.0, 7.0, num = 10)
Output:
Example #3
Program to illustrate np.linspace() function with start, stop, num and endpoint parameters.
Code:
import numpy as np
#illustrating linspace function using start, stop,num and endpoint parameters
#By default endpoint is set to True, i.e it returns the stop value
np.linspace(3.0, 7.0, num = 10, endpoint = False)
Output:
Example #4
Program to graphically illustrate np.linspace() function with start, stop, num, endpoint and retstep parameters.
Code:
import numpy as np
#Graphical illustration different parameters of the linspace function
#Note : for plotting graphs in python, we require libraries like matplotlib
import matplotlib.pyplot as plt
y = np.zeros(10)
x1 = np.linspace(3, 7, num = 10, endpoint=True)
x2 = np.linspace(3, 7, num = 10 , endpoint=False)
plt.plot(x1, y, 'o')
plt.plot(x2, y+0.1, 'o')
plt.show()
Output:
In the above output, we can see that np.linspace() generates 10 samples starting from 3 to 7. When the endpoint has been set to False, 10 samples excluding the stop value, i.e. 7 has been generated. We can also notice that the step size is different in both cases even though the start and stop values are the same.
Example #5
Program to illustrate np.linspace() function with start, stop, num, endpoint and retstep parameters.
Code:
import numpy as np
#illustrating linspace function using start, stop, num, endpoint and retstep parameters
#retstep returns the samples and the step size
#retstep has been set to True
#stepsize when endpoint is False
np.linspace(3.0, 7.0, num=10, endpoint = False, retstep=True)
Output:
Comparison of np.linspace() with other similar functions
np.linspace() is similar to np.arange() and np.geomspace() functions
np.arange() | np.linspace() |
np.arange() function creates a sequence of evenly spaced numbers structured as a ndarray array. | np.linspace() function creates a sequence of evenly spaced numbers structured as a ndarray array. |
np.arange() uses step size as a parameter | np.linspace() uses number of samples as a parameter |
Example #6
Program to illustrate difference between np.arange() and np.linspace()
Code:
import numpy as np
#In linspace we mention the number of samples along with start and stop values
print("Array using linspace function :\n", np.linspace(1,10, 10))
#In arange we mention the step size along with start and stop values
print("Array using arange function :\n", np.arange(1,10,2))
Output:
np.geomspace() | np.linspace() |
np.geompsace() function creates a sequence of evenly spaced numbers structured as a ndarray array. | np.linspace() function creates a sequence of evenly spaced numbers structured as a ndarray array. |
In np.geomspace() the numbers are evenly spaced on a logrithmic scale. | In np.geomspace() the numbers are evenly spaced on a uniform step size. |
Example #7
Program to illustrate difference between np.geomspace() and np.linspace()
Code:
import numpy as np
#In linspace the sequence is evely spread over uniform step size
print("Array using linspace function :\n", np.linspace(1,10,num = 10))
#In geomspace the sequence is evenly spread using geometric progression/on log scale
print("Array using arange function :\n", np.geomspace(1,1000, num =4))
Output:
Conclusion
np.arange(), np.linspace() and np.geomspace() can be used interchangeably. All three of the numpy functions serve the same purpose of creating a sequence of numbers. You may use any of the functions based on your requirement and comfort.
Recommended Articles
This is a guide to numpy.linspace(). Here we discuss the introduction to numpy.linspace() with appropriate syntax and respective examples for better understanding. You may also look at the following articles to learn more-