{
 "cells": [
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 127940 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.1.1 多维数组对象ndarray\n",
    "&emsp;&emsp;NumPy的核心是ndarray对象。ndarray对象是封装了相同数据类型的n维数组，可以表示一维数组、二维数组甚至多维数组。访问ndarray对象的属性值，可以获取数组的维数、形状、元素个数、数据类型等数据，narray对象的常用属性如下表4-1-1所示。\n",
    "![image.png](attachment:image.png)\n",
    "### 1. 创建多维数组对象\n",
    "#### （1）一维数组对象的创建 \n",
    "* 调用array()函数创建一维数组\n",
    "\n",
    "&emsp;&emsp;使用NumPy中的array()函数可以基于Python列表创建一维数组，其格式如下所示：\n",
    "<center>numpy.array(object, dtype=None, ndmin=0)</center>\n",
    "&emsp;&emsp;其中，object表示可被转换成数组的其它数据对象，例如列表；dtype表示数组所需的数据类型；ndmin为指定生成数组的维数。\n",
    "\n",
    "&emsp;&emsp;**例4-1-1** 创建一个名为persons的一维数组对象，存储'宋江'、'吴用'、'林冲'和'秦明'这4位水浒传英雄的名字。查看persons对象的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "persons=np.array(['宋江','吴用','林冲','秦明'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['宋江', '吴用', '林冲', '秦明'], dtype='<U2')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons.ndim #查看persons对象的维度数目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons.size #查看persons对象的尺寸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('<U2')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons.dtype #查看persons对象的数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 调用arange ()函数创建一维数组\n",
    "\n",
    "&emsp;&emsp;使用NumPy中的arange ()函数可以生成数组，其格式如下所示：\n",
    "<center>numpy.arange([start,] stop[, step,],dtype=None)</center>\n",
    "&emsp;&emsp;与Python中内置函数range()用法类似，numpy.arange()函数的参数start表示开始的数字，stop表示结束的数字，但不包括stop，step表示步长的数字。\n",
    "\n",
    "&emsp;&emsp;**例4-1-2**  示例使用arange函数生成一维数组对象，查看对象的数据类型，注意与range函数不同，arange函数支持float数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "num1=np.arange(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num1.dtype  #查看num1对象的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "num2=np.arange(1.0,10.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num2.dtype  #查看num2对象的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5., 6., 7., 8., 9.])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 调用linspace()函数创建一维数组\n",
    "\n",
    "&emsp;&emsp;使用NumPy中的linspace()函数可以创建等间隔一维数组，它的常用格式如下所示：\n",
    "<center>numpy.linspace(start,stop,num=50,…)</center>\n",
    "&emsp;&emsp;其中，start代表起始的值；表示结束的值；num表示在这个区间里生成数字的个数，生成的数组是等间隔生成的。start和stop这两个数字可以是整数或者浮点数。注意区分stop参数在numpy.linspace()与numpy.arange()函数用法上的差异，默认情况下，生成的数组元素包括stop。\n",
    "\n",
    "&emsp;&emsp;**例4-1-3**  示例使用linspace()函数创建三个等间隔的一维数组对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  4.,  7., 10.])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1. ,  2.8,  4.6,  6.4,  8.2, 10. ])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.5       ,  4.35714286,  6.21428571,  8.07142857,  9.92857143,\n",
       "       11.78571429, 13.64285714, 15.5       ])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(2.5,15.5,8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （2）二维数组对象的创建\n",
    "* 调用array()函数创建二维数组\n",
    "\n",
    "&emsp;&emsp;NumPy中的array()函数也可以基于Python的嵌套列表创建二维数组。\n",
    "\n",
    "&emsp;&emsp;**例4-1-4** 创建一个记录三位同学语、数、英三门课程的二维NumPy数组对象，查看数组对象的属性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores=np.array([[96,73,78],[90,89,92],[60,70,83]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[96, 73, 78],\n",
       "       [90, 89, 92],\n",
       "       [60, 70, 83]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.ndim #查看scores对象的维度数目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.size #查看scores对象的尺寸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.shape #查看scores对象的数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores.dtype #查看scores对象的数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （3）创建多维数组的常用方法\n",
    "&emsp;&emsp;Numpy库还提供了一些数组创建函数，以满足不同维度，不同需求。\n",
    "\n",
    "* 调用reshape()函数创建多维数组\n",
    "\n",
    "&emsp;&emsp;ndarray对象的reshape()函数用于将一维数组转换为指定的多维数组，其格式如下所示：\n",
    "\n",
    "&emsp;&emsp;ndarray.reshape(newshape,...)\n",
    "\n",
    "&emsp;&emsp;其中newshape表示新数组的形状，newshape的类型为整数或者整数元组。\n",
    "\n",
    "&emsp;&emsp;reshape()函数经常与arange()函数结合使用，生成任意n维数组。\n",
    "\n",
    "&emsp;&emsp;**例4-1-5** 创建一个形状为2×3×4的三维数组对象num_reshape。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "num=np.arange(0,24,1) #创建0到23的步长为1的一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_reshape=num.reshape((2,3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[12, 13, 14, 15],\n",
       "        [16, 17, 18, 19],\n",
       "        [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_reshape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 调用zeros()函数创建多维数组\n",
    "\n",
    "&emsp;&emsp;NumPy中的zeros()函数用于生成指定形状的全0数组，其格式如下所示：\n",
    "\n",
    "&emsp;&emsp;numpy.zeros(shape,…)\n",
    "\n",
    "&emsp;&emsp;其中，shape表示新的数组形状，shape的类型为整数或者整数元组。\n",
    "\n",
    "&emsp;&emsp;**例4-1-6** 创建一个3×2，元素都为0的二维数组对象num。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "num=np.zeros((3,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0.],\n",
       "       [0., 0.],\n",
       "       [0., 0.]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 调用ones()函数创建多维数组\n",
    "\n",
    "&emsp;&emsp;NumPy中的ones()函数用于生成指定形状的全1数组，其格式如下所示：\n",
    "\n",
    "&emsp;&emsp;numpy.ones(shape,…)\n",
    "\n",
    "&emsp;&emsp;其中，shape表示新的数组形状，shape的类型为整数或者整数元组。\n",
    "\n",
    "&emsp;&emsp;**例4-1-7** 创建一个3×2，元素都为1的二维数组对象num。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "num=np.ones((3,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 调用eye()函数创建多维数组\n",
    "\n",
    "&emsp;&emsp;NumPy中的eye()函数用于生成指定维数的单位矩阵。所谓单位矩阵是指对角线元素都为1，其他元素都为0的方阵。由于在数学中单位矩阵一般用“I”表示，因此NumPy中采用了同音的eye单词作为函数名称。eye()函数格式如下所示：\n",
    "\n",
    "&emsp;&emsp;numpy.eye(N, M,…)\n",
    "\n",
    "&emsp;&emsp;其中，N表示输出数组的行数，M表示输出数组的列数。\n",
    "\n",
    "&emsp;&emsp;**例4-1-8** 创建一个维度为3的单位矩阵数组对象num。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "num=np.eye(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 109896 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 调用随机函数创建多维数组\n",
    "\n",
    "&emsp;&emsp;表4-1-2提供了NumPy中常用随机函数。其中，size可以是一个整数，定义一维数组的长度，也可以是一个元组，定义一个二维数组的形状。\n",
    "\n",
    "![image.png](attachment:image.png)\n",
    "&emsp;&emsp;**例4-1-9** 使用随机函数创建数组示例。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#创建一个2行、2列的[0,1)之间的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1=np.random.rand(2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.97450436, 0.51136692],\n",
       "       [0.42350645, 0.86004515]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#创建一个2行、3列的[0,10)之间的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2=np.random.randint(0,10,(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 8, 2],\n",
       "       [6, 2, 1]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#创建一个3行、4列的[1,2)之间的服从均匀分布的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3=np.random.uniform(1,2,(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.79499501, 1.80873949, 1.69761431, 1.09904931],\n",
       "       [1.1572993 , 1.84023701, 1.62368693, 1.75183113],\n",
       "       [1.3513896 , 1.99152521, 1.42342451, 1.21964518]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#创建一个2行、3列的服从标准正态分布的随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr4=np.random.normal(0,1,(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.32936634,  0.19723827,  2.01196735],\n",
       "       [-0.53106596, -0.48528362,  0.14563632]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 数组元素的访问\n",
    "#### （1）通过索引方式进行查询\n",
    "\n",
    "&emsp;&emsp;每一个数组元素都是由一个整数值或整数元组标识，称为索引或下标。通过索引可以获取对应的数组元素。可以一次访问一个数组元素，也可以通过行、列索引值列表访问多个数组元素。\n",
    "\n",
    "&emsp;&emsp;**例4-1-10** 访问一维数组对象persons和persons中的数组元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['宋江', '吴用', '林冲', '秦明'], dtype='<U2')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons  #显示persons对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'林冲'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons[2]    #取索引值为2的元素，得到字符串对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['宋江', '林冲'], dtype='<U2')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons[[0,2]]  #取索引值为0和2的元素，得到数组对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;**例4-1-11** 访问二维数组scores和和scores中的数组元素。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[96, 73, 78],\n",
       "       [90, 89, 92],\n",
       "       [60, 70, 83]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores   #显示scores对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[1,0]   #取索引值为（1，0）的数组元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([96, 78])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[[0],[0,2]]  #取索引值为（0，0），（0，2）的数组元素，返回数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([96, 92])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[[0,1],[0,2]]  #取索引值为（0，0），（1，2）的数组元素，返回数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （2）通过切片方式进行查询\n",
    "&emsp;&emsp;**例4-1-12** 切片访问persons中的数组元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['林冲', '秦明'], dtype='<U2')"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons[2:]     #取索引值大于等于2的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['吴用', '林冲'], dtype='<U2')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "persons[1:3]    #取索引值从1到2的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;**例4-1-13** 切片访问scores对象中的数组元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 92],\n",
       "       [70, 83]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[1:,1:]  #取行索引值和列索引值都为大于等于1的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 78],\n",
       "       [89, 92],\n",
       "       [70, 83]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[:,1:3]  #取行列索引值从1到2的所有行的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;**例4-1-14** 同时使用索引值和切片方式访问scores对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[96, 78],\n",
       "       [90, 92],\n",
       "       [60, 83]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[:,[0,2]]  #取列索引值为0和2的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （3）通过布尔运算方式进行查询\n",
    "&emsp;&emsp;ndarry对象可以通过布尔运算方式获取满足布尔表达式的元素。布尔表达式可以由关系运算构造，多个条件使用&（与）、|（或）、~（非）连接。\n",
    "\n",
    "&emsp;&emsp;**例4-1-15** 使用布尔运算式查询scores对象中高于90或者低于80的成绩。\n",
    "\n",
    "&emsp;&emsp;mask是用于筛选的布尔数组，具有与访问数组相同的行数和列数，布尔表达式计算结果为True或False，使用布尔数组就能筛选出符合条件的数组元素。也可以不产生mask数组，在scores数组的方括号中直接使用布尔表达式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[96, 73, 78],\n",
       "       [90, 89, 92],\n",
       "       [60, 70, 83]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask=(scores>90)|(scores<80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True],\n",
       "       [False, False,  True],\n",
       "       [ True,  True, False]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([96, 73, 78, 92, 60, 70])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores[mask]"
   ]
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
