简介

咱们晓得Python中有4种数字类型,别离是int,float,bool和complex。作为科学计算的NumPy,其数据类型更加的丰盛。

明天给大家具体解说一下NumPy中的数据类型。

数组中的数据类型

NumPy是用C语言来实现的,咱们能够对标一下NumPy中数组中的数据类型跟C语言中的数据类型:

Numpy 中的类型C 中的类型阐明
np.bool_boolBoolean (True or False) stored as a byte
np.bytesigned charPlatform-defined
np.ubyteunsigned charPlatform-defined
np.shortshortPlatform-defined
np.ushortunsigned shortPlatform-defined
np.intcintPlatform-defined
np.uintcunsigned intPlatform-defined
np.int_longPlatform-defined
np.uintunsigned longPlatform-defined
np.longlonglong longPlatform-defined
np.ulonglongunsigned long longPlatform-defined
np.half / np.float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
np.singlefloatPlatform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa
np.doubledoublePlatform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa.
np.longdoublelong doublePlatform-defined extended-precision float
np.csinglefloat complexComplex number, represented by two single-precision floats (real and imaginary components)
np.cdoubledouble complexComplex number, represented by two double-precision floats (real and imaginary components).
np.clongdoublelong double complexComplex number, represented by two extended-precision floats (real and imaginary components).

咱们在Ipython环境中随机查看一下下面的类型到底是什么:

import numpy as npIn [26]: np.byteOut[26]: numpy.int8In [27]: np.bool_Out[27]: numpy.bool_In [28]: np.ubyteOut[28]: numpy.uint8In [29]: np.shortOut[29]: numpy.int16In [30]: np.ushortOut[30]: numpy.uint16

所以下面的数据类型,其底层还是固定长度的数据类型,咱们看下到底有哪些:

Numpy 类型C 类型阐明
np.int8int8_tByte (-128 to 127)
np.int16int16_tInteger (-32768 to 32767)
np.int32int32_tInteger (-2147483648 to 2147483647)
np.int64int64_tInteger (-9223372036854775808 to 9223372036854775807)
np.uint8uint8_tUnsigned integer (0 to 255)
np.uint16uint16_tUnsigned integer (0 to 65535)
np.uint32uint32_tUnsigned integer (0 to 4294967295)
np.uint64uint64_tUnsigned integer (0 to 18446744073709551615)
np.intpintptr_tInteger used for indexing, typically the same as ssize_t
np.uintpuintptr_tInteger large enough to hold a pointer
np.float32float
np.float64 / np.float_doubleNote that this matches the precision of the builtin python float.
np.complex64float complexComplex number, represented by two 32-bit floats (real and imaginary components)
np.complex128 / np.complex_double complexNote that this matches the precision of the builtin python complex.

所有这些类型都是 dtype 对象的实例。罕用的有5种根本类型,别离是bool,int,uint,float和complex。

类型前面带的数字示意的是该类型所占的字节数。

下面表格中有一些 Platform-defined的数据类型,这些类型是跟平台相干的,在应用的时候要特地留神。

这些dtype类型能够在创立数组的时候手动指定:

>>> import numpy as np>>> x = np.float32(1.0)>>> x1.0>>> y = np.int_([1,2,4])>>> yarray([1, 2, 4])>>> z = np.arange(3, dtype=np.uint8)>>> zarray([0, 1, 2], dtype=uint8)

因为历史起因,为了向下兼容,咱们也能够在创立数组的时候指定字符格局的dtype。

>>> np.array([1, 2, 3], dtype='f')array([ 1.,  2.,  3.], dtype=float32)

下面的 f 示意的是float类型。

类型转换

如果想要转换一个现有的数组类型,能够应用数组自带的astype办法,也能够调用np的强制转换方法:

In [33]: z = np.arange(3, dtype=np.uint8)In [34]: zOut[34]: array([0, 1, 2], dtype=uint8)In [35]: z.astype(float)Out[35]: array([0., 1., 2.])In [36]: np.int8(z)Out[36]: array([0, 1, 2], dtype=int8)
留神,下面咱们应用了 float , Python将会把float 主动替换成为 np.float_,同样的简化格局还有 int == np.int_, bool == np.bool_, complex == np.complex_. 其余的数据类型不能应用简化版本。

查看类型

查看一个数组的数据类型能够应用自带的dtype属性:

In [37]: z.dtypeOut[37]: dtype('uint8')

dtype作为一个对象,自身也能够进行一些类型判断操作:

>>> d = np.dtype(int)>>> ddtype('int32')>>> np.issubdtype(d, np.integer)True>>> np.issubdtype(d, np.floating)False

数据溢出

一般来说,如果超出了数据的范畴是会报异样的。比方咱们有一个十分长的int值:

In [38]: a= 1000000000000000000000000000000000000000000000000000000000000000000000000000000In [39]: aOut[39]: 1000000000000000000000000000000000000000000000000000000000000000000000000000000In [40]: np.int(1000000000000000000000000000000000000000000000000000000)Out[40]: 1000000000000000000000000000000000000000000000000000000In [41]: np.int32(1000000000000000000000000000000000000000000000000000000)---------------------------------------------------------------------------OverflowError                             Traceback (most recent call last)<ipython-input-41-71feb4433730> in <module>()----> 1 np.int32(1000000000000000000000000000000000000000000000000000000)

下面的数字太长了,超出了int32的范畴,就会抛出异样。

然而NumPy的有些操作,如果超出范围之后,并不会报异样,而是失常范畴,这时候咱们就须要留神了:

In [43]: np.power(100, 8, dtype=np.int32)Out[43]: 1874919424In [44]: np.power(100, 8, dtype=np.int64)Out[44]: 10000000000000000

NumPy提供了两个办法来测量int和float的范畴,numpy.iinfo 和 numpy.finfo :

In [45]:  np.iinfo(int)Out[45]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)In [46]: np.iinfo(np.int32)Out[46]: iinfo(min=-2147483648, max=2147483647, dtype=int32)In [47]: np.iinfo(np.int64)Out[47]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

如果64位的int还是太小的话,能够应用np.float64,float64能够应用迷信计数法,所以可能失去更大范畴的后果,然而其精度可能会放大。

In [48]: np.power(100, 100, dtype=np.int64)Out[48]: 0In [49]: np.power(100, 100, dtype=np.float64)Out[49]: 1e+200

本文已收录于 http://www.flydean.com/02-python-numpy-datatype/

最艰深的解读,最粗浅的干货,最简洁的教程,泛滥你不晓得的小技巧等你来发现!

欢送关注我的公众号:「程序那些事」,懂技术,更懂你!