博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python – numpy.linspace()
阅读量:2534 次
发布时间:2019-05-11

本文共 5637 字,大约阅读时间需要 18 分钟。

Being a sequence generator, the numpy.linspace() function is used to generate a sequence of numbers in linear space with a uniform step size.

作为序列生成器, numpy.linspace()函数用于在线性空间中以均匀步长生成数字序列。

Numpy generally can generate sequences using numpy.arange(), but when we use floating-point arguments, it can result in a loss of precision, which may cause an unpredictable output.

Numpy通常可以使用numpy.arange()生成序列,但是当我们使用浮点参数时,可能会导致精度损失,这可能会导致不可预测的输出。

To avoid any potential loss of precision due to the floating-point precision, numpy provides us with a separate sequence generator in numpy.linspace(), which is the preferred option if you already know the number of elements you need. But you generally get the same output using both linspace() and arange() with appropriate parameters, so both can be chosen for the same task.

为了避免由于浮点精度而造成的任何精度损失,numpy在numpy.linspace()为我们提供了一个单独的序列生成器,如果您已经知道所需的元素数,则这是首选。 但是通常使用带有适当参数的linspace()arange()可以得到相同的输出,因此可以为同一任务选择两者。

For example, the following code plots 2 linear sequences between 0 and 10 using numpy.linspace() to show that there is uniformity generated by the sequence.

例如,以下代码使用numpy.linspace()在0到10之间绘制2个线性序列,以显示该序列生成的均匀性。

import numpy as npimport matplotlib.pyplot as plty = 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:

输出

Np Linspace Plot
Np Linspace Plot
Np Linspace图


句法 (Syntax)

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

格式: array = numpy.linspace(start, end, num=num_points)将在startend之间生成一个统一的序列,共有num_points个元素。

Here,

这里,

  • start -> Starting point (included) of the range

    start ->范围的起点(包括)
  • end -> Endpoint (included) of the range

    end ->范围的端点(包括)
  • num -> Total number of points in the sequence

    num >序列中的总点数

Let’s understand this using a couple of examples:

让我们通过几个示例来理解这一点:

import numpy as npa = 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 snippet generates a uniform sequence between 0.02 to 2, having 10 elements in it.

上面的代码段生成了0.02到2之间的均匀序列,其中包含10个元素。

端点关键字参数 (The endpoint keyword argument)

If you don’t want to include the last point in the sequence calculations, there is another keyword argument endpoint, which can be set to False. ( It is True by default )

如果您不想在序列计算中包括最后一点,则可以使用另一个关键字参数endpoint ,可以将其设置为False 。 (默认为True

import numpy as npa = 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 observe, the last point (2) has not been included in the sequence, so the step size is also different, which will generate a totally different sequence now.

如您所见,最后一点(2)没有包含在序列中,因此步长也不同,这将产生一个完全不同的序列。

retstep关键字参数 (The retstep keyword argument)

This is a boolean optional argument, if specified, also returns the step size along with the sequence array, resulting in a tuple as the output

这是一个布尔型可选参数(如果已指定),还将返回步长以及序列数组,从而产生一个元组作为输出

import numpy as npa = 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

Since the output is a tuple, it’s length is 2, and not 10!

由于输出是元组,因此它的长度是2,而不是10!

axis关键字参数 (The axis keyword argument)

This sets the axis in the result to store the samples. It is used only if the start and endpoints are of the array datatype.

这将在结果中设置轴以存储样本。 仅当开始和端点为数组数据类型时才使用它。

By default (axis=0), the samples will be along a new axis inserted at the beginning. We can use axis=-1 to get an axis at the end.

默认情况下( axis=0 ),采样将沿着在开始处插入的新轴进行。 我们可以使用axis=-1来获得末端的轴。

import numpy as npp = 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, since axis = 0, we take sequence limits from the first axis.

在第一种情况下,由于axis = 0 ,我们从第一个轴获取序列限制。

Here, the limits are the subarray pairs [1, 2] and [5,6], as well as [3, 4] and [7,8], taking elements from the first axis of p and q. Now we compare corresponding elements from the resulting pair to generate the sequences.

在这里,限制是子数组对[1, 2] and [5,6]以及[3, 4] and [7,8] ,它们取自pq的第一轴。 现在,我们比较结果对中的相应元素以生成序列。

So the sequences are [[1 to 5], [2 to 6]] for the first row and [[3 to 7], [4 to 8]], for the second pair(row) which is evaluated and combined to form [ [[1, 2], [3, 4]], [[3, 4], [5, 6]], [[5, 6], [7,8]] ],

因此,第一行的顺序为[[1 to 5], [2 to 6]] ,第二行的顺序为[[1 to 5], [2 to 6]] [[3 to 7], [4 to 8]] ,对其进行评估并组合形成[ [[1, 2], [3, 4]], [[3, 4], [5, 6]], [[5, 6], [7,8]] ]

The second case will insert new elements in axis=1, or the columns. So the new axis will have been generated via the column sequences. instead of the row sequences.

第二种情况将在axis=1或列中插入新元素。 因此,新轴将通过列序列生成。 而不是行序列。

The sequences [1, 2] to [5, 7] and [3, 4] to [7, 8] are considered and inserted into the columns of the result, resulting in [[[1, 2], [3, 4], [5, 6]], [[3, 4], [5, 6], [7, 8]]].

考虑序列[1, 2] to [5, 7][3, 4] to [7, 8]并将其插入到结果的列中,得到[[[1, 2], [3, 4], [5, 6]], [[3, 4], [5, 6], [7, 8]]]



参考资料 (References)



翻译自:

转载地址:http://ktqzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>