{
 "cells": [
  {
   "attachments": {
    "task12%E7%BB%9F%E8%AE%A1%E7%9B%B8%E5%85%B3-%E5%8D%8F%E6%96%B9%E5%B7%AE%E7%9F%A9%E9%98%B5-%E6%95%B0%E5%AD%A6%E5%85%AC%E5%BC%8F.png": {
     "image/png": "[encoded data, 29744 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 统计相关\n",
    "\n",
    "## 次序统计\n",
    "\n",
    "### 计算最小值\n",
    "- `numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])`Return the minimum of an array or minimum along an axis.\n",
    "\n",
    "         \n",
    " 【例】计算最小值\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.amin(x)\n",
    "print(y)  # 11\n",
    "\n",
    "y = np.amin(x, axis=0)\n",
    "print(y)  # [11 12 13 14 15]\n",
    "\n",
    "y = np.amin(x, axis=1)\n",
    "print(y)  # [11 16 21 26 31]\n",
    "```\n",
    "### 计算最大值\n",
    "- `numpy.amax(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])`Return the maximum of an array or maximum along an axis.\n",
    "\n",
    "\n",
    "【例】计算最大值\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.amax(x)\n",
    "print(y)  # 35\n",
    "\n",
    "y = np.amax(x, axis=0)\n",
    "print(y)  # [31 32 33 34 35]\n",
    "\n",
    "y = np.amax(x, axis=1)\n",
    "print(y)  # [15 20 25 30 35]\n",
    "```\n",
    "### 计算极差\n",
    "- `numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue)` Range of values (maximum - minimum) along an axis. The name of the function comes from the acronym for 'peak to peak'.\n",
    "\n",
    "【例】计算极差\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(20200623)\n",
    "x = np.random.randint(0, 20, size=[4, 5])\n",
    "print(x)\n",
    "# [[10  2  1  1 16]\n",
    "#  [18 11 10 14 10]\n",
    "#  [11  1  9 18  8]\n",
    "#  [16  2  0 15 16]]\n",
    "\n",
    "print(np.ptp(x))  # 18\n",
    "print(np.ptp(x, axis=0))  # [ 8 10 10 17  8]\n",
    "print(np.ptp(x, axis=1))  # [15  8 17 16]\n",
    "```\n",
    "### 计算分位数\n",
    "\n",
    "- `numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)` Compute the q-th percentile of the data along the specified axis. Returns the q-th percentile(s) of the array elements.\n",
    "    - a：array，用来算分位数的对象，可以是多维的数组。\n",
    "    - q：介于0-100的float，用来计算是几分位的参数，如四分之一位就是25，如要算两个位置的数就[25,75]。\n",
    "    - axis：坐标轴的方向，一维的就不用考虑了，多维的就用这个调整计算的维度方向，取值范围0/1。\n",
    "\n",
    "\n",
    "【例】计算分位数\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(20200623)\n",
    "x = np.random.randint(0, 20, size=[4, 5])\n",
    "print(x)\n",
    "# [[10  2  1  1 16]\n",
    "#  [18 11 10 14 10]\n",
    "#  [11  1  9 18  8]\n",
    "#  [16  2  0 15 16]]\n",
    "\n",
    "print(np.percentile(x, [25, 50]))  \n",
    "# [ 2. 10.]\n",
    "\n",
    "print(np.percentile(x, [25, 50], axis=0))\n",
    "# [[10.75  1.75  0.75 10.75  9.5 ]\n",
    "#  [13.5   2.    5.   14.5  13.  ]]\n",
    "\n",
    "print(np.percentile(x, [25, 50], axis=1))\n",
    "# [[ 1. 10.  8.  2.]\n",
    "#  [ 2. 11.  9. 15.]]\n",
    "```\n",
    "\n",
    "\n",
    "---\n",
    "## 均值与方差\n",
    "\n",
    "### 计算中位数\n",
    "- `numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)` Compute the median along the specified axis. Returns the median of the array elements.\n",
    "\n",
    "【例】计算中位数\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(20200623)\n",
    "x = np.random.randint(0, 20, size=[4, 5])\n",
    "print(x)\n",
    "# [[10  2  1  1 16]\n",
    "#  [18 11 10 14 10]\n",
    "#  [11  1  9 18  8]\n",
    "#  [16  2  0 15 16]]\n",
    "print(np.percentile(x, 50))\n",
    "print(np.median(x))\n",
    "# 10.0\n",
    "\n",
    "print(np.percentile(x, 50, axis=0))\n",
    "print(np.median(x, axis=0))\n",
    "# [13.5  2.   5.  14.5 13. ]\n",
    "\n",
    "print(np.percentile(x, 50, axis=1))\n",
    "print(np.median(x, axis=1))\n",
    "# [ 2. 11.  9. 15.]\n",
    "```\n",
    "\n",
    "### 计算平均值\n",
    "\n",
    "- `numpy.mean(a[, axis=None, dtype=None, out=None, keepdims=np._NoValue)])`Compute the arithmetic mean along the specified axis.\n",
    "\n",
    "【例】计算平均值（沿轴的元素的总和除以元素的数量）。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.mean(x)\n",
    "print(y)  # 23.0\n",
    "\n",
    "y = np.mean(x, axis=0)\n",
    "print(y)  # [21. 22. 23. 24. 25.]\n",
    "\n",
    "y = np.mean(x, axis=1)\n",
    "print(y)  # [13. 18. 23. 28. 33.]\n",
    "```\n",
    "### 计算加权平均值\n",
    "- `numpy.average(a[, axis=None, weights=None, returned=False])`Compute the weighted average along the specified axis.\n",
    "\n",
    "\n",
    "`mean`和`average`都是计算均值的函数，在不指定权重的时候`average`和`mean`是一样的。指定权重后，`average`可以计算加权平均值。\n",
    "\n",
    "【例】计算加权平均值（将各数值乘以相应的权数，然后加总求和得到总体值，再除以总的单位数。）\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.average(x)\n",
    "print(y)  # 23.0\n",
    "\n",
    "y = np.average(x, axis=0)\n",
    "print(y)  # [21. 22. 23. 24. 25.]\n",
    "\n",
    "y = np.average(x, axis=1)\n",
    "print(y)  # [13. 18. 23. 28. 33.]\n",
    "\n",
    "\n",
    "y = np.arange(1, 26).reshape([5, 5])\n",
    "print(y)\n",
    "# [[ 1  2  3  4  5]\n",
    "#  [ 6  7  8  9 10]\n",
    "#  [11 12 13 14 15]\n",
    "#  [16 17 18 19 20]\n",
    "#  [21 22 23 24 25]]\n",
    "\n",
    "z = np.average(x, weights=y)\n",
    "print(z)  # 27.0\n",
    "\n",
    "z = np.average(x, axis=0, weights=y)\n",
    "print(z)\n",
    "# [25.54545455 26.16666667 26.84615385 27.57142857 28.33333333]\n",
    "\n",
    "z = np.average(x, axis=1, weights=y)\n",
    "print(z)\n",
    "# [13.66666667 18.25       23.15384615 28.11111111 33.08695652]\n",
    "```\n",
    "### 计算方差\n",
    "- `numpy.var(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])`Compute the variance along the specified axis.\n",
    "    - ddof=0：是“Delta Degrees of Freedom”，表示自由度的个数。\n",
    "\n",
    "要注意方差和样本方差的无偏估计，方差公式中分母上是`n`；样本方差无偏估计公式中分母上是`n-1`（`n`为样本个数）。\n",
    "\n",
    "[证明的链接](https://blog.csdn.net/SoftPoeter/article/details/78273117?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf) \n",
    "\n",
    "【例】计算方差\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.var(x)\n",
    "print(y)  # 52.0\n",
    "y = np.mean((x - np.mean(x)) ** 2)\n",
    "print(y)  # 52.0\n",
    "\n",
    "y = np.var(x, ddof=1)\n",
    "print(y)  # 54.166666666666664\n",
    "y = np.sum((x - np.mean(x)) ** 2) / (x.size - 1)\n",
    "print(y)  # 54.166666666666664\n",
    "\n",
    "y = np.var(x, axis=0)\n",
    "print(y)  # [50. 50. 50. 50. 50.]\n",
    "\n",
    "y = np.var(x, axis=1)\n",
    "print(y)  # [2. 2. 2. 2. 2.]\n",
    "```\n",
    "### 计算标准差\n",
    "\n",
    "- `numpy.std(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])`Compute the standard deviation along the specified axis.\n",
    "\n",
    "标准差是一组数据平均值分散程度的一种度量，是方差的算术平方根。\n",
    "\n",
    "\n",
    "【例】计算标准差\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.std(x)\n",
    "print(y)  # 7.211102550927978\n",
    "y = np.sqrt(np.var(x))\n",
    "print(y)  # 7.211102550927978\n",
    "\n",
    "y = np.std(x, axis=0)\n",
    "print(y)\n",
    "# [7.07106781 7.07106781 7.07106781 7.07106781 7.07106781]\n",
    "\n",
    "y = np.std(x, axis=1)\n",
    "print(y)\n",
    "# [1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]\n",
    "```\n",
    "\n",
    "---\n",
    "## 相关\n",
    "### 计算协方差矩阵\n",
    "\n",
    "![task12%E7%BB%9F%E8%AE%A1%E7%9B%B8%E5%85%B3-%E5%8D%8F%E6%96%B9%E5%B7%AE%E7%9F%A9%E9%98%B5-%E6%95%B0%E5%AD%A6%E5%85%AC%E5%BC%8F.png](attachment:task12%E7%BB%9F%E8%AE%A1%E7%9B%B8%E5%85%B3-%E5%8D%8F%E6%96%B9%E5%B7%AE%E7%9F%A9%E9%98%B5-%E6%95%B0%E5%AD%A6%E5%85%AC%E5%BC%8F.png)\n",
    "\n",
    "- `numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,aweights=None)` Estimate a covariance matrix, given data and weights.\n",
    "\n",
    "【例】计算协方差矩阵\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = [1, 2, 3, 4, 6]\n",
    "y = [0, 2, 5, 6, 7]\n",
    "print(np.cov(x))  # 3.7   #样本方差\n",
    "print(np.cov(y))  # 8.5   #样本方差\n",
    "print(np.cov(x, y))\n",
    "# [[3.7  5.25]\n",
    "#  [5.25 8.5 ]]\n",
    "\n",
    "print(np.var(x))  # 2.96    #方差\n",
    "print(np.var(x, ddof=1))  # 3.7    #样本方差\n",
    "print(np.var(y))  # 6.8    #方差\n",
    "print(np.var(y, ddof=1))  # 8.5    #样本方差\n",
    "\n",
    "z = np.mean((x - np.mean(x)) * (y - np.mean(y)))    #协方差\n",
    "print(z)  # 4.2\n",
    "\n",
    "z = np.sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1)   #样本协方差\n",
    "print(z)  # 5.25\n",
    "\n",
    "z = np.dot(x - np.mean(x), y - np.mean(y)) / (len(x) - 1)     #样本协方差     \n",
    "print(z)  # 5.25\n",
    "```\n",
    "\n",
    "### 计算相关系数\n",
    "- `numpy.corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue)` Return Pearson product-moment correlation coefficients.\n",
    "\n",
    "理解了`np.cov()`函数之后，很容易理解`np.correlate()`，二者参数几乎一模一样。\n",
    "\n",
    "`np.cov()`描述的是两个向量协同变化的程度，它的取值可能非常大，也可能非常小，这就导致没法直观地衡量二者协同变化的程度。相关系数实际上是正则化的协方差，`n`个变量的相关系数形成一个`n`维方阵。\n",
    "\n",
    "【例】计算相关系数\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(20200623)\n",
    "x, y = np.random.randint(0, 20, size=(2, 4))\n",
    "\n",
    "print(x)  # [10  2  1  1]\n",
    "print(y)  # [16 18 11 10]\n",
    "\n",
    "z = np.corrcoef(x, y)\n",
    "print(z)\n",
    "# [[1.         0.48510096]\n",
    "#  [0.48510096 1.        ]]\n",
    "\n",
    "a = np.dot(x - np.mean(x), y - np.mean(y))\n",
    "b = np.sqrt(np.dot(x - np.mean(x), x - np.mean(x)))\n",
    "c = np.sqrt(np.dot(y - np.mean(y), y - np.mean(y)))\n",
    "print(a / (b * c))  # 0.4851009629263671\n",
    "```\n",
    "\n",
    "---\n",
    "## 直方图\n",
    "\n",
    "- `numpy.digitize(x, bins, right=False)`Return the indices of the bins to which each value in input array belongs.\n",
    "    - x：numpy数组\n",
    "    - bins：一维单调数组，必须是升序或者降序\n",
    "    - right：间隔是否包含最右\n",
    "    - 返回值：x在bins中的位置。\n",
    "\n",
    "【例】\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([0.2, 6.4, 3.0, 1.6])\n",
    "bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])\n",
    "inds = np.digitize(x, bins)\n",
    "print(inds)  # [1 4 3 2]\n",
    "for n in range(x.size):\n",
    "    print(bins[inds[n] - 1], \"<=\", x[n], \"<\", bins[inds[n]])\n",
    "\n",
    "# 0.0 <= 0.2 < 1.0\n",
    "# 4.0 <= 6.4 < 10.0\n",
    "# 2.5 <= 3.0 < 4.0\n",
    "# 1.0 <= 1.6 < 2.5\n",
    "```\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1.2, 10.0, 12.4, 15.5, 20.])\n",
    "bins = np.array([0, 5, 10, 15, 20])\n",
    "inds = np.digitize(x, bins, right=True)\n",
    "print(inds)  # [1 2 3 4 4]\n",
    "\n",
    "inds = np.digitize(x, bins, right=False)\n",
    "print(inds)  # [1 3 3 4 5]\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python35",
   "language": "python",
   "name": "python35"
  },
  "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.6.10"
  },
  "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": "279.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
}
