{
 "cells": [
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 59488 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## y=X β\n",
    "对于简单线性回归，向量计法等同于\n",
    "![image.png](attachment:image.png)\n",
    "给定X跟y我们可以使用 NumPy 库解出β值，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-05T06:01:41.779628Z",
     "start_time": "2020-09-05T06:01:41.773643Z"
    }
   },
   "outputs": [],
   "source": [
    "from numpy . linalg import inv \n",
    "from numpy import dot, transpose \n",
    "X = [[1, 6, 2] , [1, 8, 1] , [1, 10, 0] , [1 , 14, 2] , [1, 18, 0]] \n",
    "y = [[7] , [9] , [13] , [17.5] , [18]] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-05T06:02:12.854939Z",
     "start_time": "2020-09-05T06:02:12.850949Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0]]\n",
      "[[7], [9], [13], [17.5], [18]]\n"
     ]
    }
   ],
   "source": [
    "print(X)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-05T06:02:47.486171Z",
     "start_time": "2020-09-05T06:02:47.416356Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.1875    ]\n",
      " [1.01041667]\n",
      " [0.39583333]]\n"
     ]
    }
   ],
   "source": [
    "print(dot(inv(dot(transpose(X) , X)) , dot(transpose(X) , y))) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-05T06:03:38.129940Z",
     "start_time": "2020-09-05T06:03:38.124978Z"
    }
   },
   "source": [
    "NumPy 库也提供了一个最小二乘函数， 它能被用来更简洁地解出参数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-05T06:04:55.522309Z",
     "start_time": "2020-09-05T06:04:55.433378Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.1875    ]\n",
      " [1.01041667]\n",
      " [0.39583333]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\dev\\anaconda\\envs\\python35\\lib\\site-packages\\ipykernel_launcher.py:2: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.\n",
      "To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "from numpy.linalg import lstsq\n",
    "print(lstsq(X,y)[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:14:01.752078Z",
     "start_time": "2020-09-06T03:14:01.747126Z"
    }
   },
   "source": [
    "\n",
    "**计算给定数组中每行的最大值。**\n",
    "\n",
    "- `a = np.random.randint(1, 10, [5, 3])`\n",
    "\n",
    "【知识点：统计相关】\n",
    "- 如何在二维numpy数组的每一行中找到最大值？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-09-06T03:14:22.429480Z",
     "start_time": "2020-09-06T03:14:22.424495Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 9 4]\n",
      " [8 8 1]\n",
      " [5 3 6]\n",
      " [3 3 3]\n",
      " [2 1 9]]\n",
      "[9 8 6 3 9]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(100)\n",
    "a = np.random.randint(1, 10, [5, 3])\n",
    "print(a)\n",
    "# [[9 9 4]\n",
    "#  [8 8 1]\n",
    "#  [5 3 6]\n",
    "#  [3 3 3]\n",
    "#  [2 1 9]]\n",
    "\n",
    "b = np.amax(a, axis=1)\n",
    "print(b)\n",
    "# [9 8 6 3 9]"
   ]
  },
  {
   "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": false
  },
  "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
}
