{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据类型\n",
    "\n",
    "## 常见数据类型\n",
    "\n",
    "Python 原生的数据类型相对较少， bool、int、float、str等。这在不需要关心数据在计算机中表示的所有方式的应用中是方便的。然而，对于科学计算，通常需要更多的控制。为了加以区分 numpy 在这些类型名称末尾都加了“_”。\n",
    "\n",
    "下表列举了常用 numpy 基本类型。\n",
    "\n",
    "\n",
    "类型 | 备注 | 说明 \n",
    "---|---|---\n",
    "bool_ = bool8 | 8位 | 布尔类型\n",
    "int8 = byte | 8位 | 整型\n",
    "int16 = short |\t16位| 整型\n",
    "int32 = intc | 32位| 整型\n",
    "int_ = int64 = long = int0 = intp | 64位| 整型\n",
    "uint8 = ubyte |8位 | 无符号整型\n",
    "uint16 = ushort|16位| 无符号整型\n",
    "uint32 = uintc|32位| 无符号整型\n",
    "uint64 = uintp = uint0 = uint| 64位| 无符号整型\n",
    "float16 = half|16位 | 浮点型\n",
    "float32 = single| 32位| 浮点型\n",
    "float_ = float64 = double| 64位| 浮点型\n",
    "str_ = unicode_ = str0 = unicode| |Unicode 字符串\n",
    "datetime64| |日期时间类型\n",
    "timedelta64| |表示两个时间之间的间隔\n",
    "\n",
    "\n",
    "\n",
    "## 创建数据类型\n",
    "\n",
    "numpy 的数值类型实际上是 dtype 对象的实例。\n",
    "\n",
    "```python\n",
    "class dtype(object):\n",
    "    def __init__(self, obj, align=False, copy=False):\n",
    "        pass\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "每个内建类型都有一个唯一定义它的字符代码，如下：\n",
    "\n",
    "字符 | \t对应类型|备注\n",
    "---|---|---\n",
    "b\t|boolean | 'b1'\n",
    "i\t|signed integer | 'i1', 'i2', 'i4', 'i8'\n",
    "u\t|unsigned integer | 'u1', 'u2' ,'u4' ,'u8'\n",
    "f\t|floating-point | 'f2', 'f4', 'f8'\n",
    "c\t|complex floating-point |\n",
    "m\t|timedelta64 |表示两个时间之间的间隔\n",
    "M\t|datetime64 |日期时间类型\n",
    "O\t|object |\n",
    "S\t|(byte-)string | S3表示长度为3的字符串\n",
    "U\t|Unicode | Unicode 字符串\n",
    "V\t|void\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.dtype('b1')\n",
    "print(a.type)  # <class 'numpy.bool_'>\n",
    "print(a.itemsize)  # 1\n",
    "\n",
    "a = np.dtype('i1')\n",
    "print(a.type)  # <class 'numpy.int8'>\n",
    "print(a.itemsize)  # 1\n",
    "a = np.dtype('i2')\n",
    "print(a.type)  # <class 'numpy.int16'>\n",
    "print(a.itemsize)  # 2\n",
    "a = np.dtype('i4')\n",
    "print(a.type)  # <class 'numpy.int32'>\n",
    "print(a.itemsize)  # 4\n",
    "a = np.dtype('i8')\n",
    "print(a.type)  # <class 'numpy.int64'>\n",
    "print(a.itemsize)  # 8\n",
    "\n",
    "a = np.dtype('u1')\n",
    "print(a.type)  # <class 'numpy.uint8'>\n",
    "print(a.itemsize)  # 1\n",
    "a = np.dtype('u2')\n",
    "print(a.type)  # <class 'numpy.uint16'>\n",
    "print(a.itemsize)  # 2\n",
    "a = np.dtype('u4')\n",
    "print(a.type)  # <class 'numpy.uint32'>\n",
    "print(a.itemsize)  # 4\n",
    "a = np.dtype('u8')\n",
    "print(a.type)  # <class 'numpy.uint64'>\n",
    "print(a.itemsize)  # 8\n",
    "\n",
    "a = np.dtype('f2')\n",
    "print(a.type)  # <class 'numpy.float16'>\n",
    "print(a.itemsize)  # 2\n",
    "a = np.dtype('f4')\n",
    "print(a.type)  # <class 'numpy.float32'>\n",
    "print(a.itemsize)  # 4\n",
    "a = np.dtype('f8')\n",
    "print(a.type)  # <class 'numpy.float64'>\n",
    "print(a.itemsize)  # 8\n",
    "\n",
    "a = np.dtype('S')\n",
    "print(a.type)  # <class 'numpy.bytes_'>\n",
    "print(a.itemsize)  # 0\n",
    "a = np.dtype('S3')\n",
    "print(a.type)  # <class 'numpy.bytes_'>\n",
    "print(a.itemsize)  # 3\n",
    "\n",
    "a = np.dtype('U3')\n",
    "print(a.type)  # <class 'numpy.str_'>\n",
    "print(a.itemsize)  # 12\n",
    "```\n",
    "\n",
    "## 数据类型信息\n",
    "\n",
    "Python 的浮点数通常是64位浮点数，几乎等同于 `np.float64`。\n",
    "\n",
    "NumPy和Python整数类型的行为在整数溢出方面存在显着差异，与 NumPy 不同，Python 的`int` 是灵活的。这意味着Python整数可以扩展以容纳任何整数并且不会溢出。\n",
    "\n",
    "Machine limits for integer types.\n",
    "```python\n",
    "class iinfo(object):\n",
    "    def __init__(self, int_type):\n",
    "        pass\n",
    "    def min(self):\n",
    "        pass\n",
    "    def max(self):\n",
    "        pass\n",
    "```\n",
    "【例】\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "ii16 = np.iinfo(np.int16)\n",
    "print(ii16.min)  # -32768\n",
    "print(ii16.max)  # 32767\n",
    "\n",
    "ii32 = np.iinfo(np.int32)\n",
    "print(ii32.min)  # -2147483648\n",
    "print(ii32.max)  # 2147483647\n",
    "```\n",
    "\n",
    "Machine limits for floating point types.\n",
    "```python\n",
    "class finfo(object):\n",
    "    def _init(self, dtype):\n",
    "```\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "ff16 = np.finfo(np.float16)\n",
    "print(ff16.bits)  # 16\n",
    "print(ff16.min)  # -65500.0\n",
    "print(ff16.max)  # 65500.0\n",
    "print(ff16.eps)  # 0.000977\n",
    "\n",
    "ff32 = np.finfo(np.float32)\n",
    "print(ff32.bits)  # 32\n",
    "print(ff32.min)  # -3.4028235e+38\n",
    "print(ff32.max)  # 3.4028235e+38\n",
    "print(ff32.eps)  # 1.1920929e-07\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "218.2px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
