本文共 2124 字,大约阅读时间需要 7 分钟。
np.dtype attributes属性 2018/6/21 2018/12/6
1.实例
import numpy as npa=np.arange(24).reshape(2,3,4)s=np.dtype({'names':['name','age','marks'],'formats':['S20',np.int32,'f4']})a1=np.array([[('Tom11', 11, 110),('John12', 12, 120),('Mark13', 13, 130)],[('Tom21', 21, 210),('John22', 22, 220),('Mark23', 23, 230)]], dtype=s)b=a.dtype #dtype('int32')s #dtype([('name', 'S20'), ('age', '
实例1:
b.base ,s.base #dtype('int32') #dtype([('name', 'S20'), ('age', '# b.subdtype,s.subdtype # #b.char ,s.char #'l' #'V'b.descr ,s.descr #[('', '
实例2:
# 所有内置数据类型对象都具有'='或'|'字节顺序。dt = np.dtype('i2')dt.byteorder# '='# endian端点与8位数无关np.dtype('i1').byteorder# '|'# or ASCII stringsnp.dtype('S2').byteorder# '|'# Even if specific code is given, and it is native '=' is the byteorderimport syssys_is_le = sys.byteorder == 'little' # Truenative_code = sys_is_le and '<' or '>' # '<'swapped_code = sys_is_le and '>' or '<' # '>'dt = np.dtype(native_code + 'i2')dt.byteorder # '='# Swapped code shows up as itselfdt = np.dtype(swapped_code + 'i2')dt.byteorder == swapped_code # True
2.一般类型数据的字符代码 (one of ‘biufcmMOSUV’)
b booleani signed integeru unsigned integerf floating-pointc complex floating-pointm timedeltaM datetimeO objectS (byte-)stringU UnicodeV void
3.属性简表
类型 | 说明 | 备注 |
alignment | 根据编译器此数据类型所需的对齐(字节) | 1 |
base | 基础 | dtype('int32') |
byteorder | 数据类型对象字节顺序的字符< 小端;>大端;| 不适用 | '='本地 |
char | 21种不同内置类型中的每一种的唯一字符代码 | 'l' |
descr | PEP3118接口描述了数据类型。 | [('', '<i4')] |
fields | 此数据类型定义命名字段字典或无。 | mappingproxy(, |
- | - | {'name': (dtype('S20'), 0) |
flags | 描述这种数据类型如何解释的位标志 | 0 |
hasobject | bool,指示此dtype是否包含任何字段或子dtype中的引用计数对象 | FALSE |
isalignedstruct | bool指示dtype是否是维护字段对齐的结构 | FALSE |
isbuiltin | 整数指示这个dtype与内置dtypes的关系 | 1 |
isnative | 布尔值,指示此dtype的字节顺序是否是平台的本地顺序。 | TRUE |
Itemsize | 此数据类型对象的元素大小 | 4 |
kind | 数据类型的字符代码('biufcmMOSUV') | 'i' |
name | 数据类型名称。 | 'int32' |
names | 字段名称列表,如果没有字段,则为无。 | ('name', 'age', 'marks') |
ndim | 则该子数组的维数,否则为0。 | 0 |
num | 21种不同内置类型中的每一种的唯一编号 | 7 |
shape | 则为子数组的形状元组,否则为()。 | () |
str | 此数据类型对象的数组协议类型字符串。 | '<i4' |
type | 用于实例化此数据类型的标量的类型对象 | <class 'numpy.int32'> |
subdtype | 元组(item_dtype,shape)如这个dtype描述一个子数组则返回None | - |
转载地址:http://tfqbf.baihongyu.com/