
创建ndarray
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
| Name | Description |
|---|---|
| object | 数组或嵌套数列 |
| dtype | 数组类型,可选 |
| copy | 对象是否需要复制,可选 |
| order | 数组样式,C行方向,F列方向,A任意方向(默认) |
| ndmin | 最小维度 |
numpy数据类型是dtype的实例,下面为各种类型:
查看数据类型
数组属性
| character | statement |
|---|---|
| ndarray.ndim | 返回int表示当前数组维度 |
| ndarray.shape | (row,col) |
| ndarray.size | 数组元素总个数,等于row*col |
| ndarray.dtype | 数组对象元素类型 |
| ndarray.itemsize | 每个元素大小,单位字节 |
| ndarray.flags | 对象的内存信息 |
| ndarray.real | 对象实部 |
| ndarray.imag | 虚部 |
| ndarray.data | 返回缓冲区,一般不需要这个属性 |
numpy.empty(shape, dtype = float, order = 'C')
numpy.zeros(shape, dtype = float, order = 'C')
numpy.ones(shape, dtype = None, order = 'C')
numpy.asarray(a, dtype = None, order = None)
类似于numpy.array(),但其参数只有三个,可以将其他list,tuple等转换为ndarray
numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)
s = b'Hello World'
a = np.frombuffer(s, dtype = 'S1')
#[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']
# bytestring
可迭代对象创建ndarray
numpy.fromiter(iterable, dtype, count=-1)
count为读取数据量,-1表示所有
Numpy从数组范围创建数组 asrange函数numpy.arange(start, stop, step, dtype)
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
num为要生成的等步长样本数,就是数组大小
endpoint为是否包含stop值
retstep为是否包含间距(最后一位存储)
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)