{"id":12380,"date":"2024-10-29T23:22:24","date_gmt":"2024-10-29T14:22:24","guid":{"rendered":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/?p=12380"},"modified":"2024-10-29T23:22:24","modified_gmt":"2024-10-29T14:22:24","slug":"pydotplus","status":"publish","type":"post","link":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/2024\/10\/29\/pydotplus\/","title":{"rendered":"\u3010Python\/scikit-learn\u3011\u6c7a\u5b9a\u6728\u3092\u53ef\u8996\u5316\u3059\u308b\uff1aGraphviz\u3068pydotplus\u306e\u4f7f\u3044\u65b9"},"content":{"rendered":"<p>Python\u3067\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u306e\u4e00\u3064\u3067\u3042\u308b\u6c7a\u5b9a\u6728\u3092\u53ef\u8996\u5316\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002\u6c7a\u5b9a\u6728\u306f\u5206\u985e\u30e2\u30c7\u30eb\u306e\u69cb\u9020\u3092\u7406\u89e3\u3057\u3084\u3059\u304f\u3057\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u30c4\u30ea\u30fc\u69cb\u9020\u3068\u3057\u3066\u53ef\u8996\u5316\u3059\u308b\u3053\u3068\u3067\u3055\u3089\u306b\u8a73\u7d30\u306a\u7406\u89e3\u304c\u3067\u304d\u307e\u3059\u3002\u4eca\u56de\u306fGraphviz\u3068pydotplus\u3092\u4f7f\u3063\u3066\u6c7a\u5b9a\u6728\u306e\u30e2\u30c7\u30eb\u3092PNG\u753b\u50cf\u3068\u3057\u3066\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<h2>\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>Python\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u524d\u306b\u3001\u3044\u304f\u3064\u304b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h5>Graphviz\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h5>\n<p>pydotplus\u304c\u5185\u90e8\u3067Graphviz\u3092\u4f7f\u7528\u3059\u308b\u305f\u3081\u307e\u305aGraphviz\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002Linux\u3067\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u30b3\u30de\u30f3\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ sudo apt-get install graphviz\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Mac\u3067\u306fbrew\u3001Windows\u3067\u306fGraphviz\u306e\u516c\u5f0f\u30b5\u30a4\u30c8\u304b\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30e9\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h5>Python\u7528\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h5>\n<p>Python\u7528\u306b\u5fc5\u8981\u306apydotplus\u3068\u6c7a\u5b9a\u6728\u306e\u751f\u6210\u306b\u4f7f\u3046scikit-learn\u3082\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ pip install pydotplus scikit-learn\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h2>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001\u6b21\u306b\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002\u4eca\u56de\u306f\u3001<strong>Iris<\/strong>\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u7528\u3044\u3066\u3001scikit-learn\u306e<strong>DecisionTreeClassifier<\/strong>\u3067\u6c7a\u5b9a\u6728\u3092\u69cb\u7bc9\u3057\u3001<strong>pydotplus<\/strong>\u3092\u7528\u3044\u3066\u30e2\u30c7\u30eb\u3092\u753b\u50cf\u3068\u3057\u3066\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom sklearn.datasets import load_iris\r\nfrom sklearn.model_selection import train_test_split\r\nfrom sklearn.tree import DecisionTreeClassifier\r\nfrom pydotplus import graph_from_dot_data\r\nfrom sklearn.tree import export_graphviz\r\n\r\niris = load_iris()\r\nX, y = iris.data, iris.target\r\n\r\nX_train, X_test, y_train, y_test = train_test_split(\r\n                                    X, y, test_size=0.3, random_state=123)\r\n\r\ntree = DecisionTreeClassifier(max_depth=3, random_state=123)\r\ntree.fit(X_train, y_train)\r\n\r\ndot_data = export_graphviz(tree, filled=True, \r\n                           rounded=True, \r\n                           class_names=&#x5B;'Setosa', 'Versicolor', 'Virginica'],\r\n                           feature_names=&#x5B;'Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width'],\r\n                           out_file=None)\r\n# \u6c7a\u5b9a\u6728\u306e\u30d7\u30ed\u30c3\u30c8\u3092\u51fa\u529b\r\ngraph = graph_from_dot_data(dot_data)\r\ngraph.write_png('tree.png')\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5>\u30b3\u30fc\u30c9\u89e3\u8aac<\/h5>\n<ul>\n<li><strong>\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30ed\u30fc\u30c9\u3068\u5206\u5272<\/strong>\uff1aload_iris\u3067Iris\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u30ed\u30fc\u30c9\u3057\u3001train_test_split\u3092\u7528\u3044\u3066\u30c7\u30fc\u30bf\u3092\u8a13\u7df4\u30c7\u30fc\u30bf\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5206\u5272\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30e2\u30c7\u30eb\u306e\u5b66\u7fd2<\/strong>\uff1aDecisionTreeClassifier\u3092\u4f7f\u3044\u3001\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u3092\u751f\u6210\u3057\u307e\u3059\u3002max_depth=3\u3067\u6df1\u30553\u306b\u5236\u9650\u3057\u3001\u904e\u5b66\u7fd2\u3092\u9632\u304e\u307e\u3059\u3002<\/li>\n<li><strong>\u53ef\u8996\u5316\u7528\u30c7\u30fc\u30bf\u306e\u751f\u6210<\/strong>\uff1aexport_graphviz\u95a2\u6570\u3092\u7528\u3044\u3066\u3001\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u306e\u53ef\u8996\u5316\u7528\u306e\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3057\u307e\u3059\u3002filled=True\u306f\u30ce\u30fc\u30c9\u306e\u8272\u5206\u3051\u3092\u884c\u3044\u3001class_names\u3068feature_names\u3067\u5206\u985e\u30af\u30e9\u30b9\u3068\u7279\u5fb4\u91cf\u306e\u540d\u524d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e\u51fa\u529b<\/strong>\uff1agraph_from_dot_data\u95a2\u6570\u3067dot_data\u3092\u8aad\u307f\u8fbc\u307f\u3001PNG\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u51fa\u529b\u3057\u307e\u3059\u3002\u751f\u6210\u3055\u308c\u308btree.png\u3092\u958b\u304f\u3053\u3068\u3067\u3001\u6c7a\u5b9a\u6728\u306e\u69cb\u9020\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<h2>\u51fa\u529b\u7d50\u679c<\/h2>\n<p>\u4e0a\u8a18\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u753b\u50cf\u304c\u4fdd\u5b58\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/tree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-12381\" src=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/tree-700x481.png\" alt=\"\" width=\"700\" height=\"481\" srcset=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/tree-700x481.png 700w, http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/10\/tree.png 839w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3>\u6c7a\u5b9a\u6728\u306e\u69cb\u9020<\/h3>\n<p>\u3053\u306e\u6c7a\u5b9a\u6728\u306f\u3001Iris\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u57fa\u306b\u3001Petal Length\uff08\u82b1\u5f01\u306e\u9577\u3055\uff09\u3001Petal Width\uff08\u82b1\u5f01\u306e\u5e45\uff09\u3068\u3044\u3063\u305f\u7279\u5fb4\u91cf\u3092\u4f7f\u3063\u3066\u3001Setosa\u3001Versicolor\u3001Virginica\u306e3\u3064\u306e\u30af\u30e9\u30b9\u306b\u5206\u985e\u3059\u308b\u30e2\u30c7\u30eb\u3067\u3059\u3002\u6728\u306e\u5404\u30ce\u30fc\u30c9\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u60c5\u5831\u304c\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059\uff1a<\/p>\n<h5>\u6761\u4ef6\uff08\u4f8b: Petal Length &lt;= 2.45\uff09<\/h5>\n<p>\u7279\u5b9a\u306e\u7279\u5fb4\u91cf\u306b\u57fa\u3065\u3044\u3066\u30c7\u30fc\u30bf\u3092\u5206\u5c90\u3057\u307e\u3059\u3002True\uff08\u6761\u4ef6\u3092\u6e80\u305f\u3059\uff09\u3068False\uff08\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\uff09\u306e2\u3064\u306e\u679d\u304c\u7d9a\u304d\u307e\u3059\u3002<\/p>\n<h5>Gini\u6307\u6570\uff08gini\uff09<\/h5>\n<p>\u4e0d\u7d14\u5ea6\u306e\u6307\u6a19\u3067\u3001\u5024\u304c0\u306b\u8fd1\u3044\u307b\u3069\u7d14\u5ea6\u304c\u9ad8\u304f\u3001\u30ce\u30fc\u30c9\u5185\u306e\u30b5\u30f3\u30d7\u30eb\u304c\u307b\u307c\u5358\u4e00\u30af\u30e9\u30b9\u306b\u5206\u985e\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h5>\u30b5\u30f3\u30d7\u30eb\u6570\uff08samples\uff09<\/h5>\n<p>\u30ce\u30fc\u30c9\u306b\u5c5e\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u306e\u6570\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n<h5>\u30af\u30e9\u30b9\u306e\u5206\u5e03\uff08value\uff09<\/h5>\n<p>\u5404\u30af\u30e9\u30b9\u306e\u30b5\u30f3\u30d7\u30eb\u6570\u3092\u793a\u3057\u307e\u3059\u3002\u4f8b: [32, 0, 0]\u306f32\u30b5\u30f3\u30d7\u30eb\u304cSetosa\u3067\u3042\u308b\u3053\u3068\u3092\u610f\u5473\u3057\u307e\u3059\u3002<\/p>\n<h5>\u4e88\u6e2c\u30af\u30e9\u30b9\uff08class\uff09<\/h5>\n<p>\u30ce\u30fc\u30c9\u3067\u4e88\u6e2c\u3055\u308c\u308b\u30af\u30e9\u30b9\u3092\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u306e\u5927\u90e8\u5206\u3092\u5360\u3081\u308b\u30af\u30e9\u30b9\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>\u51fa\u529b\u7d50\u679c\u306e\u89e3\u91c8<\/h3>\n<h5>\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9<\/h5>\n<p>\u4e00\u756a\u4e0a\u306e\u30ce\u30fc\u30c9\uff08Petal Length &lt;= 2.45\uff09\u3067\u306f\u3001105\u500b\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u57fa\u306b\u6761\u4ef6\u304c\u8a2d\u5b9a\u3055\u308c\u3001\u5de6\u306e\u679d\u306fTrue\u3001\u53f3\u306e\u679d\u306fFalse\u306e\u30b5\u30f3\u30d7\u30eb\u306b\u5206\u5c90\u3057\u307e\u3059\u3002\u3053\u306e\u30ce\u30fc\u30c9\u3067\u306f\u3001gini=0.663\u3067\u30013\u3064\u306e\u30af\u30e9\u30b9\u304c\u6df7\u5728\u3057\u3066\u3044\u307e\u3059\u304c\u3001Versicolor\u304c\u591a\u3044\u72b6\u614b\u3067\u3059\u3002<\/p>\n<h5>\u5de6\u306e\u679d\uff08True\uff09<\/h5>\n<p>Petal Length &lt;= 2.45\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u30b5\u30f3\u30d7\u30eb\uff08\u5de6\uff09\u306f\u3001Setosa\u30af\u30e9\u30b9\u306e\u307f\u304b\u3089\u6210\u308a\u3001gini\u304c0\u306e\u305f\u3081\u7d14\u5ea6\u304c\u9ad8\u3044\u72b6\u614b\u3067\u3059\u3002\u3053\u3053\u3067Setosa\u3068\u5206\u985e\u3055\u308c\u307e\u3059\u3002<\/p>\n<h5>\u53f3\u306e\u679d\uff08False\uff09<\/h5>\n<p>Petal Length &gt; 2.45\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u30b5\u30f3\u30d7\u30eb\uff08\u53f3\uff09\u306f\u3001\u3055\u3089\u306bPetal Width &lt;= 1.75\u306e\u6761\u4ef6\u3067\u5206\u5c90\u3057\u307e\u3059\u3002\u3053\u306e\u30ce\u30fc\u30c9\u3082Versicolor\u304c\u512a\u52e2\u3067\u3059\u304c\u3001\u307e\u3060\u4ed6\u306e\u30af\u30e9\u30b9\u3082\u542b\u307e\u308c\u3066\u3044\u307e\u3059\uff08gini=0.495\uff09\u3002<\/p>\n<h5>\u8449\u30ce\u30fc\u30c9<\/h5>\n<p>\u6700\u7d42\u7684\u306b\u3001\u4e0b\u90e8\u306e\u30ce\u30fc\u30c9\u3067\u30b5\u30f3\u30d7\u30eb\u304c\u30af\u30e9\u30b9\u3054\u3068\u306b\u307b\u307c\u5206\u985e\u3055\u308c\u3001gini\u304c0\u307e\u305f\u306f\u5c0f\u3055\u3044\u5024\uff08\u9ad8\u7d14\u5ea6\uff09\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u53f3\u5074\u306eclass = Virginica\u306e\u30ce\u30fc\u30c9\u3067\u306f\u300129\u30b5\u30f3\u30d7\u30eb\u5168\u3066\u304cVirginica\u3067\u3001gini\u304c0\u306e\u305f\u3081\u7d14\u7c8b\u306a\u72b6\u614b\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>Graphviz\u3068pydotplus\u3092\u7528\u3044\u308b\u3053\u3068\u3067\u3001Python\u3067\u3082\u7c21\u5358\u306b\u6c7a\u5b9a\u6728\u30e2\u30c7\u30eb\u3092\u8996\u899a\u5316\u3067\u304d\u308b\u3053\u3068\u304c\u5206\u304b\u308a\u307e\u3057\u305f\u3002\u30e2\u30c7\u30eb\u306e\u89e3\u91c8\u3084\u3001\u7279\u5fb4\u91cf\u306e\u91cd\u8981\u5ea6\u306e\u7406\u89e3\u306b\u5f79\u7acb\u3064\u306e\u3067\u3001\u305c\u3072\u6d3b\u7528\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff01<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3067\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u306e\u4e00\u3064\u3067\u3042\u308b\u6c7a\u5b9a\u6728\u3092\u53ef\u8996\u5316\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002\u6c7a\u5b9a &#8230; <\/p>\n","protected":false},"author":1,"featured_media":12385,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-12380","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/comments?post=12380"}],"version-history":[{"count":3,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12380\/revisions"}],"predecessor-version":[{"id":12384,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12380\/revisions\/12384"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media\/12385"}],"wp:attachment":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media?parent=12380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/categories?post=12380"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/tags?post=12380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}