public
Last active

milk_tree_learner

  • Download Gist
gistfile1.txt
1
# to come
milk_tree_learner.ipynb
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
{
"metadata": {
"name": "milk_tree_learner"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import milk"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simulate some labeled data in a two-dimensional feature space."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"features = np.random.randn(100,2) # 2d array of features: 100 examples of 2 features each\n",
"labels = np.empty(100)\n",
"for i in range(100):\n",
" if features[i,0] < 0:\n",
" if features[i,1] < -1:\n",
" labels[i] = np.random.rand() < .001\n",
" else:\n",
" labels[i] = np.random.rand() < .999\n",
" else:\n",
" if features[i,1] < 1:\n",
" labels[i] = np.random.rand() < .001\n",
" else:\n",
" labels[i] = np.random.rand() < .999\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the decision tree for this data?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the data is two-dimensional, we can take a look at it easily."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plot(features[labels==True,0], features[labels==True,1], 'kx', mec='k', ms=6, mew=3)\n",
"plot(features[labels==False,0], features[labels==False,1], 'wo', mec='k', ms=8, mew=1)\n",
"grid()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD9CAYAAAClQCyNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXtsHNUZxY+3wXH8WDvFeSAFCCKGECdk44qHaB2MUOwo\ntrurIpRIFcoaHCRUKkUKCQKT2ImUoBakQCu19jbCaUWp1FKzZls1DyCbhD+aJmteCggRiahUgjhQ\nEvMK8uP2j3QXZzO7O7NzZ+bO3fOTVspm1rPft3PnzL3nfnOnTAghQAghxHcEvA6AEEJIcVDACSHE\np1DACSHEp1DACSHEp1DACSHEp1DACSHEp9gS8AsXLuC2225DKBTC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npnBvA/EoBCjghhPgU\nWiiEEKJGNxA7AAAFUElEQVQgtFAIIURjKOA20d2H0zk/nXMDmF8pQAEnhBCfQg+cEEIUhB44IYRo\nDAXcJrr7cDrnp3NuAPMrBSjghBDiU+iBE0KIgtADJ4QQjaGA20R3H07n/HTODWB+pQAFnBBCfAo9\ncEIIURB64IQQojEUcJvo7sPpnJ/OuQHMrxQoWsC3bt2K5cuXIxQK4b777sNnn30mMy7f8Oabb3od\ngqPonJ/OuQHMrxQoWsC3bNmCt956C2+++SYaGhrw7LPPyozLN5w7d87rEBxF5/x0zg1gfqVA0QJe\nU1MDAJiYmMBXX32FiooKaUERQggpjC0PvKenB/Pnz8frr7+ORx55RFZMvuL06dNeh+AoOuenc24A\n8ysF8pYRrlq1Cp988sll/79r1y50dnYCAL7++mv09PQAAHbv3n35F5SVyYqVEEJKikJlhFLqwN95\n5x1s2LAB//znP+3uihBCiEmKtlA++OADABc98D/96U/4yU9+Ii0oQgghhSlawB977DEsW7YMd9xx\nByYmJrBhwwaZcRFCCClA0QL+4osv4p133sG//vUv/PKXv8Ts2bMNP6d7vfjmzZtx0003oampCRs3\nbsQ333zjdUhS+ctf/oLGxkZ873vfw8jIiNfhSOHIkSO46aab0NDQgF//+tdehyOd+++/H/PmzcOy\nZcu8DkU6H330Ee666y40NjaipaUFL7zwgtchSeXChQu47bbbEAqFcPvttxvOK16CcJixsbHMv7dv\n3y62bt3q9Fe6yoEDB8Tk5KSYnJwU3d3dYs+ePV6HJJX33ntPvP/++6KlpUWkUimvw5FCKBQShw8f\nFqdPnxY33nijOHv2rNchSeXIkSNiZGRELF261OtQpPPxxx+LN954QwghxNmzZ8V11113icbowFdf\nfSWEEOLChQuisbFRfPDBBzk/6/it9LrXi69atQqBQACBQABtbW04fPiw1yFJZfHixbjhhhu8DkMa\n58+fBwCsXLkS1157LVpbW3Hs2DGPo5JLc3NzzhGx35k/fz5CoRAAoL6+Ho2NjThx4oTHUcmlsrIS\nAPDll19iYmICM2fOzPlZV9ZCKZV68d/97neZ8kqiJsePH8fixYsz75csWcLqKZ9y6tQpnDx5Erfe\neqvXoUhlamoKy5cvx7x58/Dwww/j6quvzvlZKQK+atUqLFu27LJXIpEAAOzcuRP//ve/ceutt+LR\nRx+V8ZWuUig/ANixYwdqampw7733ehhpcZjJjxCV+OKLL7B27Vrs3r0bVVVVXocjlUAggLfeegun\nTp3Cb37zG7zxxhs5PztDxhcePHiw4GcqKytx//33+7JapVB+e/fuxf79+/Hqq6+6FJFczBw/Xbjl\nlluwefPmzPuTJ09i9erVHkZErDI+Po577rkH9913H8LhsNfhOMbChQuxZs0aHDt2DCtWrDD8jOMW\niu714vv27cNTTz2Fl19+WTt/PxuhwYM5amtrAVysRDl9+jQOHjyI2267zeOoiFmEEHjggQewdOlS\nbNy40etwpPPpp59mFun67LPPcODAgfwXKadnVO+55x6xdOlSccstt4jNmzeL//73v05/passWrRI\nXHPNNSIUColQKCQeeughr0OSytDQkFiwYIGoqKgQ8+bNE6tXr/Y6JNskk0mxePFicf3114tnn33W\n63Cks27dOnHVVVeJ8vJysWDBAvHcc895HZI0jh49KsrKysTy5csz59w//vEPr8OSxttvvy1WrFgh\nbr75ZtHa2ip+//vf5/28449UI4QQ4gx8Ig8hhPgUCjghhPgUCjghhPgUCjghhPgUCjghhPgUCjgh\nhPiU/wHq4IZYv3dmagAAAABJRU5ErkJggg==\n",
"text": [
"<matplotlib.figure.Figure at 0x1fc54a10>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fitting the model is easy:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"learner = milk.supervised.tree_learner()\n",
"model = learner.train(features, labels)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using it is easy, too:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"model.apply([-1,1])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": [
"True"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualizing the decision boundary is a bit of a pain..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x_range = np.linspace(-3,3,100)\n",
"y_range = np.linspace(-3,3,100)\n",
"\n",
"val = np.zeros((len(x_range), len(y_range)))\n",
"\n",
"for i, x_i in enumerate(x_range):\n",
" for j, y_j in enumerate(y_range):\n",
" val[i,j] = model.apply([x_i,y_j])\n",
"\n",
"imshow(val[::1,::-1].T, extent=[x_range[0],x_range[-1],y_range[0],y_range[-1]], cmap=cm.Greys)\n",
"plot(features[labels==True,0], features[labels==True,1], 'kx', mec='w', ms=7, mew=5)\n",
"plot(features[labels==True,0], features[labels==True,1], 'kx', mec='k', ms=5, mew=3)\n",
"plot(features[labels==False,0], features[labels==False,1], 'wo', mec='k', ms=8, mew=1)\n",
"grid()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAD9CAYAAABTCakVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXtsFNfVwI9pYvzGbLAhgiRUeIkDJl6gcR7F4NT1Q8bU\nTlC0/FFqmwBKJCpoComIgS2pggj5gMaViHGrOCFuGoWy2cRVKY/WBveFig0holGwpRDXMrYXcMwr\ngNmc7w93NvuYndfembm7c37SUbKe2bmH2Tlnzj333HsTEBGBIAhLMs5sBQiCMA9yAARhYcgBEISF\nIQdAEBaGHABBWBhyAARhYaJyADdv3oRHH30UHA4HPPbYY7B7925WehEEYQAJ0dYB3LhxA1JSUuDW\nrVswf/588Hg8kJOTw0o/giB0JOouQEpKCgAAXLt2De7cuQPjx4+PWimCIIwhagfwzTffQH5+Pkye\nPBnWrFkD9913Hwu9CIIwAmTEF198gQ899BB2dXUF/R0ASEhITBI5mI0CTJ8+HSoqKuDEiROsLkkQ\nhM5E5QA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nkEdywYQNevnxZ7yZlycnJ\nwfvvvx8dDgc6HA58/vnnzVYJERHdbjdOmzYNk5KScPLkyVheXm6aLu3t7Zibm4szZszAN954wzQ9\nBJYtW4b33nsvJiYm4rRp0/Ctt94yWyXs6OjAhIQEzM/P9z9LBw8eNFWnM2fO4Ny5c/Hhhx/G0tJS\nfOeddyTP131JMIIg+MWSKwIRBDEGOQCCsDDkAAjCwpADIAgLQw6AICwMOQCCsDD/D5CkcdJRbyvQ\nAAAAAElFTkSuQmCC\n",
"text": [
"<matplotlib.figure.Figure at 0x1fc5a410>"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And can we have a picture of the decision tree itself? It is hidden in the model instance somewhere..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"model.tree"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 7,
"text": [
"<milk.supervised.tree.Node at 0x1fc54850>"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"model.tree.featid, model.tree.featval, model.tree.left, model.tree.right"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 8,
"text": [
"(0,\n",
" -0.024916427900016365,\n",
" <milk.supervised.tree.Node at 0x1fc5ab50>,\n",
" <milk.supervised.tree.Node at 0x1fc54810>)"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def describe_tree(node, prefix=''):\n",
" print prefix + 'if x[%d] < %.2f:' % (node.featid, node.featval)\n",
" if isinstance(node.left, milk.supervised.tree.Node):\n",
" describe_tree(node.left, prefix+' ')\n",
" else:\n",
" print prefix+' ', node.left\n",
" print prefix + 'else:'\n",
" if isinstance(node.right, milk.supervised.tree.Node):\n",
" describe_tree(node.right, prefix+' ')\n",
" else:\n",
" print prefix+' ', node.right"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"describe_tree(model.tree)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"if x[0] < -0.02:\n",
" if x[1] < -0.97:\n",
" if x[0] < -1.51:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < -1.12:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < -0.43:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < -0.99:\n",
" if x[0] < -1.50:\n",
" if x[0] < -2.53:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -1.87:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -1.85:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
" else:\n",
" if x[0] < -1.41:\n",
" Leaf(1.0,2.0)\n",
" else:\n",
" if x[0] < -1.30:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -1.28:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -1.16:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
" else:\n",
" if x[0] < -0.94:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.63:\n",
" if x[0] < -0.82:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
" else:\n",
" if x[0] < -0.37:\n",
" if x[0] < -0.63:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.49:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.49:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
" else:\n",
" if x[0] < -0.25:\n",
" if x[0] < -0.37:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.35:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.35:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
" else:\n",
" if x[0] < -0.13:\n",
" if x[0] < -0.17:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
" else:\n",
" if x[0] < -0.13:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.12:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < -0.05:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n",
"else:\n",
" if x[1] < 1.05:\n",
" if x[0] < 0.00:\n",
" Leaf(0.5,2.0)\n",
" else:\n",
" if x[0] < 0.79:\n",
" if x[0] < 0.14:\n",
" if x[0] < 0.02:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.04:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.11:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 0.35:\n",
" if x[0] < 0.15:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.18:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.21:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 0.53:\n",
" if x[0] < 0.38:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 0.58:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.61:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.71:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 0.95:\n",
" if x[0] < 0.80:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.85:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 0.85:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 1.31:\n",
" if x[0] < 1.01:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 1.06:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 1.09:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 1.46:\n",
" if x[0] < 1.35:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 1.48:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 1.51:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" if x[0] < 1.73:\n",
" Leaf(0.0,1.0)\n",
" else:\n",
" Leaf(0.0,3.0)\n",
" else:\n",
" if x[0] < 0.37:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < 0.48:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < 0.62:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" if x[0] < 1.34:\n",
" Leaf(1.0,1.0)\n",
" else:\n",
" Leaf(1.0,3.0)\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not as simple as it seemed in the lecture, huh?"
]
}
],
"metadata": {}
}
]
}
milk_tree_learner.py
Python
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
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
 
# <codecell>
 
import numpy as np
import milk
 
# <markdowncell>
 
# Simulate some labeled data in a two-dimensional feature space.
 
# <codecell>
 
features = np.random.randn(100,2) # 2d array of features: 100 examples of 2 features each
labels = np.empty(100)
for i in range(100):
if features[i,0] < 0:
if features[i,1] < -1:
labels[i] = np.random.rand() < .001
else:
labels[i] = np.random.rand() < .999
else:
if features[i,1] < 1:
labels[i] = np.random.rand() < .001
else:
labels[i] = np.random.rand() < .999
 
# <markdowncell>
 
# What is the decision tree for this data?
 
# <markdowncell>
 
# Since the data is two-dimensional, we can take a look at it easily.
 
# <codecell>
 
plot(features[labels==True,0], features[labels==True,1], 'kx', mec='k', ms=6, mew=3)
plot(features[labels==False,0], features[labels==False,1], 'wo', mec='k', ms=8, mew=1)
grid()
 
# <markdowncell>
 
# Fitting the model is easy:
 
# <codecell>
 
learner = milk.supervised.tree_learner()
model = learner.train(features, labels)
 
# <markdowncell>
 
# Using it is easy, too:
 
# <codecell>
 
model.apply([-1,1])
 
# <markdowncell>
 
# Visualizing the decision boundary is a bit of a pain...
 
# <codecell>
 
x_range = np.linspace(-3,3,100)
y_range = np.linspace(-3,3,100)
 
val = np.zeros((len(x_range), len(y_range)))
 
for i, x_i in enumerate(x_range):
for j, y_j in enumerate(y_range):
val[i,j] = model.apply([x_i,y_j])
 
imshow(val[::1,::-1].T, extent=[x_range[0],x_range[-1],y_range[0],y_range[-1]], cmap=cm.Greys)
plot(features[labels==True,0], features[labels==True,1], 'kx', mec='w', ms=7, mew=5)
plot(features[labels==True,0], features[labels==True,1], 'kx', mec='k', ms=5, mew=3)
plot(features[labels==False,0], features[labels==False,1], 'wo', mec='k', ms=8, mew=1)
grid()
 
# <markdowncell>
 
# And can we have a picture of the decision tree itself? It is hidden in the model instance somewhere...
 
# <codecell>
 
model.tree
 
# <codecell>
 
model.tree.featid, model.tree.featval, model.tree.left, model.tree.right
 
# <codecell>
 
def describe_tree(node, prefix=''):
print prefix + 'if x[%d] < %.2f:' % (node.featid, node.featval)
if isinstance(node.left, milk.supervised.tree.Node):
describe_tree(node.left, prefix+' ')
else:
print prefix+' ', node.left
print prefix + 'else:'
if isinstance(node.right, milk.supervised.tree.Node):
describe_tree(node.right, prefix+' ')
else:
print prefix+' ', node.right
 
# <codecell>
 
describe_tree(model.tree)
 
# <markdowncell>
 
# Not as simple as it seemed in the lecture, huh?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.