{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy 基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://imgbed.momodel.cn/1200px_NumPy_logo.svg.png\" width=300>\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "在这节课中，我们将会学习到  Numpy 的相关内容。\n",
    "\n",
    "`NumPy（Numerical Python）`是一个开源的 **Python** 科学计算库，用于快速处理任意维度的数组。\n",
    "\n",
    "对于同样的数值计算任务，使用 `NumPy` 比直接使用 **Python** 要简洁的多。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `ndarray` 介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`NumPy` 提供了一个`N` 维数组类型 `ndarray`，它描述了**相同类型**的 `items` 的集合。\n",
    "   \n",
    "|语文|数学|英语|政治|体育|\n",
    "|--|--|--|--|--|\n",
    "|80|89|86|67|79|\n",
    "|78|97|89|76|81|\n",
    "\n",
    "用 `ndarray` 进行存储："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建ndarray\n",
    "score = np.array([[80, 89, 86, 67, 79],[78, 97, 89, 67, 81]])\n",
    "\n",
    "# 打印结果\n",
    "score\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  `ndarray` 的属性  \n",
    "数组属性反映了数组本身固有的信息。\n",
    "\n",
    "|属性名字|\t属性解释|\n",
    "|--|--|\n",
    "|ndarray.shape|\t数组维度的元组|\n",
    "|ndarray.ndim|\t数组维数|\n",
    "|ndarray.size|\t数组中的元素数量|\n",
    "|ndarray.dtype|\t数组元素的类型|\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ `shape`：数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建不同形状的数组\n",
    "a = np.array([[1, 2, 3],[4, 5, 6]])\n",
    "b = np.array([1, 2, 3, 4])\n",
    "c = np.array([[[1, 2, 3],[4, 5, 6]],[[1, 2, 3],[4, 5, 6.0]]])\n",
    "\n",
    "# 分别打印出形状\n",
    "print(a.shape)\n",
    "print(b.shape)\n",
    "print(c.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ `ndim`:数组维数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建不同形状的数组\n",
    "a = np.array([[1, 2, 3],[4, 5, 6]])\n",
    "b = np.array([1, 2, 3, 4])\n",
    "c = np.array([[[1, 2, 3],[4, 5, 6]],[[1, 2, 3],[4, 5, 6.0]]])\n",
    "\n",
    "# 分别打印出维数\n",
    "print(a.ndim)\n",
    "print(b.ndim)\n",
    "print(c.ndim)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ `size`：数组元素数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建不同形状的数组\n",
    "a = np.array([[1, 2, 3],[4, 5, 6]])\n",
    "b = np.array([1, 2, 3, 4])\n",
    "c = np.array([[[1, 2, 3],[4, 5, 6]],[[1, 2, 3],[4, 5, 6.0]]])\n",
    "\n",
    "# 分别打印出数组元素数量\n",
    "print(a.size)\n",
    "print(b.size)\n",
    "print(c.size)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ `dtype`：数组元素的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建不同形状的数组\n",
    "a = np.array([[1, 2, 3],[4, 5, 6]])\n",
    "b = np.array([1, 2, 3, 4])\n",
    "c = np.array([[[1, 2, 3],[4, 5, 6]],[[1, 2, 3],[4, 5, 6.0]]])\n",
    "\n",
    "# 分别打印出数组元素数量\n",
    "print(a.dtype)\n",
    "print(b.dtype)\n",
    "print(c.dtype)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px>  **编程练习**\n",
    "\n",
    "要求：把二维数组 [[1, 2, 3],[4, 5, 6],[7,8,9]] 转为 ndarray 格式。并查看其形状、维数和元素数量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请编写你的答案\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])\n",
    "print(a)\n",
    "print(a.shape)\n",
    "print(a.ndim)\n",
    "print(a.size)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `ndarray` 的类型\n",
    "\n",
    "|名称|描述|\n",
    "|--|--|\n",
    "|np.bool|用一个字节存储的布尔类型（True或False）|\n",
    "|np.int8|一个字节大小，-128 至 127|\n",
    "|np.int16|整数，-32768 至 32767|\n",
    "|np.int32|整数，$-2^{31}$ 至 $2^{32} -1$|\n",
    "|np.int64|整数，$-2^{63}$ 至 $2^{63} - 1$|\n",
    "|np.uint8|无符号整数，0 至 255|\n",
    "|np.uint16|无符号整数，0 至 65535|\n",
    "|np.uint32|\t无符号整数，0 至 $2^{32} - 1$|\n",
    "|np.uint64|\t无符号整数，0 至 $2^{64} - 1$ |\n",
    "|np.float16\t|半精度浮点数：16位，正负号1位，指数5位，精度10位|\n",
    "|np.float32\t|单精度浮点数：32位，正负号1位，指数8位，精度23位|\n",
    "|np.float64\t|双精度浮点数：64位，正负号1位，指数11位，精度52位|\n",
    "|np.complex64|复数，分别用两个32位浮点数表示实部和虚部|\n",
    "|np.complex128|复数，分别用两个64位浮点数表示实部和虚部|\n",
    "|np.object_|python对象|\n",
    "|np.string_|字符串|\n",
    "|np.unicode_|unicode类型|  \n",
    "\n",
    "\n",
    "**注意：创建数组的时候指定类型**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建数组时指定类型为 np.float32\n",
    "a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)\n",
    "\n",
    "# 创建数组时未指定类型\n",
    "b = np.array([[1, 2, 3],[4, 5, 6]])\n",
    "\n",
    "# 打印结果\n",
    "print(\"数组a：\\n%s,\\n数据类型：%s\"%(a,a.dtype))\n",
    "print(\"数组b：\\n%s,\\n数据类型：%s\"%(b,b.dtype))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "###  基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  生成元素值为 `0 ` 和 `1` 的数组的方法 \n",
    "\n",
    "+ 生成全部元素值为 `0` 的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "zero = np.zeros([3, 4])\n",
    "zero\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 生成全部元素值为 `1` 的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "one = np.ones([3, 4])\n",
    "one\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 生成对角数组(对角线的地方是 `1`，其余地方是 `0`)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eyes = np.eye(10, 5)\n",
    "eyes\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 创建方阵对角矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.eye(5, 5) 可简写为 (5)\n",
    "eyes1 = np.eye(5)\n",
    "eyes1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 从现有数组生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [[1, 2, 3], [4, 5, 6]]\n",
    "\n",
    "# 从现有的数组中创建\n",
    "a1 = np.array(a)\n",
    "a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 生成固定范围的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成等间隔的数组\n",
    "a = np.linspace(0, 90, 10)\n",
    "a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成等间隔的数组\n",
    "b = np.arange(0, 90, 10)\n",
    "b\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 形状修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import array\n",
    "a = array([[ 0, 1, 2, 3, 4, 5],\n",
    "           [10,11,12,13,14,15],\n",
    "           [20,21,22,23,24,25],\n",
    "           [30,31,32,33,34,35]])\n",
    "a.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在转换形状的时候，一定要注意数组的元素匹配\n",
    "# 只是将形状进行了修改，但并没有将行列进行转换\n",
    "b = a.reshape([3, 8])\n",
    "b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数组的形状被修改为: (2, 12), -1: 表示同过自动计算得到此处的值\n",
    "c = a.reshape([-1, 12])\n",
    "c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = a.T\n",
    "d.shape\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  类型修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([[[1, 2, 3], [4, 5, 6]], [[12, 3, 34], [5, 6, 7]]])\n",
    "arr.dtype\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr.astype(np.float32)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px>  **编程练习**\n",
    "\n",
    "要求：创建一个 3x3 并且值从0到8的矩阵\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请编写你的答案\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "Z = np.arange(9).reshape(3,3)\n",
    "print(Z)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])\n",
    "np.unique(arr)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组运算\n",
    "\n",
    "数组的算术运算是元素级别的操作，新的数组被创建并且被结果填充。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运算|函数\n",
    "--- | --- \n",
    "`a + b` | `add(a,b)`\n",
    "`a - b` | `subtract(a,b)`\n",
    "`a * b` | `multiply(a,b)`\n",
    "`a / b` | `divide(a,b)`\n",
    "`a ** b` | `power(a,b)`\n",
    "`a % b` | `remainder(a,b)`\n",
    "\n",
    "以乘法为例，数组与标量相乘，相当于数组的每个元素乘以这个标量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([1, 2, 3, 4])\n",
    "a * 3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数组按元素相乘："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1, 2])\n",
    "b = np.array([3, 4])\n",
    "a * b\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.multiply(a, b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数还可以接受第三个参数，表示将结果存入第三个参数中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.multiply(a, b, a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px>  **编程练习**\n",
    "\n",
    "要求：把二维数组 [[1, 2, 3],[4, 5, 6],[7,8,9]] 转为 ndarray 格式。并对每个元素 +1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请编写你的答案\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])\n",
    "print(a+1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵  \n",
    "使用 `mat` 方法将 `2` 维数组转化为矩阵："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1, 2, 4],\n",
    "              [2, 5, 3],\n",
    "              [7, 8, 9]])\n",
    "A = np.mat(a)\n",
    "A\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 也可以使用 **Matlab** 的语法传入一个字符串来生成矩阵：\n",
    "A = np.mat('1,2,4;2,5,3;7,8,9')\n",
    "A\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵与向量的乘法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([[1], [2], [3]])\n",
    "x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A*x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([[1, 2],\n",
    "              [3, 4],\n",
    "             [5, 6]])\n",
    "B = np.mat(b)\n",
    "A*B\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`A.I` 表示 `A` 矩阵的逆矩阵："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A.I\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵指数表示矩阵连乘："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A ** 4\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px>  **编程练习**\n",
    "\n",
    "要求：分别把二维数组 [[1, 2] ,[3, 4]] 和 [5, 6] ,[7, 8]]转为  matrix 格式。并计算矩阵的乘积。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请编写你的答案\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.mat([[1, 2],[4, 5]])\n",
    "b = np.mat([[5, 6],[7, 8]])\n",
    "print(a*b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|方法|作用|\n",
    "|--|--|\n",
    "|`a.sum(axis=None)`|求和|\n",
    "|`a.prod(axis=None)`|求积|\n",
    "|`a.min(axis=None)`|最小值|\n",
    "|`a.max(axis=None)`|最大值|\n",
    "|`a.argmin(axis=None)`|最小值索引|\n",
    "|`a.argmax(axis=None)`|最大值索引|\n",
    "|`a.ptp(axis=None)`|最大值减最小值|\n",
    "|`a.mean(axis=None)`|平均值|\n",
    "|`a.std(axis=None)`|标准差|\n",
    "|`a.var(axis=None)`|方差|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "from numpy import array\n",
    "a = array([[1, 2, 3],\n",
    "           [4, 5, 6]])\n",
    "a\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求所有元素的和："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.sum()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**指定求和的维度**：\n",
    "沿着第一维求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(a, axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.sum(axis=0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "沿着第二维求和："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(a, axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.sum(axis=1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "沿着最后一维求和："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(a, axis=-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.sum(axis=-1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px>  **编程练习**\n",
    "\n",
    "要求：创建一个长度为30的随机向量并找到它的平均值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请编写你的答案\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "Z = np.random.random(30)\n",
    "m = Z.mean()\n",
    "print(m)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 比较和逻辑函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运算符|函数|\n",
    ":---: | :---: \n",
    "`==` | `equal`\n",
    "`!=` | `not_equal`\n",
    "`>` | `greater`\n",
    "`>=` | `greater_equal`\n",
    "`<` | `less`\n",
    "`<=` | `less_equal`\n",
    "\n",
    "数组元素的比对，我们可以直接使用运算符进行比较，比如判断数组中元素是否大于某个数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import array\n",
    "a = array([[ 0, 1, 2, 3, 4, 5],\n",
    "           [10,11,12,13,14,15],\n",
    "           [20,21,22,23,24,25],\n",
    "           [30,31,32,33,34,35]])\n",
    "\n",
    "a > 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断数组中元素大于10的元素赋值为 -10 \n",
    "a[a > 10] = -10\n",
    "a\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是当数组元素较多时，查看输出结果便变得很麻烦，这时我们可以使用`all()`方法，直接比对矩阵的所有对应的元素是否满足条件。假如判断某个区间的值是否全是大于 `20`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import array\n",
    "a = array([[ 0, 1, 2, 3, 4, 5],\n",
    "           [10,11,12,13,14,15],\n",
    "           [20,21,22,23,24,25],\n",
    "           [30,31,32,33,34,35]])\n",
    "\n",
    "a[1:3,1:3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.all(a[1:3,1:3] > 20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 `any()` 来判断数组某个区间的元素是否存在大于 `20`的元素:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.any(a[1:3,1:3] > 20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='http://imgbed.momodel.cn/5cc1a0b8e3067ce9b6abf76f.jpg' width=16px height=16px>  **编程练习**\n",
    "\n",
    "要求：把二维数组 [[1, 2, 3],[4, 5, 6],[7,8,9]] 转为 ndarray 格式。判断是否全部大于 0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请编写你的答案\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])\n",
    "print(np.all(a > 0))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `IO` 操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`savetxt` 可以将数组写入文件，默认使用科学计数法的形式保存："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "data = np.array([[1, 2],\n",
    "                 [3, 4]])\n",
    "\n",
    "# 保存文件\n",
    "np.savetxt('out.txt', data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取文件\n",
    "with open('out.txt') as f:\n",
    "    for line in f:\n",
    "        print(line)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取文件\n",
    "np.loadtxt('out.txt')\n"
   ]
  }
 ],
 "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
