{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.1.2 多维数组的运算 \n",
    "### 1. 基本算术运算\n",
    "&emsp;&emsp;Python支持的常见算术运算，如：“+”，“-”，“*”，“/”，“**”等都可以对NumPy的ndarray对象实现整体运算。\n",
    "#### （1）多维数组与标量运算\n",
    "&emsp;&emsp;多维数组运算规则是，同维数组相应位置的两个元素执行运算。不同维的数组运算时，Python使用“广播机制”来处理。单个标量会扩展为与原来数组一样的形状，再逐一运算。\n",
    "\n",
    "&emsp;&emsp;**例4-1-16** 将scores数组中的成绩都加2分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores=np.array([[96,73,78],[90,89,92],[60,70,83]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[96, 73, 78],\n",
       "       [90, 89, 92],\n",
       "       [60, 70, 83]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "scores=scores+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[98, 75, 80],\n",
       "       [92, 91, 94],\n",
       "       [62, 72, 85]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （2）二维数组与一维数组的运算\n",
    "&emsp;&emsp;二维数组与一维数组运算时，两个数组的列数要一致，一维数组将扩展行数，再逐一计算。\n",
    "\n",
    "&emsp;&emsp;**例4-1-17** 创建二维数组对象a和一维数组对象b，相加后赋值给c。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.ones((2,5)) #创建2行5列的全1二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=np.arange(1,6,1) #创建数值为1到5的一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "c=a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2., 3., 4., 5., 6.],\n",
       "       [2., 3., 4., 5., 6.]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 117012 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 函数运算\n",
    "#### （1）通用函数\n",
    "&emsp;&emsp;NumPy中常用的数学函数如表4-1-3所示。\n",
    "![image.png](attachment:image.png)\n",
    "&emsp;&emsp;上述函数中，除了astype()函数的使用方法为“对象名.astype('数据类型')”外，其余函数的使用方法都是“numpy.函数名(参数)”。 \n",
    "\n",
    "&emsp;&emsp;**例4-1-18** 将浮点数类型的一维数组转换为整数类型的一维数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([1.1, 2.8, 3.3, 4.4, 5.3221])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2= arr1.astype('int32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 37544 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （2）聚集函数\n",
    "&emsp;&emsp;NumPy中常用的统计函数如表4-1-5所示。\n",
    "![image.png](attachment:image.png)\n",
    "&emsp;&emsp;NumPy的统计函数的统计含义与内置模块或其他数值计算模块的同名函数相同，不同之处在于增加了axis参数设置轴，统计则沿着轴方向分组进行统计。如果不设置axis参数，对所有数组元素统计。"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 46520 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;在NumPy中，维度(ndim)是由轴(axis)构成的，如图4-1-3所示。一维数组只有一个维度，只有一个水平轴，axis的值只有一个，默认为0。二维数组的维度为2，有行列两个轴，行方向的轴axis=1，列方向的轴axis=0。如果求每行的最大值则是在行方向上求最大值，axis的值为1。如果求每列的最大值，则是在方向上求最大值，axis的值为0。\n",
    "![image.png](attachment:image.png)\n",
    "&emsp;&emsp;**例4-1-19** 求二维数组arr1的最大值、最大值的序号和每行的最大值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1=np.random.randint(1,100,(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[58, 43, 32, 30],\n",
       "       [70,  5, 25, 70],\n",
       "       [81, 75, 69, 94]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "94"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.max()     #数组的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.argmax()  #数组的最大值的序号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([58, 70, 94])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.max(axis=1) #每行的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([81, 75, 69, 94])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.max(axis=0)   #每列的最大值"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 54856 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. NumPy的综合运用\n",
    "&emsp;&emsp;**例4-1-20** 创建一个存储了9个城市的12个月降雨量的NumPy对象rain，降雨量的值由NumPy的随机函数randint()产生，降雨量的范围为0~300毫米。求每个城市的平均降水量和每个城市最大降水量出现的月份。\n",
    "\n",
    "&emsp;&emsp;程序实现代码如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （1）NumPy对象rain的创建\n",
    "&emsp;&emsp;np.random.randint(start,end,size)可以生成取值范围为从start到end-1的数组元素，size是一个整数或一个整组元组，给出数组的形状。通过调用randint()函数完成对rain的创建，生成一个9行12列的数组，表示9个城市，12个月的降雨量。\n",
    "程序运行结果如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[297 270  61 153  42 239  14 139  34  42 222 186]\n",
      " [ 91 292  89 121  15 127 210  94 271 115  24 220]\n",
      " [ 79 165 146 186 101  47  11 288 267  88  55 154]\n",
      " [183  74  31 272  18   8 239 200 203 161 200  23]\n",
      " [229 115 187 211  93  92 117 156 290 145 226  56]\n",
      " [300 115 236 210  31 217 280  89 251 164  43 108]\n",
      " [102 192  81  99  35 228  81 208  72 157 256 142]\n",
      " [195 229  39 200 174 269  33 273 234  72 257  32]\n",
      " [300 298 120  83 140 110   2 168 143 195 197  30]]\n"
     ]
    }
   ],
   "source": [
    "rain=np.random.randint(0,301,(9,12))\n",
    "print(rain)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （2）每个城市平均降水量的计算\n",
    "&emsp;&emsp;使用np.mean()函数，并设置按行方向计算每个城市的降雨量的平均值，同时使用了np.around()函数设置输出精度为浮点数小数点后保留一位小数。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[141.6 139.1 132.2 134.3 159.8 170.3 137.8 167.2 148.8]\n"
     ]
    }
   ],
   "source": [
    "print(np.around(rain.mean(axis=1),decimals=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （3）每个城市最大降水量月份的计算\n",
    "&emsp;&emsp;使用np.argmax()函数取得最大值对应的索引值，由于索引值从0开始，而月份从一月记录，所以做了加1操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  8  4  9  1 11  8  1]\n"
     ]
    }
   ],
   "source": [
    "print(rain.argmax(axis=1)+1)"
   ]
  },
  {
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
