【Numpy】

【Numpy】

N维数组:Ndarray对象

构造

numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

image

数据类型对象 dtype

numpy.dtype(object, align, copy)

结构化数据类型

eg 1

# int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替
import numpy as np
dt=np.dtype([('age',np.int8)])
print(dt)
a=np.array([(10,),(20,),(30,)],dtype=dt)
print(a)
print(a['age'])

image

eg 2 student实例

import numpy as np
student=np.dtype([('name','S20'),('age','i1'),('marks','f4')])
a=np.array([('pwy',90,0),('lzp',3,90)],dtype=student)
print(a)

image

数组属性

数组的秩ndarray.ndim

import numpy as np
a=np.arange(24) # 可变长数组
print(a.ndim)
b=a.reshape(2,4,3)
print(b.ndim)
posted @ 2025-04-13 21:08  White_ink  阅读(4)  评论(0)    收藏  举报