{"id":1396,"date":"2019-08-30T10:09:11","date_gmt":"2019-08-30T02:09:11","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=1396"},"modified":"2019-08-30T10:09:11","modified_gmt":"2019-08-30T02:09:11","slug":"%e3%80%90%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e3%80%91%e7%ba%bf%e6%80%a7%e5%9b%9e%e5%bd%92","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/machine-learning-in-action\/1396\/","title":{"rendered":"\u3010\u673a\u5668\u5b66\u4e60\u3011\u7ebf\u6027\u56de\u5f52"},"content":{"rendered":"\n<p>\u5434\u6069\u8fbeMachine-Learning  \u7b2c\u4e00\u3001\u4e8c\u5468\uff1a\u7ebf\u6027\u56de\u5f52\uff08linear regression\uff09<\/p>\n\n\n<p>\u7ebf\u6027\u56de\u5f52\uff0c\u5c31\u662f\u5728\u6563\u5217\u7684\u6570\u636e\u70b9\u4e2d\u5bfb\u627e\u4e00\u6761\u5206\u5272\u7ebf\uff0c\u4f7f\u8fd9\u6761\u5206\u5272\u7ebf\u5bf9\u4e8e\u8fd9\u7ec4\u6570\u636e\u7684\u5212\u5206\u6700\u5408\u7406\uff0c\u4e5f\u5c31\u662f\u4ee3\u4ef7\u6700\u4f4e\u3002<br \/><\/p>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/05\/\u56fe\u7247-8.png\" alt=\"\" class=\"wp-image-2357\"\/><\/figure>\n\n\n<p>\u8ba1\u7b97\u51fa\u5f53\u524d\u4ee3\u4ef7\u540e\uff0c\u4f7f\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5\u66f4\u65b0theta\u503c\uff0c\u4ee5\u6539\u53d8\u76f4\u7ebf\u7684\u4f4d\u7f6e\u4e5f\u5c31\u662f\u7f29\u5c0f\u4ee3\u4ef7\uff0c\u76f4\u5230\u8fbe\u5230\u533a\u57df\u6700\u4f18\u89e3\uff08\u4f46\u4e0d\u4e00\u5b9a\u662f\u5168\u5c40\u6700\u4f18\u89e3\uff09\u3002<\/p>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/05\/\u56fe\u7247-9.png\" alt=\"\" class=\"wp-image-2359\"\/><\/figure>\n\n\n<p>\u9996\u5148\u5bfc\u5165\u76f8\u5173\u5305\u4f9d\u8d56<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt<\/code><\/pre>\n\n\n<p>\u968f\u540e\u5b9a\u4e49\u6570\u636e\u76ee\u5f55\u548c\u8bfb\u53d6\u6570\u636e<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u4e49\u6570\u636e\u96c6\ndataPath = '.\/data\/ex1data1.txt'\n# names\u6dfb\u52a0\u5217\u540d\uff0cheader\u7528\u6307\u5b9a\u7684\u884c\u6765\u4f5c\u4e3a\u6807\u9898\uff0c\u82e5\u539f\u65e0\u6807\u9898\u4e14\u6307\u5b9a\u6807\u9898\u5219\u8bbe\u4e3aNone\ndata = pd.read_csv(dataPath, header=None, names=[\"Population\", 'Profit'])<\/code><\/pre>\n\n\n<p>\u753b\u56fe\uff0c\u663e\u793a\u6570\u636e\u5206\u5e03\u56fe\u50cf<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u753b\u56fe\ndata.plot(kind='scatter', x='Population', y='Profit', figsize=(8, 5))\n# \u663e\u793a\u56fe\u50cf\nplt.show()<\/code><\/pre>\n\n\n<p>\u6211\u4eec\u770b\u5230\uff0c\u6570\u636e\u56fe\u50cf\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"280\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-49.png\" alt=\"\" class=\"wp-image-1511\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-49.png 446w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-49-300x188.png 300w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/figure><\/div>\n\n\n<p>\u968f\u540e\u83b7\u53d6\u8f93\u5165\u5411\u91cf\u548c\u8f93\u51fa\u5411\u91cf<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u5728\u6570\u636e\u4e2d\u6dfb\u52a0\u4e00\u4e2aOnes\u5217\uff0c\u65b9\u4fbf\u6211\u4eec\u8ba1\u7b97\u4ee3\u4ef7\u548c\u68af\u5ea6\uff0c\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u4e3a\uff0c\u63d2\u5165\u4f4d\u7f6e\u3001\u63d2\u5165\u5217\u540d\u3001\u9ed8\u8ba4\u503c\n# \u8fd9\u6837\u5728\u5207\u5272\u6389\u76ee\u6807\u5411\u91cf\u4e4b\u540e\u8f93\u5165\u5411\u91cf\u4f9d\u7136\u4fdd\u6301\u57282\u5217\uff0c\u65b9\u4fbf\u4e0e \u672a\u77e5\u6570 \u77e9\u9635\u76f8\u4e58\uff08\u56e0\u4e3a\u672a\u77e5\u6570\u67092\u4e2a\uff09\ndata.insert(0, 'Ones', 1)\n# \u83b7\u53d6\u6570\u636e\n# \u83b7\u53d6\u5217\u6570\ncols = data.shape[1]\n# \u53d6\u524dcols-1\u5217\uff0c\u5373\u8f93\u5165\u5411\u91cf\nx = data.iloc[:, 0:cols - 1]\n# \u53d6\u6700\u540e\u4e00\u5217\uff0c\u5373\u76ee\u6807\u5411\u91cf\ny = data.iloc[:, cols - 1:cols]<\/code><\/pre>\n\n\n<p>\u8f6c\u6362\u6570\u636e\u4e0e\u521d\u59cb\u5316<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u5206\u522b\u5c06\u4e24\u7ec4\u6570\u636e\u8f6c\u6362\u6210\u77e9\u9635\u5f62\u5f0f\uff0c\u65b9\u4fbf\u8fd0\u7b97\nx = np.mat(x.values)\ny = np.mat(y.values)\n# \u521d\u59cb\u5316theta(\u8fd9\u4e2a\u503c\u4e3a\u6700\u7ec8\u5206\u5272\u7ebf\u7684 [\u622a\u8ddd\uff0c\u659c\u7387]\ntheta = np.mat([0, 0])<\/code><\/pre>\n\n\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u8ba1\u7b97\u4ee3\u4ef7\uff0c\u8ba1\u7b97\u4ee3\u4ef7\u7684\u65b9\u6cd5\u5982\u4e0b<\/p>\n\n\n<p>\u516c\u5f0f\u4e3a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"276\" height=\"62\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-50.png\" alt=\"\" class=\"wp-image-1512\"\/><\/figure><\/div>\n\n\n<pre class=\"wp-block-code\"><code>def computeCost(x, y, theta):\n    \"\"\"\n    \u8ba1\u7b97\u4ee3\u4ef7\u51fd\u6570\n    :param x: \u8f93\u5165\u5411\u91cf\n    :param y: \u76ee\u6807\u5411\u91cf\n    :param theta: theta\n    :return:\n    \"\"\"\n    inner = np.power(((x * theta.T) - y), 2)\n    return np.sum(inner) \/ (2 * len(x))<\/code><\/pre>\n\n\n<p>\u8c03\u7528computeCost\u65b9\u6cd5<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u8ba1\u7b97\u4ee3\u4ef7\uff0832.072733877455676\uff09\nnowCost = computeCost(x, y, theta)<\/code><\/pre>\n\n\n<p>\u5b9a\u4e49\u5b66\u4e60\u7387\u548c\u8ba1\u7b97\u6b21\u6570<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u5b66\u4e60\u7387\nalpha = 0.01\n# \u8ba1\u7b97\u6b21\u6570\nepoch = 1000<\/code><\/pre>\n\n\n<p>\u7136\u540e\u53ef\u4ee5\u8fdb\u884c\u68af\u5ea6\u4e0b\u964d\u7b97\u6cd5\uff0c\u7b97\u6cd5\u5982\u4e0b<\/p>\n\n\n<p>\u5176\u4e2d\u91cc\u9762\u7684\u516c\u5f0f<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"175\" height=\"54\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-52.png\" alt=\"\" class=\"wp-image-1514\"\/><\/figure><\/div>\n\n\n<pre class=\"wp-block-code\"><code>def gradientDescent(x, y, theta, alpha, epoch):\n    \"\"\"\n    \u68af\u5ea6\u4e0b\u964d\n    :param x: x\u8f74\u6570\u636e\n    :param y: y\u8f74\u6570\u636e\n    :param theta: theta\n    :param alpha: \u5b66\u4e60\u7387\n    :param epoch:\u03b8\n    :return:\n    \"\"\"\n    # \u53c2\u6570 \u03b8\u7684\u6570\u91cf\uff0c\u7528\u6765\u7ed8\u5236\u8bad\u7ec3\u6b21\u6570\u548c\u4ee3\u4ef7\u7684\u5173\u7cfb\n    cost = np.zeros(epoch)\n    # \u8f93\u5165\u6570\u636e\u7684\u5927\u5c0f\n    m = x.shape[0]\n    # \u8fdb\u884cepoch\u6b21\u8fed\u4ee3\n    for i in range(epoch):\n        # \u5229\u7528\u5411\u91cf\u5316\u4e00\u6b65\u6c42\u89e3\n        theta = theta - (alpha \/ m) * (x * theta.T - y).T * x\n        # \u4fdd\u5b58\u5f53\u524d\u7684\u4ee3\u4ef7\u5230\u6570\u7ec4\u4e2d\n        cost[i] = computeCost(x, y, theta)\n    return theta, cost<\/code><\/pre>\n\n\n<p>\u8c03\u7528\u68af\u5ea6\u4e0b\u964d\u6210\u672c\u5e76\u8c03\u7528\uff0c\u8f93\u51fa\u5f53\u524d\u4ee3\u4ef7<\/p>\n\n\n<pre class=\"wp-block-code\"><code>final_theta, cost = gradientDescent(x, y, theta, alpha, epoch)\n# \u663e\u793a\u5f53\u524d\u4ee3\u4ef7 \uff084.515955503078912\nnowCost = computeCost(x, y, final_theta)<\/code><\/pre>\n\n\n<p>\u968f\u540e\u753b\u51fa\u5206\u5272\u7ebf<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u5728data.Population\u7684\u6700\u5927\u6700\u5c0f\u503c\u8303\u56f4\u5185\u8fd4\u56de\u5747\u5300\u95f4\u9694\u7684\u6570\u5b57\uff08100\u4e2a  \u6a2a\u5750\u6807\nx = np.linspace(data.Population.min(), data.Population.max(), 100)  # \u7eb5\u5750\u6807\nf = (final_theta[0, 1] * x) + final_theta[0, 0]\nfig, ax = plt.subplots(figsize=(6, 4))\n# \u7ed8\u5236\u76f4\u7ebf  (\u6a2a\u5750\u6807  \u7eb5\u5750\u6807 \u989c\u8272  \u540d\u5b57\nax.plot(x, f, 'r', label='Prediction')\n# \u7ed8\u5236\u70b9   ( \u6a2a\u5750\u6807  \u7eb5\u5750\u6807  \u540d\u5b57\nax.scatter(data['Population'], data.Profit, label='Traning Data')\n# \u6807\u7b7e\u663e\u793a\u4f4d\u7f6e  2\u8868\u793a\u5728\u5de6\u4e0a\u89d2\nax.legend(loc=2)\n# \u8bbe\u7f6e\u6a2a\u5750\u6807\u540d\u79f0\nax.set_xlabel('Population')\n# \u8bbe\u7f6e\u7eb5\u5750\u6807\u540d\u79f0\nax.set_ylabel('Profit')\n# \u8bbe\u7f6e\u6807\u9898\nax.set_title('Predicted Profit vs. Population Size')\nplt.show()<\/code><\/pre>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"288\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-53.png\" alt=\"\" class=\"wp-image-1515\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-53.png 448w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-53-300x193.png 300w\" sizes=\"(max-width: 448px) 100vw, 448px\" \/><\/figure><\/div>\n\n\n<p>\u753b\u51fa\u4e0d\u540c\u6279\u6b21\u4e0b\u7684\u4ee3\u4ef7\u53d8\u5316<\/p>\n\n\n<pre class=\"wp-block-code\"><code>fig, ax = plt.subplots(figsize=(8, 4))\n# \u7ed8\u5236\u76f4\u7ebf  arange \u548crange\u529f\u80fd\u7c7b\u4f3c \u5747\u5206\u6570\u636e\nax.plot(np.arange(epoch), cost, 'r')\n# \u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u4ee5\u53ca\u56fe\u7247\u540d\u79f0\nax.set_xlabel('Iterations')\nax.set_ylabel('Cost')\nax.set_title('Error vs. Training Epoch')\nplt.show()<\/code><\/pre>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"222\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-54.png\" alt=\"\" class=\"wp-image-1516\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-54.png 453w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-54-300x147.png 300w\" sizes=\"(max-width: 453px) 100vw, 453px\" \/><\/figure><\/div>\n\n\n<p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code># -*- coding:utf-8 -*-\n\"\"\"\n\u7ebf\u6027\u56de\u5f52\uff0c\u6839\u636e\u63d0\u793a\u5b8c\u6210\u3002\n\"\"\"\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n# \u5b9a\u4e49\u6570\u636e\u96c6\ndataPath = '.\/data\/ex1data1.txt'\n# names\u6dfb\u52a0\u5217\u540d\uff0cheader\u7528\u6307\u5b9a\u7684\u884c\u6765\u4f5c\u4e3a\u6807\u9898\uff0c\u82e5\u539f\u65e0\u6807\u9898\u4e14\u6307\u5b9a\u6807\u9898\u5219\u8bbe\u4e3aNone\ndata = pd.read_csv(dataPath, header=None, names=[\"Population\", 'Profit'])\n# \u83b7\u53d6\u6570\u636e\u96c6\u7684\u524dn\u884c\u6570\u636e\uff0c\u9ed8\u8ba4\u4e3a5\n# data.head()\n# \u8ba1\u7b97\u6570\u636e\u96c6\u7684\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u7b49\u5c5e\u6027\u503c\n# data.describe()\n# \u753b\u56fe\n# data.plot(kind='scatter', x='Population', y='Profit', figsize=(8, 5))\n# \u663e\u793a\u56fe\u50cf\n# plt.show()\ndef computeCost(x, y, theta):\n    \"\"\"\n    \u8ba1\u7b97\u4ee3\u4ef7\u51fd\u6570\n    :param x: \u8f93\u5165\u5411\u91cf\n    :param y: \u76ee\u6807\u5411\u91cf\n    :param theta: theta\n    :return:\n    \"\"\"\n    inner = np.power(((x * theta.T) - y), 2)\n    return np.sum(inner) \/ (2 * len(x))\n# \u5728\u6570\u636e\u4e2d\u6dfb\u52a0\u4e00\u4e2aOnes\u5217\uff0c\u65b9\u4fbf\u6211\u4eec\u8ba1\u7b97\u4ee3\u4ef7\u548c\u68af\u5ea6\uff0c\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u4e3a\uff0c\u63d2\u5165\u4f4d\u7f6e\u3001\u63d2\u5165\u5217\u540d\u3001\u9ed8\u8ba4\u503c\n# \u8fd9\u6837\u5728\u5207\u5272\u6389\u76ee\u6807\u5411\u91cf\u4e4b\u540e\u8f93\u5165\u5411\u91cf\u4f9d\u7136\u4fdd\u6301\u57282\u5217\uff0c\u65b9\u4fbf\u4e0e \u672a\u77e5\u6570 \u77e9\u9635\u76f8\u4e58\uff08\u56e0\u4e3a\u672a\u77e5\u6570\u67092\u4e2a\uff09\ndata.insert(0, 'Ones', 1)\n# \u83b7\u53d6\u6570\u636e\n# \u83b7\u53d6\u5217\u6570\ncols = data.shape[1]\n# \u53d6\u524dcols-1\u5217\uff0c\u5373\u8f93\u5165\u5411\u91cf\nx = data.iloc[:, 0:cols - 1]\n# \u53d6\u6700\u540e\u4e00\u5217\uff0c\u5373\u76ee\u6807\u5411\u91cf\ny = data.iloc[:, cols - 1:cols]\n# \u5206\u522b\u5c06\u4e24\u7ec4\u6570\u636e\u8f6c\u6362\u6210\u77e9\u9635\u5f62\u5f0f\uff0c\u65b9\u4fbf\u8fd0\u7b97\nx = np.mat(x.values)\ny = np.mat(y.values)\n# \u521d\u59cb\u5316theta(\u8fd9\u4e2a\u503c\u4e3a\u6700\u7ec8\u5206\u5272\u7ebf\u7684 [\u622a\u8ddd\uff0c\u659c\u7387]\ntheta = np.mat([0, 0])\n# \u8ba1\u7b97\u4ee3\u4ef7\uff0832.072733877455676\uff09\nnowCost = computeCost(x, y, theta)\ndef gradientDescent(x, y, theta, alpha, epoch):\n    \"\"\"\n    \u68af\u5ea6\u4e0b\u964d\n    :param x: x\u8f74\u6570\u636e\n    :param y: y\u8f74\u6570\u636e\n    :param theta: theta\n    :param alpha: \u5b66\u4e60\u7387\n    :param epoch:\u03b8\n    :return:\n    \"\"\"\n    # \u53c2\u6570 \u03b8\u7684\u6570\u91cf\uff0c\u7528\u6765\u7ed8\u5236\u8bad\u7ec3\u6b21\u6570\u548c\u4ee3\u4ef7\u7684\u5173\u7cfb\n    cost = np.zeros(epoch)\n    # \u8f93\u5165\u6570\u636e\u7684\u5927\u5c0f\n    m = x.shape[0]\n    # \u8fdb\u884cepoch\u6b21\u8fed\u4ee3\n    for i in range(epoch):\n        # \u5229\u7528\u5411\u91cf\u5316\u4e00\u6b65\u6c42\u89e3\n        theta = theta - (alpha \/ m) * (x * theta.T - y).T * x\n        # \u4fdd\u5b58\u5f53\u524d\u7684\u4ee3\u4ef7\u5230\u6570\u7ec4\u4e2d\n        cost[i] = computeCost(x, y, theta)\n    return theta, cost\n# \u5b66\u4e60\u7387\nalpha = 0.01\n# \u8ba1\u7b97\u6b21\u6570\nepoch = 1000\nfinal_theta, cost = gradientDescent(x, y, theta, alpha, epoch)\n# \u663e\u793a\u5f53\u524d\u4ee3\u4ef7 \uff084.515955503078912\nnowCost = computeCost(x, y, final_theta)\n# \u5728data.Population\u7684\u6700\u5927\u6700\u5c0f\u503c\u8303\u56f4\u5185\u8fd4\u56de\u5747\u5300\u95f4\u9694\u7684\u6570\u5b57\uff08100\u4e2a   \u6a2a\u5750\u6807\nx = np.linspace(data.Population.min(), data.Population.max(), 100)  # \u7eb5\u5750\u6807\nf = (final_theta[0, 1] * x) + final_theta[0, 0]\nfig, ax = plt.subplots(figsize=(6, 4))\n# \u7ed8\u5236\u76f4\u7ebf  (\u6a2a\u5750\u6807  \u7eb5\u5750\u6807 \u989c\u8272  \u540d\u5b57\nax.plot(x, f, 'r', label='Prediction')\n# \u7ed8\u5236\u70b9   ( \u6a2a\u5750\u6807  \u7eb5\u5750\u6807  \u540d\u5b57\nax.scatter(data['Population'], data.Profit, label='Traning Data')\n# \u6807\u7b7e\u663e\u793a\u4f4d\u7f6e  2\u8868\u793a\u5728\u5de6\u4e0a\u89d2\nax.legend(loc=2)\n# \u8bbe\u7f6e\u6a2a\u5750\u6807\u540d\u79f0\nax.set_xlabel('Population')\n# \u8bbe\u7f6e\u7eb5\u5750\u6807\u540d\u79f0\nax.set_ylabel('Profit')\n# \u8bbe\u7f6e\u6807\u9898\nax.set_title('Predicted Profit vs. Population Size')\nplt.show()\nfig, ax = plt.subplots(figsize=(8, 4))\n# \u7ed8\u5236\u76f4\u7ebf  arange \u548crange\u529f\u80fd\u7c7b\u4f3c \u5747\u5206\u6570\u636e\nax.plot(np.arange(epoch), cost, 'r')\n# \u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u4ee5\u53ca\u56fe\u7247\u540d\u79f0\nax.set_xlabel('Iterations')\nax.set_ylabel('Cost')\nax.set_title('Error vs. Training Epoch')\nplt.show()<\/code><\/pre>\n\n\n<p>\u4e0b\u9762\u662f\u9009\u4fee\u4f5c\u4e1a\uff0c\u591a\u4e86\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u64cd\u4f5c\uff0c\u8fd9\u4e00\u6b65\u662f\u5c06\u5f88\u5927\u7684\u6570\u636e\u6620\u5c04\u5230\u4e00\u4e2a\u5f88\u5c0f\u7684\u8303\u56f4\u5e76\u4e14\u4fdd\u7559\u6570\u636e\u7684\u7279\u5f81\uff0c\u5e76\u4e14\u9632\u6b62\u4e86\u68af\u5ea6\u7206\u70b8<\/p>\n\n\n<pre class=\"wp-block-code\"><code># -*- coding:utf-8 -*-\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\ndata = pd.read_csv(\".\/data\/ex1data2.txt\", names=['Size', 'Bedrooms', 'Price'])\nplt.show()\n# \u8fdb\u884c\u5f52\u4e00\u5316\u7279\u5f81 mean \u83b7\u53d6\u5e73\u5747\u503c  std\u83b7\u53d6\u6807\u51c6\u5dee\n# \u8fd9\u4e00\u6b65\u662f\u5c06\u5f88\u5927\u7684\u6570\u636e\u6620\u5c04\u5230\u4e00\u4e2a\u5f88\u5c0f\u7684\u8303\u56f4\u5e76\u4e14\u4fdd\u7559\u6570\u636e\u7684\u7279\u5f81\uff0c\u9632\u6b62\u4e86\u68af\u5ea6\u7206\u70b8\ndata = (data - data.mean()) \/ data.std()\n# \u9996\u5148\u8fdb\u884c\u6570\u636e\u5212\u5206\uff0c\u589e\u808cOnes\u5217\ndata.insert(0, \"Ones\", 1)\n# \u83b7\u53d6\u5217\u6570\ncols = data.shape[1]\n# \u5206\u5272\u8f93\u5165\u5411\u91cf\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635\ninputVector = data.iloc[:, 0:cols - 1]\ninputVector = np.mat(inputVector)\n# \u83b7\u53d6\u76ee\u6807\u5411\u91cf\u5e76\u8f6c\u6362\u4e3a\u77e9\u9635\naimVector = data.iloc[:, cols - 1:cols]\naimVector = np.mat(aimVector)\n# \u5b9a\u4e49theta\ntheta = np.mat([0, 0, 0])\ndef computeCost(inputVector, aimVector, theta):\n    \"\"\"\n    \u8ba1\u7b97\u4ee3\u4ef7\u51fd\u6570\n    :param inputVector: \u8f93\u5165\u5411\u91cf\n    :param aimVector: \u76ee\u6807\u5411\u91cf\n    :param theta: theta\n    :return:\n    \"\"\"\n    inner = np.power(((inputVector * theta.T) - aimVector), 2)\n    return np.sum(inner) \/ (2 * len(inputVector))\n# \u5f53\u524d\u4ee3\u4ef7 \uff080.48936170212765967\nnowCost = computeCost(inputVector, aimVector, theta)\n# \u5b9a\u4e49\u5b66\u4e60\u7387\nalpha = 0.01\n# \u5b9a\u4e49\u8fed\u4ee3\u6b21\u6570\nepoch = 1000\ndef gradientDescent(inputVector, aimVector, theta, alpha, epoch):\n    # \u4ee3\u4ef7\u6570\u7ec4\uff0c\u8bb0\u5f55\u4ee3\u4ef7\u53d8\u5316\n    cost = np.zeros(epoch)\n    length = len(inputVector)\n    for i in range(epoch):\n        theta = theta - (alpha \/ length) * ((inputVector * theta.T) - aimVector).T * inputVector\n        cost[i] = computeCost(inputVector, aimVector, theta)\n    return theta, cost\nfinalTheta, costList = gradientDescent(inputVector, aimVector, theta, alpha, epoch)\n# \u5f53\u524d\u7684\u4ee3\u4ef7 \uff080.13070336960771892\nnowCost = computeCost(inputVector, aimVector, finalTheta)\n# \u7ed8\u5236\u4ee3\u4ef7\u53d8\u5316\u56fe\nfig, ax = plt.subplots()\nax.plot(np.arange(epoch), costList, 'r')\nax.set_xlabel('Iterations')\nax.set_ylabel('Cost')\nax.set_title('Error vs. Training Epoch')\nplt.show()\n# \u4f7f\u7528\u6b63\u89c4\u65b9\u7a0b\u6765\u8ba1\u7b97theta\ndef normalEqn(X, y):\n    theta = np.linalg.inv(X.T @ X) @ X.T @ y\n    return theta\nfinalTheta2 = normalEqn(inputVector, aimVector)  # \u611f\u89c9\u548c\u6279\u91cf\u68af\u5ea6\u4e0b\u964d\u7684theta\u7684\u503c\u6709\u70b9\u5dee\u8ddd\n<\/code><\/pre>\n\n\n<p>\u5728\u6700\u540e\u8fd8\u9700\u8981\u63d0\u5230\u6807\u51c6\u65b9\u7a0b<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"174\" height=\"42\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-55.png\" alt=\"\" class=\"wp-image-1517\"\/><\/figure><\/div>\n\n\n<p>\u8be5\u65b9\u7a0b\u53ef\u4ee5\u5f97\u5230\u548c\u68af\u5ea6\u4e0b\u964d\u4e00\u6837\u7684\u7ed3\u679c<\/p>\n\n\n<p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u6b63\u89c4\u65b9\u7a0b\u6765\u8ba1\u7b97theta\ndef normalEqn(X, y):\n    theta = np.linalg.inv(X.T @ X) @ X.T @ y\n    return theta<\/code><\/pre>\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4e0d\u4f7f\u7528\u68af\u5ea6\u4e0b\u964d\u800c\u5f97\u5230theta\u503c\uff0c\u4e24\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u5982\u4e0b<\/p>\n\n\n<p>\u6b63\u89c4\u65b9\u7a0b\u4e0d\u9700\u8981\u6307\u5b9a\u5b66\u4e60\u7387\u548c\u8fed\u4ee3\u6b21\u6570\uff0c\u51cf\u5c11\u4e86\u53c2\u6570\u4e5f\u968f\u4e4b\u964d\u4f4e\u4e86\u96be\u5ea6\u3002\u4f46\u662f\u6b63\u89c4\u65b9\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n\u00b3)\uff0c\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u6240\u4ee5\u5728\u9ad8\u6570\u91cf\u7ea7\u7684\u6570\u636e\u96c6\u4e2d\u4f7f\u7528\u6b63\u89c4\u65b9\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5434\u6069\u8fbeMachine-Learning \u7b2c\u4e00\u3001\u4e8c\u5468\uff1a\u7ebf\u6027\u56de\u5f52\uff08linear regression\uff09  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6,10],"tags":[],"views":4915,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1396"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1396"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1396\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1396"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}