{"id":386,"date":"2017-02-22T17:08:18","date_gmt":"2017-02-22T09:08:18","guid":{"rendered":"http:\/\/www.mrtblog.cn\/?p=386"},"modified":"2023-03-05T15:25:47","modified_gmt":"2023-03-05T07:25:47","slug":"logistic%e5%9b%9e%e5%bd%92%e7%ae%97%e6%b3%95%e5%8f%8a%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"http:\/\/www.mrtblog.cn\/?p=386","title":{"rendered":"Logistic\u56de\u5f52\u7b97\u6cd5\u53ca\u5b9e\u8df5"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,407<\/span><span class='epvc-label'> Views<\/span><\/div><p><em>\u53c2\u8003\u4e66\u7c4d\u300a\u673a\u5668\u5b66\u4e60\u5b9e\u6218\u300b<\/em><\/p>\n<p>\u6211\u4eec\u7684\u751f\u6d3b\u4e2d\u9047\u5230\u5f88\u591a\u6700\u4f18\u5316\u95ee\u9898\uff0c\u6bd4\u5982\u5982\u4f55\u5728\u6700\u77ed\u65f6\u95f4\u4eceA\u5230\u8fbeB\uff1f\u5982\u679c\u6295\u5165\u6700\u5c11\u7684\u5de5\u4f5c\u91cf\u83b7\u5f97\u6700\u5927\u6548\u76ca\uff1f\u7b49\u7b49&#8230;\u73b0\u5728\uff0c\u6211\u4eec\u5047\u8bbe\u6709\u4e00\u4e9b\u6570\u636e\u70b9\uff0c\u6211\u4eec\u7528\u4e00\u6761\u76f4\u7ebf\u5bf9\u8fd9\u4e9b\u70b9\u8fdb\u884c\u62df\u5408\uff0c\u8fd9\u4e2a\u62df\u5408\u7684\u8fc7\u7a0b\u5c31\u79f0\u4e3a\u56de\u5f52\u3002\u5229\u7528Logistic\u56de\u5f52\u8fdb\u884c\u5206\u7c7b\u7684\u4e3b\u8981\u601d\u60f3\u662f\uff1a\u6839\u636e\u73b0\u6709\u6570\u636e\u5bf9\u5206\u7c7b\u8fb9\u754c\u7ebf\u5efa\u7acb\u56de\u5f52\u516c\u5f0f\uff0c\u4f9d\u6b21\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n<p><strong>Logistic<\/strong><strong>\u56de\u5f52\u7684\u4e00\u822c\u8fc7\u7a0b<\/strong><\/p>\n<ul>\n<li>\u6536\u96c6\u6570\u636e\uff1a\u91c7\u7528\u4efb\u610f\u65b9\u6cd5<\/li>\n<li>\u51c6\u5907\u6570\u636e\uff1a\u7531\u4e8e\u9700\u8981\u8fdb\u884c\u8ddd\u79bb\u8ba1\u7b97\uff0c\u56e0\u6b64\u8981\u6c42\u6570\u636e\u7c7b\u578b\u4e3a\u6570\u503c\u578b\u3002\u53e6\u5916\uff0c\u7ed3\u6784\u5316\u6570\u636e\u683c\u5f0f\u6700\u4f73<\/li>\n<li>\u5206\u6790\u6570\u636e\uff1a\u91c7\u7528\u4efb\u610f\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u6790<\/li>\n<li>\u8bad\u7ec3\u7b97\u6cd5\uff1a\u5927\u90e8\u5206\u65f6\u95f4\u5c06\u7528\u4e8e\u8bad\u7ec3\uff0c\u8bad\u7ec3\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u627e\u5230\u6700\u4f73\u7684\u5206\u7c7b\u56de\u5f52\u7cfb\u6570<\/li>\n<li>\u6d4b\u8bd5\u7b97\u6cd5\uff1a\u4e00\u65e6\u8bad\u7ec3\u6b65\u9aa4\u5b8c\u6210\uff0c\u5206\u7c7b\u5c06\u4f1a\u5f88\u5feb<\/li>\n<li>\u4f7f\u7528\u7b97\u6cd5\uff1a\u4f7f\u7528\u7b97\u6cd5\uff1a\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8f93\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u5e76\u5c06\u8fd9\u4e9b\u6570\u503c\u8fdb\u884c\u7b80\u5355\u7684\u56de\u5f52\u8ba1\u7b97\uff0c\u5224\u5b9a\u4ed6\u4eec\u5c5e\u4e8e\u54ea\u4e2a\u7c7b\u522b\uff1b\u5728\u8fd9\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u8f93\u51fa\u7684\u7c7b\u522b\u4e0a\u505a\u4e00\u4e9b\u5176\u4ed6\u7684\u5206\u6790\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<h2>\u57fa\u4e8eLogistic\u56de\u5f52\u548cSigmoid\u51fd\u6570\u7684\u5206\u7c7b<\/h2>\n<p><strong>Logistic<\/strong><strong>\u56de\u5f52<\/strong><\/p>\n<p>\u4f18\u70b9\uff1a\u8ba1\u7b97\u4ee3\u4ef7\u4e0d\u9ad8\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0<br \/>\n\u7f3a\u70b9\uff1a\u5bb9\u6613\u6b20\u62df\u5408\uff0c\u5206\u7c7b\u7cbe\u5ea6\u53ef\u80fd\u4e0d\u9ad8<br \/>\n\u9002\u7528\u6570\u636e\u7c7b\u578b\uff1a\u6570\u503c\u578b\u548c\u6807\u79f0\u578b\u6570\u636e<\/p>\n<p>\u6211\u4eec\u60f3\u8981\u7684\u51fd\u6570\u5e94\u8be5\u662f\uff0c\u80fd\u63a5\u53d7\u6240\u6709\u7684\u8f93\u5165\u7136\u540e\u9884\u6d4b\u51fa\u7c7b\u522b\u3002\u4f8b\u5982\u5728\u4e24\u4e2a\u7c7b\u7684\u60c5\u51b5\u4e0b\u8f93\u51fa0\u6216\u80051\u3002 Heaviside step funtion\u5c31\u6709\u8fd9\u79cd\u529f\u80fd\uff0c\u4f46\u5b83\u7684\u95ee\u9898\u5728\u4e8e\u8be5\u51fd\u6570\u5728\u8df3\u8dc3\u70b9\u4e0a\u76f4\u63a5\u75310\u8df3\u8dc3\u52301\uff0c\u8fd9\u4e2a\u77ac\u95f4\u7684\u8df3\u8dc3\u8fc7\u7a0b\u5f88\u96be\u5904\u7406\u3002\u5e78\u597d\uff0c\u53e6\u4e00\u4e2a\u51fd\u6570\u4e5f\u6709\u7c7b\u4f3c\u7684\u6027\u8d28\uff0c\u4e5f\u66f4\u5bb9\u6613\u5904\u7406\uff0c\u5b83\u5c31\u662fSigmoid\u51fd\u6570\u3002<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-387\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-1.png\" alt=\"\" width=\"163\" height=\"78\"><\/a><\/p>\n<p>\u56fe5-1\u7ed9\u51fa\u4e86Sigmoid\u51fd\u6570\u5728\u4e0d\u540c\u5750\u6807\u5c3a\u5ea6\u4e0b\u7684\u4e24\u6761\u66f2\u7ebf\u3002\u5f53x\u903c\u8fd1\u4e8e0\u65f6\uff0cSigmoid\u51fd\u6570\u503c\u4e3a0.5\uff0c\u968f\u7740x\u589e\u5927\uff0cy\u903c\u8fd11\uff1bx\u51cf\u5c0f\uff0cy\u903c\u8fd10.\u5982\u679c\u6a2a\u5750\u6807\u523b\u5ea6\u8db3\u591f\u5927\uff0cSigmoid\u51fd\u6570\u5f88\u50cf\u9636\u8dc3\u51fd\u6570\u3002<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-388\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-1.png\" alt=\"\" width=\"821\" height=\"653\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-1.png 821w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-1-300x239.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-1-768x611.png 768w\" sizes=\"auto, (max-width: 821px) 100vw, 821px\" \/><\/a><\/p>\n<p>\u56e0\u6b64\uff0c\u4e3a\u4e86\u5b9e\u73b0Logistic\u56de\u5f52\u5206\u7c7b\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u4e2a\u7279\u5f81\u4e0a\u90fd\u4e58\u4ee5\u4e00\u4e2a\u56de\u5f52\u7cfb\u6570\uff0c\u7136\u540e\u628a\u6240\u6709\u7684\u7ed3\u679c\u503c\u76f8\u52a0\uff0c\u5c06\u603b\u548c\u5e26\u5165Sigmoid\u51fd\u6570\u4e2d\uff0c\u8fdb\u800c\u5f97\u5230\u4e00\u4e2a\u8303\u56f4\u57280~1\u7684\u6570\u503c\uff0c\u4efb\u4f55\u5927\u4e8e0.5\u7684\u6570\u636e\u88ab\u5206\u51651\u7c7b\uff0c\u5c0f\u4e8e0.5\u88ab\u5f52\u4e3a0\u7c7b\u3002\u6240\u4ee5Logistic\u56de\u5f52\u4e5f\u53ef\u4ee5\u88ab\u770b\u6210\u662f\u4e00\u79cd\u6982\u7387\u4f30\u8ba1\u3002<\/p>\n<p>\u786e\u5b9a\u4e86\u5206\u7c7b\u5668\u7684\u51fd\u6570\u5f62\u5f0f\u540e\uff0c\u95ee\u9898\u5c31\u53d8\u6210\u4e86\uff1a\u6700\u4f73\u56de\u5f52\u7cfb\u6570\u662f\u591a\u5c11\uff1f\u5982\u4f55\u786e\u5b9a\uff1f<\/p>\n<h2>\u57fa\u4e8e\u6700\u4f18\u5316\u65b9\u6cd5\u7684\u6700\u4f73\u56de\u5f52\u7cfb\u6570\u786e\u5b9a<\/h2>\n<p>Sigmoid\u51fd\u6570\u7684\u8f93\u5165\u8bb0\u4e3az\uff0c\u7531\u4e0b\u9762\u516c\u5f0f\u5f97\u51fa\uff1a<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-389\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-2.png\" alt=\"\" width=\"327\" height=\"30\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-2.png 327w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-2-300x28.png 300w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/a><\/p>\n<p>\u91c7\u7528\u5411\u91cf\u7684\u5199\u6cd5\uff0c\u4e0a\u9762\u516c\u5f0f\u53ef\u4ee5\u5199\u6210z=<em><strong>w<\/strong><\/em>x\uff0c\u5411\u91cfw\u5c31\u662f\u6211\u4eec\u8981\u627e\u7684\u6700\u4f73\u53c2\u6570\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u5b66\u4e60\u5230\u5982\u4f55\u4f7f\u7528\u65b9\u6cd5\u6c42\u5f97\u6570\u636e\u96c6\u7684\u6700\u4f73\u53c2\u6570\u3002<\/p>\n<h3>\u68af\u5ea6\u4e0a\u5347\u6cd5<\/h3>\n<p>\u68af\u5ea6\u4e0a\u5347\u6cd5\u57fa\u4e8e\u7684\u601d\u60f3\u662f\uff1a\u8981\u627e\u5230\u67d0\u51fd\u6570\u7684\u6700\u5927\u503c\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u5b9e\u9a8c\u8005\u8be5\u51fd\u6570\u7684\u68af\u5ea6\u65b9\u5411\u63a2\u5bfb\u3002\u5982\u679c\u68af\u5ea6\u8bb0\u4e3a\u0394\uff0c\u5219\u51fd\u6570f(x,y)\u7684\u68af\u5ea6\u7531\u4e0b\u5f0f\u8868\u793a\uff1a<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-390\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-3.png\" alt=\"\" width=\"295\" height=\"185\"><\/a><\/p>\n<p>\u53c2\u8003\u56fe5-2<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-391\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-2.png\" alt=\"\" width=\"857\" height=\"735\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-2.png 857w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-2-300x257.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-p5-2-768x659.png 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/a><\/p>\n<p>\u56fe5-2\u4e2d\u68af\u5ea6\u4e0a\u5347\u7b97\u6cd5\u6cbf\u68af\u5ea6\u65b9\u5411\u79fb\u52a8\u4e86\u4e00\u6b65\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u68af\u5ea6\u7b97\u5b50\u603b\u662f\u6307\u5411\u51fd\u6570\u503c\u589e\u957f\u6700\u5feb\u7684\u65b9\u5411\u3002\u8be5\u91cf\u503c\u79f0\u4e3a\u6b65\u957f\uff0c\u8bb0\u505a\u03b1\u3002\u7528\u5411\u91cf\u6765\u8868\u793a\u7684\u8bdd\uff0c\u68af\u5ea6\u4e0a\u5347\u7b97\u6cd5\u7684\u8fed\u4ee3\u516c\u5f0f\u5982\u4e0b\uff1a<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-392\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-4.png\" alt=\"\" width=\"188\" height=\"43\"><\/a><\/p>\n<p>\u8be5\u516c\u5f0f\u5c06\u4e00\u76f4\u88ab\u8fed\u4ee3\u6267\u884c\uff0c\u76f4\u81f3\u8fbe\u5230\u67d0\u4e2a\u505c\u6b62\u6761\u4ef6\u4e3a\u6b62\uff0c\u6bd4\u5982\u8fed\u4ee3\u6b21\u6570\u8fbe\u5230\u67d0\u4e2a\u6307\u5b9a\u503c\u6216\u8005\u7b97\u6cd5\u8fbe\u5230\u67d0\u4e2a\u53ef\u4ee5\u5141\u8bb8\u7684\u8bef\u5dee\u8303\u56f4\u3002<\/p>\n<hr>\n<h3>\u8bad\u7ec3\u7b97\u6cd5\uff1a\u4f7f\u7528\u68af\u5ea6\u4e0a\u5347\u627e\u5230\u6700\u4f73\u53c2\u6570<\/h3>\n<p>\u56fe5-3\u4e2d\u6709100\u4e2a\u6837\u672c\u70b9\uff0c\u6bcf\u4e2a\u70b9\u5305\u542b\u4e24\u4e2a\u6570\u503c\u578b\u7279\u5f81\uff1aX1\u548cX2\u3002<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-399\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-5.png\" alt=\"\" width=\"792\" height=\"613\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-5.png 792w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-5-300x232.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/02\/Logistic-formula-5-768x594.png 768w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/a><\/p>\n<p>\u68af\u5ea6\u4e0a\u5347\u7684\u4f2a\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>\u6bcf\u4e2a\u56de\u5f52\u7cfb\u6570\u521d\u59cb\u5316\u4e3a1<br \/>\n\u91cd\u590dR\u6b21\uff1a<br \/>\n\u8ba1\u7b97\u6574\u4e2a\u6570\u636e\u96c6\u7684\u68af\u5ea6<br \/>\n\u4f7f\u7528alphaxgradient\u66f4\u65b0\u56de\u5f52\u7cfb\u6570\u7684\u5411\u91cf<br \/>\n\u8fd4\u56de\u56de\u5f52\u7cfb\u6570<\/p>\n<p>\u521b\u5efa\u6587\u4ef6logRegres.py\uff0c\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#coding=utf-8\nfrom numpy import *\ndef loadDataSet():\n    dataMat = []\n    labelMat = []\n    fr = open(&#039;testSet.txt&#039;)\n    for line in fr.readlines():\n        lineArr = line.strip().split()\n        #\u56de\u5f52\u7cfb\u6570\uff0c\u5750\u68071\uff0c\u5750\u68072\n        dataMat.append([1.0,float(lineArr[0]),float(lineArr[1])])\n        labelMat.append(int(lineArr[2]))\n    return dataMat, labelMat\n\ndef sigmoid(inX):\n    return 1.0 \/ (1 + exp(-inX))\n\ndef gradAscent(dataMatIn,classLabels):\n    dataMatrix = mat(dataMatIn)#\u8bad\u7ec3\u96c6\n    labelMat = mat(classLabels).transpose()#\u8bad\u7ec3\u96c6\u6807\u7b7e\n    m,n = shape(dataMatrix)#m\u4e3a\u8bad\u7ec3\u6837\u672c\u6570\uff0cn\u4e3a\u6bcf\u4e2a\u6837\u672c\u7684\u5c5e\u6027\u6570\n    alpha = 0.001\n    maxCycles = 500\n    weights = ones((n,1))\n    for k in range(maxCycles):\n        h = sigmoid(dataMatrix*weights)\n        error = (labelMat - h)\n        weights = weights + alpha * dataMatrix.transpose() * error\n    return weights\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u51fd\u6570\u4e3b\u8981\u529f\u80fd\u4e3a\uff1a<br \/>\nloadDataSet\uff1a\u4eceTreeSet.txt\u8bfb\u53d6\u6837\u672c<br \/>\nsigmoid\uff1a\u9636\u8dc3\u51fd\u6570<br \/>\ngradAscent\uff1a\u5c06\u5f97\u5230\u7684\u6837\u672c\u901a\u8fc7\u68af\u5ea6\u4e0a\u5347\u7b97\u6cd5\uff0c\u53cd\u590d\u8fed\u4ee3\u540e\u5f97\u5230\u5bf9\u6837\u672c\u6bcf\u4e2a\u5c5e\u6027\u7684\u6743\u91cd\uff08\u56de\u5f52\u7cfb\u6570\uff09<\/p>\n<p>\u4e3a\u4e86\u9a8c\u8bc1\u5b9e\u9645\u6548\u679c\uff0c\u8f93\u5165\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\nimport logRegres\ndataArr,labelMat = logRegres.loadDataSet()\nprint(logRegres.gradAscent(dataArr,labelMat))\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-1.png\" alt=\"\" width=\"341\" height=\"151\" class=\"alignnone size-full wp-image-423\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-1.png 341w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-1-300x133.png 300w\" sizes=\"auto, (max-width: 341px) 100vw, 341px\" \/><\/a><\/p>\n<h3>\u5206\u6790\u6570\u636e\uff1a\u753b\u51fa\u51b3\u7b56\u8fb9\u754c<\/h3>\n<p>\u4e0a\u9762\u63a5\u89e3\u51fa\u4e86\u56de\u5f52\u7cfb\u6570\uff0c\u4ed6\u786e\u5b9a\u4e86\u4e0d\u540c\u7c7b\u522b\u6570\u636e\u4e4b\u95f4\u7684\u5206\u9694\u7ebf\u3002\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u753b\u51fa\u5206\u5272\u7ebf\u3002\u4ece\u800c\u4f7f\u5f97\u4f18\u5316\u7684\u8fc7\u7a0b\u4fbf\u4e8e\u7406\u89e3\u5462\uff1f<br \/>\n\u5728logRegres.py\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\ndef plotBestFit(weights):\n    import matplotlib.pyplot as plt\n    dataMat,labelMat = loadDataSet()\n    dataArr = array(dataMat)\n    n = shape(dataArr)[0]\n    xcord1 = [];\n    ycord1 = []\n    xcord2 = [];\n    ycord2 = []\n    for i in range(n):\n        if int(labelMat[i]) == 1:\n            xcord1.append(dataArr[i,1]);\n            ycord1.append(dataArr[i,2])\n        else:\n            xcord2.append(dataArr[i,1]);ycord2.append(dataArr[i,2])\n    fig = plt.figure()\n    ax = fig.add_subplot(111)\n    ax.scatter(xcord1,ycord1,s=30,c=&#039;red&#039;,marker=&#039;s&#039;)\n    ax.scatter(xcord2,ycord2,s=30,c=&#039;green&#039;)\n    x = arange(-3.0,3.0,0.1)\n    y = (-weights[0] - weights[1] * x) \/ weights[2]\n    ax.plot(x,y)\n    plt.xlabel(&#039;X1&#039;)\n    plt.ylabel(&#039;X2&#039;)\n    plt.show()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c0\u662f\u4e24\u4e2a\u5206\u7c7b\u7684\u5206\u89e3\u5904\uff0c\u56e0\u6b64\u6211\u4eec\u8bbe\u5b9a0=w0x0+w1x1+w2x2\uff0c\u7136\u540e\u89e3\u51faX2\u4e0eX1\u7684\u5173\u7cfb\u5f0f\u3002<br \/>\n\u8f93\u5165\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\nimport logRegres\ndataArr,labelMat = logRegres.loadDataSet()\nweights = logRegres.gradAscent(dataArr,labelMat)\nlogRegres.plotBestFit(weights.getA())\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-2.png\" alt=\"\" width=\"656\" height=\"553\" class=\"alignnone size-full wp-image-424\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-2.png 656w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-2-300x253.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/a><br \/>\n\u53ef\u4ee5\u770b\u5230\u5206\u7c7b\u7684\u7ed3\u679c\u76f8\u5f53\u4e0d\u9519\u3002<\/p>\n<h3>\u8bad\u7ec3\u7b97\u6cd5\uff1a\u968f\u673a\u68af\u5ea6\u4e0a\u5347<\/h3>\n<p>\u867d\u7136\u5206\u7c7b\u7ed3\u679c\u4e0d\u9519\uff0c\u4f46\u662f\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u6570\u636e\u96c6\u5f88\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5927\u91cf\u8ba1\u7b97\uff08300\u6b21\u4e58\u6cd5\uff09\u3002\u5982\u679c\u6570\u636e\u4e0a\u5347\u5230\u6210\u5343\u4e0a\u4e07\u7684\u7279\u5f81\u3002\u8be5\u65b9\u6cd5\u8ba1\u7b97\u590d\u6742\u5ea6\u5c31\u592a\u9ad8\u4e86\u3002\u4e00\u79cd\u6539\u8fdb\u65b9\u6cd5\u662f\u4e00\u6b21\u4ec5\u7528\u4e00\u4e2a\u6837\u672c\u70b9\u6765\u66f4\u65b0\u56de\u5f52\u7cfb\u6570\uff0c\u8be5\u65b9\u6cd5\u79f0\u4e3a\u968f\u673a\u68af\u5ea6\u4e0a\u5347\u7b97\u6cd5\u3002<br \/>\n\u968f\u673a\u68af\u5ea6\u7b97\u6cd5\u53ef\u4ee5\u5199\u6210\u5982\u4e0b\u4f2a\u4ee3\u7801\uff1a<br \/>\n\u6240\u6709\u56de\u5f52\u7cfb\u6570\u521d\u59cb\u5316\u4e3a1<br \/>\n\u5bf9\u6570\u636e\u96c6\u4e2d\u6bcf\u4e2a\u6837\u672c\uff1a<br \/>\n\u8ba1\u7b97\u8be5\u6837\u672c\u7684\u68af\u5ea6<br \/>\n\u4f7f\u7528alpha x gradient\u66f4\u65b0\u56de\u5f52\u7cfb\u6570\u503c<br \/>\n\u8fd4\u56de\u56de\u5f52\u7cfb\u6570\u503c<br \/>\n\u5728logRegres.py\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\ndef stocGradAscent0(dataMatrix,classLabels):\n    m,n = shape(dataMatrix)\n    alpha = 0.01\n    weights = ones(n)\n    for i in range(m):\n        h = sigmoid(sum(dataMatrix[i] * weights))\n        error = classLabels[i] - h\n        weights = weights + alpha * error * dataMatrix[i]\n    return weights\n<\/code><\/pre>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\nfrom numpy import *\nimport logRegres\ndataArr,labelMat = logRegres.loadDataSet()\n#weights = logRegres.gradAscent(dataArr,labelMat)\n#logRegres.plotBestFit(weights.getA())\nweights = logRegres.stocGradAscent0(array(dataArr),labelMat)\nlogRegres.plotBestFit(weights)\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-3.png\" alt=\"\" width=\"656\" height=\"553\" class=\"alignnone size-full wp-image-425\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-3.png 656w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-3-300x253.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/a><br \/>\n\u53ef\u4ee5\u53d1\u73b0\uff0c\u5206\u7c7b\u5668\u5206\u9519\u4e86\u4e09\u5206\u4e4b\u4e00\u7684\u6837\u672c\u3002\u5f53\u7136\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2a\u5206\u7c7b\u7ed3\u679c\u662f\u4e0d\u516c\u5e73\u7684\uff0c\u524d\u8005\u7684\u7ed3\u679c\u662f\u5728\u6574\u4e2a\u6570\u636e\u96c6\u4e0a\u8fed\u4ee3500\u6b21\u5f97\u5230\u3002\u6240\u4ee5\u6211\u4eec\u5bf9\u968f\u673a\u68af\u5ea6\u7b97\u6cd5\u505a\u4e00\u4e9b\u6539\u8fdb\uff0c\u4f7f\u5176\u5728\u6570\u636e\u96c6\u4e0a\u8fd0\u884c\u591a\u6b21\uff0c\u6700\u7ec8\u4f1a\u5740\u7684\u4e09\u4e2a\u56de\u5f52\u7cfb\u6570\u53d8\u5316\u60c5\u51b5\u5982\u56fe\u6240\u793a<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-6.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-6.png\" alt=\"\" width=\"818\" height=\"570\" class=\"alignnone size-full wp-image-426\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-6.png 818w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-6-300x209.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-6-768x535.png 768w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/a><br \/>\n\u7cfb\u65702\uff0c\u4e5f\u5c31\u662fX2\u53ea\u7ecf\u8fc7\u4e8650\u6b21\u8fed\u4ee3\u5c31\u8fbe\u5230\u4e86\u7a33\u5b9a\u503c\uff0c\u4f46\u7cfb\u65701\u548c0\u5219\u9700\u8981\u66f4\u591a\u6b21\u8fed\u4ee3\u3002\u53e6\u5916\uff0c\u5728\u5927\u7684\u6ce2\u52a8\u7ed3\u675f\u540e\uff0c\u4f9d\u7136\u6709\u4e00\u4e9b\u5c0f\u7684\u5468\u671f\u6027\u6ce2\u52a8\u3002\u8fd9\u662f\u56e0\u4e3a\u5b58\u5728\u4e00\u4e9b\u4e0d\u80fd\u6b63\u786e\u5206\u7c7b\u7684\u6837\u672c\u70b9\uff0c\u6bcf\u6b21\u8fed\u4ee3\u65f6\u4f1a\u5f15\u53d1\u7cfb\u6570\u7684\u5267\u70c8\u6539\u53d8\u3002\u6211\u4eec\u671f\u671b\u7b97\u6cd5\u80fd\u56de\u907f\u6765\u56de\u6ce2\u52a8\uff0c\u4ece\u800c\u6536\u655b\u5230\u67d0\u4e2a\u503c\u3002\u53e6\u5916\uff0c\u6536\u655b\u7684\u901f\u5ea6\u4e5f\u8981\u52a0\u5feb\u3002<br \/>\n\u5728logRegres.py\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\ndef stocGradAscent1(dataMatrix,classLabels,numIter=150):\n    m,n = shape(dataMatrix)\n    weights = ones(n)\n    for j in range(numIter):\n        dataIndex = range(m)\n        for i in range(m):\n            alpha = 4 \/ (1.0 + j + i) + 0.01\n            randIndex = int(random.uniform(0,len(dataIndex)))\n            h = sigmoid(sum(dataMatrix[randIndex] * weights))\n            error = classLabels[randIndex] - h\n            weights = weights + alpha * error * dataMatrix[randIndex]\n        del(dataIndex[randIndex])\n    return weights\n<\/code><\/pre>\n<p>\u4ee3\u7801\u6539\u8fdb\u4e24\u5904\uff0calpha\u5728\u6bcf\u6b21\u8fed\u4ee3\u7684\u65f6\u5019\u90fd\u4f1a\u8c03\u6574\uff0c\u8fd9\u4f1a\u7f13\u89e3\u56fe5-6\u7684\u6570\u636e\u6d41\u52a8\u6216\u9ad8\u9891\u6ce2\u52a8\u3002\u53e6\u5916alpha\u4f1a\u968f\u7740\u8fed\u4ee3\u6b21\u6570\u4e0d\u65ad\u53d8\u5c0f\uff0c\u4f46\u6c38\u4e0d\u4f1a\u51cf\u5c0f\u52300\uff0c\u8fd9\u6837\u4fdd\u8bc1\u5728\u591a\u6b21\u8fed\u4ee3\u540e\u65b0\u6570\u636e\u4f9d\u7136\u4f1a\u6709\u5f71\u54cd\u3002\u5982\u679c\u8981\u5904\u7406\u7684\u95ee\u9898\u662f\u52a8\u6001\u53d8\u5316\u7684\uff0c\u53ef\u4ee5\u9002\u5f53\u52a0\u5927\u5e38\u6570\u9879\u3002\u53e6\u5916\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u964d\u4f4ealpha\u7684\u51fd\u6570\u4e2d\uff0calpha\u6bcf\u6b21\u51cf\u5c111\/(j+i)\uff0c\u5176\u4e2dj\u662f\u8fed\u4ee3\u6b21\u6570\uff0ci\u662f\u6837\u672c\u70b9\u7684\u4e0b\u6807\uff0c\u8fd9\u6837\u5f53j&lt;&lt;max(i)\u65f6\uff0calpha\u5c31\u4e0d\u662f\u4e25\u683c\u4e0b\u964d\u7684\u3002\u7b2c\u4e8c\u4e2a\u6539\u8fdb\u5730\u65b9\u5728\u4e8e\u968f\u673a\u9009\u53d6\u66f4\u65b0\uff0c\u5b83\u5c06\u51cf\u5c11\u5468\u671f\u6027\u7684\u6ce2\u52a8\u3002<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-7.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-7.png\" alt=\"\" width=\"855\" height=\"659\" class=\"alignnone size-full wp-image-427\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-7.png 855w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-7-300x231.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-p5-7-768x592.png 768w\" sizes=\"auto, (max-width: 855px) 100vw, 855px\" \/><\/a><br \/>\n\u8f93\u5165\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\nfrom numpy import *\nimport logRegres\ndataArr,labelMat = logRegres.loadDataSet()\n#weights = logRegres.gradAscent(dataArr,labelMat)\n#logRegres.plotBestFit(weights.getA())\nweights = logRegres.stocGradAscent1(array(dataArr),labelMat)\nlogRegres.plotBestFit(weights)\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-4.png\" alt=\"\" width=\"656\" height=\"553\" class=\"alignnone size-full wp-image-428\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-4.png 656w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/Logistic-example-4-300x253.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/a><br \/>\n\u6548\u679c\u5df2\u7ecf\u5f88\u63a5\u8fd1\u4e4b\u524d\u7684\u7b97\u6cd5\uff0c\u540c\u65f6\u51cf\u5c11\u4e86\u8ba1\u7b97\u590d\u6742\u5ea6\u548c\u8fed\u4ee3\u6b21\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,407 Views\u53c2\u8003\u4e66\u7c4d\u300a\u673a\u5668\u5b66\u4e60\u5b9e\u6218\u300b \u6211\u4eec\u7684\u751f\u6d3b\u4e2d\u9047\u5230\u5f88\u591a\u6700\u4f18\u5316\u95ee\u9898\uff0c\u6bd4\u5982\u5982\u4f55\u5728\u6700\u77ed\u65f6\u95f4\u4eceA\u5230\u8fbeB\uff1f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[63,43],"class_list":["post-386","post","type-post","status-publish","format-standard","hentry","category-machine-learning","tag-logistic","tag-python"],"_links":{"self":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386"}],"version-history":[{"count":3,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":1169,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions\/1169"}],"wp:attachment":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}