What is Linspace() in Numpy
Numpy Linspace() function is used to create a numpy array with evenly spaced numbers between the two intervals provided as input.Â In this tutorial, we will see the syntax of np.linspace() and see various examples by using various parameters.
Numpy Linspace: np.linspace()
Syntax
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
 start â€“ This signifies the starting value of the sequence and can be a number or an arraylike value.
 stop â€“ This signifies the stop value of the sequence and can be a number or any arraylike value. However, when the endpoint is False the stop value is excluded from the sequence.
 num (optional) â€“ It denotes the number of samples to be generated between the start and stop. The input should be of int type and nonnegative. The default value is 50.
 endpoint (optional) â€“ When its True, the last value mentioned in the stop is included otherwise not included when False. The default value is True.
 retstep (optional) â€“ When True the function also returns the step size used to generate the sequence of samples. The default value is False.
 dtype (optional) â€“ It denotes the data type of the numpy array that is returned as the output. If it is not mentioned, then it will infer from the start and stop parameters. However, it should be noted that even though start and stop are integers, the output numpy array will be of type float only.
 axis (optional) â€“ It denotes the axis in the result to store the samples and is relevant only when start or stop values are arraylike. The default value is 0, where samples are along a new axis inserted at the beginning. 1 is used to have the axis at the end.
Examples of Numpy Linspace
Import Numpy Library
Before going into the examples, let us first import the numpy library as below.
In [0]:
importÂ numpyÂ asÂ np
Â
Example 1 â€“ Simple Example of Numpy Linspace
Let us begin with the simplest example where we are using only start and stop parameters in np.linspace function.
In this example, the start =1 and stop=10 are passed, and a 1D numpy array is generated consisting of 50 evenly spaced numbers. (Do remember that when the parameter num is not used, it produces an array of 50 elements by default.)
In [1]:
arrÂ =Â np.linspace(startÂ =Â 1,Â stopÂ =Â 10); print(arr)
Out[1]:
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735 2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816 3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898 4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898 5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061 6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143 7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224 8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306 9.81632653 10. ]
Â
Example 2 â€“ Using Num Parameter
The numÂ parameter is used to denote that how many elements would be there in the numpy array. In the below example, num=4 is passed and the output numpy array contains only 4 elements between 0 and 24.
In [2]:
arrÂ =Â np.linspace(startÂ =Â 0,Â stopÂ =Â 24,Â num=4); print(arr)
Out[2]:
[ 0., 8., 16., 24.]
Example 3 â€“ Using dtype Parameter
dtype parameter is used to explicitly assign the data type to the numpy array. In the above example, the output array consists of float. Let us apply dtype as int and we can see in the output that it now contains numbers as int.
In [3]:
arrÂ =Â np.linspace(startÂ =Â 0,Â stopÂ =Â 24,Â num=4,Â dtype=int); print(arr)
[ 0 8 16 24]
Example 4 â€“ Using endpoint in np.linspace
arrÂ =Â np.linspace(startÂ =Â 0,Â stopÂ =Â 24,Â num=4,Â endpoint=False,Â dtype=int); print(arr)
Out[4]:
[ 0 6 12 18]
Example 5 â€“ Using retstep Parameter
arrÂ =Â np.linspace(startÂ =Â 0,Â stopÂ =Â 24,Â num=4,Â endpoint=False,Â dtype=int,Â retstep=True); print(arr)
Out[5]:
(array([ 0, 6, 12, 18]), 6.0)
Example 6 â€“ Using Linspace for creating 2D Numpy Array
To create the 2D numpy array with linspace you can use a vector of numbers in either start or stop or both the parameters as shown in the below examples
In [6]:
arrÂ =Â np.linspace(startÂ =Â (3,9),Â stopÂ =Â 24,Â num=4,dtype=int); print(arr)
[[ 3 9] [10 14] [17 19] [24 24]]
Â
In [7]:
arrÂ =Â np.linspace(startÂ =Â (3,9),Â stopÂ =Â (24,48),Â num=4,dtype=int); print(arr)
Out[7]:
[[ 3 9] [10 22] [17 35] [24 48]]
Â
In [8]:
arrÂ =Â np.linspace(startÂ =Â 9,Â stopÂ =Â (24,48),Â num=4,dtype=int); print(arr)
Out[8]:
[[ 9 9] [14 22] [19 35] [24 48]]
Â
Reference â€“ Numpy Documentation

MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.