{"id":6363,"date":"2022-01-15T18:02:04","date_gmt":"2022-01-15T10:02:04","guid":{"rendered":"https:\/\/www.wangonc.com\/?p=6363"},"modified":"2024-03-01T14:50:31","modified_gmt":"2024-03-01T06:50:31","slug":"sklearn-decision-trees-1","status":"publish","type":"post","link":"https:\/\/www.wangonc.com\/index.php\/2022\/01\/15\/sklearn-decision-trees-1\/","title":{"rendered":"sklearn\u2014\u2014\u51b3\u7b56\u6811\uff08\u4e00\uff09"},"content":{"rendered":"<h1>\u4e00\u3001\u51b3\u7b56\u6811<\/h1>\n<h1>1\u3001 \u57fa\u672c\u6982\u5ff5<\/h1>\n<p>\u51b3\u7b56\u6811\u662f\u4e00\u79cd\u975e\u53c2\u6570\u6709\u76d1\u7763\u5b66\u4e60\u7b97\u6cd5<\/p>\n<p>\u51b3\u7b56\u6811\u7684\u6838\u5fc3\u601d\u60f3\u662f\u7528\u6811\u72b6\u56fe\u6765\u8868\u793a\u4e00\u7ec4\u7ed9\u5b9a\u6570\u636e\u4e2d\u7684\u6807\u7b7e\u6216\u89c4\u5219\uff0c\u5e76\u4ee5\u6b64\u6765\u89e3\u51b3\u5206\u7c7b\u548c\u56de\u5f52\u95ee\u9898<\/p>\n<p><strong>\u6839\u8282\u70b9\uff1a<\/strong>\u6700\u521d\u63d0\u51fa\u7684\u95ee\u9898<\/p>\n<p><strong>\u4e2d\u95f4\u8282\u70b9\uff08\u5185\u90e8\u8282\u70b9\uff09\uff1a<\/strong>\u5728\u6700\u7ec8\u7ed3\u8bba\u5f97\u51fa\u4e4b\u524d\u63d0\u51fa\u7684\u95ee\u9898<\/p>\n<p><strong>\u53f6\u5b50\u7ed3\u70b9<\/strong>\uff1a\u5f97\u5230\u7684\u7ed3\u8bba<\/p>\n<p>\u4e0b\u56fe\u4e3a\u51b3\u7b56\u6811\u793a\u4f8b<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.wangonc.com\/wp-content\/uploads\/2022\/01\/Untitled.png'><img class=\"lazyload lazyload-style-3\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/www.wangonc.com\/wp-content\/uploads\/2022\/01\/Untitled.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"Untitled.png\/\" \/><\/div><\/p>\n<p><strong>\u51b3\u7b56\u6811\u7b97\u6cd5\u7684\u6838\u5fc3\u662f\u8981\u89e3\u51b3\u4e24\u4e2a\u95ee\u9898\uff1a<\/strong><\/p>\n<p>1\uff09\u5982\u4f55\u4ece\u6570\u636e\u8868\u4e2d\u627e\u51fa\u6700\u4f73\u8282\u70b9\u548c\u6700\u4f73\u5206\u679d\uff1f<\/p>\n<p>2\uff09\u5982\u4f55\u8ba9\u51b3\u7b56\u6811\u505c\u6b62\u751f\u957f\uff0c\u9632\u6b62\u8fc7\u62df\u5408\uff1f<\/p>\n<h2>1.1\u3001sklearn\u5efa\u6a21\u6d41\u7a0b<\/h2>\n<ol>\n<li>\u5b9e\u4f8b\u5316\uff0c\u5efa\u7acb\u8bc4\u4f30\u6a21\u578b\u5bf9\u8c61\uff08\u5b9e\u4f8b\u5316\uff09<\/li>\n<li>\u901a\u8fc7\u6a21\u578b\u63a5\u53e3\u8bad\u7ec3\u6a21\u578b\uff08\u8bad\u7ec3\uff09<\/li>\n<li>\u901a\u8fc7\u6a21\u578b\u63a5\u53e3\u63d0\u53d6\u9700\u8981\u7684\u4fe1\u606f\uff08\u4f7f\u7528\u3001\u6d4b\u8bd5\uff09<\/li>\n<\/ol>\n<pre><code class=\"language-python\">from sklearn import tree                              #\u5bfc\u5165\u9700\u8981\u7684\u6a21\u5757\n\nclf = tree.DecisionTreeClassifier()                   #\u5b9e\u4f8b\u5316\nclf = clf.fit(X_train,y_train)                        #\u7528\u8bad\u7ec3\u96c6\u6570\u636e\u8bad\u7ec3\u6a21\u578b\nresult = clf.score(X_test,y_test)                     #\u5bfc\u5165\u6d4b\u8bd5\u96c6\uff0c\u4ece\u63a5\u53e3\u4e2d\u8c03\u7528\u9700\u8981\u7684\u4fe1\u606f<\/code><\/pre>\n<h2>1.2\u3001sklearn\u4e2d\u7684\u51b3\u7b56\u6811<\/h2>\n<p>\u6a21\u5757\uff1asklearn.tree<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>tree.DecisionTreeClassifier<\/td>\n<td>\u5206\u7c7b\u6811<\/td>\n<\/tr>\n<tr>\n<td>tree.DecisionTreeRegressor<\/td>\n<td>\u56de\u5f52\u6811<\/td>\n<\/tr>\n<tr>\n<td>tree.export_graphviz<\/td>\n<td>\u5c06\u751f\u6210\u7684\u51b3\u7b56\u6811\u5bfc\u51fa\u4e3aDOT\u683c\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>tree.ExtraTreeClassifier<\/td>\n<td>\u9ad8\u968f\u673a\u7248\u672c\u7684\u5206\u7c7b\u6811<\/td>\n<\/tr>\n<tr>\n<td>tree.ExtraTreeRegressor<\/td>\n<td>\u9ad8\u968f\u673a\u7248\u672c\u7684\u56de\u5f52\u6811<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>2\u3001DecisionTreeClassifier\uff08\u5206\u7c7b\u6811\uff09<\/h1>\n<h2>2.1\u3001 \u53c2\u6570\u4ecb\u7ecd<\/h2>\n<h3>2.1.1\u3001\u57fa\u672c\u53c2\u6570<\/h3>\n<ul>\n<li><code>criterion:{\u201cgini\u201d, \u201centropy\u201d}, default=\u201dgini\u201d <\/code><\/li>\n<\/ul>\n<p><strong>\u4e0d\u7eaf\u5ea6\uff1a<\/strong>\u8861\u91cf\u51b3\u7b56\u6811\u8282\u70b9\u7684\u6307\u6807\uff0c\u8be5\u53c2\u6570\u9ed8\u8ba4\u4e3a\u57fa\u5c3c\u7cfb\u6570<\/p>\n<p>\u6811\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u6709\u4e0d\u5b58\u5ea6\uff0c\u7236\u8282\u70b9\u7684\u4e0d\u5b58\u5ea6\u9ad8\u4e8e\u5b50\u8282\u70b9\uff0c\u53f6\u5b50\u8282\u70b9\u7684\u4e0d\u5b58\u5ea6\u6700\u4f4e<\/p>\n<p>criterion\u53c2\u6570\u7528\u4e8e\u51b3\u5b9a\u4e0d\u7eaf\u5ea6\u8ba1\u7b97\u65b9\u5f0f\uff0c\u5bf9\u4e8e\u5176\u9009\u62e9\u5982\u4e0b<\/p>\n<p>entropy\u2014\u2014\u4fe1\u606f\u71b5\uff0c<code class=\"katex-inline\">Entropy(t)=-\\sum_{i=0}^{c-1}p(i|t)log_{2}p(i|t)<\/code><\/p>\n<p>gini\u2014\u2014\u57fa\u5c3c\u7cfb\u6570\uff0c<code class=\"katex-inline\">Gini(t)=1-\\sum_{i=0}^{c-1}p(i|t)^{2}<\/code><\/p>\n<blockquote>\n<p>\u5f53\u4f7f\u7528\u4fe1\u606f\u71b5\u7684\u65f6\u5019\uff0csklearn\u5b9e\u9645\u4f7f\u7528\u7684\u662f\u7236\u8282\u70b9\u4e0e\u5b50\u8282\u70b9\u7684\u4fe1\u606f\u71b5\u5dee\u503c\uff0c\u5373\u4fe1\u606f\u589e\u76ca(Information Gain)<\/p>\n<\/blockquote>\n<ul>\n<li><code> splitter:{\u201cbest\u201d, \u201crandom\u201d}, default=\u201dbest\u201d <\/code><\/li>\n<\/ul>\n<p>\u7528\u4e8e\u9009\u62e9\u5206\u652f\u8282\u7684\u7b56\u7565\uff0c\u5176\u9009\u62e9\u5982\u4e0b<\/p>\n<p>best\u2014\u2014\u5728\u968f\u673a\u7684\u57fa\u7840\u4e0a\u9009\u62e9\u91cd\u8981\u7a0b\u5ea6\u66f4\u9ad8\u7684\u7279\u5f81\u6784\u5efa\u5206\u652f<\/p>\n<p>random\u2014\u2014\u66f4\u52a0\u968f\u673a\u7684\u9009\u62e9\u8282\u70b9\u5206\u652f\uff08\u9632\u6b62\u8fc7\u62df\u5408\u7684\u65b9\u5f0f\u4e4b\u4e00\uff09<\/p>\n<ul>\n<li><code>random_stateint:int, RandomState instance or None, default=None<\/code><\/li>\n<\/ul>\n<p>\u7528\u4e8e\u6307\u5b9a\u968f\u673a\u72b6\u6001\uff0c\u5176\u4e5f\u53ef\u4ee5\u770b\u505a\u968f\u673a\u6570\u79cd\u5b50\uff0c\u7c7b\u4f3c\u4e8erandom.seed()\uff0c\u6240\u4ee5\u9700\u8981\u4e3a\u6574\u6570<\/p>\n<h3>2.1.2\u3001\u526a\u679d\u53c2\u6570<\/h3>\n<p>\u526a\u679d\u53c2\u6570\u4e3b\u8981\u7684\u4f5c\u7528\u5c31\u662f\u5bf9\u751f\u6210\u7684\u51b3\u7b56\u6811\u8fdb\u884c\u526a\u679d\uff08\u51cf\u5c11\u5206\u652f\uff09\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u4e3a\u4e86\u9632\u6b62\u8fc7\u62df\u5408\u60c5\u51b5\u7684\u51fa\u73b0<\/p>\n<ul>\n<li><code>max_depth\uff1aint, default=None <\/code><\/li>\n<\/ul>\n<p>\u7528\u4e8e\u9650\u5236\u6811\u7684\u6700\u5927\u6df1\u5ea6\uff0c\u5c06\u8d85\u8fc7\u6700\u5927\u6df1\u5ea6\u7684\u6811\u679d\u5168\u90e8\u526a\u6389\uff0c\u5bf9\u4e8e\u9ad8\u7eac\u5ea6\u4f4e\u6837\u672c\u91cf\u7684\u60c5\u51b5\u6bd4\u8f83\u6709\u7528\uff0c\u5efa\u8bae\u4ece3\u5f00\u59cb\u5c1d\u8bd5<\/p>\n<ul>\n<li><code>min_samples_split\uff1aint or float, default=2<\/code><\/li>\n<\/ul>\n<p>\u4e00\u4e2a\u8282\u70b9\u5728\u5206\u652f\u540e\u7684\u6bcf\u4e2a\u5b50\u8282\u70b9\u90fd\u5fc5\u987b\u5305\u542b\u81f3\u5c11min_samples_split\u4e2a\u8bad\u7ec3\u6837\u672c\uff0c\u5426\u5219\u5206\u652f\u5c31\u4e0d\u4f1a\u51fa\u73b0\uff0c\u8fd9\u53ef\u80fd\u5177\u6709\u5e73\u6ed1\u6a21\u578b\u7684\u6548\u679c\uff0c\u5c24\u5176\u662f\u5728\u56de\u5f52\u4e2d\u3002\u4e00\u822c\u5efa\u8bae\u4ece5\u5f00\u59cb\u5c1d\u8bd5\u3002<\/p>\n<p>\u5f53\u8be5\u53c2\u6570\u4e3a\u6574\u6570\u65f6\uff0c\u5219\u5c06\u8be5\u503c\u4f5c\u4e3a\u6700\u5c0f\u7684\u6837\u672c\u9650\u5b9a\u6570<\/p>\n<p>\u5f53\u8be5\u503c\u4e3a\u6d6e\u70b9\u6570\u65f6\uff0c\u5219\u4e3a\u6837\u672c\u7684\u6bd4\u4f8b\uff0c\u5373\u5c06\u8be5\u503c*\u603b\u6837\u672c\u91cf\u4f5c\u4e3a\u6700\u5c0f\u6837\u672c\u6570\u7684\u9650\u5b9a<\/p>\n<ul>\n<li><code>min_samples_leaf\uff1aint or float, default=1<\/code><\/li>\n<\/ul>\n<p>\u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u7ed3\u70b9\u5141\u8bb8\u88ab\u5206\u652f\u7684\u6240\u9700\u6700\u5c0f\u6837\u672c\u6570\uff0c\u6574\u6570\u4e0e\u6d6e\u70b9\u6570\u542b\u4e49\u4e0e\u4e0a\u9762\u76f8\u540c<\/p>\n<ul>\n<li><code>max_features\uff1aint, float or {\u201cauto\u201d, \u201csqrt\u201d, \u201clog2\u201d}, default=None<\/code><\/li>\n<\/ul>\n<p>\u7528\u4e8e\u9650\u5236\u5206\u652f\u65f6\u6240\u8003\u8651\u7684\u6700\u5927\u7279\u5f81\u6570\u3002\u8be5\u53c2\u6570\u4f1a\u76f4\u63a5\u6839\u636e\u7ed9\u5b9a\u503c\u66b4\u529b\u7684\u9650\u5236\u4f7f\u7528\u989d\u7279\u5f81\uff0c\u5f3a\u884c\u4f7f\u51b3\u7b56\u6811\u505c\u6b62<\/p>\n<p>\u5f53\u4e3a\u6574\u6570\u65f6\uff0c\u5176\u4e3a\u6700\u5927\u7279\u5f81\u6570<\/p>\n<p>\u5f53\u4e3a\u6d6e\u70b9\u6570\u65f6\uff0c\u5176\u6700\u5927\u7279\u5f81\u6570\u5360\u603b\u7279\u5f81\u6570\u7684\u6bd4\u4f8b\uff08\u4e0e\u4e0a\u9762\u76f8\u4f3c\uff09<\/p>\n<p>\u5f53\u4e3aauto\u65f6\uff0c\u6700\u5927\u7279\u5f81\u6570\u4e3a<code class=\"katex-inline\">\\sqrt {\u603b\u7279\u5f81\u6570}<\/code><\/p>\n<p>\u5f53\u4e3asqrt\u65f6\uff0c\u6700\u5927\u7279\u5f81\u6570\u4e3a<code class=\"katex-inline\">\\sqrt {\u603b\u7279\u5f81\u6570}<\/code><\/p>\n<p>\u5f53\u4e3alog2\u65f6\uff0c\u6700\u5927\u7279\u5f81\u6570\u4e3a<code class=\"katex-inline\">\\log_2\u603b\u7279\u5f81\u6570<\/code><\/p>\n<p>\u5f53\u4e3aNone\u65f6\uff0c\u6700\u5927\u7279\u5f81\u6570\u4e3a\u603b\u7279\u5f81\u6570<\/p>\n<ul>\n<li><code>min_impurity_decrease\uff1afloat, default=0.0<\/code><\/li>\n<\/ul>\n<p>\u7528\u4e8e\u9650\u5236\u4fe1\u606f\u71b5\u589e\u76ca\u7684\u5927\u5c0f\uff0c\u4fe1\u606f\u71b5\u589e\u76ca\u5c0f\u4e8e\u8be5\u503c\u5219\u5206\u652f\u4e0d\u4f1a\u51fa\u73b0\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u8d85\u53c2\u6570\u66f2\u7ebf\u6765\u51b3\u5b9a\u6700\u7ec8\u7684\u526a\u679d\u53c2\u6570\uff0c\u5373\u4ee5\u8d85\u53c2\u6570\u53d6\u503c\u4e3a\u6a2a\u5750\u6807\uff0c\u6a21\u578b\u8861\u91cf\u6307\u6807\u4e3a\u7eb5\u5750\u6807\u7684\u66f2\u7ebf\uff0c\u53ef\u4ee5\u7528\u4e8e\u8861\u91cf\u4e0d\u540c\u8d85\u53c2\u6570\u4e0b\u7684\u6a21\u578b\u8868\u73b0<\/p>\n<p>\u5bf9\u4e8e\u8d85\u53c2\u6570\u8f83\u5c11\uff08\u4e00\u4e2a\u6216\u4e24\u4e2a\uff09\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.pyplot<\/code>\u5e93\u6765\u753b\u51fa\u51fd\u6570\u56fe\u50cf<\/p>\n<p>\u5bf9\u4e8e\u8d85\u53c2\u6570\u8f83\u591a\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sklearn.model_selection<\/code>\u5e93\u7684<code>GridSearchCV<\/code>\u51fd\u6570\u6216<code>RandomizedSearchCV<\/code>\u51fd\u6570\u8fdb\u884c\u4f18\u5316\uff0c\u5176\u5206\u522b\u4e3a\u7f51\u683c\u641c\u7d22\u6cd5\u548c\u968f\u673a\u53c2\u6570\u4f18\u5316\u6cd5<\/p>\n<p>\u8d85\u53c2\u6570\u4e3a\u5728\u5f00\u59cb\u673a\u5668\u5b66\u4e60\u4e4b\u524d\uff0c\u5c31\u4eba\u4e3a\u8bbe\u7f6e\u597d\u7684\u53c2\u6570<\/p>\n<p>\u524d\u8005\u901f\u5ea6\u6162\u4e8e\u540e\u8005\u4f46\u662f\u51c6\u786e\u6027\u7565\u9ad8\u4e8e\u540e\u8005\uff0c\u8be6\u7ec6\u5bf9\u6bd4\u89c1\u9875\u9762<\/p>\n<p><a href=\"https:\/\/scikit-learn.org\/stable\/\/auto_examples\/model_selection\/plot_randomized_search.html\">Comparing randomized search and grid search for hyperparameter estimation &#8211; scikit-learn 0.24.2 documentation<\/a><\/p>\n<h3>2.1.3\u3001\u76ee\u6807\u6743\u91cd\u53c2\u6570<\/h3>\n<ul>\n<li><code>class_weight\uff1adict, list of dict or \u201cbalanced\u201d, default=None <\/code><\/li>\n<\/ul>\n<p>\u5206\u7c7b\u6743\u91cd\uff0c\u7528\u4e8e\u89e3\u51b3\u6570\u636e\u96c6\u4e2d\u4e0d\u540c\u6807\u7b7e\u7ed3\u679c\u7684\u6570\u636e\u7ed3\u679c\u6bd4\u4f8b\u4e0d\u540c\u7684\u95ee\u9898\u3002<\/p>\n<ul>\n<li><code>min_weight_fraction_leaf\uff1afloat, default=0.0<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u51b3\u7b56\u6811 1\u3001 \u57fa\u672c\u6982\u5ff5 \u51b3\u7b56\u6811\u662f\u4e00\u79cd\u975e\u53c2\u6570\u6709\u76d1\u7763\u5b66\u4e60\u7b97\u6cd5 \u51b3\u7b56\u6811\u7684\u6838\u5fc3\u601d\u60f3\u662f\u7528\u6811\u72b6\u56fe\u6765\u8868\u793a\u4e00\u7ec4\u7ed9\u5b9a\u6570\u636e\u4e2d\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6395,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,2],"tags":[22],"series":[],"class_list":["post-6363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-study-notes","tag-sklearn"],"_links":{"self":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts\/6363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/comments?post=6363"}],"version-history":[{"count":21,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts\/6363\/revisions"}],"predecessor-version":[{"id":7374,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/posts\/6363\/revisions\/7374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/media\/6395"}],"wp:attachment":[{"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/media?parent=6363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/categories?post=6363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/tags?post=6363"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.wangonc.com\/index.php\/wp-json\/wp\/v2\/series?post=6363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}