|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import numpy as np\n", |
| 10 | + "from sklearn import datasets\n", |
| 11 | + "from sklearn.tree import DecisionTreeClassifier\n", |
| 12 | + "from sklearn.tree import export_graphviz\n", |
| 13 | + "from sklearn.model_selection import train_test_split" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 2, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "iris = datasets.load_iris()\n", |
| 23 | + "x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state = 1)" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": 3, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "data": { |
| 33 | + "text/plain": [ |
| 34 | + "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n", |
| 35 | + " max_features=None, max_leaf_nodes=None,\n", |
| 36 | + " min_impurity_decrease=0.0, min_impurity_split=None,\n", |
| 37 | + " min_samples_leaf=1, min_samples_split=2,\n", |
| 38 | + " min_weight_fraction_leaf=0.0, presort=False, random_state=None,\n", |
| 39 | + " splitter='best')" |
| 40 | + ] |
| 41 | + }, |
| 42 | + "execution_count": 3, |
| 43 | + "metadata": {}, |
| 44 | + "output_type": "execute_result" |
| 45 | + } |
| 46 | + ], |
| 47 | + "source": [ |
| 48 | + "clf = DecisionTreeClassifier()\n", |
| 49 | + "clf.fit(x_train,y_train)#data is fitted in the several features of Decision Tree Classifier" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 4, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "import pydotplus\n", |
| 59 | + "dot_data=export_graphviz(clf,out_file=None,feature_names=iris.feature_names,class_names=iris.target_names)\n", |
| 60 | + "import os\n", |
| 61 | + "os.environ[\"PATH\"] += os.pathsep + r'C:\\Users\\Psyfer\\Anaconda3\\Library\\bin\\graphviz'" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": 5, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "data": { |
| 71 | + "text/plain": [ |
| 72 | + "True" |
| 73 | + ] |
| 74 | + }, |
| 75 | + "execution_count": 5, |
| 76 | + "metadata": {}, |
| 77 | + "output_type": "execute_result" |
| 78 | + } |
| 79 | + ], |
| 80 | + "source": [ |
| 81 | + "dot_data = export_graphviz(clf,out_file=None)\n", |
| 82 | + "graph = pydotplus.graph_from_dot_data(dot_data)\n", |
| 83 | + "graph.write_pdf('iris22.pdf')" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 6, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "y_train_pred = clf.predict(x_train)\n", |
| 93 | + "y_test_pred = clf.predict(x_test)" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": 7, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "from sklearn.metrics import confusion_matrix" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 8, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "data": { |
| 112 | + "text/plain": [ |
| 113 | + "array([[37, 0, 0],\n", |
| 114 | + " [ 0, 34, 0],\n", |
| 115 | + " [ 0, 0, 41]], dtype=int64)" |
| 116 | + ] |
| 117 | + }, |
| 118 | + "execution_count": 8, |
| 119 | + "metadata": {}, |
| 120 | + "output_type": "execute_result" |
| 121 | + } |
| 122 | + ], |
| 123 | + "source": [ |
| 124 | + "confusion_matrix(y_train,y_train_pred)" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 9, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "data": { |
| 134 | + "text/plain": [ |
| 135 | + "array([[13, 0, 0],\n", |
| 136 | + " [ 0, 15, 1],\n", |
| 137 | + " [ 0, 0, 9]], dtype=int64)" |
| 138 | + ] |
| 139 | + }, |
| 140 | + "execution_count": 9, |
| 141 | + "metadata": {}, |
| 142 | + "output_type": "execute_result" |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "confusion_matrix(y_test,y_test_pred)" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": null, |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [] |
| 155 | + } |
| 156 | + ], |
| 157 | + "metadata": { |
| 158 | + "kernelspec": { |
| 159 | + "display_name": "Python 3", |
| 160 | + "language": "python", |
| 161 | + "name": "python3" |
| 162 | + }, |
| 163 | + "language_info": { |
| 164 | + "codemirror_mode": { |
| 165 | + "name": "ipython", |
| 166 | + "version": 3 |
| 167 | + }, |
| 168 | + "file_extension": ".py", |
| 169 | + "mimetype": "text/x-python", |
| 170 | + "name": "python", |
| 171 | + "nbconvert_exporter": "python", |
| 172 | + "pygments_lexer": "ipython3", |
| 173 | + "version": "3.7.1" |
| 174 | + } |
| 175 | + }, |
| 176 | + "nbformat": 4, |
| 177 | + "nbformat_minor": 2 |
| 178 | +} |
0 commit comments