File size: 23,783 Bytes
0ab7b0c |
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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"# import seaborn as sns\n",
"# import matplotlib.pyplot as plt\n",
"import os\n",
"from sklearn.model_selection import train_test_split\n",
"from feature_engineering import discretization as dc\n",
"\n",
"# plt.style.use('seaborn-colorblind')\n",
"# %matplotlib inline\n",
"#from feature_cleaning import rare_values as ra"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Dataset"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"use_cols = [\n",
" 'Pclass', 'Sex', 'Age', 'Fare', 'SibSp',\n",
" 'Survived'\n",
"]\n",
"\n",
"data = pd.read_csv('./data/titanic.csv', usecols=use_cols)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Fare</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>male</td>\n",
" <td>22.0</td>\n",
" <td>1</td>\n",
" <td>7.2500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>female</td>\n",
" <td>38.0</td>\n",
" <td>1</td>\n",
" <td>71.2833</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>female</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>7.9250</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived Pclass Sex Age SibSp Fare\n",
"0 0 3 male 22.0 1 7.2500\n",
"1 1 1 female 38.0 1 71.2833\n",
"2 1 3 female 26.0 0 7.9250"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head(3)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((623, 6), (268, 6))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Note that we include target variable in the X_train \n",
"# because we need it to supervise our discretization\n",
"# this is not the standard way of using train-test-split\n",
"X_train, X_test, y_train, y_test = train_test_split(data, data.Survived, test_size=0.3,\n",
" random_state=0)\n",
"X_train.shape, X_test.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Equal width binning\n",
"divides the scope of possible values into N bins of the same width"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn.preprocessing import KBinsDiscretizer\n",
"enc_equal_width = KBinsDiscretizer(n_bins=3,encode='ordinal',strategy='uniform').fit(X_train[['Fare']])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([array([ 0. , 170.7764, 341.5528, 512.3292])], dtype=object)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# equal width for every bins\n",
"enc_equal_width.bin_edges_"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0 610\n",
"1.0 11\n",
"2.0 2\n",
"Name: 0, dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = enc_equal_width.transform(X_train[['Fare']])\n",
"pd.DataFrame(result)[0].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Survived Pclass Sex Age SibSp Fare Fare_equal_width\n",
"857 1 1 male 51.0 0 26.5500 0.0\n",
"52 1 1 female 49.0 1 76.7292 0.0\n",
"386 0 3 male 1.0 5 46.9000 0.0\n",
"124 0 1 male 54.0 0 77.2875 0.0\n",
"578 0 3 female NaN 1 14.4583 0.0\n",
"549 1 2 male 8.0 1 36.7500 0.0\n",
"118 0 1 male 24.0 0 247.5208 1.0\n",
"12 0 3 male 20.0 0 8.0500 0.0\n",
"157 0 3 male 30.0 0 8.0500 0.0\n",
"127 1 3 male 24.0 0 7.1417 0.0\n"
]
}
],
"source": [
"# add the new discretized variable\n",
"X_train_copy = X_train.copy(deep=True)\n",
"X_train_copy['Fare_equal_width'] = enc_equal_width.transform(X_train[['Fare']])\n",
"print(X_train_copy.head(10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Equal frequency binning\n",
"divides the scope of possible values of the variable into N bins, \n",
"where each bin carries the same amount of observations"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"enc_equal_freq = KBinsDiscretizer(n_bins=3,encode='ordinal',strategy='quantile').fit(X_train[['Fare']])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([array([ 0. , 8.69303333, 26.2875 , 512.3292 ])],\n",
" dtype=object)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check the bin edges\n",
"enc_equal_freq.bin_edges_"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0 209\n",
"0.0 208\n",
"1.0 206\n",
"Name: 0, dtype: int64"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# equal number of case for every bins\n",
"result = enc_equal_freq.transform(X_train[['Fare']])\n",
"pd.DataFrame(result)[0].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Survived Pclass Sex Age SibSp Fare Fare_equal_freq\n",
"857 1 1 male 51.0 0 26.5500 2.0\n",
"52 1 1 female 49.0 1 76.7292 2.0\n",
"386 0 3 male 1.0 5 46.9000 2.0\n",
"124 0 1 male 54.0 0 77.2875 2.0\n",
"578 0 3 female NaN 1 14.4583 1.0\n",
"549 1 2 male 8.0 1 36.7500 2.0\n",
"118 0 1 male 24.0 0 247.5208 2.0\n",
"12 0 3 male 20.0 0 8.0500 0.0\n",
"157 0 3 male 30.0 0 8.0500 0.0\n",
"127 1 3 male 24.0 0 7.1417 0.0\n"
]
}
],
"source": [
"# add the new discretized variable\n",
"X_train_copy = X_train.copy(deep=True)\n",
"X_train_copy['Fare_equal_freq'] = enc_equal_freq.transform(X_train[['Fare']])\n",
"print(X_train_copy.head(10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## K-means binning\n",
"using k-means to partition values into clusters"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"enc_kmeans = KBinsDiscretizer(n_bins=3,encode='ordinal',strategy='kmeans').fit(X_train[['Fare']])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([array([ 0. , 93.5271531 , 338.08506324, 512.3292 ])],\n",
" dtype=object)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check the bin edges\n",
"enc_kmeans.bin_edges_"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0 587\n",
"1.0 34\n",
"2.0 2\n",
"Name: 0, dtype: int64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = enc_kmeans.transform(X_train[['Fare']])\n",
"pd.DataFrame(result)[0].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Survived Pclass Sex Age SibSp Fare Fare_kmeans\n",
"857 1 1 male 51.0 0 26.5500 0.0\n",
"52 1 1 female 49.0 1 76.7292 0.0\n",
"386 0 3 male 1.0 5 46.9000 0.0\n",
"124 0 1 male 54.0 0 77.2875 0.0\n",
"578 0 3 female NaN 1 14.4583 0.0\n",
"549 1 2 male 8.0 1 36.7500 0.0\n",
"118 0 1 male 24.0 0 247.5208 1.0\n",
"12 0 3 male 20.0 0 8.0500 0.0\n",
"157 0 3 male 30.0 0 8.0500 0.0\n",
"127 1 3 male 24.0 0 7.1417 0.0\n"
]
}
],
"source": [
"# add the new discretized variable\n",
"X_train_copy = X_train.copy(deep=True)\n",
"X_train_copy['Fare_kmeans'] = enc_kmeans.transform(X_train[['Fare']])\n",
"print(X_train_copy.head(10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Discretisation with Decision Tree\n",
"using a decision tree to identify the optimal splitting points that would determine the bins"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"enc1 = dc.DiscretizeByDecisionTree(col='Fare',max_depth=2).fit(X=X_train,y=y_train)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=2,\n",
" max_features=None, max_leaf_nodes=None,\n",
" min_impurity_decrease=0.0, min_impurity_split=None,\n",
" min_samples_leaf=1, min_samples_split=2,\n",
" min_weight_fraction_leaf=0.0, presort=False, random_state=None,\n",
" splitter='best')"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"enc1.tree_model"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data1 = enc1.transform(data)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Survived Pclass Sex Age SibSp Fare Fare_tree_discret\n",
"0 0 3 male 22.0 1 7.2500 0.107143\n",
"1 1 1 female 38.0 1 71.2833 0.442308\n",
"2 1 3 female 26.0 0 7.9250 0.255319\n",
"3 1 1 female 35.0 1 53.1000 0.442308\n",
"4 0 3 male 35.0 0 8.0500 0.255319\n",
"[0.10714286 0.44230769 0.25531915 0.74626866]\n"
]
}
],
"source": [
"# see how the new column Fare_tree_discret is distributed\n",
"# the values are corresponding to the proba of the prediction by the tree\n",
"print(data1.head(5))\n",
"\n",
"# the unique value of the discretisized column\n",
"print(data1.Fare_tree_discret.unique())"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Fare Fare\n",
"Fare_tree_discret \n",
"0.107143 0.0000 7.5208\n",
"0.255319 7.5500 10.5167\n",
"0.442308 11.1333 73.5000\n",
"0.746269 75.2500 512.3292\n"
]
}
],
"source": [
"# see how the bins are cut\n",
"# because we use a tree with max-depth of 2, we have at most 2*2=4 bins generated by the tree\n",
"col='Fare'\n",
"bins = pd.concat([data1.groupby([col+'_tree_discret'])[col].min(),\n",
" data1.groupby([col+'_tree_discret'])[col].max()], axis=1)\n",
"print(bins)\n",
"\n",
"# all values between 0 to 7.5208 in the original variable 'Fare' \n",
"# are given new value 0.107143 in the new column 'Fare_tree_discret'\n",
"# and so on"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Discretisation with Decision Tree with optimal depth search"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"result ROC-AUC for each depth\n",
" depth roc_auc_mean roc_auc_std\n",
"0 2 0.662132 0.026253\n",
"1 3 0.647950 0.045010\n",
"2 4 0.650984 0.035127\n",
"3 5 0.651180 0.027663\n",
"4 6 0.653961 0.037421\n",
"5 7 0.643688 0.033513\n",
"optimal_depth: [2]\n"
]
}
],
"source": [
"# search for the best depth from range 2-7\n",
"# we see when depth=2 we get the best roc-auc mean\n",
"enc2 = dc.DiscretizeByDecisionTree(col='Fare',max_depth=[2,3,4,5,6,7]).fit(X=X_train,y=y_train)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DecisionTreeClassifier(class_weight=None, criterion='gini',\n",
" max_depth=array([2], dtype=int64), max_features=None,\n",
" max_leaf_nodes=None, min_impurity_decrease=0.0,\n",
" min_impurity_split=None, min_samples_leaf=1,\n",
" min_samples_split=2, min_weight_fraction_leaf=0.0,\n",
" presort=False, random_state=None, splitter='best')"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# using optimal depth=2 we train the model, same result as last one\n",
"enc2.tree_model"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Survived</th>\n",
" <th>Pclass</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Fare</th>\n",
" <th>Fare_tree_discret</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>male</td>\n",
" <td>22.0</td>\n",
" <td>1</td>\n",
" <td>7.2500</td>\n",
" <td>0.107143</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>female</td>\n",
" <td>38.0</td>\n",
" <td>1</td>\n",
" <td>71.2833</td>\n",
" <td>0.442308</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>female</td>\n",
" <td>26.0</td>\n",
" <td>0</td>\n",
" <td>7.9250</td>\n",
" <td>0.255319</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>female</td>\n",
" <td>35.0</td>\n",
" <td>1</td>\n",
" <td>53.1000</td>\n",
" <td>0.442308</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>male</td>\n",
" <td>35.0</td>\n",
" <td>0</td>\n",
" <td>8.0500</td>\n",
" <td>0.255319</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Survived Pclass Sex Age SibSp Fare Fare_tree_discret\n",
"0 0 3 male 22.0 1 7.2500 0.107143\n",
"1 1 1 female 38.0 1 71.2833 0.442308\n",
"2 1 3 female 26.0 0 7.9250 0.255319\n",
"3 1 1 female 35.0 1 53.1000 0.442308\n",
"4 0 3 male 35.0 0 8.0500 0.255319"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data2 = enc2.transform(data)\n",
"data2.head(5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Discretisation with ChiMerge\n",
"supervised hierarchical bottom-up (merge) method that locally exploits the chi-square criterion to decide whether two adjacent intervals are similar enough to be merged"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Interval for variable Fare\n",
" variable interval flag_0 flag_1\n",
"0 Fare -inf,7.875 94.0 28.0\n",
"1 Fare 7.875,7.8792 0.0 3.0\n",
"2 Fare 7.8792,7.8958 25.0 1.0\n",
"3 Fare 7.8958,73.5 245.0 160.0\n",
"4 Fare 73.5+ 17.0 50.0\n"
]
}
],
"source": [
"enc3 = dc.ChiMerge(col='Fare',num_of_bins=5).fit(X=X_train,y='Survived')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-0.1, 7.875, 7.8792, 7.8958, 73.5, 512.3292]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# the bins boundary created by ChiMerge\n",
"\n",
"enc3.bins"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data3 = enc3.transform(data)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Survived Pclass Sex Age SibSp Fare Fare_chimerge\n",
"0 0 3 male 22.0 1 7.2500 (-0.101, 7.875]\n",
"1 1 1 female 38.0 1 71.2833 (7.896, 73.5]\n",
"2 1 3 female 26.0 0 7.9250 (7.896, 73.5]\n",
"3 1 1 female 35.0 1 53.1000 (7.896, 73.5]\n",
"4 0 3 male 35.0 0 8.0500 (7.896, 73.5]\n"
]
}
],
"source": [
"print(data3.head(5))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(-0.101, 7.875], (7.896, 73.5], (73.5, 512.329], (7.875, 7.879], (7.879, 7.896]]\n",
"Categories (5, interval[float64]): [(-0.101, 7.875] < (7.875, 7.879] < (7.879, 7.896] < (7.896, 73.5] < (73.5, 512.329]]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# all values are grouped into 5 intervals\n",
"data3.Fare_chimerge.unique()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|