numpy 学习汇总10-dtype数据类型 ( 基础学习 tcy)

numpy.dtype                     创建:2018/6/21 修改:2018/11/20

    
===================================================================
1.class numpy.dtype(obj,[align = False,copy = False])#创建一个数据类型
    
参数:
    obj:#要转换为数据类型的对象。
    align:bool,#将填充添加到字段以匹配C编译器为类似C结构输出的内容。
    copy:布尔,#True制作数据类型对象新副本。False只是对内置数据类型对象引用。
===================================================================
属性:参考备注1
    
方法:
newbyteorder([new_order='S'])#用不同的字节顺序返回一个新的dtype。还在数据类型的所有字段和子数组中进行更改。
# 参数new_order:str忽略大小写,可选 变换的字节顺序;
#     # 可以是以下任何一种:
#     'S' - 将dtype从当前转换为相反  本机改相反
#     {'<','L'} - 小端  忽略
#     {'>','B'} - 大端
#     {'=','N'} - 本地订单
#     {'|','I'} - 忽略(不改变字节顺序)
#
# 返回:new_dtype:dtype
    
# 实例1:
sys.byteorder                      # 'little'
dt.newbyteorder('S')            #dtype('<i4')
dt.newbyteorder('B')           #dtype('>i4') 
# 调用dtype的mro方桂即可查看其所有的父类:
np.float64.mro()
#[numpy.float64,numpy.floating,numpy.inexact,numpy.number,numpy.generic,float,object]
===================================================================
# 实例2:
dt=np.dtype(np.int32)#标量
    
dt1=np.dtype('str,int,float')                                                                              #字符串创建
dt2= np.dtype(np.str_,np.int32,np.float64)                                                        #逗号分隔
dt3=np.dtype([  ('name','U10'),('age','i'), ('weight','f')]  )                                     #list创建
dt4=np.dtype({'names':['name','age','weight'],'formats':['S10',np.int32,'f4']})#字典创建
    
dt   #dtype('int32')
dt1 #dtype([('f0', '<U'), ('f1', '<i4'), ('f2', '<f8')])
dt2 #dtype('<U')
dt3 #dtype([('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])
dt4 #dtype([('name', 'S10'), ('age', '<i4'), ('weight', '<f4')])
    
a=np.array([('Tom', 21, 50),('John', 18, 75)], dtype=dt4)
    # array([('Tom', 21, 50.), ('John', 18, 75.)],dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])
==================================================================
# 实例3:
b=np.dtype([('hello',int,(2,3,4)),('world',np.void,10)])#void灵活类型,大小为10
# dtype([('hello', '<i4', (2, 3, 4)), ('world', 'V10')])
    
c=np.dtype('i4,(2,3)f8')# dtype([('f0', '<i4'), ('f1', '<f8', (2, 3))])
np.array([1,2],dtype=c)
    # array([(1, [[1., 1., 1.], [1., 1., 1.]]),(2, [[2., 2., 2.], [2., 2., 2.]])],dtype=[('f0', '<i4'), ('f1', '<f8', (2, 3))])
    
# 实例4:将int16细分为2个int8,称为x和y。 0和1是以字节为单位的偏移量:
np.dtype((np.int16,{'x':( np.int8,0),'y':( np.int8,1)}))#dtype((''i2',[('x','| i1'),('y','| i1')]))
    
# 实例5:以字节为单位的偏移量,这里是0和25:
np.dtype({'surname':('S25',0),'age':( np.uint8,25)})#dtype([('surname', 'S25'), ('age', 'u1')])
===================================================================
备注1:
实例-属性 
import numpy as np
a=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', '<i4'), ('marks', '<f4')])
                      
b.base#dtype('int32')
s.base#dtype([('name', 'S20'), ('age', '<i4'), ('marks', '<f4')])
b.byteorder#'='
s.byteorder#'|'
b.char#'l'
s.char#'V'
b.descr#[('', '<i4')]
b.descr#[('name', '|S20'), ('age', '<i4'), ('marks', '<f4')]
b.fields#
s.fields#mappingproxy({'name': (dtype('S20'), 0), 'age': (dtype('int32'), 20), 'marks': (dtype('float32'), 24)})
b.flags#0
s.flags#16
b.hasobject#False
s.hasobject#False
b.isalignedstruct#False
b.isalignedstruct#False
b.isbuiltin#1
b.isbuiltin#0
b.isnative#True
b.isnative#True
b.itemsize#4
s.itemsize#28
b.kind#'i'
s.kind#'V'
b.name#'int32'
s.name#'void224'
b.names#
s.names#('name', 'age', 'marks')
b.ndim#0
s.ndim#0
b.num#7
s.num#20
b.shape#()
s.shape#()
b.str#'<i4'
s.str#'|V28'
b.type#<class 'numpy.int32'>
s.type#<class 'numpy.void'>
b.subdtype#
s.subdtype#
===============================================================================
备注2:属性列表 
类型 类型代码 说明 备注
base   基础                                                              dtype('int32')
byteorder   指示此数据类型对象的字节顺序的字符              '='
char   21种不同内置类型中的每一种的唯一字符代码     'l'
descr   PEP3118接口描述了数据类型。                       [('', '<i4')]
fields   此数据类型定义命名字段字典或无。                mappingproxy(,
      {'name': (dtype('S20'), 0)
flags   描述这种数据类型如何解释的位标志                0
hasobject   bool,指示此dtype是否包含任何字段或子dtype中的引用计数对象 FALSE
isalignedstruct   布尔值,指示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  

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/84311591
今日推荐