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, 2, 3])
In [5]: num = np.asarray(x, dtype=float)
In [6]: num
Out[6]: array([1., 2., 3.])
In [7]: x = (1, 2, 3)
In [8]: num = np.asarray(x)
In [9]: num
Out[9]: array([1, 2, 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'l', b'o', b' ', b'W', b'o', b'r', b'l', 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., 2., 3., 4.])