小白教程
所有教程
关于
Search
172.69.59.79
172.69.59.79
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“NumPy 从数值范围创建数组”的源代码
本页内容
上一节:
NumPy_从已有的数组创建数组
下一节:
NumPy_切片和索引
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:NumPy 从数值范围创建数组}}[[Category:NumPy 教程|8]] = NumPy 从数值范围创建数组 = 这一章节我们将学习如何从数值范围创建数组。 === numpy.arange === numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数格式如下: <sample title="" desc="" lang="python" hererun="1"> numpy.arange(start, stop, step, dtype) </sample> 根据 start 与 stop 指定的范围以及 step 设定的步长,生成一个 ndarray。 参数说明: {| class="table table-striped table-hover" ! 参数 ! 描述 |- | <code>start</code> | 起始值,默认为<code>0</code> |- | <code>stop</code> | 终止值(不包含) |- | <code>step</code> | 步长,默认为<code>1</code> |- | <code>dtype</code> | 返回<code>ndarray</code>的数据类型,如果没有提供,则会使用输入数据的类型。 |} == 生成 0 到 5 的数组: <sample title="" desc="" lang="python" hererun="1"> import numpy as np x = np.arange(5) print (x) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> [0 1 2 3 4] </sample> 设置返回类型位 float: <sample title="" desc="" lang="python" hererun="1"> import numpy as np # 设置了 dtype x = np.arange(5, dtype = float) print (x) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> [0. 1. 2. 3. 4.] </sample> 设置了起始值、终止值及步长: <sample title="" desc="" lang="python" hererun="1"> import numpy as np x = np.arange(10,20,2) print (x) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> [10 12 14 16 18] </sample> === numpy.linspace === numpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成的,格式如下: <sample title="" desc="" lang="python" hererun="1"> np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) </sample> 参数说明: {| class="table table-striped table-hover" ! 参数 ! 描述 |- | <code>start</code> | 序列的起始值 |- | <code>stop</code> | 序列的终止值,如果<code>endpoint</code>为<code>true</code>,该值包含于数列中 |- | <code>num</code> | 要生成的等步长的样本数量,默认为<code>50</code> |- | <code>endpoint</code> | 该值为 <code>true</code> 时,数列中包含<code>stop</code>值,反之不包含,默认是True。 |- | <code>retstep</code> | 如果为 True 时,生成的数组中会显示间距,反之不显示。 |- | <code>dtype</code> | <code>ndarray</code> 的数据类型 |} 以下实例用到三个参数,设置起始点为 1 ,终止点为 10,数列个数为 10。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.linspace(1,10,10) print(a) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.] </sample> 设置元素全部是1的等差数列: <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.linspace(1,1,10) print(a) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] </sample> 将 endpoint 设为 false,不包含终止值: <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.linspace(10, 20, 5, endpoint = False) print(a) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [10. 12. 14. 16. 18.] </sample> 如果将 endpoint 设为 true,则会包含 20。 以下实例设置间距。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a =np.linspace(1,10,10,retstep= True) print(a) # 拓展例子 b =np.linspace(1,10,10).reshape([10,1]) print(b) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> (array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0) [[ 1.] [ 2.] [ 3.] [ 4.] [ 5.] [ 6.] [ 7.] [ 8.] [ 9.] [10.]] </sample> === numpy.logspace === numpy.logspace 函数用于创建一个于等比数列。格式如下: <sample title="" desc="" lang="python" hererun="1"> np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None) </sample> base 参数意思是取对数的时候 log 的下标。 {| class="table table-striped table-hover" ! 参数 ! 描述 |- | <code>start</code> | 序列的起始值为:base ** start |- | <code>stop</code> | 序列的终止值为:base ** stop。如果<code>endpoint</code>为<code>true</code>,该值包含于数列中 |- | <code>num</code> | 要生成的等步长的样本数量,默认为<code>50</code> |- | <code>endpoint</code> | 该值为 <code>true</code> 时,数列中中包含<code>stop</code>值,反之不包含,默认是True。 |- | <code>base</code> | 对数 log 的底数。 |- | <code>dtype</code> | <code>ndarray</code> 的数据类型 |} <sample title="" desc="" lang="python" hererun="1"> import numpy as np # 默认底数是 10 a = np.logspace(1.0, 2.0, num = 10) print (a) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [ 10. 12.91549665 16.68100537 21.5443469 27.82559402 35.93813664 46.41588834 59.94842503 77.42636827 100. ] </sample> 将对数的底数设置为 2 : <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.logspace(0,9,10,base=2) print (a) </sample> 输出如下: <sample title="" desc="" lang="python" hererun="1"> [ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.] </sample>
返回至“
NumPy 从数值范围创建数组
”。
上一节:
NumPy_从已有的数组创建数组
下一节:
NumPy_切片和索引