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

NumPy 教程(第 5 章):从已有的 Python 数组创建 Ndarray 数组

发布时间:2021-05-21 04:51:35 所属栏目:大数据 来源: https://blog.csdn.net/yilovex
导读:numpy.asarray 此函数类似于numpy.array,它有较少的参数 numpy.asarray(a,dtype = None,order = None) a 任意形式的输入参数,比如列表、列表的元组、元组、元组的元组、元组的列表 dtype 通常,输入数据的类型会应用到返回的 ndarray order ‘C’ 为按行的

numpy.asarray 此函数类似于numpy.array,它有较少的参数

numpy.asarray(a,dtype = None,order = None)
  • a 任意形式的输入参数,比如列表、列表的元组、元组、元组的元组、元组的列表

  • dtype 通常,输入数据的类型会应用到返回的 ndarray

  • order ‘C’ 为按行的 C 风格数组,‘F’ 为按列的 Fortran 风格数组

asarray 方法语法:

In [1]: import numpy as np

In [2]: x = [1,2,3]

In [3]: num = np.asarray(x)

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

In [5]: num = np.asarray(x,dtype=float)

In [6]: num
Out[6]: array([1.,2.,3.])

In [7]: x = (1,3)

In [8]: num = np.asarray(x)

In [9]: num
Out[9]: array([1,3])

numpy.frombuffer 用于实现动态数组,接受 buffer 输入参数,以流的形式读入转化成 ndarray 对象

numpy.frombuffer(buffer,dtype = float,count = -1,offset = 0)
  • buffer 可以是任意对象,会以流的形式读入

  • dtype 返回数组的数据类型,可选

  • count 读取的数据数量,默认为-1,读取所有数据

  • offset 读取的起始位置,默认为0

frombuffer 方法语法:

In [1]: import numpy as np

In [2]: s =  b'Hello World'

In [3]: num = np.frombuffer(s,dtype='S1')

In [4]: num
Out[4]:
array([b'H',b'e',b'l',b'o',b' ',b'W',b'r',b'd'],dtype='|S1')

numpy.fromiter 方法从可迭代对象中建立 ndarray 对象,返回一维数组

numpy.fromiter(iterable,dtype,count = -1)
  • iterable 可迭代对象

  • dtype 返回数组的数据类型

  • count 读取的数据数量,默认为-1,读取所有数据

fromiter 方法语法:

In [1]: import numpy as np

In [2]: x = range(5)

In [3]: it = iter(x)

In [4]: num = np.fromiter(it,dtype=float)

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

(编辑:北几岛)

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

    推荐文章
      热点阅读