{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:20:09.947636Z",
     "start_time": "2020-09-06T03:20:09.941689Z"
    }
   },
   "source": [
    "**计算两个数组a和数组b之间的欧氏距离。**\n",
    "\n",
    "- `a = np.array([1, 2, 3, 4, 5])`\n",
    "- `b = np.array([4, 5, 6, 7, 8])`\n",
    "\n",
    "【知识点：数学函数、线性代数】\n",
    "- 如何计算两个数组之间的欧式距离？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:20:29.252448Z",
     "start_time": "2020-09-06T03:20:29.247462Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.708203932499369\n",
      "6.708203932499369\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([4, 5, 6, 7, 8])\n",
    "\n",
    "# 方法1\n",
    "d = np.sqrt(np.sum((a - b) ** 2))\n",
    "print(d)  # 6.708203932499369\n",
    "\n",
    "# 方法2\n",
    "d = np.linalg.norm(a - b)\n",
    "print(d)  # 6.708203932499369"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**计算矩阵的行列式和矩阵的逆**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-14T09:14:17.578966Z",
     "start_time": "2020-09-14T09:14:17.574254Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0, 0, 0, 0],\n",
       "       [0, 5, 0, 0, 0],\n",
       "       [0, 0, 5, 0, 0],\n",
       "       [0, 0, 0, 5, 0],\n",
       "       [0, 0, 0, 0, 5]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag([5,5,5,5,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-14T09:14:06.174005Z",
     "start_time": "2020-09-14T09:14:06.171012Z"
    }
   },
   "outputs": [],
   "source": [
    " result=np.diag([5,5,5,5,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-14T09:14:07.806217Z",
     "start_time": "2020-09-14T09:14:07.800758Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3124.999999999999"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.det(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-14T09:16:30.223746Z",
     "start_time": "2020-09-14T09:16:30.217896Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.2, 0. , 0. , 0. , 0. ],\n",
       "       [0. , 0.2, 0. , 0. , 0. ],\n",
       "       [0. , 0. , 0.2, 0. , 0. ],\n",
       "       [0. , 0. , 0. , 0.2, 0. ],\n",
       "       [0. , 0. , 0. , 0. , 0.2]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.inv(result)"
   ]
  },
  {
   "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": {},
   "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
}
