{"id":173,"date":"2016-09-21T18:17:00","date_gmt":"2016-09-21T10:17:00","guid":{"rendered":"http:\/\/ayonel.me\/?p=173"},"modified":"2016-12-30T14:44:10","modified_gmt":"2016-12-30T06:44:10","slug":"sklearn-naive-bayes","status":"publish","type":"post","link":"https:\/\/ayonel.malash.net\/index.php\/2016\/09\/21\/sklearn-naive-bayes\/","title":{"rendered":"\u7528sklearn\u5b9e\u73b0\u591a\u9879\u5f0f\u6734\u7d20\u8d1d\u53f6\u65af\u589e\u91cf\u5b66\u4e60"},"content":{"rendered":"<p>\u4ee5\u524d\u5199\u8fc7<a href=\"https:\/\/ayonel.malash.net\/index.php\/2016\/08\/12\/weka_naivebayes\/\" target=\"_blank\">\u4e00\u7bc7\u535a\u6587<\/a>\u662f\u5229\u7528weka\u6765\u5b9e\u73b0\u591a\u9879\u5f0f\u6734\u7d20\u8d1d\u53f6\u65af\u589e\u91cf\u5b66\u4e60\u3002\u8fd9\u6b21\u5229\u7528python\u4e2d\u7684sklearn\u5b66\u4e60\u5de5\u5177\u6765\u5b9e\u73b0\u3002\u5176\u5b9e\u4e2a\u4eba\u611f\u89c9\u50cf\u673a\u5668\u5b66\u4e60\uff0c\u6570\u636e\u5904\u7406\u8fd9\u79cd\u6d3b\u672c\u6765\u5c31\u5e94\u8be5\u8ba9\u6211\u4eec\u9ad8\u96c5\u800c\u4f18\u7f8e\u7684Python\u6765\u5b9e\u73b0\uff0cJava\u4f60\u778e\u51d1\u4ec0\u4e48\u70ed\u95f9\u554a~~<\/p>\n<p>\u4e0eweka\u7684\u5b9e\u73b0\u76f8\u6bd4\uff0csklearn\u679c\u7136\u8fd8\u662f\u7ee7\u627f\u4e86Python\u7b80\u6d01\u4f18\u96c5\u7684\u4f20\u7edf\uff0c\u5b9e\u73b0\u8d77\u6765\u5341\u5206\u65b9\u4fbf\u3002\u5982\u4f55\u5224\u65adsklearn\u4e2d\u5206\u7c7b\u5668\u662f\u5426\u652f\u6301\u589e\u91cf\u5b66\u4e60(incremental learning &#038; online learning)\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u67e5\u9605<a href=\"http:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB\" target=\"_blank\">sklearn\u6587\u6863<\/a>\uff0c\u5982\u679c\u8be5\u5206\u7c7b\u5668\u5b58\u5728<strong>partial_fit()<\/strong>\u65b9\u6cd5\uff0c\u5373\u8bf4\u660e\u5b83\u652f\u6301\u589e\u91cf\u5b66\u4e60\u3002<br \/>\n\u6bd4\u5982\u6211\u4eec\u7684<a href=\"http:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB\" target=\"_blank\">\u591a\u9879\u5f0f\u6734\u7d20\u8d1d\u53f6\u65af<\/a>\u4e2d\u5c31\u6709\u8be5\u65b9\u6cd5\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ayonel.malash.net\/wp-content\/uploads\/2016\/09\/sklearn-300x92.jpg\" alt=\"sklearn\" width=\"300\" height=\"92\" class=\"alignnone size-medium wp-image-196\" srcset=\"https:\/\/ayonel.malash.net\/wp-content\/uploads\/2016\/09\/sklearn-300x92.jpg 300w, https:\/\/ayonel.malash.net\/wp-content\/uploads\/2016\/09\/sklearn-768x237.jpg 768w, https:\/\/ayonel.malash.net\/wp-content\/uploads\/2016\/09\/sklearn-1024x315.jpg 1024w, https:\/\/ayonel.malash.net\/wp-content\/uploads\/2016\/09\/sklearn.jpg 1656w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u4e0b\u9762\u6211\u4ee5\u4e00\u4e2a\u5341\u5206\u7b80\u5355\u7684\u5b9e\u4f8b\u6765\u8fdb\u884c\u6f14\u793a\uff1a<\/p>\n<p>\u6bd4\u5982\u6211\u4eec\u6709\u4e09\u4efd\u6570\u636e\u96c6\uff0c\u5148\u7528\u6570\u636e\u96c61\u8fdb\u884c\u8bad\u7ec3\uff0c\u7136\u540e\u9884\u6d4b\u6570\u636e\u96c63\uff1b\u7136\u540e\u518d\u5229\u7528\u6570\u636e\u96c62\u518d\u5bf9\u6a21\u578b\u8fdb\u884c\u66f4\u65b0\uff0c\u4e4b\u540e\u518d\u9884\u6d4b\u6570\u636e\u96c63\u3002<br \/>\n\u6570\u636e\u96c61\uff1a<strong>\u2018[ ]\u2019\u4e2d\u4ee3\u8868\u6837\u672c\u5411\u91cf\uff0c \u2018#\u2019\u540e\u9762\u4ee3\u8868\u6837\u672c\u6807\u7b7e<\/strong><\/p>\n<div class=\"codecolorer-container text railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"text codecolorer\">&nbsp;[1 1 0 1 2 0 3 1 2 2] &nbsp; &nbsp;# 1<br \/>\n&nbsp;[0 3 1 1 2 2 1 3 2 1] &nbsp; &nbsp;# 0<br \/>\n&nbsp;[0 3 0 3 2 2 2 1 4 0] &nbsp; &nbsp;# 0 <br \/>\n&nbsp;[1 4 3 2 4 0 2 1 3 4] &nbsp; &nbsp;# 1<br \/>\n&nbsp;[4 4 3 4 0 3 4 2 4 4] &nbsp; &nbsp;# 0<br \/>\n&nbsp;[0 2 1 3 3 1 0 1 2 3] &nbsp; &nbsp;# 1<\/div><\/div>\n<p>\u6570\u636e\u96c62\uff1a<\/p>\n<div class=\"codecolorer-container text railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"text codecolorer\">&nbsp;[0 1 2 2 2 1 0 0 2 3] &nbsp; &nbsp;# 0<br \/>\n&nbsp;[2 1 2 1 0 3 1 2 1 1] &nbsp; &nbsp;# 1<br \/>\n&nbsp;[4 0 3 3 3 4 2 2 4 2] &nbsp; &nbsp;# 0<br \/>\n&nbsp;[1 3 3 4 1 4 3 0 3 3] &nbsp; &nbsp;# 1<br \/>\n&nbsp;[1 4 4 0 3 4 0 2 2 2] &nbsp; &nbsp;# 1<br \/>\n&nbsp;[1 4 0 4 1 0 4 1 4 1] &nbsp; &nbsp;# 1<\/div><\/div>\n<p>\u6570\u636e\u96c63\uff08\u6d4b\u8bd5\u96c6\uff0c\u6ca1\u6709\u6807\u7b7e\uff09\uff1a<\/p>\n<div class=\"codecolorer-container text railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"text codecolorer\">&nbsp;[1 4 1 3 0 3 1 2 3 4]<br \/>\n&nbsp;[0 2 0 2 3 2 0 0 2 2]<br \/>\n&nbsp;[0 3 4 3 0 1 4 4 4 2]<br \/>\n&nbsp;[0 4 3 0 2 2 2 1 0 3]<br \/>\n&nbsp;[4 3 1 3 3 2 2 0 0 1]<br \/>\n&nbsp;[3 0 3 2 4 2 1 2 1 0]<\/div><\/div>\n<p>\u4ee3\u7801\u5341\u5206\u7b80\u5355\uff0c\u5148\u6765\u770b\u770b\u5427\uff1a<\/p>\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;height:100%;\"><div class=\"python codecolorer\"><span class=\"kw1\">import<\/span> numpy <span class=\"kw1\">as<\/span> np<br \/>\n<span class=\"co1\">#\u6784\u9020\u6570\u636e\u96c6\uff0c\u6bcf\u4efd\u6570\u636e\u96c6\u662f6*10\u7684\u968f\u673a\u751f\u6210\u7684\u77e9\u9635\uff0c\u4ee3\u8868\u67096\u4e2a\u6837\u672c\uff0c\u6bcf\u4e2a\u6837\u672c\u4e2d\u670910\u4e2a\u5411\u91cf\u3002<\/span><br \/>\nX1 <span class=\"sy0\">=<\/span> np.<span class=\"kw3\">random<\/span>.<span class=\"me1\">randint<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span> size<span class=\"sy0\">=<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">6<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"co1\">#\u6570\u636e\u96c61<\/span><br \/>\nX2 <span class=\"sy0\">=<\/span> np.<span class=\"kw3\">random<\/span>.<span class=\"me1\">randint<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span> size<span class=\"sy0\">=<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">6<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"co1\">#\u6570\u636e\u96c62<\/span><br \/>\nX3 <span class=\"sy0\">=<\/span> np.<span class=\"kw3\">random<\/span>.<span class=\"me1\">randint<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span> size<span class=\"sy0\">=<\/span><span class=\"br0\">&#40;<\/span><span class=\"nu0\">6<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">10<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"co1\">#\u6570\u636e\u96c63<\/span><br \/>\n<br \/>\n<span class=\"co1\">#\u6253\u5370\u51fa3\u4efd\u6570\u636e\u96c6<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span>X1<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span>X2<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span>X3<span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\ny1 <span class=\"sy0\">=<\/span> np.<span class=\"kw3\">array<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"co1\">#\u6570\u636e\u96c61\u4e2d\u6bcf\u4efd\u6570\u636e\u5bf9\u5e94\u7684\u6807\u7b7e<\/span><br \/>\ny2 <span class=\"sy0\">=<\/span> np.<span class=\"kw3\">array<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"co1\">#\u6570\u636e\u96c62\u4e2d\u6bcf\u4efd\u6570\u636e\u5bf9\u5e94\u7684\u6807\u7b7e<\/span><br \/>\n<br \/>\n<span class=\"co1\">#\u6240\u6709\u6570\u636e\u96c6\u4e2d\u51fa\u73b0\u7684\u5206\u7c7b\u6807\u7b7e<\/span><br \/>\nlabels <span class=\"sy0\">=<\/span> np.<span class=\"kw3\">array<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span> <span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">from<\/span> sklearn.<span class=\"me1\">naive_bayes<\/span> <span class=\"kw1\">import<\/span> MultinomialNB<br \/>\nclf <span class=\"sy0\">=<\/span> MultinomialNB<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"co1\">#\u5b9a\u4e49\u5206\u7c7b\u5668<\/span><br \/>\n<br \/>\n<span class=\"co1\">#\u5148\u5229\u7528X1\u4e0ey1\u8fdb\u884c\u90e8\u5206\u8bad\u7ec3<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'start tranning...'<\/span><span class=\"br0\">&#41;<\/span><br \/>\nclf.<span class=\"me1\">partial_fit<\/span><span class=\"br0\">&#40;<\/span>X1<span class=\"sy0\">,<\/span> y1<span class=\"sy0\">,<\/span> classes<span class=\"sy0\">=<\/span>labels<span class=\"br0\">&#41;<\/span><span class=\"co1\">#\u6838\u5fc3\u65b9\u6cd5\uff0c\u90e8\u5206\u8bad\u7ec3\uff0c\u7b2c\u4e00\u6b21partial_fit,\u9700\u8981\u5728classes\u53c2\u6570\u4e2d\u7ed9\u51fa\u6240\u6709\u6807\u7b7e<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'\u7b2c\u4e00\u6b21\u9884\u6d4b\u7ed3\u679c:'<\/span>+<span class=\"kw2\">str<\/span><span class=\"br0\">&#40;<\/span>clf.<span class=\"me1\">predict<\/span><span class=\"br0\">&#40;<\/span>X3<span class=\"br0\">&#91;<\/span>:<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"co1\">#\u6253\u5370\u51fa\u5bf9\u6570\u636e\u96c63\u7684\u9884\u6d4b\u7ed3\u679c<\/span><br \/>\n<br \/>\n<span class=\"co1\">#\u518d\u5229\u7528X2\u4e0ey2\u8fdb\u884c\u6a21\u578b\u66f4\u65b0<\/span><br \/>\nclf.<span class=\"me1\">partial_fit<\/span><span class=\"br0\">&#40;<\/span>X2<span class=\"sy0\">,<\/span> y2<span class=\"br0\">&#41;<\/span><span class=\"co1\">#<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">'\u66f4\u65b0\u540e\u9884\u6d4b\u7ed3\u679c:'<\/span>+<span class=\"kw2\">str<\/span><span class=\"br0\">&#40;<\/span>clf.<span class=\"me1\">predict<\/span><span class=\"br0\">&#40;<\/span>X3<span class=\"br0\">&#91;<\/span>:<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"co1\">#\u518d\u6253\u5370\u51fa\u5bf9\u6570\u636e\u96c63\u7684\u9884\u6d4b\u7ed3\u679c<\/span><\/div><\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a(\u7531\u4e8e\u6570\u636e\u96c6\u662f\u968f\u673a\u751f\u6210\u7684\uff0c\u6240\u4ee5\u6bcf\u6b21\u7684\u7ed3\u679c\u5747\u4f1a\u4e0d\u540c)<\/p>\n<div class=\"codecolorer-container text railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;height:100%;\"><div class=\"text codecolorer\">[[1 1 0 1 2 0 3 1 2 2]<br \/>\n&nbsp;[0 3 1 1 2 2 1 3 2 1]<br \/>\n&nbsp;[0 3 0 3 2 2 2 1 4 0]<br \/>\n&nbsp;[1 4 3 2 4 0 2 1 3 4]<br \/>\n&nbsp;[4 4 3 4 0 3 4 2 4 4]<br \/>\n&nbsp;[0 2 1 3 3 1 0 1 2 3]]<br \/>\n[[0 1 2 2 2 1 0 0 2 3]<br \/>\n&nbsp;[2 1 2 1 0 3 1 2 1 1]<br \/>\n&nbsp;[4 0 3 3 3 4 2 2 4 2]<br \/>\n&nbsp;[1 3 3 4 1 4 3 0 3 3]<br \/>\n&nbsp;[1 4 4 0 3 4 0 2 2 2]<br \/>\n&nbsp;[1 4 0 4 1 0 4 1 4 1]]<br \/>\n[[1 4 1 3 0 3 1 2 3 4]<br \/>\n&nbsp;[0 2 0 2 3 2 0 0 2 2]<br \/>\n&nbsp;[0 3 4 3 0 1 4 4 4 2]<br \/>\n&nbsp;[0 4 3 0 2 2 2 1 0 3]<br \/>\n&nbsp;[4 3 1 3 3 2 2 0 0 1]<br \/>\n&nbsp;[3 0 3 2 4 2 1 2 1 0]]<br \/>\nstart tranning...<br \/>\n\u7b2c\u4e00\u6b21\u9884\u6d4b\u7ed3\u679c:[0 1 0 1 0 0]<br \/>\n\u66f4\u65b0\u540e\u9884\u6d4b\u7ed3\u679c:[1 1 1 1 1 0]<\/div><\/div>\n<p>\u6700\u7ec8\u7684\u9884\u6d4b\u7ed3\u679c\u8868\u793a\u7684\u662f\u5bf9\u6570\u636e\u96c63\u4e2d\u76846\u4e2a\u6837\u672c\uff0c\u5206\u522b\u9884\u6d4b\u51fa\u7684\u6807\u7b7e\u3002<br \/>\n\u5982\u679c\u6700\u7ec8\u9700\u8981\u7684\u7ed3\u679c\u4e0d\u662f\u9884\u6d4b\u51fa\u7c7b\u522b\uff0c\u800c\u662f\u9700\u8981\u6bcf\u4e2a\u7c7b\u522b\u7cbe\u786e\u7684\u6982\u7387\u503c\u3002<br \/>\n\u53ef\u4ee5\u91c7\u7528\u65b9\u6cd5<a href=\"http:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB.predict_proba\" target=\"_blank\">clf.predict_proba()<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u5199\u8fc7\u4e00\u7bc7\u535a\u6587\u662f\u5229\u7528weka\u6765\u5b9e\u73b0\u591a\u9879\u5f0f\u6734\u7d20\u8d1d\u53f6\u65af\u589e\u91cf\u5b66\u4e60\u3002\u8fd9\u6b21\u5229\u7528python\u4e2d\u7684sklearn\u5b66\u4e60\u5de5\u5177\u6765 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":14,"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":199,"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions\/199"}],"wp:attachment":[{"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ayonel.malash.net\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}