Python Numpy data-type dtype 自定义数据类型

一、实例

BIG-ENDIAN和LITTLE_ENDIAN区别
数据类型定义:

>>> dt = np.dtype('>i4')  定义一个big-endian int 4*8=32位的数据类型
>>> dt
dtype('>i4')
>>> dt.byteorder    //字节顺序:>为big-edian <为little-endian 
'>'
>>> dt.itemsize    //字节大小
4
>>> dt.name       //dt类型
'int32'
>>> dt.type is np.int32
True

自定义数据类型:
定义dt:

>>> dt = np.dtype([('name', np.str_, 16), ('grades', np.float64, (2,))])   //定义一个数据类型,其中name为16为字符串,grades为2个float64的子数组
>>> dt['name']
dtype('<U16')
>>> dt['grades']
dtype(('<f8',(2,)))

使用:

>>> x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
>>> x[1]
('John', [6.0, 7.0])
>>> x[1]['grades']
array([ 6.,  7.])
>>> type(x[1])
<type 'numpy.void'>
>>> type(x[1]['grades'])
<type 'numpy.ndarray'>

二、dtype参数

在声明数据类型时dtype能够自动将参数转为相应类型。默认数据类型为float_。
24个內建参数:
24內建Array-scalar types

>>> dt = np.dtype(np.int32)      # 32位int,注意32为位
>>> dt = np.dtype(np.complex128) # 128位复数

numpy.sctypeDict.keys()参数:
存在于numpy.sctypeDict.keys()中的字符串参数:

>>> dt = np.dtype('uint32')   # 32位uint,注意32为位
>>> dt = np.dtype('Float64')  # 64位float

python类型参数:

TablesAre
intint_
boolbool_
floatfloat_
complexcfloat
strstring
unicodeunicode_
buffervoid
(all others)object_
>>> dt = np.dtype(float)   # Python的浮点
>>> dt = np.dtype(int)     # Python的整型
>>> dt = np.dtype(object)  # Python的对象

简略字符参数:

'b'     boolean
'i'     (signed) integer
'u'     unsigned integer
'f'     floating-point
'c'     complex-floating point
'm'     timedelta
'M'     datetime
'O'     (Python) objects
'S', 'a'    (byte-)string
'U'     Unicode
'V'     raw data (void)
>>> dt = np.dtype('f8')   # 64位浮点,注意8为字节
>>> dt = np.dtype('c16')  # 128位复数   

带逗号字符串参数:

>>> dt = np.dtype("a3, 3u8, (3,4)a10")  //3字节字符串、3个64位整型子数组、3*4的10字节字符串数组,注意8为字节

其他:
(flexible_dtype, itemsize)第一个参数类型参数大小不固定,第二传入大小:

>>> dt = np.dtype((void, 10))  #10位
>>> dt = np.dtype((str, 35))   # 35字符字符串
>>> dt = np.dtype(('U', 10))   # 10字符unicode string

(fixed_dtype, shape)第一个参数传入固定大小参数,第二参数传入个数

>>> dt = np.dtype((np.int32, (2,2)))          # 2*2int子数组
>>> dt = np.dtype(('S10', 1))                 # 10字符字符串
>>> dt = np.dtype(('i4, (2,3)f8, f4', (2,3))) # 2x3结构子数组

[(field_name, field_dtype, field_shape), …]:

>>> dt = np.dtype([('big', '>i4'), ('little', '<i4')])
>>> dt = np.dtype([('R','u1'), ('G','u1'), ('B','u1'), ('A','u1')])

{‘names’: …, ‘formats’: …, ‘offsets’: …, ‘titles’: …, ‘itemsize’: …}:

>>> dt = np.dtype({'names': ['r','g','b','a'],'formats': [uint8, uint8, uint8, uint8]})
>>> dt = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'offsets': [0, 2],'titles': ['Red pixel', 'Blue pixel']})

{‘field1’: …, ‘field2’: …, …}:
不推荐使用,可能会产生冲突

>>> dt = np.dtype({'col1': ('S10', 0), 'col2': (float32, 10),'col3': (int, 14)}) //col1在字节0处,col2在字节10处,col3在字节14处

(base_dtype, new_dtype):

>>> dt = np.dtype((np.int32,{'real':(np.int16, 0),'imag':(np.int16, 2)})  //base_dtype前两个字节放置real,后两个字节放置imag
>>>dt = np.dtype((np.int32, (np.int8, 4)))  //base_dtype被分成4个int8的子数组

三、切换类型

使用astype,不可直接更改对象的dtype值

>>> b = np.array([1., 2., 3., 4.])
>>> b.dtype
dtype(‘float64‘)
>>> c = b.astype(int)
>>> c
array([1, 2, 3, 4])
>>> c.shape
(8,)
>>> c.dtype
dtype(‘int32‘)
>>> b
array([ 1.,  2.,  3.,  4.])
>>> b.dtype = ‘int‘
>>> b.dtype
dtype(‘int32‘)
>>> b
array([0, 1072693248,0, 1073741824,0,1074266112,          0, 1074790400])  //数组长度加倍
>>> b.shape
(8,)

查看类型取值范围:

np.finfo(np.float32)

英文版

  • 11
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
FY-2G是中国的静止气象卫星,可以传输到地面的数据包括温度、湿度、云图等。其中TBB是指亮温数据,可以用来分析大气温度和云层特征。 下面是一份绘制FY-2G TBB数据的Python代码,需要使用到Numpy、Matplotlib、Basemap等库: ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap # 读取数据 data = np.fromfile('FY2G_20180102_TBB.dat', dtype=np.float32) data = data.reshape(121, 121) # 绘制地图 map = Basemap(projection='cyl', llcrnrlon=70, llcrnrlat=15, urcrnrlon=140, urcrnrlat=55, resolution='l') map.drawcoastlines(linewidth=0.5) map.drawcountries(linewidth=0.5) map.drawparallels(np.arange(15, 55, 10), labels=[1, 0, 0, 0], fontsize=10) map.drawmeridians(np.arange(70, 140, 10), labels=[0, 0, 0, 1], fontsize=10) # 绘制TBB数据 x, y = map(np.linspace(70, 140, 121), np.linspace(15, 55, 121)) cs = map.pcolor(x, y, data, cmap='jet', vmin=180, vmax=300) cbar = map.colorbar(cs, location='right', pad='5%') cbar.set_label('TBB (K)', fontsize=10) # 显示图像 plt.title('FY-2G TBB 2018-01-02', fontsize=12) plt.show() ``` 这份代码可以读取FY2G_20180102_TBB.dat文件中的数据,并将其绘制成地图和TBB数据的热力图。其中,Basemap库用来绘制地图,pcolor函数用来绘制热力图,cmap参数指定了颜色映射方案,vmin和vmax参数指定了热力图的最小值和最大值,colorbar函数用来添加颜色条。最后,通过plt.show()显示图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值