1.3.3.1 更多的数据类型

1.3.3.1.1 投射

“更大”的类型在混合类型操作中胜出:

In [175]:

  1. np.array([1, 2, 3]) + 1.5

Out[175]:

  1. array([ 2.5, 3.5, 4.5])

赋值不会改变类型!

In [176]:

  1. a = np.array([1, 2, 3])
  2. a.dtype

Out[176]:

  1. dtype('int64')

In [178]:

  1. a[0] = 1.9 # <-- 浮点被截取为整数
  2. a

Out[178]:

  1. array([1, 2, 3])

强制投射:

In [179]:

  1. a = np.array([1.7, 1.2, 1.6])
  2. b = a.astype(int) # <-- 截取整数
  3. b

Out[179]:

  1. array([1, 1, 1])

四舍五入:

In [180]:

  1. a = np.array([1.2, 1.5, 1.6, 2.5, 3.5, 4.5])
  2. b = np.around(a)
  3. b # 仍然是浮点

Out[180]:

  1. array([ 1., 2., 2., 2., 4., 4.])

In [181]:

  1. c = np.around(a).astype(int)
  2. c

Out[181]:

  1. array([1, 2, 2, 2, 4, 4])

1.3.3.1.2 不同数据类型的大小

整数 (带有符号):

类型字节数
int88 bits
int1616 bits
int3232 bits (与32位平台的int相同)
int6464 bits (与64位平台的int相同)

In [182]:

  1. np.array([1], dtype=int).dtype

Out[182]:

  1. dtype('int64')

In [183]:

  1. np.iinfo(np.int32).max, 2**31 - 1

Out[183]:

  1. (2147483647, 2147483647)

In [184]:

  1. np.iinfo(np.int64).max, 2**63 - 1

Out[184]:

  1. (9223372036854775807, 9223372036854775807L)

无符号整数:

类型字节数
uint88 bits
uint1616 bits
uint3232 bits
uint6464 bits

In [185]:

  1. np.iinfo(np.uint32).max, 2**32 - 1

Out[185]:

  1. (4294967295, 4294967295)

In [186]:

  1. np.iinfo(np.uint64).max, 2**64 - 1

Out[186]:

  1. (18446744073709551615L, 18446744073709551615L)

浮点数据:

类型字节数
float1616 bits
float3232 bits
float6464 bits (与浮点相同)
float9696 bits, 平台依赖 (与 np.longdouble 相同)
float128128 bits, 平台依赖 (与 np.longdouble相同)

In [187]:

  1. np.finfo(np.float32).eps

Out[187]:

  1. 1.1920929e-07

In [188]:

  1. np.finfo(np.float64).eps

Out[188]:

  1. 2.2204460492503131e-16

In [189]:

  1. np.float32(1e-8) + np.float32(1) == 1

Out[189]:

  1. True

In [190]:

  1. np.float64(1e-8) + np.float64(1) == 1

Out[190]:

  1. False

浮点复数:

类型字节数
complex64两个 32-bit 浮点
complex128两个 64-bit 浮点
complex192两个 96-bit 浮点, 平台依赖
complex256两个 128-bit 浮点, 平台依赖

更小的数据类型

如果你不知道需要特殊数据类型,那你可能就不需要。

比较使用 float32代替 float64:

  • 一半的内存和硬盘大小
  • 需要一半的宽带(可能在一些操作中更快)

In [191]:

  1. a = np.zeros((1e6,), dtype=np.float64)
  2. b = np.zeros((1e6,), dtype=np.float32)
  3. %timeit a*a
  1. 1000 loops, best of 3: 1.41 ms per loop

In [192]:

  1. %timeit b*b
  1. 1000 loops, best of 3: 739 µs per loop
  • 但是更大的四舍五入误差 - 有时在一些令人惊喜的地方(即,不要使用它们除非你真的需要)