May 30, 2021 Article blog
As a sequence generator,
numpy.linspace()
function is used to generate digital sequences in linear space at uniform steps.
Numpy can usually use
numpy.arange()
to generate sequences, but when we use floating-point parameters, it can result in a loss of precision, which can lead to unpredictable output. T
o avoid any loss of precision due to floating-point accuracy, numpy provides us with a separate sequence generator in
numpy.linspace()
that is preferred if you already know the number of elements required. H
owever,
linspace()
and
arange()
with the appropriate parameters are usually used to get the same output, so you can choose both for the same task.
For example, the following code uses
numpy.linspace()
to draw two linear sequences between 0 and 10 to show the uniformity generated by that sequence.
import numpy as np
import matplotlib.pyplot as plt
y = np.zeros(5)
x1 = np.linspace(0, 10, 5)
x2 = np.linspace(0, 10, 5)
plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.5, 'o')
plt.ylim([-0.5, 1])
plt.show()
Output:
grammar:
Format:
array = numpy.linspace(start, end, num=num_points)
generates a uniform sequence between
start
and
end
with
num_points
elements.
Let's understand this in a few examples:
import numpy as np
a = np.linspace(0.02, 2, 10)
print('Linear Sequence from 0.02 to 2:', a)
print('Length:', len(a))
Linear Sequence from 0.02 to 2: [0.02 0.24 0.46 0.68 0.9 1.12 1.34 1.56 1.78 2. ]
Length: 10
The above snippet produces an even sequence of 0.02 to 2, which contains 10 elements.
If you don't want to include the last point in the sequence calculation, you can use another keyword parameter,
endpoint
to set it to
False
(True
True
by default)
import numpy as np
a = np.linspace(0.02, 2, 10, endpoint=False)
print('Linear Sequence from 0.02 to 2:', a)
print('Length:', len(a))
Linear Sequence from 0.02 to 2: [0.02 0.218 0.416 0.614 0.812 1.01 1.208 1.406 1.604 1.802]
Length: 10
As you can see, the last point (2) is not included in the sequence, so the steps are different, which results in a completely different sequence.
This is a Boolean optional parameter, if specified, and also returns the step and sequence array, resulting in a tuple as output
import numpy as np
a = np.linspace(0.02, 2, 10, retstep=True)
print('Linear Sequence from 0.02 to 2:', a)
print('Length:', len(a))
output
Linear Sequence from 0.02 to 2: (array([0.02, 0.24, 0.46, 0.68, 0.9 , 1.12, 1.34, 1.56, 1.78, 2. ]), 0.22)
Length: 2
Because the output is a tuple, its length is 2 instead of 10!
This sets the axis in the result to store the sample.
Use it only if the start and endpoint are array data types.
By default
axis=0
sampling is done along the new axis inserted at the beginning.
We can use
axis=-1
to get the shaft at the end.
import numpy as np
p = np.array([[1, 2], [3, 4]])
q = np.array([[5, 6], [7, 8]])
r = np.linspace(p, q, 3, axis=0)
print(r)
s = np.linspace(p, q, 3, axis=1)
print(s)
output
array([[[1., 2.],
[3., 4.]],
[[3., 4.],
[5., 6.]],
[[5., 6.],
[7., 8.]]])
array([[[1., 2.],
[3., 4.],
[5., 6.]],
[[3., 4.],
[5., 6.],
[7., 8.]]])
In the first case, we get the sequence limit from the first axis because
axis = 0
Here, the limits are the sub-array pairs
[1, 2] and [5,6]
[3, 4] and [7,8]
which are taken from the first axis of
p
and
q
Now we compare the corresponding elements in the result pair to generate the sequence.
"Thus, the order of the first row is .
[[1 to 5], [2 to 6]]
[[1 to 5], [2 to 6]]
[ [[1, 2], [3, 4]], [[3, 4], [5, 6]], [[5, 6], [7,8]] ]
[[3 to 7], [4 to 8]]
In the second case, a new element is inserted in
axis=1
or column. T
herefore, the new axis is generated through a column sequence.
instead of a sequence of rows.
Consider the sequences of
[1, 2] to [5, 7]
[3, 4] to [7, 8]
to , .
[[[1, 2], [3, 4], [5, 6]], [[3, 4], [5, 6], [7, 8]]]
Recommended lessons: Python3 Advanced: Data Analysis and Visualization, Python Automation Office