File size: 7,566 Bytes
0b73a08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
{
"cells": [
{
"cell_type": "markdown",
"id": "ab540ee7",
"metadata": {},
"source": [
"# Decision Tree"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "92d3ce84",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import confusion_matrix\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.metrics import classification_report\n",
"from sklearn.datasets import load_iris\n",
"iris=load_iris()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dd4c544d",
"metadata": {},
"outputs": [],
"source": [
"X,y=iris.data,iris.target"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "abe99084",
"metadata": {},
"outputs": [],
"source": [
"def train_using_gini(X_train, y_train):\n",
" clf_gini = DecisionTreeClassifier(criterion = \"gini\", random_state = 100,max_depth=3, min_samples_leaf=4)\n",
" clf_gini.fit(X_train, y_train)\n",
" return clf_gini"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3e9ddda5",
"metadata": {},
"outputs": [],
"source": [
"#Using Entropy\n",
"def train_using_entropy(X_train,y_train):\n",
"#Creating a classifier object\n",
" clf_entropy = DecisionTreeClassifier(criterion=\"entropy\",random_state = 100,max_depth=3,min_samples_leaf=4)\n",
"#Training\n",
" clf_entropy.fit(X_train,y_train)\n",
" return clf_entropy"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "74fd9b39",
"metadata": {},
"outputs": [],
"source": [
"def prediction(X_test,clf_object):\n",
" y_pred=clf_object.predict(X_test)\n",
" print(\"Predicted values:\",y_pred)\n",
" return y_pred"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0b47818b",
"metadata": {},
"outputs": [],
"source": [
"#Function to calculate accuracy\n",
"def cal_accuracy(y_test,y_pred):\n",
" print(\"Confusion Matrix: \",confusion_matrix(y_test,y_pred))\n",
" print(\"Accuracy:\",accuracy_score(y_test,y_pred)*100)\n",
" print(\"Report :\",classification_report(y_test,y_pred))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0f94ba7d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimensions for training data (105, 4)\n",
"Dimensions for testing data (105,)\n"
]
}
],
"source": [
"X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.3, random_state = 100)\n",
"print(\"Dimensions for training data\",X_train.shape)\n",
"print(\"Dimensions for testing data\",y_train.shape)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a7ed365c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Results Using Gini Index:\n",
"Predicted values: [2 0 2 0 2 2 0 0 2 0 0 2 0 0 2 1 1 2 2 2 2 0 2 0 1 2 1 0 1 2 1 1 1 0 0 1 0\n",
" 1 2 2 0 1 2 2 0]\n",
"Confusion Matrix: [[16 0 0]\n",
" [ 0 10 1]\n",
" [ 0 1 17]]\n",
"Accuracy: 95.55555555555556\n",
"Report : precision recall f1-score support\n",
"\n",
" 0 1.00 1.00 1.00 16\n",
" 1 0.91 0.91 0.91 11\n",
" 2 0.94 0.94 0.94 18\n",
"\n",
" accuracy 0.96 45\n",
" macro avg 0.95 0.95 0.95 45\n",
"weighted avg 0.96 0.96 0.96 45\n",
"\n"
]
}
],
"source": [
"#Gini Index\n",
"clf_gini = train_using_gini(X_train, y_train)\n",
"print(\"Results Using Gini Index:\")\n",
"# Prediction using gini\n",
"y_pred_gini = prediction(X_test, clf_gini)\n",
"cal_accuracy(y_test, y_pred_gini)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "0cd3759c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted values: [2 0 2 0 2 2 0 0 2 0 0 2 0 0 2 1 1 2 2 2 2 0 2 0 1 2 1 0 1 2 1 1 1 0 0 1 0\n",
" 1 2 2 0 1 2 2 0]\n",
"Confusion Matrix: [[16 0 0]\n",
" [ 0 10 1]\n",
" [ 0 1 17]]\n",
"Accuracy: 95.55555555555556\n",
"Report : precision recall f1-score support\n",
"\n",
" 0 1.00 1.00 1.00 16\n",
" 1 0.91 0.91 0.91 11\n",
" 2 0.94 0.94 0.94 18\n",
"\n",
" accuracy 0.96 45\n",
" macro avg 0.95 0.95 0.95 45\n",
"weighted avg 0.96 0.96 0.96 45\n",
"\n"
]
}
],
"source": [
"#Analysing Metrics using entropy\n",
"clf_entropy = train_using_entropy(X_train,y_train)\n",
"# Prediction using entropy\n",
"y_pred_entropy = prediction(X_test, clf_entropy)\n",
"cal_accuracy(y_test, y_pred_entropy)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "bfb36a8a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Results Using Gini Index:\n",
"Predicted values: [2 0 2 0 2 2 0 0 2 0 0 2 0 0 2 1 1 2 2 2 2 0 2 0 1 2 1 0 1 2 1 1 1 0 0 1 0\n",
" 1 2 2 0 1 2 2 0]\n",
"Confusion Matrix: [[16 0 0]\n",
" [ 0 10 1]\n",
" [ 0 1 17]]\n",
"Accuracy: 95.55555555555556\n",
"Report : precision recall f1-score support\n",
"\n",
" 0 1.00 1.00 1.00 16\n",
" 1 0.91 0.91 0.91 11\n",
" 2 0.94 0.94 0.94 18\n",
"\n",
" accuracy 0.96 45\n",
" macro avg 0.95 0.95 0.95 45\n",
"weighted avg 0.96 0.96 0.96 45\n",
"\n"
]
}
],
"source": [
"#lets observe what the result will be if we change dept to 2 and leafs to 3\n",
"def train_using_gini(X_train, y_train):\n",
" clf_gini = DecisionTreeClassifier(criterion = \"gini\", random_state = 150,max_depth=5, min_samples_leaf=3)\n",
" clf_gini.fit(X_train, y_train)\n",
" return clf_gini\n",
"clf_gini = train_using_gini(X_train, y_train)\n",
"print(\"Results Using Gini Index:\")\n",
"# Prediction using gini\n",
"y_pred_gini = prediction(X_test, clf_gini)\n",
"cal_accuracy(y_test, y_pred_gini)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ec89b9d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|