{"id":1519,"date":"2019-08-30T10:27:46","date_gmt":"2019-08-30T02:27:46","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=1519"},"modified":"2019-08-30T10:27:46","modified_gmt":"2019-08-30T02:27:46","slug":"%e3%80%90%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e3%80%91%e9%80%bb%e8%be%91%e5%9b%9e%e5%bd%92","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/machine-learning-in-action\/1519\/","title":{"rendered":"\u3010\u5434\u6069\u8fbe\u673a\u5668\u5b66\u4e60\u3011\u903b\u8f91\u56de\u5f52"},"content":{"rendered":"\n<p> \u5434\u6069\u8fbeMachine-Learning  \u7b2c\u4e09\u5468\uff1a\u903b\u8f91\u56de\u5f52\uff08logistic regression\uff09 <\/p>\n\n\n<p>\u903b\u8f91\u56de\u5f52\u548c\u7ebf\u6027\u56de\u5f52\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u4e00\u4e2a\u4f7f\u7528\u5e73\u65b9\u4ee3\u4ef7\u51fd\u6570\u540c\u6765\u9884\u6d4b\u503c\uff0c\u4e00\u4e2a\u4f7f\u7528log\u4ee3\u4ef7\u51fd\u6570\u7528\u6765\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n\n\n<p>\u9996\u5148\u4e00\u6837\u662f\u5bfc\u5305\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import numpy\nimport pandas\nimport matplotlib.pyplot as plt\nimport scipy.optimize as opt\nfrom sklearn.metrics import classification_report<\/code><\/pre>\n\n\n<p>\u7136\u540e\u662f\u753b\u56fe\u67e5\u770b\u539f\u59cb\u6570\u636e\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>data = pandas.read_csv('.\/data\/ex2data1.txt', header=None, names=['exam1', 'exam2', 'admitted'])\n# \u67e5\u770b\u6570\u636e\u6837\u5f0f\nfig, ax = plt.subplots()\npositive = data[data.admitted.isin(['1'])]  # 1\nnegative = data[data.admitted.isin(['0'])]  # 0\nax.scatter(positive['exam1'], positive['exam2'], c='b', marker='v', label='Admitted')\nax.scatter(negative['exam1'], negative['exam2'], c='r', marker='x', label='Not Admitted')\n# \u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u540d\nax.set_xlabel('Exam 1 Score')\nax.set_ylabel('Exam 2 Score')\nplt.show()<\/code><\/pre>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"443\" height=\"333\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-56.png\" alt=\"\" class=\"wp-image-1520\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-56.png 443w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-56-300x226.png 300w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/figure><\/div>\n\n\n<p>\u7136\u540e\u662fsigmoid\u548c\u4ee3\u4ef7\u8ba1\u7b97\u51fd\u6570\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def sigmoid(z):\n    \"\"\"\n    \u5b9a\u4e49sigmoid\u51fd\u6570\n    :param z: \u539f\u51fd\u6570\n    :return: \u53d8\u6362\u540e\u7684\u51fd\u6570\n    \"\"\"\n    return 1.0 \/ (1 + numpy.exp(-z))\ndef computeCost(theta, X, y):\n    \"\"\"\n    \u8ba1\u7b97\u4ee3\u4ef7\n    :param X: \u8f93\u5165\u6570\u636e\n    :param y:\u76ee\u6807\u6570\u636e\n    :param theta: theta\n    :return:\u8ba1\u7b97\u540e\u7684\u4ee3\u4ef7\n    \"\"\"\n    # X.T@X\u7b49\u4ef7\u4e8eX.T.dot(X)\n    return numpy.mean(-y * numpy.log(sigmoid(X @ theta)) - (1 - y) * numpy.log(1 - sigmoid(X @ theta)))\n<\/code><\/pre>\n\n\n<p>\u7136\u540e\u662f\u6807\u51c6\u5316\u6570\u636e\uff0c\u589e\u52a0Ones\u5217\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u63d2\u5165\u4e00\u884c\u6570\u636e\uff0c\u65b9\u4fbf\u8fd0\u7b97\ndata.insert(0, 'Ones', 1)\n# \u8f93\u5165\u3001\u76ee\u6807\u5411\u91cf\u521d\u59cb\u5316\uff0c\u8fd9\u91cc\u6ca1\u6709\u50cf\u4e4b\u524d\u4e00\u6837\u8f6c\u6362\u6210\u77e9\u9635\uff0c\u56e0\u4e3a\u68af\u5ea6\u4e0b\u964d\u53ef\u4ee5\u5411\u91cf\u5316\u8ba1\u7b97\nX = data.iloc[:, :-1].values\ny = data.iloc[:, -1].values\ntheta = numpy.zeros(X.shape[1])\n# \u8ba1\u7b97\u4ee3\u4ef7 (0.6931471805599453\nprint(computeCost(theta, X, y))\n<\/code><\/pre>\n\n\n<p>\u68af\u5ea6\u4e0b\u964d\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def gradientDescent(theta, X, y):\n    \"\"\"\n    \u68af\u5ea6\u4e0b\u964d\u8ba1\u7b97\n    :param X: \u8f93\u5165\u5411\u91cf\n    :param y: \u76ee\u6807\u5411\u91cf\n    :param theta: theta\n    :return: theta\n    \"\"\"\n    for i in range(10000):\n        theta = theta - 0.01 * (X.T @ (sigmoid(X @ theta) - y)) \/ len(X)\n    return theta\n\"\"\"\n\u8fd9\u7684\u5751\u4e5f\u592a\u5927\u4e86\uff0c\u7f51\u4e0a\u5f88\u591a\u7248\u672c\uff08\u9ec4\u535agithub\u91cc\u7684\u4e5f\u662f\uff0c \u90fd\u662f\u5728\u68af\u5ea6\u4e0b\u964d\u91cc\u4e0d\u4f7f\u7528\u5faa\u73af\uff0c\u53ea\u6709\u4e00\u6b21\n\u8fd9\u91cc\u4ed6\u4eec\u53ea\u662f\u6f14\u793a\u68af\u5ea6\u4e0b\u964d\u8fc7\u7a0b\uff0c\u5b9e\u9645\u4e0a\u662f\u9700\u8981\u5faa\u73af\u7684\n\u4ed6\u4eec\u540e\u9762\u7684\u6d4b\u8bd5\u548c\u7ed8\u56fe\u90fd\u662f\u4f7f\u7528\u540e\u9762\u7684scipy\u65b9\u6cd5\u6240\u8ba1\u7b97\u51fa\u7684\u6570\u636e\u6765\u8ba1\u7b97\uff0c\u56e0\u6b64\u8fd9\u91cc\u4e0d\u5faa\u73af\u8fd4\u56de\u7684\u201c\u52a3\u8d28\u201d\u7ed3\u679c\u5bf9\u540e\u9762\u6ca1\u5f71\u54cd\n\u540c\u65f6\uff0c\u68af\u5ea6\u4e0b\u964d\u7684\u5faa\u73af\u8c03\u7528\u7531scipy\u8fdb\u884c\uff0c\u4e0d\u9700\u8981\u5728\u65b9\u6cd5\u5185\u8fdb\u884c\u5faa\u73af\n\"\"\"\n# [-7.65900397  0.41024768 -0.05324509]\nprint(gradientDescent(theta, X, y))\ndef gradient(theta, X, y):\n    \"\"\"\n    \u8fd9\u4e2a\u662f\u6ca1\u6709\u5faa\u73af\u7684\u7248\u672c\uff0c\u7531scipy\u6765\u8c03\u7528\n    :param theta:\n    :param X:\n    :param y:\n    :return:\n    \"\"\"\n    return (1 \/ len(X)) * X.T @ (sigmoid(X @ theta) - y)<\/code><\/pre>\n\n\n<p>\u4f7f\u7528sklearn\u6765\u8ba1\u7b97\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528sklearn\u8ba1\u7b97\n\"\"\"\na) liblinear\uff1a\u4f7f\u7528\u4e86\u5f00\u6e90\u7684liblinear\u5e93\u5b9e\u73b0\uff0c\u5185\u90e8\u4f7f\u7528\u4e86\u5750\u6807\u8f74\u4e0b\u964d\u6cd5\u6765\u8fed\u4ee3\u4f18\u5316\u635f\u5931\u51fd\u6570\u3002\nb) lbfgs\uff1a\u62df\u725b\u987f\u6cd5\u7684\u4e00\u79cd\uff0c\u5229\u7528\u635f\u5931\u51fd\u6570\u4e8c\u9636\u5bfc\u6570\u77e9\u9635\u5373\u6d77\u68ee\u77e9\u9635\u6765\u8fed\u4ee3\u4f18\u5316\u635f\u5931\u51fd\u6570\u3002\nc) newton-cg\uff1a\u4e5f\u662f\u725b\u987f\u6cd5\u5bb6\u65cf\u7684\u4e00\u79cd\uff0c\u5229\u7528\u635f\u5931\u51fd\u6570\u4e8c\u9636\u5bfc\u6570\u77e9\u9635\u5373\u6d77\u68ee\u77e9\u9635\u6765\u8fed\u4ee3\u4f18\u5316\u635f\u5931\u51fd\u6570\u3002\nd) sag\uff1a\u5373\u968f\u673a\u5e73\u5747\u68af\u5ea6\u4e0b\u964d\uff0c\u662f\u68af\u5ea6\u4e0b\u964d\u6cd5\u7684\u53d8\u79cd\uff0c\u548c\u666e\u901a\u68af\u5ea6\u4e0b\u964d\u6cd5\u7684\u533a\u522b\u662f\u6bcf\u6b21\u8fed\u4ee3\u4ec5\u4ec5\u7528\u4e00\u90e8\u5206\u7684\u6837\u672c\u6765\u8ba1\u7b97\u68af\u5ea6\uff0c\n        \u9002\u5408\u4e8e\u6837\u672c\u6570\u636e\u591a\u7684\u65f6\u5019\u3002\n\"\"\"\nprint(numpy.shape(X))\nprint(numpy.shape(y))\nprint(numpy.shape(theta))\nres = opt.minimize(fun=computeCost, x0=theta, args=(X, y), method='Newton-CG', jac=gradient)\n# [-25.16143003,   0.20623248,   0.20147238]\nprint(res)<\/code><\/pre>\n\n\n<p>\u6267\u884c\u9884\u6d4b\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>def predict(x, theta):\n    \"\"\"\n    \u9884\u6d4b\u51fd\u6570\n    :param x:\n    :param theta:\n    :return:\n    \"\"\"\n    prob = sigmoid(x @ theta)\n    return (prob >= 0.5).astype(int)\n# \u67e5\u770b\u8ba1\u7b97\u51fa\u7684\u53c2\u6570\u5728\u8bad\u7ec3\u96c6\u4e0a\u7684\u8868\u73b0\nfinal_theta = res.x\ny_pred = predict(X, final_theta)\n\"\"\"\n                precision    recall  f1-score   support\n           0       0.87      0.85      0.86        40\n           1       0.90      0.92      0.91        60\n    accuracy                           0.89       100\n   macro avg       0.89      0.88      0.88       100\n\"\"\"\nprint(classification_report(y, y_pred))<\/code><\/pre>\n\n\n<p>\u5bfb\u627e\u51b3\u7b56\u8fb9\u754c\u4e0e\u753b\u56fe\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code># \u5bfb\u627e\u51b3\u7b56\u8fb9\u754c\n\"\"\"\n\u8fd9\u91cc\u8fd9\u4e48\u64cd\u4f5c\u662f\u5c06\u8ba1\u7b97\u51fa\u7684\u53c2\u6570\u8f6c\u5316\u6210y=ax+b\u7684\u683c\u5f0f\uff0c\u5c06\u6700\u540e\u4e00\u4f4d\u8bbe\u6cd5\u53d6\u6210-1\uff0c\u518d\u79fb\u5230\u7b49\u5f0f\u7684\u53e6\u4e00\u4fa7\u5373\u4e3ay=ax+b \u683c\u5f0f\n\"\"\"\ncoef = -(res.x \/ res.x[2])\nprint(coef)\nx = numpy.linspace(data['exam1'].min(), data['exam1'].max(), 100)\ny = coef[0] + coef[1] * x\nfig, ax = plt.subplots()\npositive = data[data.admitted.isin(['1'])]  # 1\nnegative = data[data.admitted.isin(['0'])]  # 0\nax.scatter(positive['exam1'], positive['exam2'], c='b', marker='v', label='Admitted')\nax.scatter(negative['exam1'], negative['exam2'], c='r', marker='x', label='Not Admitted')\n# \u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u540d\nax.set_xlabel('Exam 1 Score')\nax.set_ylabel('Exam 2 Score')\n# \u7ed8\u5236\u5206\u5272\u7ebf\nplt.plot(x, y, 'r')\nplt.legend(loc=2)\nplt.show()<\/code><\/pre>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"438\" height=\"334\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-57.png\" alt=\"\" class=\"wp-image-1521\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-57.png 438w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2019\/08\/\u56fe\u7247-57-300x229.png 300w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5434\u6069\u8fbeMachine-Learning \u7b2c\u4e09\u5468\uff1a\u903b\u8f91\u56de\u5f52\uff08logistic 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":4712,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1519"}],"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=1519"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1519\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1519"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}