NumPy 创建数组方法详解

NumPy 创建数组方法详解,第1张

文章目录
  • numpy数组

  • 一、基于数组对象

    • 1. array和asarray
    • 2. frombuffer
    • 3. fromiter

  • 二、基于构造函数

    • 1. empty
    • 2. zeros
    • 3. ones

  • 三、基于数值范围

    • 1. arange
    • 2. linspace
    • 3. logspace


numpy数组

NumPy 最重要的一个特点是其 N 维数组对象 ndarray,用于存放同类型数据的集合,内部结构如下:

  • 一个指向数据(内存或内存映射文件中的一块数据)的指针
  • 数据类型或 dtype,描述在数组中的固定大小值的格子
  • 一个表示数组形状(shape)的元组,表示各维度大小的元组
  • 一个跨度元组(stride),其中的整数指的是为了前进到当前维度下一个元素需要"跨过"的字节数


其生成函数如下:

numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
  • object是数组对象,必选
  • dtype是数据类型
  • copy对象是否复制
  • order创建数组的样式,C为航方向,F为列方向,默认A为任意方向
  • subok默认返回一个与基类类型一直的数组
  • ndmin指定生产数组的最小维度

一、基于数组对象 1. array和asarray

array和asarray都可以将结构数据转化为ndarray,主要区别就是当数据源是ndarray时,array会copy出一个副本,占用新的内存,属于深拷贝,但asarray不会。


x = [1,2,3,4,5]
a = np.array(x)
b = np.asarray(x)
a == b
   array([ True,  True,  True,  True,  True])
2. frombuffer

该方法用于实现动态数组,接收buffer输入,以流的形式读入转化ndarray对象

s = b'I like python'
sbf = np.frombuffer(s,dtype='S1')
print(sbf)
   [b'I' b' ' b'l' b'i' b'k' b'e' b' ' b'p' b'y' b't' b'h' b'o' b'n']

buffer 是字符串的时候,Python3 默认 str 是 Unicode 类型,所以要转成 bytestring 在原 str 前加上 b

3. fromiter

从可迭代对象中建立 ndarray 对象,返回一维数组。


list=range(5)
it=iter(list) ## 构造迭代器
x=np.fromiter(it, dtype=float)
print(x)

output:
[0. 1. 2. 3. 4.]


二、基于构造函数 1. empty

创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组(数组元素为随机值)

x = np.empty([3,3], dtype = float) 
print(x)
   array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
          [0.00000000e+000, 0.00000000e+000, 6.71929278e-321],
          [7.56593016e-307, 2.22522596e-306, 2.56765117e-312]])
2. zeros

创建指定大小的数组,数组元素为 0

x = np.zeros(5) # 默认为浮点数
y = np.zeros((5,), dtype = np.int) # 设置类型为整数
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')]) # 自定义类型 
print('x: {0}\n y: {1} \n z: {2}'.format(x,y,z))
x: [0. 0. 0. 0. 0.]
y: [0 0 0 0 0] 
z: [[(0, 0.) (0, 0.)]
   [(0, 0.) (0, 0.)]]
3. ones

创建指定形状的数组,数组元素为 1

x = np.ones(5) # 默认为浮点数
y = np.ones([3,3], dtype = int)# 自定义类型
print('x: {0}\n y: {1}'.format(x,y))
x: [1. 1. 1. 1. 1.]
y: [[1 1 1]
    [1 1 1]
    [1 1 1]]

三、基于数值范围 1. arange

根据 start 与 stop 指定的范围以及 step 设定的步长,生成一个 ndarray
numpy.arange(start, stop, step, dtype)

  • start:起始值,默认为0
  • stop:终止值(不包含)
  • step:步长,默认为1
x = np.arange(1,100,3).reshape(3,11) ##从1
print(x)
array([[ 1,  4,  7, 10, 13, 16, 19, 22, 25, 28, 31],
       [34, 37, 40, 43, 46, 49, 52, 55, 58, 61, 64],
       [67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97]])
2. linspace

创建等差数列的一维数组
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

  • start:起始值
  • stop:终止值,如果endpoint为true,该值包含于数列中
  • num:样本数量,默认为50
  • endpoint:该值为 true 时,数列中包含stop值,反之不包含,默认是True
  • retstep:如果为 True 时,生成的数组中会显示间距,反之不显示
x = np.linspace(1,10,10, retstep= True)
print(x)
(array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)
3. logspace

创建对数等比数列
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

  • start:起始值为basestart
  • stop:终止值basestop
  • num:样本数量,默认为50
  • endpoint:该值为 true 时,数列中包含stop值,反之不包含,默认是True
  • base:对数 log 的底数
x = np.logspace(0,10,10,endpoint=False,base=2)
print(x)
[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/569262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存