加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

NumPy 教程(第 6 章): 从数值范围创建 Ndarray 数组

发布时间:2021-05-21 04:51:36 所属栏目:大数据 来源: https://blog.csdn.net/yilovex
导读:numpy.arange 方法创建数值范围并返回 ndarray 对象 numpy.arange(start,stop,step,dtype) start 起始值,默认为 0 stop 终止值(不包含) step 步长,默认为 1 dtype 返回 ndarray 的数据类型,如果没有提供,则会使用输入数据的类型 arange 方法语法: In

numpy.arange 方法创建数值范围并返回 ndarray 对象

numpy.arange(start,stop,step,dtype)
  • start 起始值,默认为 0

  • stop 终止值(不包含)

  • step 步长,默认为 1

  • dtype 返回 ndarray 的数据类型,如果没有提供,则会使用输入数据的类型

arange 方法语法:

In [1]: import numpy as np

In [2]: num = np.arange(5)

In [3]: num
Out[3]: array([0,1,2,3,4])

In [4]: num = np.arange(5,dtype=float)

In [5]: num
Out[5]: array([0.,1.,2.,3.,4.])

In [6]: num = np.arange(10,20,2)

In [7]: num
Out[7]: array([10,12,14,16,18])

numpy.linspace 类似于arange()方法,用于创建一个一维数组,数组是一个等差数列构成的,在此函数中,指定了范围之间的均匀间隔数量,而不是步长

np.linspace(start,num=50,endpoint=True,retstep=False,dtype=None)
  • start 序列的起始值

  • stop 序列的终止值,如果endpoint为true,该值包含于数列中

  • num 要生成的等步长的样本数量,默认为50

  • endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。

  • retstep 如果为 True 时,生成的数组中会显示间距,反之不显示。

  • dtype ndarray 的数据类型

linspace 方法语法:

In [1]: import numpy as np

In [2]: num = np.linspace(10,5)

In [3]: num
Out[3]: array([10.,12.5,15.,17.5,20. ])

In [4]: np.linspace(10,5,endpoint=False)
Out[4]: array([10.,12.,14.,16.,18.])

In [5]: np.linspace(1,retstep=True)
Out[5]: (array([1.,1.25,1.5,1.75,2.  ]),0.25)

numpy.logspace 返回一个 ndarray 对象,其中包含在对数刻度上均匀分布的数字。 刻度的开始和结束端点是某个底数的幂,通常为 10,用于创建一个于等比数列

np.logspace(start,base=10.0,dtype=None)
  • start 序列的起始值为:base ** start

  • stop 序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中

  • num 要生成的等步长的样本数量,默认为50

  • endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True

  • base 对数 log 的底数。

  • dtype ndarray 的数据类型

logspace 方法语法:

In [1]: import numpy as np

In [2]: np.logspace(1.0,2.0,num=10)
Out[2]:
array([ 10.,12.91549665,16.68100537,21.5443469,27.82559402,35.93813664,46.41588834,59.94842503,77.42636827,100.        ])

In [3]: np.logspace(1,10,num=10,base=2)
Out[3]:
array([   2.,4.,8.,32.,64.,128.,256.,512.,1024.])

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读