decisionTree.ipynb (27948B)
1 { 2 "cells": [ 3 { 4 "cell_type": "code", 5 "execution_count": 83, 6 "metadata": {}, 7 "outputs": [ 8 { 9 "data": { 10 "text/html": [ 11 "<style>#sk-container-id-19 {\n", 12 " /* Definition of color scheme common for light and dark mode */\n", 13 " --sklearn-color-text: black;\n", 14 " --sklearn-color-line: gray;\n", 15 " /* Definition of color scheme for unfitted estimators */\n", 16 " --sklearn-color-unfitted-level-0: #fff5e6;\n", 17 " --sklearn-color-unfitted-level-1: #f6e4d2;\n", 18 " --sklearn-color-unfitted-level-2: #ffe0b3;\n", 19 " --sklearn-color-unfitted-level-3: chocolate;\n", 20 " /* Definition of color scheme for fitted estimators */\n", 21 " --sklearn-color-fitted-level-0: #f0f8ff;\n", 22 " --sklearn-color-fitted-level-1: #d4ebff;\n", 23 " --sklearn-color-fitted-level-2: #b3dbfd;\n", 24 " --sklearn-color-fitted-level-3: cornflowerblue;\n", 25 "\n", 26 " /* Specific color for light theme */\n", 27 " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n", 28 " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n", 29 " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n", 30 " --sklearn-color-icon: #696969;\n", 31 "\n", 32 " @media (prefers-color-scheme: dark) {\n", 33 " /* Redefinition of color scheme for dark theme */\n", 34 " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n", 35 " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n", 36 " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n", 37 " --sklearn-color-icon: #878787;\n", 38 " }\n", 39 "}\n", 40 "\n", 41 "#sk-container-id-19 {\n", 42 " color: var(--sklearn-color-text);\n", 43 "}\n", 44 "\n", 45 "#sk-container-id-19 pre {\n", 46 " padding: 0;\n", 47 "}\n", 48 "\n", 49 "#sk-container-id-19 input.sk-hidden--visually {\n", 50 " border: 0;\n", 51 " clip: rect(1px 1px 1px 1px);\n", 52 " clip: rect(1px, 1px, 1px, 1px);\n", 53 " height: 1px;\n", 54 " margin: -1px;\n", 55 " overflow: hidden;\n", 56 " padding: 0;\n", 57 " position: absolute;\n", 58 " width: 1px;\n", 59 "}\n", 60 "\n", 61 "#sk-container-id-19 div.sk-dashed-wrapped {\n", 62 " border: 1px dashed var(--sklearn-color-line);\n", 63 " margin: 0 0.4em 0.5em 0.4em;\n", 64 " box-sizing: border-box;\n", 65 " padding-bottom: 0.4em;\n", 66 " background-color: var(--sklearn-color-background);\n", 67 "}\n", 68 "\n", 69 "#sk-container-id-19 div.sk-container {\n", 70 " /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n", 71 " but bootstrap.min.css set `[hidden] { display: none !important; }`\n", 72 " so we also need the `!important` here to be able to override the\n", 73 " default hidden behavior on the sphinx rendered scikit-learn.org.\n", 74 " See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n", 75 " display: inline-block !important;\n", 76 " position: relative;\n", 77 "}\n", 78 "\n", 79 "#sk-container-id-19 div.sk-text-repr-fallback {\n", 80 " display: none;\n", 81 "}\n", 82 "\n", 83 "div.sk-parallel-item,\n", 84 "div.sk-serial,\n", 85 "div.sk-item {\n", 86 " /* draw centered vertical line to link estimators */\n", 87 " background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n", 88 " background-size: 2px 100%;\n", 89 " background-repeat: no-repeat;\n", 90 " background-position: center center;\n", 91 "}\n", 92 "\n", 93 "/* Parallel-specific style estimator block */\n", 94 "\n", 95 "#sk-container-id-19 div.sk-parallel-item::after {\n", 96 " content: \"\";\n", 97 " width: 100%;\n", 98 " border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n", 99 " flex-grow: 1;\n", 100 "}\n", 101 "\n", 102 "#sk-container-id-19 div.sk-parallel {\n", 103 " display: flex;\n", 104 " align-items: stretch;\n", 105 " justify-content: center;\n", 106 " background-color: var(--sklearn-color-background);\n", 107 " position: relative;\n", 108 "}\n", 109 "\n", 110 "#sk-container-id-19 div.sk-parallel-item {\n", 111 " display: flex;\n", 112 " flex-direction: column;\n", 113 "}\n", 114 "\n", 115 "#sk-container-id-19 div.sk-parallel-item:first-child::after {\n", 116 " align-self: flex-end;\n", 117 " width: 50%;\n", 118 "}\n", 119 "\n", 120 "#sk-container-id-19 div.sk-parallel-item:last-child::after {\n", 121 " align-self: flex-start;\n", 122 " width: 50%;\n", 123 "}\n", 124 "\n", 125 "#sk-container-id-19 div.sk-parallel-item:only-child::after {\n", 126 " width: 0;\n", 127 "}\n", 128 "\n", 129 "/* Serial-specific style estimator block */\n", 130 "\n", 131 "#sk-container-id-19 div.sk-serial {\n", 132 " display: flex;\n", 133 " flex-direction: column;\n", 134 " align-items: center;\n", 135 " background-color: var(--sklearn-color-background);\n", 136 " padding-right: 1em;\n", 137 " padding-left: 1em;\n", 138 "}\n", 139 "\n", 140 "\n", 141 "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n", 142 "clickable and can be expanded/collapsed.\n", 143 "- Pipeline and ColumnTransformer use this feature and define the default style\n", 144 "- Estimators will overwrite some part of the style using the `sk-estimator` class\n", 145 "*/\n", 146 "\n", 147 "/* Pipeline and ColumnTransformer style (default) */\n", 148 "\n", 149 "#sk-container-id-19 div.sk-toggleable {\n", 150 " /* Default theme specific background. It is overwritten whether we have a\n", 151 " specific estimator or a Pipeline/ColumnTransformer */\n", 152 " background-color: var(--sklearn-color-background);\n", 153 "}\n", 154 "\n", 155 "/* Toggleable label */\n", 156 "#sk-container-id-19 label.sk-toggleable__label {\n", 157 " cursor: pointer;\n", 158 " display: block;\n", 159 " width: 100%;\n", 160 " margin-bottom: 0;\n", 161 " padding: 0.5em;\n", 162 " box-sizing: border-box;\n", 163 " text-align: center;\n", 164 "}\n", 165 "\n", 166 "#sk-container-id-19 label.sk-toggleable__label-arrow:before {\n", 167 " /* Arrow on the left of the label */\n", 168 " content: \"▸\";\n", 169 " float: left;\n", 170 " margin-right: 0.25em;\n", 171 " color: var(--sklearn-color-icon);\n", 172 "}\n", 173 "\n", 174 "#sk-container-id-19 label.sk-toggleable__label-arrow:hover:before {\n", 175 " color: var(--sklearn-color-text);\n", 176 "}\n", 177 "\n", 178 "/* Toggleable content - dropdown */\n", 179 "\n", 180 "#sk-container-id-19 div.sk-toggleable__content {\n", 181 " max-height: 0;\n", 182 " max-width: 0;\n", 183 " overflow: hidden;\n", 184 " text-align: left;\n", 185 " /* unfitted */\n", 186 " background-color: var(--sklearn-color-unfitted-level-0);\n", 187 "}\n", 188 "\n", 189 "#sk-container-id-19 div.sk-toggleable__content.fitted {\n", 190 " /* fitted */\n", 191 " background-color: var(--sklearn-color-fitted-level-0);\n", 192 "}\n", 193 "\n", 194 "#sk-container-id-19 div.sk-toggleable__content pre {\n", 195 " margin: 0.2em;\n", 196 " border-radius: 0.25em;\n", 197 " color: var(--sklearn-color-text);\n", 198 " /* unfitted */\n", 199 " background-color: var(--sklearn-color-unfitted-level-0);\n", 200 "}\n", 201 "\n", 202 "#sk-container-id-19 div.sk-toggleable__content.fitted pre {\n", 203 " /* unfitted */\n", 204 " background-color: var(--sklearn-color-fitted-level-0);\n", 205 "}\n", 206 "\n", 207 "#sk-container-id-19 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n", 208 " /* Expand drop-down */\n", 209 " max-height: 200px;\n", 210 " max-width: 100%;\n", 211 " overflow: auto;\n", 212 "}\n", 213 "\n", 214 "#sk-container-id-19 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n", 215 " content: \"▾\";\n", 216 "}\n", 217 "\n", 218 "/* Pipeline/ColumnTransformer-specific style */\n", 219 "\n", 220 "#sk-container-id-19 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", 221 " color: var(--sklearn-color-text);\n", 222 " background-color: var(--sklearn-color-unfitted-level-2);\n", 223 "}\n", 224 "\n", 225 "#sk-container-id-19 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", 226 " background-color: var(--sklearn-color-fitted-level-2);\n", 227 "}\n", 228 "\n", 229 "/* Estimator-specific style */\n", 230 "\n", 231 "/* Colorize estimator box */\n", 232 "#sk-container-id-19 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", 233 " /* unfitted */\n", 234 " background-color: var(--sklearn-color-unfitted-level-2);\n", 235 "}\n", 236 "\n", 237 "#sk-container-id-19 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", 238 " /* fitted */\n", 239 " background-color: var(--sklearn-color-fitted-level-2);\n", 240 "}\n", 241 "\n", 242 "#sk-container-id-19 div.sk-label label.sk-toggleable__label,\n", 243 "#sk-container-id-19 div.sk-label label {\n", 244 " /* The background is the default theme color */\n", 245 " color: var(--sklearn-color-text-on-default-background);\n", 246 "}\n", 247 "\n", 248 "/* On hover, darken the color of the background */\n", 249 "#sk-container-id-19 div.sk-label:hover label.sk-toggleable__label {\n", 250 " color: var(--sklearn-color-text);\n", 251 " background-color: var(--sklearn-color-unfitted-level-2);\n", 252 "}\n", 253 "\n", 254 "/* Label box, darken color on hover, fitted */\n", 255 "#sk-container-id-19 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n", 256 " color: var(--sklearn-color-text);\n", 257 " background-color: var(--sklearn-color-fitted-level-2);\n", 258 "}\n", 259 "\n", 260 "/* Estimator label */\n", 261 "\n", 262 "#sk-container-id-19 div.sk-label label {\n", 263 " font-family: monospace;\n", 264 " font-weight: bold;\n", 265 " display: inline-block;\n", 266 " line-height: 1.2em;\n", 267 "}\n", 268 "\n", 269 "#sk-container-id-19 div.sk-label-container {\n", 270 " text-align: center;\n", 271 "}\n", 272 "\n", 273 "/* Estimator-specific */\n", 274 "#sk-container-id-19 div.sk-estimator {\n", 275 " font-family: monospace;\n", 276 " border: 1px dotted var(--sklearn-color-border-box);\n", 277 " border-radius: 0.25em;\n", 278 " box-sizing: border-box;\n", 279 " margin-bottom: 0.5em;\n", 280 " /* unfitted */\n", 281 " background-color: var(--sklearn-color-unfitted-level-0);\n", 282 "}\n", 283 "\n", 284 "#sk-container-id-19 div.sk-estimator.fitted {\n", 285 " /* fitted */\n", 286 " background-color: var(--sklearn-color-fitted-level-0);\n", 287 "}\n", 288 "\n", 289 "/* on hover */\n", 290 "#sk-container-id-19 div.sk-estimator:hover {\n", 291 " /* unfitted */\n", 292 " background-color: var(--sklearn-color-unfitted-level-2);\n", 293 "}\n", 294 "\n", 295 "#sk-container-id-19 div.sk-estimator.fitted:hover {\n", 296 " /* fitted */\n", 297 " background-color: var(--sklearn-color-fitted-level-2);\n", 298 "}\n", 299 "\n", 300 "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n", 301 "\n", 302 "/* Common style for \"i\" and \"?\" */\n", 303 "\n", 304 ".sk-estimator-doc-link,\n", 305 "a:link.sk-estimator-doc-link,\n", 306 "a:visited.sk-estimator-doc-link {\n", 307 " float: right;\n", 308 " font-size: smaller;\n", 309 " line-height: 1em;\n", 310 " font-family: monospace;\n", 311 " background-color: var(--sklearn-color-background);\n", 312 " border-radius: 1em;\n", 313 " height: 1em;\n", 314 " width: 1em;\n", 315 " text-decoration: none !important;\n", 316 " margin-left: 1ex;\n", 317 " /* unfitted */\n", 318 " border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n", 319 " color: var(--sklearn-color-unfitted-level-1);\n", 320 "}\n", 321 "\n", 322 ".sk-estimator-doc-link.fitted,\n", 323 "a:link.sk-estimator-doc-link.fitted,\n", 324 "a:visited.sk-estimator-doc-link.fitted {\n", 325 " /* fitted */\n", 326 " border: var(--sklearn-color-fitted-level-1) 1pt solid;\n", 327 " color: var(--sklearn-color-fitted-level-1);\n", 328 "}\n", 329 "\n", 330 "/* On hover */\n", 331 "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n", 332 ".sk-estimator-doc-link:hover,\n", 333 "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n", 334 ".sk-estimator-doc-link:hover {\n", 335 " /* unfitted */\n", 336 " background-color: var(--sklearn-color-unfitted-level-3);\n", 337 " color: var(--sklearn-color-background);\n", 338 " text-decoration: none;\n", 339 "}\n", 340 "\n", 341 "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n", 342 ".sk-estimator-doc-link.fitted:hover,\n", 343 "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n", 344 ".sk-estimator-doc-link.fitted:hover {\n", 345 " /* fitted */\n", 346 " background-color: var(--sklearn-color-fitted-level-3);\n", 347 " color: var(--sklearn-color-background);\n", 348 " text-decoration: none;\n", 349 "}\n", 350 "\n", 351 "/* Span, style for the box shown on hovering the info icon */\n", 352 ".sk-estimator-doc-link span {\n", 353 " display: none;\n", 354 " z-index: 9999;\n", 355 " position: relative;\n", 356 " font-weight: normal;\n", 357 " right: .2ex;\n", 358 " padding: .5ex;\n", 359 " margin: .5ex;\n", 360 " width: min-content;\n", 361 " min-width: 20ex;\n", 362 " max-width: 50ex;\n", 363 " color: var(--sklearn-color-text);\n", 364 " box-shadow: 2pt 2pt 4pt #999;\n", 365 " /* unfitted */\n", 366 " background: var(--sklearn-color-unfitted-level-0);\n", 367 " border: .5pt solid var(--sklearn-color-unfitted-level-3);\n", 368 "}\n", 369 "\n", 370 ".sk-estimator-doc-link.fitted span {\n", 371 " /* fitted */\n", 372 " background: var(--sklearn-color-fitted-level-0);\n", 373 " border: var(--sklearn-color-fitted-level-3);\n", 374 "}\n", 375 "\n", 376 ".sk-estimator-doc-link:hover span {\n", 377 " display: block;\n", 378 "}\n", 379 "\n", 380 "/* \"?\"-specific style due to the `<a>` HTML tag */\n", 381 "\n", 382 "#sk-container-id-19 a.estimator_doc_link {\n", 383 " float: right;\n", 384 " font-size: 1rem;\n", 385 " line-height: 1em;\n", 386 " font-family: monospace;\n", 387 " background-color: var(--sklearn-color-background);\n", 388 " border-radius: 1rem;\n", 389 " height: 1rem;\n", 390 " width: 1rem;\n", 391 " text-decoration: none;\n", 392 " /* unfitted */\n", 393 " color: var(--sklearn-color-unfitted-level-1);\n", 394 " border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n", 395 "}\n", 396 "\n", 397 "#sk-container-id-19 a.estimator_doc_link.fitted {\n", 398 " /* fitted */\n", 399 " border: var(--sklearn-color-fitted-level-1) 1pt solid;\n", 400 " color: var(--sklearn-color-fitted-level-1);\n", 401 "}\n", 402 "\n", 403 "/* On hover */\n", 404 "#sk-container-id-19 a.estimator_doc_link:hover {\n", 405 " /* unfitted */\n", 406 " background-color: var(--sklearn-color-unfitted-level-3);\n", 407 " color: var(--sklearn-color-background);\n", 408 " text-decoration: none;\n", 409 "}\n", 410 "\n", 411 "#sk-container-id-19 a.estimator_doc_link.fitted:hover {\n", 412 " /* fitted */\n", 413 " background-color: var(--sklearn-color-fitted-level-3);\n", 414 "}\n", 415 "</style><div id=\"sk-container-id-19\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeClassifier(max_depth=2, random_state=10)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-19\" type=\"checkbox\" checked><label for=\"sk-estimator-id-19\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow fitted\"> DecisionTreeClassifier<a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.tree.DecisionTreeClassifier.html\">?<span>Documentation for DecisionTreeClassifier</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div class=\"sk-toggleable__content fitted\"><pre>DecisionTreeClassifier(max_depth=2, random_state=10)</pre></div> </div></div></div></div>" 416 ], 417 "text/plain": [ 418 "DecisionTreeClassifier(max_depth=2, random_state=10)" 419 ] 420 }, 421 "execution_count": 83, 422 "metadata": {}, 423 "output_type": "execute_result" 424 } 425 ], 426 "source": [ 427 "from sklearn.datasets import load_iris\n", 428 "from sklearn.tree import DecisionTreeClassifier\n", 429 "\n", 430 "iris = load_iris(as_frame=True)\n", 431 "X_iris = iris.data[['petal length (cm)' , 'petal width (cm)']].values\n", 432 "y_iris = iris.target\n", 433 "\n", 434 "tree_clf = DecisionTreeClassifier(max_depth=2, random_state=10, criterion='gini')\n", 435 "tree_clf.fit(X_iris,y_iris)" 436 ] 437 }, 438 { 439 "cell_type": "code", 440 "execution_count": 84, 441 "metadata": {}, 442 "outputs": [], 443 "source": [ 444 "from sklearn.tree import export_graphviz\n", 445 "\n", 446 "graphData = export_graphviz(\n", 447 " tree_clf,\n", 448 " out_file='../graphs/iris_tree.dot',\n", 449 " feature_names=[\"petal length (cm)\", \"petal width (cm)\"],\n", 450 " class_names=iris.target_names,\n", 451 " rounded=True,\n", 452 " filled=True\n", 453 ")" 454 ] 455 }, 456 { 457 "cell_type": "code", 458 "execution_count": 85, 459 "metadata": {}, 460 "outputs": [ 461 { 462 "data": { 463 "image/svg+xml": [ 464 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", 465 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", 466 " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", 467 "<!-- Generated by graphviz version 2.43.0 (0)\n", 468 " -->\n", 469 "<!-- Title: Tree Pages: 1 -->\n", 470 "<svg width=\"412pt\" height=\"314pt\"\n", 471 " viewBox=\"0.00 0.00 412.00 314.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", 472 "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 310)\">\n", 473 "<title>Tree</title>\n", 474 "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-310 408,-310 408,4 -4,4\"/>\n", 475 "<!-- 0 -->\n", 476 "<g id=\"node1\" class=\"node\">\n", 477 "<title>0</title>\n", 478 "<path fill=\"#ffffff\" stroke=\"black\" d=\"M242.5,-306C242.5,-306 81.5,-306 81.5,-306 75.5,-306 69.5,-300 69.5,-294 69.5,-294 69.5,-235 69.5,-235 69.5,-229 75.5,-223 81.5,-223 81.5,-223 242.5,-223 242.5,-223 248.5,-223 254.5,-229 254.5,-235 254.5,-235 254.5,-294 254.5,-294 254.5,-300 248.5,-306 242.5,-306\"/>\n", 479 "<text text-anchor=\"middle\" x=\"162\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">petal width (cm) <= 0.8</text>\n", 480 "<text text-anchor=\"middle\" x=\"162\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.667</text>\n", 481 "<text text-anchor=\"middle\" x=\"162\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 150</text>\n", 482 "<text text-anchor=\"middle\" x=\"162\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [50, 50, 50]</text>\n", 483 "<text text-anchor=\"middle\" x=\"162\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = setosa</text>\n", 484 "</g>\n", 485 "<!-- 1 -->\n", 486 "<g id=\"node2\" class=\"node\">\n", 487 "<title>1</title>\n", 488 "<path fill=\"#e58139\" stroke=\"black\" d=\"M128,-179.5C128,-179.5 12,-179.5 12,-179.5 6,-179.5 0,-173.5 0,-167.5 0,-167.5 0,-123.5 0,-123.5 0,-117.5 6,-111.5 12,-111.5 12,-111.5 128,-111.5 128,-111.5 134,-111.5 140,-117.5 140,-123.5 140,-123.5 140,-167.5 140,-167.5 140,-173.5 134,-179.5 128,-179.5\"/>\n", 489 "<text text-anchor=\"middle\" x=\"70\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\n", 490 "<text text-anchor=\"middle\" x=\"70\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 50</text>\n", 491 "<text text-anchor=\"middle\" x=\"70\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [50, 0, 0]</text>\n", 492 "<text text-anchor=\"middle\" x=\"70\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = setosa</text>\n", 493 "</g>\n", 494 "<!-- 0->1 -->\n", 495 "<g id=\"edge1\" class=\"edge\">\n", 496 "<title>0->1</title>\n", 497 "<path fill=\"none\" stroke=\"black\" d=\"M130.08,-222.91C121.14,-211.54 111.42,-199.18 102.45,-187.77\"/>\n", 498 "<polygon fill=\"black\" stroke=\"black\" points=\"105.01,-185.36 96.08,-179.67 99.51,-189.69 105.01,-185.36\"/>\n", 499 "<text text-anchor=\"middle\" x=\"93.11\" y=\"-200.79\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\n", 500 "</g>\n", 501 "<!-- 2 -->\n", 502 "<g id=\"node3\" class=\"node\">\n", 503 "<title>2</title>\n", 504 "<path fill=\"#ffffff\" stroke=\"black\" d=\"M340,-187C340,-187 170,-187 170,-187 164,-187 158,-181 158,-175 158,-175 158,-116 158,-116 158,-110 164,-104 170,-104 170,-104 340,-104 340,-104 346,-104 352,-110 352,-116 352,-116 352,-175 352,-175 352,-181 346,-187 340,-187\"/>\n", 505 "<text text-anchor=\"middle\" x=\"255\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">petal width (cm) <= 1.75</text>\n", 506 "<text text-anchor=\"middle\" x=\"255\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.5</text>\n", 507 "<text text-anchor=\"middle\" x=\"255\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 100</text>\n", 508 "<text text-anchor=\"middle\" x=\"255\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 50, 50]</text>\n", 509 "<text text-anchor=\"middle\" x=\"255\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = versicolor</text>\n", 510 "</g>\n", 511 "<!-- 0->2 -->\n", 512 "<g id=\"edge2\" class=\"edge\">\n", 513 "<title>0->2</title>\n", 514 "<path fill=\"none\" stroke=\"black\" d=\"M194.27,-222.91C201.41,-213.92 209.04,-204.32 216.41,-195.05\"/>\n", 515 "<polygon fill=\"black\" stroke=\"black\" points=\"219.31,-197.03 222.79,-187.02 213.83,-192.67 219.31,-197.03\"/>\n", 516 "<text text-anchor=\"middle\" x=\"225.63\" y=\"-208.16\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\n", 517 "</g>\n", 518 "<!-- 3 -->\n", 519 "<g id=\"node4\" class=\"node\">\n", 520 "<title>3</title>\n", 521 "<path fill=\"#4de88e\" stroke=\"black\" d=\"M234,-68C234,-68 118,-68 118,-68 112,-68 106,-62 106,-56 106,-56 106,-12 106,-12 106,-6 112,0 118,0 118,0 234,0 234,0 240,0 246,-6 246,-12 246,-12 246,-56 246,-56 246,-62 240,-68 234,-68\"/>\n", 522 "<text text-anchor=\"middle\" x=\"176\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.168</text>\n", 523 "<text text-anchor=\"middle\" x=\"176\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 54</text>\n", 524 "<text text-anchor=\"middle\" x=\"176\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 49, 5]</text>\n", 525 "<text text-anchor=\"middle\" x=\"176\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = versicolor</text>\n", 526 "</g>\n", 527 "<!-- 2->3 -->\n", 528 "<g id=\"edge3\" class=\"edge\">\n", 529 "<title>2->3</title>\n", 530 "<path fill=\"none\" stroke=\"black\" d=\"M225.58,-103.73C219.2,-94.88 212.44,-85.51 206.03,-76.63\"/>\n", 531 "<polygon fill=\"black\" stroke=\"black\" points=\"208.71,-74.36 200.02,-68.3 203.04,-78.46 208.71,-74.36\"/>\n", 532 "</g>\n", 533 "<!-- 4 -->\n", 534 "<g id=\"node5\" class=\"node\">\n", 535 "<title>4</title>\n", 536 "<path fill=\"#843de6\" stroke=\"black\" d=\"M392,-68C392,-68 276,-68 276,-68 270,-68 264,-62 264,-56 264,-56 264,-12 264,-12 264,-6 270,0 276,0 276,0 392,0 392,0 398,0 404,-6 404,-12 404,-12 404,-56 404,-56 404,-62 398,-68 392,-68\"/>\n", 537 "<text text-anchor=\"middle\" x=\"334\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.043</text>\n", 538 "<text text-anchor=\"middle\" x=\"334\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 46</text>\n", 539 "<text text-anchor=\"middle\" x=\"334\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 1, 45]</text>\n", 540 "<text text-anchor=\"middle\" x=\"334\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = virginica</text>\n", 541 "</g>\n", 542 "<!-- 2->4 -->\n", 543 "<g id=\"edge4\" class=\"edge\">\n", 544 "<title>2->4</title>\n", 545 "<path fill=\"none\" stroke=\"black\" d=\"M284.42,-103.73C290.8,-94.88 297.56,-85.51 303.97,-76.63\"/>\n", 546 "<polygon fill=\"black\" stroke=\"black\" points=\"306.96,-78.46 309.98,-68.3 301.29,-74.36 306.96,-78.46\"/>\n", 547 "</g>\n", 548 "</g>\n", 549 "</svg>\n" 550 ], 551 "text/plain": [ 552 "<graphviz.sources.Source at 0x7ff614a01850>" 553 ] 554 }, 555 "execution_count": 85, 556 "metadata": {}, 557 "output_type": "execute_result" 558 } 559 ], 560 "source": [ 561 "from graphviz import Source\n", 562 "\n", 563 "Source.from_file('../graphs/iris_tree.dot')" 564 ] 565 }, 566 { 567 "cell_type": "code", 568 "execution_count": 86, 569 "metadata": {}, 570 "outputs": [ 571 { 572 "data": { 573 "text/plain": [ 574 "array([[0. , 0.91, 0.09]])" 575 ] 576 }, 577 "execution_count": 86, 578 "metadata": {}, 579 "output_type": "execute_result" 580 } 581 ], 582 "source": [ 583 "# Probability based on the values list.\n", 584 "tree_clf.predict_proba([[4 , 1]]).round(2)" 585 ] 586 } 587 ], 588 "metadata": { 589 "kernelspec": { 590 "display_name": "notebook", 591 "language": "python", 592 "name": "notebook" 593 }, 594 "language_info": { 595 "codemirror_mode": { 596 "name": "ipython", 597 "version": 3 598 }, 599 "file_extension": ".py", 600 "mimetype": "text/x-python", 601 "name": "python", 602 "nbconvert_exporter": "python", 603 "pygments_lexer": "ipython3", 604 "version": "3.11.2" 605 } 606 }, 607 "nbformat": 4, 608 "nbformat_minor": 2 609 }