Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the linspace () function of numpy

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use the linspace () function of numpy", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the linspace () function of numpy".

Linspace () function

As a sequence generator, the numpy.linspace () function is used to generate digital sequences in uniform steps in linear space.

Numpy can usually use numpy.arange () to generate sequences, but when we use floating-point parameters, it may result in a loss of precision, which can lead to unpredictable output. To avoid any loss of precision due to floating-point precision, numpy provides us with a separate sequence generator at numpy.linspace (), which is preferred if you already know the number of elements you need. But you can usually get the same output by using linspace () and arange () with the appropriate parameters, 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 of the sequence generation.

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 ()

Syntax:

Format: array = numpy.linspace (start, end, num=num_points) will generate a unified sequence between start and end, with a total of num_points elements.

Start-> Starting point (included) of the rangestart-> the beginning of the scope (including)

End-> Endpoint (included) of the rangeend-> endpoints of the range (including)

Total number of points in num-> Total number of points in the sequencenum > sequence

Let's understand this through 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))

Output

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 code snippet generates a uniform sequence between 0.02 and 2, containing 10 elements.

Endpoint keyword parameter

If you don't want to include the last point in the sequence calculation, you can use another keyword parameter, endpoint, which can be set to False. (default is True)

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))

Output

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 step size is also different, which results in a completely different sequence.

Retstep keyword parameter

This is a Boolean optional parameter (if specified) and returns the step size 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, not 10!

Axis keyword parameter

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 takes place along the new axis inserted at the beginning. We can use axis=-1 to get the axis 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, because axis = 0, we get the sequence limit from the first axis.

In this case, the restrictions are subarray pairs [1,2] and [5rect 6] and [3,4] and [7je 8], which are taken from the first axes of p and Q. Now, let's compare the corresponding elements in the result pair to generate a sequence.

Therefore, the order of the first line is [[1 to 5], [2 to 6]], and the order of the second line is [[1 to 5], [2 to 6]] [[3 to 7], [4 to 8]], evaluated and combined to form [[1, 2], [3, 4], [3, 4], [5, 6], [5, 6], [7 to 8]].

In the second case, a new element is inserted into the axis=1 or column. Therefore, the new axis will be generated from the column sequence. Not a row sequence.

Consider the sequences [1,2] to [5,7] and [3,4] to [7,8] and insert them into the column of the result, and get [[1,2], [3,4], [5,6], [[3,4], [5,6], [7,8]].

The above is about the content of this article on "how to use the linspace () function of numpy". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report