简介

我们知道Python中有4种数字类型,分别是int,float,bool和complex。作为科学计算的NumPy,其数据类型更加的丰富。

今天给大家详细讲解一下NumPy中的数据类型。

数组中的数据类型

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

Numpy 中的类型 C 中的类型 说明
np.bool_ bool Boolean (True or False) stored as a byte
np.byte signed char Platform-defined
np.ubyte unsigned char Platform-defined
np.short short Platform-defined
np.ushort unsigned short Platform-defined
np.intc int Platform-defined
np.uintc unsigned int Platform-defined
np.int_ long Platform-defined
np.uint unsigned long Platform-defined
np.longlong long long Platform-defined
np.ulonglong unsigned long long Platform-defined
np.half / np.float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
np.single float Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa
np.double double Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa.
np.longdouble long double Platform-defined extended-precision float
np.csingle float complex Complex number, represented by two single-precision floats (real and imaginary components)
np.cdouble double complex Complex number, represented by two double-precision floats (real and imaginary components).
np.clongdouble long double complex Complex number, represented by two extended-precision floats (real and imaginary components).

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

import numpy as np

In [26]: np.byte
Out[26]: numpy.int8 In [27]: np.bool_
Out[27]: numpy.bool_ In [28]: np.ubyte
Out[28]: numpy.uint8 In [29]: np.short
Out[29]: numpy.int16 In [30]: np.ushort
Out[30]: numpy.uint16

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

Numpy 类型 C 类型 说明
np.int8 int8_t Byte (-128 to 127)
np.int16 int16_t Integer (-32768 to 32767)
np.int32 int32_t Integer (-2147483648 to 2147483647)
np.int64 int64_t Integer (-9223372036854775808 to 9223372036854775807)
np.uint8 uint8_t Unsigned integer (0 to 255)
np.uint16 uint16_t Unsigned integer (0 to 65535)
np.uint32 uint32_t Unsigned integer (0 to 4294967295)
np.uint64 uint64_t Unsigned integer (0 to 18446744073709551615)
np.intp intptr_t Integer used for indexing, typically the same as ssize_t
np.uintp uintptr_t Integer large enough to hold a pointer
np.float32 float
np.float64 / np.float_ double Note that this matches the precision of the builtin python float.
np.complex64 float complex Complex number, represented by two 32-bit floats (real and imaginary components)
np.complex128 / np.complex_ double complex Note 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)
>>> x
1.0
>>> y = np.int_([1,2,4])
>>> y
array([1, 2, 4])
>>> z = np.arange(3, dtype=np.uint8)
>>> z
array([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]: z
Out[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.dtype
Out[37]: dtype('uint8')

dtype作为一个对象,本身也可以进行一些类型判断操作:

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

数据溢出

一般来说,如果超出了数据的范围是会报异常的。比如我们有一个非常长的int值:

In [38]: a= 1000000000000000000000000000000000000000000000000000000000000000000000000000000

In [39]: a
Out[39]: 1000000000000000000000000000000000000000000000000000000000000000000000000000000 In [40]: np.int(1000000000000000000000000000000000000000000000000000000)
Out[40]: 1000000000000000000000000000000000000000000000000000000 In [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]: 1874919424 In [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]: 0 In [49]: np.power(100, 100, dtype=np.float64)
Out[49]: 1e+200

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

最通俗的解读,最深刻的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

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

NumPy之:数据类型的更多相关文章

  1. 3.1Python数据处理篇之Numpy系列(一)---ndarray对象的属性与numpy的数据类型

    目录 目录 (一)简单的数组创建 1.numpy的介绍: 2.numpy的数组对象ndarray: 3.np.array(list/tuple)创建数组: (二)ndarray对象的属性 1.五个常用 ...

  2. NumPy之:数据类型对象dtype

    目录 简介 dtype的定义 可转换为dtype的对象 dtype对象 None 数组标量类型 通用类型 内置Python类型 带有.dtype属性的对象 一个字符的string对象 数组类型的Str ...

  3. python -- numpy 基本数据类型,算术运算,组合,分割 函数

    0 NumPy数组 NumPy数组:NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成: 实际的数据 描述这些数据的元数据 NumPy数组属性: ndim(纬数,x,y 2),sha ...

  4. Numpy | 03 数据类型

    numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型. 下表列举了常用 NumPy 基本类型: 名称 描 ...

  5. Numpy:ndarray数据类型和运算

    Numpy的ndarray:一种多维数组对象 N维数组对象,该对象是一个快速而灵活的大数据集容器,nadarry是一个通用的同构数据多维容器,也就是说,其中的所有元素必须是相同类型的.每个数组都有一个 ...

  6. python 数据处理中各种存储方式里数据类型的转换

    自己记录,仅供参考 在数据处理时经常会遇到数据类型不匹配的事情,为了方便查看各种存储方式中数据类型的改变.我把一些自己常用的整理方式记录下来,希望可以为以后数据类型的处理工作提供便利. 数据常用的基本 ...

  7. Numpy基础学习

    Numpy(Numerical Python的简称)是高性能科学计算和数据分析的基础包. 主要的功能: 1.ndarray,一个具有矢量运算和复杂广播工能的快速且节省空间的多维数组 2.用于对整组数据 ...

  8. ndarray数据类型

    dtype(数据类型)是一个特殊的对象,它含有ndarray将一块内存解释为特定数据类型所需的信息 In [18]: sim1 = np.array([1,2,3],dtype=np.float64) ...

  9. 【机器学习】--Python机器学习库之Numpy

    一.前述 NumPy(Numerical Python的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然地使用数组和矩阵. NumPy包含很多实用的数学函数,涵盖线性代数运算.傅 ...

随机推荐

  1. Java 开源办公开发平台 O2OA V5.4.0 发布 | 设计元素搜索功能上线

    O2OA V5.4.0版本此次更新的设计元素搜索功能,可以让用户在海量的脚本.页面.表单.视图等信息中迅速锁定有价值的信息,以便提高用户获取信息的效率.拥有此搜索功能后,在开发过程中,可以加速定位脚本 ...

  2. JS语法-ES6

    1.介绍 ECMAScript 6简称ES6,是JavaScript语言的下一代标准. 2.常用属性及方法 2.1常量与变量的定义 在JavaScript中,我们一般使用var来定义变量,实际上它是有 ...

  3. Fastdfs数据迁移方案

    1.     方案背景描述 环境迁移,需要迁移旧环境的fastdfs集群的数据到新环境,由于之前数据迁移仅仅是针对mysql和mongodb,对fastdfs数据的迁移了解甚少,本文档主要是针对fas ...

  4. SpringBoot(三):SpringBoot热部署插件

    SpringBoot热部署插件 在实际开发中,我们修改了某些代码逻辑功能或页面都需要重启应用,这无形中降低了开发效率!热部署是指当我们修改代码后,服务能自动启动加载新修改的内容,这样大大提高了我们开发 ...

  5. 微信小程序滚动条设置

    隐藏滚动条 ::-webkit-scrollbar { width: 0rpx; height: 0rpx; background-color: transparent; } 其实设置为宽高为 0 或 ...

  6. 从头捋了一遍 Java 代理机制,收获颇丰

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  7. 02.从0实现一个JVM语言之词法分析器-Lexer-03月02日更新

    从0实现JVM语言之词法分析器-Lexer 本次有较大幅度更新, 老读者如果对前面的一些bug, 错误有疑问可以复盘或者留言. 源码github仓库, 如果这个系列文章对你有帮助, 希望获得你的一个s ...

  8. 给新手的 11 个 Docker 免费上手项目

    转: 给新手的 11 个 Docker 免费上手项目 作者:老K玩代码 来源:toutiao.com/i6882755471015576072 Docker 是一个开源的应用容器引擎,让开发者可以打包 ...

  9. 在 .NET Core 中应用六边形架构

    在本文中,您会看到一个Web API应用的模板,在.NET Core 中应用了六边形架构,并且里面包含了一些基础功能. 介绍 这是一个模板项目,里面集成了一些必备的基础功能,当我们需要开发一个新项目时 ...

  10. `vi`——终端中的编辑器

    `vi`--终端中的编辑器 目标* `vi` 简介* 打开和新建文件* 三种工作模式* 常用命令* 分屏命令* 常用命令速查图 01. `vi` 简介 1.1 学习 `vi` 的目的 * 在工作中,要 ...