Skip to content
Snippets Groups Projects
Anomaly Detection - Part 1 - Multivariate Gaussian.ipynb 26.6 KiB
Newer Older
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 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Anomaly Detection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mpl\n",
    "\n",
    "from scipy.stats import multivariate_normal\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "from sklearn.metrics import precision_recall_fscore_support\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.metrics.pairwise import euclidean_distances\n",
    "\n",
    "from sklearn.cluster import KMeans\n",
    "\n",
    "import matplotlib.gridspec as gridspec\n",
    "\n",
    "from tqdm.notebook import tqdm\n",
    "import ipywidgets as widgets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1 -  Multivariate Gaussian distribution\n",
    "You watched the video tutorial by Andrew Ng on anomaly detection using the multivariate Gaussian distribution. There is a data set on ILIAS containing credit card transactions made available by a European bank in September 2013 on [Kaggle](https://www.kaggle.com/mlg-ulb/creditcardfraud). Not surprisingly, the data set is anonymized, i.e. a PCA transformation was executed on all features (each feature therefore is a linear combination of the original but unknown features). The only exceptions are amount and time (milliseconds since first transaction). Finally, there is a class feature to indicate whether the transaction is fraudulent (value: 1) or genuine (value: 0). Implement fraud detection using the statistical approach introduced by Andrew Ng."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import YouTubeVideo\n",
    "YouTubeVideo('g2YBWQnqOpw')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data Quality Assessment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('creditcard.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Check for null values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.isnull().any().any()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Split fraudulent data from genuine data\n",
    "We start by separating the genuine data from the fraudulent data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "# genuine = ...\n",
    "# anomalies = ...\n",
    "# print(\"Genuine Transactions:\", genuine.shape[0])\n",
    "# print(\"Anomalous Transaction:\", anomalies.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "genuine = df[df.Class == 0]\n",
    "anomalies = df[df.Class == 1]\n",
    "print(\"Genuine Transactions:\", genuine.shape[0])\n",
    "print(\"Anomalous Transaction:\", anomalies.shape[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Amount feature\n",
    "Fortunately, that *Amount* feature has not been anonymized with the PCA transformation. So let's take a look at the *Amount* feature and see if we can find a pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "genuine.Amount.loc[genuine.Amount < 500].hist(bins=100)\n",
    "print(\"Mean\", genuine.Amount.mean())\n",
    "print(\"Median\", genuine.Amount.median())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "anomalies.Amount.loc[anomalies.Amount < 500].hist(bins=100)\n",
    "print(\"Mean\", anomalies.Amount.mean())\n",
    "print(\"Median\", anomalies.Amount.median())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Interestingly the median is lower but the mean is higher for the fraudulent transactions. This suggests there are some high value oriented criminals and some that focus on withdrawals \"below the radar\" to avoid detection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Feature Engineering"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Distribution of the features\n",
    "We want to model our anomaly detection by fitting a multivariate gaussian distribution. Therefore it makes sense to plot the distribution of our features. We plot the genuine and the fraudulent transactions separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "feature_cols = genuine.drop(columns=[\"Class\"]).columns\n",
    "plt.figure(figsize=(12, len(feature_cols)*4))\n",
    "gs = gridspec.GridSpec(len(feature_cols), 2)\n",
    "\n",
    "for i, col in enumerate(tqdm(feature_cols)):\n",
    "    ax = plt.subplot(gs[i])\n",
    "    sns.distplot(genuine[col], color='g',label='Genuine')\n",
    "    sns.distplot(anomalies[col], color='r',label='Anomaly')\n",
    "    ax.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Based on the plots we made, drop the features that might not be useful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "# features_to_drop = [...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "features_to_drop = ['Time', 'V8', 'V13', 'V15', 'V20', 'V22', 'V23', 'V24', 'V25', 'V26', 'V27', 'V28']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* We can see that the distribution of the fraudulent transaction (class=1) is matching with the genuine transactions (class=0) for the following features:\n",
    " * *V8*, *V13*, *V15*, *V20*, *V22*, *V23*, *V24*, *V25*, *V26*, *V27*, *V28*\n",
    " * Therefore they might not be useful in finding fradulent transactions. \n",
    "* The variable *Time* is also not a useful variable since it contains the seconds elapsed between the transaction for that record and the first transaction in the dataset. The data is in increasing order, which can lead to a singular covariance matrix. Also it is not normally distributed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's drop these features in our splitted dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "genuine = genuine.drop(features_to_drop, axis=1)\n",
    "anomalies = anomalies.drop(features_to_drop, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Split the data\n",
    "We split the targets from the features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "genuine_X = genuine.drop(columns=[\"Class\"]).values\n",
    "genuine_y = genuine.Class.values\n",
    "\n",
    "anomalies_X = anomalies.drop(columns=[\"Class\"]).values\n",
    "anomalies_y = anomalies.Class.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we use 60% of the *genuine* data as the training set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(genuine_X, genuine_y, test_size=0.4, random_state=42)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And add the anomalies to the test set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add anomalies to the test set\n",
    "X_test = np.concatenate([X_test, anomalies_X])\n",
    "y_test = np.concatenate([y_test, anomalies_y])\n",
    "\n",
    "print(X_test.shape)\n",
    "print(y_test.shape)\n",
    "print(\"Number of anomalies:\", y_test.sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*(Note that because we further split the data we don't have to shuffle it)*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can further split the test data into a test set and a validation set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5, random_state=42)\n",
    "print(X_test.shape)\n",
    "print(y_test.shape)\n",
    "print(\"Number of anomalies y_test:\", y_test.sum())\n",
    "print(X_val.shape)\n",
    "print(y_val.shape)\n",
    "print(\"Number of anomalies y_val:\", y_val.sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Data Normalization\n",
    "Now that we have splitted our data, we can normalize it by applying z-normalization.\n",
    "\n",
    "> Normalize the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "# scaler = ...\n",
    "# X_train = ...\n",
    "# X_test = ...\n",
    "# X_val = ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)\n",
    "X_val = scaler.transform(X_val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Baseline model\n",
    "To get an idea on how good our model performs, let's implement a dummy predictor. Our dummy model should mark each transaction as genuine\n",
    "\n",
    "> Implement the dummy predictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "lines_to_next_cell": 2,
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "def dummy_predict(X):\n",
    "    # START YOUR CODE\n",
    "    pass\n",
    "    # END YOUR CODE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "def dummy_predict(X):\n",
    "    return np.zeros(X.shape[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Now predict on the test set and calculate the accuracy by using the Scikit-Learn function [accuracy_score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "# Calculate the accuracy on the test set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "y_pred = dummy_predict(X_test)\n",
    "accuracy_score(y_pred, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wow, such a high accuracy! It seems like our work is done and we can spend the rest of our day drinking coffee!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, let's be honest there must be a mistake. \n",
    "\n",
    "> Let's check how many fraudulent and genuine data we have."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "# Calculate the percentage of fraudulent data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "print(df.Class.value_counts())\n",
    "print(\"Percentage of fraudulent data:\", 100*df.Class.value_counts()[1] /len(df))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like our dataset is highly imbalanced! We could create a balanced test set and then calculate the accuracy. But there must be another way... Let's plot the confusion matrix first."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_confusion_matrix(cm):\n",
    "    fig, (ax1) = plt.subplots(ncols=1, figsize=(5,5))\n",
    "    sns.heatmap(cm, \n",
    "                xticklabels=['Genuine', 'Fraud'],\n",
    "                yticklabels=['Genuine', 'Fraud'],\n",
    "                annot=True,ax=ax1,\n",
    "                linewidths=.2,linecolor=\"Darkblue\", cmap=\"Blues\")\n",
    "    plt.title('Confusion Matrix', fontsize=14)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cm = confusion_matrix(y_test, y_pred)\n",
    "plot_confusion_matrix(cm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that we have a lot of *True Positives* and only few *True Negatives*. This is due to the class imbalance."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Precision, Recall and F1 Score\n",
    "For a skewed data set, we often use other metrics like [Precision](https://en.wikipedia.org/wiki/Precision_and_recall), [Recall](https://en.wikipedia.org/wiki/Precision_and_recall) or the harmonic mean of both, called [F1 Score](https://en.wikipedia.org/wiki/F1_score). Scikit-Learn provides the function [precision_recall_fscore_support](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html) which calculates all these metrics at once."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "precision, recall, f1, _ = precision_recall_fscore_support(y_test, y_pred, average=\"binary\")\n",
    "print(\"Precision:\", precision)\n",
    "print(\"Recall\", recall)\n",
    "print(\"F1\", f1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now it's clear that our dummy anomaly detection model performs really bad! So let's stick to those metrics."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fit the multivariate gaussian distribution\n",
    "Now that we have splitted our data set into a training, test and cross validation set, we can train our anomaly detection model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Complete the `fit` function which calculate the *mean* and the *covariance* matrix from the training set and then fits a multivariate gaussian distribution. Use the [multivariate_normal](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.multivariate_normal.html) function from SciPy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "lines_to_next_cell": 2,
    "solution2": "shown",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "def fit(X):\n",
    "    pass\n",
    "    # START YOUR CODE HERE\n",
    "    #return gauss\n",
    "    # END YOUR CODE HERE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "shown"
   },
   "outputs": [],
   "source": [
    "def fit(X):\n",
    "    mu = np.mean(X, axis=0)\n",
    "    cov = np.cov(X.T)\n",
    "    gauss = multivariate_normal(mean=mu, cov=cov)\n",
    "    return gauss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gauss = fit(X_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Predict anomalies\n",
    "Let's use our fitted gaussian distribution to predict anomalies. \n",
    "\n",
    "> Use the `pdf` method of the gauss model to calculate the densities. Then mark all elements which are below the threshold `epsilon` as an anomaly. We use the value 10<sup>-20</sup> as the threshold."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "def predict(X, gauss, eps):\n",
    "    # START YOUR CODE\n",
    "    pass\n",
    "    # END YOUR CODE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "def predict(X, gauss, eps):\n",
    "    densities = gauss.pdf(X)\n",
    "    y_pred = densities < eps\n",
    "    return y_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eps = 1e-20\n",
    "y_pred = predict(X_test, gauss, eps)\n",
    "precision, recall, f1, _ = precision_recall_fscore_support(y_test, y_pred, average=\"binary\")\n",
    "print(\"Precision:\", precision)\n",
    "print(\"Recall\", recall)\n",
    "print(\"F1\", f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cm = confusion_matrix(y_test, y_pred)\n",
    "plot_confusion_matrix(cm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* We have a relatively high recall value, which means that our model does detect most of the anomalies. \n",
    "* The low precision value  however indicates that we have a lot of *False Positives*. *False Positives* are the genuine transaction which were classified as frauds. \n",
    "* This might be a good model if for example the model is only used to determine if the user has to enter an extra verification code. However if those cards would be blocked, this would be really bad."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hyperparameter tuning\n",
    "Our threshold `eps` is the hyperparameter we would like to tune. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But first we take a look at our densities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "densities = gauss.pdf(X_val)\n",
    "print(\"Min density:\", densities.min())\n",
    "print(\"Max density:\", densities.max())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The values of our densities seem to be rather small! Luckily when we are dealing with probabilities we can always apply a logarithmic transformation as it is a strictly monotonic function. Scipy already privdes an implementation `logpdf`. \n",
    "Let's check the values of our log transformed densities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "densities = gauss.logpdf(X_val)\n",
    "print(\"Min density:\", densities.min())\n",
    "print(\"Max density:\", densities.max())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These values look much better. However in order to reuse our existing code we need to redefine our `predict` function. \n",
    "\n",
    "> Reimplement the `predict` function but this time use `logpdf` instead of the `pdf` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "def predict(X, gauss, eps):\n",
    "    # YOUR CODE\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "def predict(X, gauss, eps):\n",
    "    densities = gauss.logpdf(X)\n",
    "    y_pred = densities < eps\n",
    "    return y_pred"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Fit our threshold by sorting\n",
    "We can try to find the optimal threshold by sorting our densities and then picking the density with the index of the number of anomalies on our validation set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "expected_anomalies = y_val.sum()\n",
    "sorted_densities = np.sort(gauss.logpdf(X_val))\n",
    "best_eps = (sorted_densities[expected_anomalies] + \n",
    "            0.5 * (sorted_densities[expected_anomalies + 1] - \n",
    "                   sorted_densities[expected_anomalies]))\n",
    "\n",
    "\n",
    "y_pred = predict(X_val, gauss, best_eps)\n",
    "f1 = f1_score(y_val, y_pred, average=\"binary\")\n",
    "print(\"Best epsilon {} with f1 score {}\".format(best_eps, f1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wow this worked out very well on our validation set! Let's check if we can still improve our result by applying a grid search approach."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Grid Search Approach"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We calculate the densities for each anomaly and assume that the maximum epsilon is equal to the maximum density. We take $-500$ as our minimum `epsilon` for now."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "densities.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_anomalies =  anomalies.drop(columns=[\"Class\"]).values\n",
    "densities = gauss.logpdf(X_val)\n",
    "\n",
    "max_eps = densities.max()\n",
    "print(\"Maximum epsilon:\", max_eps)\n",
    "\n",
    "min_eps = -500\n",
    "print(\"Minimum epsilon:\", min_eps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This means the optimal value of `epsilon` lies between -500 and -16. Let's try to find it by using a grid search approach.\n",
    "> Loop through the list of epsilons and calculate for each epsilon the *precision*, *recall* and *f1* score. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden",
    "solution2_first": true
   },
   "outputs": [],
   "source": [
    "f1_scores = []\n",
    "recall_scores = []\n",
    "precision_scores = []\n",
    "\n",
    "epsilons = np.linspace(min_eps, max_eps, num=1000)\n",
    "\n",
    "# START YOUR CODE\n",
    "#for eps in tqdm(epsilons):\n",
    "\n",
    "#best_eps = epsilons[np.argmax(f1_scores)]\n",
    "#best_f1 = np.max(f1_scores)\n",
    "\n",
    "#print(\"Best epsilon {} with f1 score {}\".format(best_eps, best_f1))\n",
    "# END YOUR CODE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "solution2": "hidden"
   },
   "outputs": [],
   "source": [
    "f1_scores = []\n",
    "recall_scores = []\n",
    "precision_scores = []\n",
    "\n",
    "epsilons = np.linspace(min_eps, max_eps, num=1000)\n",
    "densities = gauss.logpdf(X_val)\n",
    "for eps in tqdm(epsilons):\n",
    "    y_pred = densities < eps\n",
    "    precision, recall, f1, _ = precision_recall_fscore_support(y_val, y_pred, average=\"binary\")\n",
    "    precision_scores.append(precision)\n",
    "    recall_scores.append(recall)\n",
    "    f1_scores.append(f1)\n",
    "    \n",
    "best_eps_gridsearch = epsilons[np.argmax(f1_scores)]\n",
    "best_f1 = np.max(f1_scores)\n",
    "\n",
    "print(\"Best epsilon {} with f1 score {}\".format(best_eps_gridsearch, best_f1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Visualization\n",
    "Let's visualize the *F1*, *Precision* and *Recall* curves"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(10,8))\n",
    "ax.plot(epsilons, f1_scores, label=\"f1\")\n",
    "ax.plot(epsilons, precision_scores, label=\"precision\")\n",
    "ax.plot(epsilons, recall_scores, label=\"recall\")\n",
    "ax.plot(best_eps, best_f1, marker=\"o\")\n",
    "ax.set_xlabel(\"Epsilon\")\n",
    "ax.set_ylabel(\"Score\")\n",
    "ax.legend()\n",
    "ax.set_xlim(max_eps, min_eps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Evaluate the model on the test set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we have selected an epsilon, we can check how good our model performs on the test set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = predict(X_test, gauss, best_eps)\n",
    "precision, recall, f1, _ = precision_recall_fscore_support(y_test, y_pred, average=\"binary\")\n",
    "print(\"Precision:\", precision)\n",
    "print(\"Recall\", recall)\n",
    "print(\"F1\", f1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We plot the confusion matrix again"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cm = confusion_matrix(y_test, y_pred)\n",
    "plot_confusion_matrix(cm)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "text_representation": {
    "extension": ".py",
    "format_name": "percent",
    "format_version": "1.2",
    "jupytext_version": "0.8.6"
   }
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"