{"action":"created","comment":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/42601136","id":42601136,"diff_hunk":"@@ -0,0 +1,134 @@\n+\"\"\"\n+=========================================\n+Understanding the decision tree structure\n+=========================================\n+\n+The decision tree structure can be analysed to gain further insight on the\n+relation between the features and the target to predict. In this example, we\n+show how to retrieve:\n+    - the binary tree structure;\n+    - the depth of each node and whether or not it's a leaf.\n+    - the nodes that were reached by a sample using the decision_paths method;\n+    - the leaf that was reached by a sample using the apply method;\n+    - the rules that were used to predict a sample;\n+    - the decision path shared by a group of samples.\n+\n+\"\"\"\n+import numpy as np\n+\n+from sklearn.cross_validation import train_test_split\n+from sklearn.datasets import load_iris\n+from sklearn.tree import DecisionTreeClassifier\n+\n+iris = load_iris()\n+X = iris.data\n+y = iris.target\n+X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n+\n+estimator = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0)\n+estimator.fit(X_train, y_train)\n+\n+# The decision estimator has an attribute called tree_  which stores the entire\n+# tree structure and allows access to low level attributes. The binary tree\n+# tree_ is represented as a number of parallel arrays. The i-th element of each\n+# array holds information about the node `i`. Node 0 is the tree's root. NOTE:\n+# Some of the arrays only apply to either leaves or split nodes, resp. In this\n+# case the values of nodes of the other type are arbitrary!\n+#\n+# Among those arrays, we have:\n+#   - left_child, id of the left child of the node\n+#   - right_child, id of the right child of the node\n+#   - feature, feature used for splitting the node\n+#   - threshold, threshold value at the node\n+#\n+\n+# Using those arrays, we can parse the tree structure:\n+\n+n_nodes = estimator.tree_.node_count\n+children_left = estimator.tree_.children_left\n+children_right = estimator.tree_.children_right\n+feature = estimator.tree_.feature\n+threshold = estimator.tree_.threshold\n+\n+\n+# The tree structure can be traversed to compute various properties such\n+# as the depth of each node and whether or not it is a leaf.\n+node_depth = np.zeros(shape=n_nodes)\n+is_leaves = np.zeros(shape=n_nodes, dtype=bool)\n+stack = [(0, -1)]  # seed is the root node id and its parent depth\n+while len(stack) > 0:\n+    node_id, parent_depth = stack.pop()\n+    node_depth[node_id] = parent_depth + 1\n+\n+    # If we have a test node\n+    if (children_left[node_id] != children_right[node_id]):\n+        stack.append((children_left[node_id], parent_depth + 1))\n+        stack.append((children_right[node_id], parent_depth + 1))\n+    else:\n+        is_leaves[node_id] = True\n+\n+print(\"The binary tree structure has %s nodes and has \"\n+      \"the following tree structure:\"\n+      % n_nodes)\n+for i in range(n_nodes):\n+    if is_leaves[i]:\n+        print(\"%snode=%s leaf node.\" % (node_depth[i] * \"\\t\", i))\n+    else:\n+        print(\"%snode=%s test node: go to node %s if X[:, %s] <= %ss else to \"\n+              \"node %s.\"\n+              % (node_depth[i] * \"\\t\",\n+                 i,\n+                 children_left[i],\n+                 feature[i],\n+                 threshold[i],\n+                 children_right[i],\n+                 ))\n+print()\n+\n+# First let's retrieve the decision path of each sample. The decision_paths","path":"examples/tree/plot_structure.py","position":88,"original_position":88,"commit_id":"c2b41906e88159b7b8d79f68b4a5fee575d90249","original_commit_id":"c2b41906e88159b7b8d79f68b4a5fee575d90249","user":{"login":"glouppe","id":477771,"avatar_url":"https://avatars.githubusercontent.com/u/477771?v=3","gravatar_id":"","url":"https://api.github.com/users/glouppe","html_url":"https://github.com/glouppe","followers_url":"https://api.github.com/users/glouppe/followers","following_url":"https://api.github.com/users/glouppe/following{/other_user}","gists_url":"https://api.github.com/users/glouppe/gists{/gist_id}","starred_url":"https://api.github.com/users/glouppe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/glouppe/subscriptions","organizations_url":"https://api.github.com/users/glouppe/orgs","repos_url":"https://api.github.com/users/glouppe/repos","events_url":"https://api.github.com/users/glouppe/events{/privacy}","received_events_url":"https://api.github.com/users/glouppe/received_events","type":"User","site_admin":false},"body":"path","created_at":"2015-10-21T09:25:25Z","updated_at":"2015-10-21T09:25:25Z","html_url":"https://github.com/scikit-learn/scikit-learn/pull/5487#discussion_r42601136","pull_request_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487","_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments/42601136"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/5487#discussion_r42601136"},"pull_request":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487"}}},"pull_request":{"url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487","id":48202050,"html_url":"https://github.com/scikit-learn/scikit-learn/pull/5487","diff_url":"https://github.com/scikit-learn/scikit-learn/pull/5487.diff","patch_url":"https://github.com/scikit-learn/scikit-learn/pull/5487.patch","issue_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5487","number":5487,"state":"open","locked":false,"title":"[MRG] Add an example and a method to analyse the decision tree stucture","user":{"login":"arjoly","id":1274722,"avatar_url":"https://avatars.githubusercontent.com/u/1274722?v=3","gravatar_id":"","url":"https://api.github.com/users/arjoly","html_url":"https://github.com/arjoly","followers_url":"https://api.github.com/users/arjoly/followers","following_url":"https://api.github.com/users/arjoly/following{/other_user}","gists_url":"https://api.github.com/users/arjoly/gists{/gist_id}","starred_url":"https://api.github.com/users/arjoly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjoly/subscriptions","organizations_url":"https://api.github.com/users/arjoly/orgs","repos_url":"https://api.github.com/users/arjoly/repos","events_url":"https://api.github.com/users/arjoly/events{/privacy}","received_events_url":"https://api.github.com/users/arjoly/received_events","type":"User","site_admin":false},"body":"Ping @glouppe, @pprett, @ogrisel, @amueller \r\n\r\nSuggestions are welcome to improve the example. \r\n\r\nIt should fix #1105 and #5441.","created_at":"2015-10-20T13:42:19Z","updated_at":"2015-10-21T09:25:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1a34a63d15563536de31902411b12ed203d0c9e6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487/commits","review_comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487/comments","review_comment_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5487/comments","statuses_url":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/c2b41906e88159b7b8d79f68b4a5fee575d90249","head":{"label":"arjoly:example-node","ref":"example-node","sha":"c2b41906e88159b7b8d79f68b4a5fee575d90249","user":{"login":"arjoly","id":1274722,"avatar_url":"https://avatars.githubusercontent.com/u/1274722?v=3","gravatar_id":"","url":"https://api.github.com/users/arjoly","html_url":"https://github.com/arjoly","followers_url":"https://api.github.com/users/arjoly/followers","following_url":"https://api.github.com/users/arjoly/following{/other_user}","gists_url":"https://api.github.com/users/arjoly/gists{/gist_id}","starred_url":"https://api.github.com/users/arjoly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjoly/subscriptions","organizations_url":"https://api.github.com/users/arjoly/orgs","repos_url":"https://api.github.com/users/arjoly/repos","events_url":"https://api.github.com/users/arjoly/events{/privacy}","received_events_url":"https://api.github.com/users/arjoly/received_events","type":"User","site_admin":false},"repo":{"id":6792691,"name":"scikit-learn","full_name":"arjoly/scikit-learn","owner":{"login":"arjoly","id":1274722,"avatar_url":"https://avatars.githubusercontent.com/u/1274722?v=3","gravatar_id":"","url":"https://api.github.com/users/arjoly","html_url":"https://github.com/arjoly","followers_url":"https://api.github.com/users/arjoly/followers","following_url":"https://api.github.com/users/arjoly/following{/other_user}","gists_url":"https://api.github.com/users/arjoly/gists{/gist_id}","starred_url":"https://api.github.com/users/arjoly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arjoly/subscriptions","organizations_url":"https://api.github.com/users/arjoly/orgs","repos_url":"https://api.github.com/users/arjoly/repos","events_url":"https://api.github.com/users/arjoly/events{/privacy}","received_events_url":"https://api.github.com/users/arjoly/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/arjoly/scikit-learn","description":"scikit-learn: machine learning in Python","fork":true,"url":"https://api.github.com/repos/arjoly/scikit-learn","forks_url":"https://api.github.com/repos/arjoly/scikit-learn/forks","keys_url":"https://api.github.com/repos/arjoly/scikit-learn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/arjoly/scikit-learn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/arjoly/scikit-learn/teams","hooks_url":"https://api.github.com/repos/arjoly/scikit-learn/hooks","issue_events_url":"https://api.github.com/repos/arjoly/scikit-learn/issues/events{/number}","events_url":"https://api.github.com/repos/arjoly/scikit-learn/events","assignees_url":"https://api.github.com/repos/arjoly/scikit-learn/assignees{/user}","branches_url":"https://api.github.com/repos/arjoly/scikit-learn/branches{/branch}","tags_url":"https://api.github.com/repos/arjoly/scikit-learn/tags","blobs_url":"https://api.github.com/repos/arjoly/scikit-learn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/arjoly/scikit-learn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/arjoly/scikit-learn/git/refs{/sha}","trees_url":"https://api.github.com/repos/arjoly/scikit-learn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/arjoly/scikit-learn/statuses/{sha}","languages_url":"https://api.github.com/repos/arjoly/scikit-learn/languages","stargazers_url":"https://api.github.com/repos/arjoly/scikit-learn/stargazers","contributors_url":"https://api.github.com/repos/arjoly/scikit-learn/contributors","subscribers_url":"https://api.github.com/repos/arjoly/scikit-learn/subscribers","subscription_url":"https://api.github.com/repos/arjoly/scikit-learn/subscription","commits_url":"https://api.github.com/repos/arjoly/scikit-learn/commits{/sha}","git_commits_url":"https://api.github.com/repos/arjoly/scikit-learn/git/commits{/sha}","comments_url":"https://api.github.com/repos/arjoly/scikit-learn/comments{/number}","issue_comment_url":"https://api.github.com/repos/arjoly/scikit-learn/issues/comments{/number}","contents_url":"https://api.github.com/repos/arjoly/scikit-learn/contents/{+path}","compare_url":"https://api.github.com/repos/arjoly/scikit-learn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/arjoly/scikit-learn/merges","archive_url":"https://api.github.com/repos/arjoly/scikit-learn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/arjoly/scikit-learn/downloads","issues_url":"https://api.github.com/repos/arjoly/scikit-learn/issues{/number}","pulls_url":"https://api.github.com/repos/arjoly/scikit-learn/pulls{/number}","milestones_url":"https://api.github.com/repos/arjoly/scikit-learn/milestones{/number}","notifications_url":"https://api.github.com/repos/arjoly/scikit-learn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/arjoly/scikit-learn/labels{/name}","releases_url":"https://api.github.com/repos/arjoly/scikit-learn/releases{/id}","created_at":"2012-11-21T08:55:17Z","updated_at":"2015-04-14T08:09:33Z","pushed_at":"2015-10-21T09:10:12Z","git_url":"git://github.com/arjoly/scikit-learn.git","ssh_url":"git@github.com:arjoly/scikit-learn.git","clone_url":"https://github.com/arjoly/scikit-learn.git","svn_url":"https://github.com/arjoly/scikit-learn","homepage":"http://scikit-learn.org","size":75251,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":0,"forks":4,"open_issues":0,"watchers":3,"default_branch":"master"}},"base":{"label":"scikit-learn:master","ref":"master","sha":"baf4ade61ffd5908b48238967194361d5909a5f7","user":{"login":"scikit-learn","id":365630,"avatar_url":"https://avatars.githubusercontent.com/u/365630?v=3","gravatar_id":"","url":"https://api.github.com/users/scikit-learn","html_url":"https://github.com/scikit-learn","followers_url":"https://api.github.com/users/scikit-learn/followers","following_url":"https://api.github.com/users/scikit-learn/following{/other_user}","gists_url":"https://api.github.com/users/scikit-learn/gists{/gist_id}","starred_url":"https://api.github.com/users/scikit-learn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scikit-learn/subscriptions","organizations_url":"https://api.github.com/users/scikit-learn/orgs","repos_url":"https://api.github.com/users/scikit-learn/repos","events_url":"https://api.github.com/users/scikit-learn/events{/privacy}","received_events_url":"https://api.github.com/users/scikit-learn/received_events","type":"Organization","site_admin":false},"repo":{"id":843222,"name":"scikit-learn","full_name":"scikit-learn/scikit-learn","owner":{"login":"scikit-learn","id":365630,"avatar_url":"https://avatars.githubusercontent.com/u/365630?v=3","gravatar_id":"","url":"https://api.github.com/users/scikit-learn","html_url":"https://github.com/scikit-learn","followers_url":"https://api.github.com/users/scikit-learn/followers","following_url":"https://api.github.com/users/scikit-learn/following{/other_user}","gists_url":"https://api.github.com/users/scikit-learn/gists{/gist_id}","starred_url":"https://api.github.com/users/scikit-learn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scikit-learn/subscriptions","organizations_url":"https://api.github.com/users/scikit-learn/orgs","repos_url":"https://api.github.com/users/scikit-learn/repos","events_url":"https://api.github.com/users/scikit-learn/events{/privacy}","received_events_url":"https://api.github.com/users/scikit-learn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/scikit-learn/scikit-learn","description":"scikit-learn: machine learning in Python","fork":false,"url":"https://api.github.com/repos/scikit-learn/scikit-learn","forks_url":"https://api.github.com/repos/scikit-learn/scikit-learn/forks","keys_url":"https://api.github.com/repos/scikit-learn/scikit-learn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/scikit-learn/scikit-learn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/scikit-learn/scikit-learn/teams","hooks_url":"https://api.github.com/repos/scikit-learn/scikit-learn/hooks","issue_events_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/events{/number}","events_url":"https://api.github.com/repos/scikit-learn/scikit-learn/events","assignees_url":"https://api.github.com/repos/scikit-learn/scikit-learn/assignees{/user}","branches_url":"https://api.github.com/repos/scikit-learn/scikit-learn/branches{/branch}","tags_url":"https://api.github.com/repos/scikit-learn/scikit-learn/tags","blobs_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/refs{/sha}","trees_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/{sha}","languages_url":"https://api.github.com/repos/scikit-learn/scikit-learn/languages","stargazers_url":"https://api.github.com/repos/scikit-learn/scikit-learn/stargazers","contributors_url":"https://api.github.com/repos/scikit-learn/scikit-learn/contributors","subscribers_url":"https://api.github.com/repos/scikit-learn/scikit-learn/subscribers","subscription_url":"https://api.github.com/repos/scikit-learn/scikit-learn/subscription","commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/commits{/sha}","git_commits_url":"https://api.github.com/repos/scikit-learn/scikit-learn/git/commits{/sha}","comments_url":"https://api.github.com/repos/scikit-learn/scikit-learn/comments{/number}","issue_comment_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/comments{/number}","contents_url":"https://api.github.com/repos/scikit-learn/scikit-learn/contents/{+path}","compare_url":"https://api.github.com/repos/scikit-learn/scikit-learn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/scikit-learn/scikit-learn/merges","archive_url":"https://api.github.com/repos/scikit-learn/scikit-learn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/scikit-learn/scikit-learn/downloads","issues_url":"https://api.github.com/repos/scikit-learn/scikit-learn/issues{/number}","pulls_url":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls{/number}","milestones_url":"https://api.github.com/repos/scikit-learn/scikit-learn/milestones{/number}","notifications_url":"https://api.github.com/repos/scikit-learn/scikit-learn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/scikit-learn/scikit-learn/labels{/name}","releases_url":"https://api.github.com/repos/scikit-learn/scikit-learn/releases{/id}","created_at":"2010-08-17T09:43:38Z","updated_at":"2015-10-21T08:51:40Z","pushed_at":"2015-10-21T09:21:45Z","git_url":"git://github.com/scikit-learn/scikit-learn.git","ssh_url":"git@github.com:scikit-learn/scikit-learn.git","clone_url":"https://github.com/scikit-learn/scikit-learn.git","svn_url":"https://github.com/scikit-learn/scikit-learn","homepage":"http://scikit-learn.org","size":138274,"stargazers_count":7925,"watchers_count":7925,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4720,"mirror_url":null,"open_issues_count":775,"forks":4720,"open_issues":775,"watchers":7925,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487"},"html":{"href":"https://github.com/scikit-learn/scikit-learn/pull/5487"},"issue":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5487"},"comments":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/issues/5487/comments"},"review_comments":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487/comments"},"review_comment":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/pulls/5487/commits"},"statuses":{"href":"https://api.github.com/repos/scikit-learn/scikit-learn/statuses/c2b41906e88159b7b8d79f68b4a5fee575d90249"}}}}