{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p  style=\"z-index: 101;background: #fde073;text-align: center;line-height: 2.5;overflow: hidden;font-size:22px;\">Please <a href=\"https://www.pycm.ir/doc/#Cite\" target=\"_blank\">cite us</a> if you use the software</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example-3 (Activation threshold)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Binary classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pycm import ConfusionMatrix\n",
    "\n",
    "def activation1(i):\n",
    "    if i<0.75:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_actual = [1,1,0,1,1,0]\n",
    "y_pred = [0.65,0.34,0.80,0.54,0.32,0.12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cm = ConfusionMatrix(y_actual,y_pred,threshold=activation1)\n",
    "cm.classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predict 0       1       \n",
      "Actual\n",
      "0       1       1       \n",
      "\n",
      "1       0       4       \n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "Overall Statistics : \n",
      "\n",
      "95% CI                                                            (0.53513,1.13154)\n",
      "ACC Macro                                                         0.83333\n",
      "ARI                                                               0.34783\n",
      "AUNP                                                              0.75\n",
      "AUNU                                                              0.75\n",
      "Bangdiwala B                                                      0.77273\n",
      "Bennett S                                                         0.66667\n",
      "CBA                                                               0.65\n",
      "CSI                                                               0.65\n",
      "Chi-Squared                                                       2.4\n",
      "Chi-Squared DF                                                    1\n",
      "Conditional Entropy                                               0.33333\n",
      "Cramer V                                                          0.63246\n",
      "Cross Entropy                                                     1.03701\n",
      "F1 Macro                                                          0.77778\n",
      "F1 Micro                                                          0.83333\n",
      "FNR Macro                                                         0.25\n",
      "FNR Micro                                                         0.16667\n",
      "FPR Macro                                                         0.25\n",
      "FPR Micro                                                         0.16667\n",
      "Gwet AC1                                                          0.73333\n",
      "Hamming Loss                                                      0.16667\n",
      "Joint Entropy                                                     1.25163\n",
      "KL Divergence                                                     0.11871\n",
      "Kappa                                                             0.57143\n",
      "Kappa 95% CI                                                      (-0.19538,1.33824)\n",
      "Kappa No Prevalence                                               0.66667\n",
      "Kappa Standard Error                                              0.39123\n",
      "Kappa Unbiased                                                    0.55556\n",
      "Krippendorff Alpha                                                0.59259\n",
      "Lambda A                                                          0.5\n",
      "Lambda B                                                          0.0\n",
      "Mutual Information                                                0.31669\n",
      "NIR                                                               0.66667\n",
      "Overall ACC                                                       0.83333\n",
      "Overall CEN                                                       0.39624\n",
      "Overall J                                                         (1.3,0.65)\n",
      "Overall MCC                                                       0.63246\n",
      "Overall MCEN                                                      0.27683\n",
      "Overall RACC                                                      0.61111\n",
      "Overall RACCU                                                     0.625\n",
      "P-Value                                                           0.35117\n",
      "PPV Macro                                                         0.9\n",
      "PPV Micro                                                         0.83333\n",
      "Pearson C                                                         0.53452\n",
      "Phi-Squared                                                       0.4\n",
      "RCI                                                               0.34487\n",
      "RR                                                                3.0\n",
      "Reference Entropy                                                 0.9183\n",
      "Response Entropy                                                  0.65002\n",
      "SOA1(Landis & Koch)                                               Moderate\n",
      "SOA2(Fleiss)                                                      Intermediate to Good\n",
      "SOA3(Altman)                                                      Moderate\n",
      "SOA4(Cicchetti)                                                   Fair\n",
      "SOA5(Cramer)                                                      Strong\n",
      "SOA6(Matthews)                                                    Moderate\n",
      "Scott PI                                                          0.55556\n",
      "Standard Error                                                    0.15215\n",
      "TNR Macro                                                         0.75\n",
      "TNR Micro                                                         0.83333\n",
      "TPR Macro                                                         0.75\n",
      "TPR Micro                                                         0.83333\n",
      "Zero-one Loss                                                     1\n",
      "\n",
      "Class Statistics :\n",
      "\n",
      "Classes                                                           0             1             \n",
      "ACC(Accuracy)                                                     0.83333       0.83333       \n",
      "AGF(Adjusted F-score)                                             0.68041       0.89087       \n",
      "AGM(Adjusted geometric mean)                                      0.82426       0.65533       \n",
      "AM(Difference between automatic and manual classification)        -1            1             \n",
      "AUC(Area under the ROC curve)                                     0.75          0.75          \n",
      "AUCI(AUC value interpretation)                                    Good          Good          \n",
      "AUPR(Area under the PR curve)                                     0.75          0.9           \n",
      "BCD(Bray-Curtis dissimilarity)                                    0.08333       0.08333       \n",
      "BM(Informedness or bookmaker informedness)                        0.5           0.5           \n",
      "CEN(Confusion entropy)                                            0.52832       0.35221       \n",
      "DOR(Diagnostic odds ratio)                                        None          None          \n",
      "DP(Discriminant power)                                            None          None          \n",
      "DPI(Discriminant power interpretation)                            None          None          \n",
      "ERR(Error rate)                                                   0.16667       0.16667       \n",
      "F0.5(F0.5 score)                                                  0.83333       0.83333       \n",
      "F1(F1 score - harmonic mean of precision and sensitivity)         0.66667       0.88889       \n",
      "F2(F2 score)                                                      0.55556       0.95238       \n",
      "FDR(False discovery rate)                                         0.0           0.2           \n",
      "FN(False negative/miss/type 2 error)                              1             0             \n",
      "FNR(Miss rate or false negative rate)                             0.5           0.0           \n",
      "FOR(False omission rate)                                          0.2           0.0           \n",
      "FP(False positive/type 1 error/false alarm)                       0             1             \n",
      "FPR(Fall-out or false positive rate)                              0.0           0.5           \n",
      "G(G-measure geometric mean of precision and sensitivity)          0.70711       0.89443       \n",
      "GI(Gini index)                                                    0.5           0.5           \n",
      "GM(G-mean geometric mean of specificity and sensitivity)          0.70711       0.70711       \n",
      "IBA(Index of balanced accuracy)                                   0.25          0.75          \n",
      "ICSI(Individual classification success index)                     0.5           0.8           \n",
      "IS(Information score)                                             1.58496       0.26303       \n",
      "J(Jaccard index)                                                  0.5           0.8           \n",
      "LS(Lift score)                                                    3.0           1.2           \n",
      "MCC(Matthews correlation coefficient)                             0.63246       0.63246       \n",
      "MCCI(Matthews correlation coefficient interpretation)             Moderate      Moderate      \n",
      "MCEN(Modified confusion entropy)                                  0.5           0.46439       \n",
      "MK(Markedness)                                                    0.8           0.8           \n",
      "N(Condition negative)                                             4             2             \n",
      "NLR(Negative likelihood ratio)                                    0.5           0.0           \n",
      "NLRI(Negative likelihood ratio interpretation)                    Negligible    Good          \n",
      "NPV(Negative predictive value)                                    0.8           1.0           \n",
      "OC(Overlap coefficient)                                           1.0           1.0           \n",
      "OOC(Otsuka-Ochiai coefficient)                                    0.70711       0.89443       \n",
      "OP(Optimized precision)                                           0.5           0.5           \n",
      "P(Condition positive or support)                                  2             4             \n",
      "PLR(Positive likelihood ratio)                                    None          2.0           \n",
      "PLRI(Positive likelihood ratio interpretation)                    None          Poor          \n",
      "POP(Population)                                                   6             6             \n",
      "PPV(Precision or positive predictive value)                       1.0           0.8           \n",
      "PRE(Prevalence)                                                   0.33333       0.66667       \n",
      "Q(Yule Q - coefficient of colligation)                            None          None          \n",
      "QI(Yule Q interpretation)                                         None          None          \n",
      "RACC(Random accuracy)                                             0.05556       0.55556       \n",
      "RACCU(Random accuracy unbiased)                                   0.0625        0.5625        \n",
      "TN(True negative/correct rejection)                               4             1             \n",
      "TNR(Specificity or true negative rate)                            1.0           0.5           \n",
      "TON(Test outcome negative)                                        5             1             \n",
      "TOP(Test outcome positive)                                        1             5             \n",
      "TP(True positive/hit)                                             1             4             \n",
      "TPR(Sensitivity, recall, hit rate, or true positive rate)         0.5           1.0           \n",
      "Y(Youden index)                                                   0.5           0.5           \n",
      "dInd(Distance index)                                              0.5           0.5           \n",
      "sInd(Similarity index)                                            0.64645       0.64645       \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(cm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiclass classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def activation2(i):\n",
    "    ref = [\"cat\",\"dog\",\"ship\"]\n",
    "    return ref[i.index(max(i))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_actual = [\"ship\",\"cat\",\"dog\",\"cat\",\"dog\",\"ship\"]\n",
    "y_pred = [[90,433,600],[345,100,2],[100,90,10],[432,102,80],[156,402,2],[73,20,532]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['cat', 'dog', 'ship']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cm2 = ConfusionMatrix(y_actual,y_pred,threshold=activation2)\n",
    "cm2.classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predict    cat        dog        ship       \n",
      "Actual\n",
      "cat        2          0          0          \n",
      "\n",
      "dog        1          1          0          \n",
      "\n",
      "ship       0          0          2          \n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "Overall Statistics : \n",
      "\n",
      "95% CI                                                            (0.53513,1.13154)\n",
      "ACC Macro                                                         0.88889\n",
      "ARI                                                               0.44444\n",
      "AUNP                                                              0.875\n",
      "AUNU                                                              0.875\n",
      "Bangdiwala B                                                      0.75\n",
      "Bennett S                                                         0.75\n",
      "CBA                                                               0.72222\n",
      "CSI                                                               0.72222\n",
      "Chi-Squared                                                       8.0\n",
      "Chi-Squared DF                                                    4\n",
      "Conditional Entropy                                               0.33333\n",
      "Cramer V                                                          0.8165\n",
      "Cross Entropy                                                     1.72331\n",
      "F1 Macro                                                          0.82222\n",
      "F1 Micro                                                          0.83333\n",
      "FNR Macro                                                         0.16667\n",
      "FNR Micro                                                         0.16667\n",
      "FPR Macro                                                         0.08333\n",
      "FPR Micro                                                         0.08333\n",
      "Gwet AC1                                                          0.75258\n",
      "Hamming Loss                                                      0.16667\n",
      "Joint Entropy                                                     1.9183\n",
      "KL Divergence                                                     0.13835\n",
      "Kappa                                                             0.75\n",
      "Kappa 95% CI                                                      (0.30269,1.19731)\n",
      "Kappa No Prevalence                                               0.66667\n",
      "Kappa Standard Error                                              0.22822\n",
      "Kappa Unbiased                                                    0.74468\n",
      "Krippendorff Alpha                                                0.76596\n",
      "Lambda A                                                          0.75\n",
      "Lambda B                                                          0.66667\n",
      "Mutual Information                                                1.12581\n",
      "NIR                                                               0.33333\n",
      "Overall ACC                                                       0.83333\n",
      "Overall CEN                                                       0.16279\n",
      "Overall J                                                         (2.16667,0.72222)\n",
      "Overall MCC                                                       0.78335\n",
      "Overall MCEN                                                      0.18464\n",
      "Overall RACC                                                      0.33333\n",
      "Overall RACCU                                                     0.34722\n",
      "P-Value                                                           0.01783\n",
      "PPV Macro                                                         0.88889\n",
      "PPV Micro                                                         0.83333\n",
      "Pearson C                                                         0.75593\n",
      "Phi-Squared                                                       1.33333\n",
      "RCI                                                               0.71031\n",
      "RR                                                                2.0\n",
      "Reference Entropy                                                 1.58496\n",
      "Response Entropy                                                  1.45915\n",
      "SOA1(Landis & Koch)                                               Substantial\n",
      "SOA2(Fleiss)                                                      Intermediate to Good\n",
      "SOA3(Altman)                                                      Good\n",
      "SOA4(Cicchetti)                                                   Excellent\n",
      "SOA5(Cramer)                                                      Very Strong\n",
      "SOA6(Matthews)                                                    Strong\n",
      "Scott PI                                                          0.74468\n",
      "Standard Error                                                    0.15215\n",
      "TNR Macro                                                         0.91667\n",
      "TNR Micro                                                         0.91667\n",
      "TPR Macro                                                         0.83333\n",
      "TPR Micro                                                         0.83333\n",
      "Zero-one Loss                                                     1\n",
      "\n",
      "Class Statistics :\n",
      "\n",
      "Classes                                                           cat           dog           ship          \n",
      "ACC(Accuracy)                                                     0.83333       0.83333       1.0           \n",
      "AGF(Adjusted F-score)                                             0.92319       0.68041       1.0           \n",
      "AGM(Adjusted geometric mean)                                      0.81962       0.82426       1.0           \n",
      "AM(Difference between automatic and manual classification)        1             -1            0             \n",
      "AUC(Area under the ROC curve)                                     0.875         0.75          1.0           \n",
      "AUCI(AUC value interpretation)                                    Very Good     Good          Excellent     \n",
      "AUPR(Area under the PR curve)                                     0.83333       0.75          1.0           \n",
      "BCD(Bray-Curtis dissimilarity)                                    0.08333       0.08333       0.0           \n",
      "BM(Informedness or bookmaker informedness)                        0.75          0.5           1.0           \n",
      "CEN(Confusion entropy)                                            0.23219       0.26416       0             \n",
      "DOR(Diagnostic odds ratio)                                        None          None          None          \n",
      "DP(Discriminant power)                                            None          None          None          \n",
      "DPI(Discriminant power interpretation)                            None          None          None          \n",
      "ERR(Error rate)                                                   0.16667       0.16667       0.0           \n",
      "F0.5(F0.5 score)                                                  0.71429       0.83333       1.0           \n",
      "F1(F1 score - harmonic mean of precision and sensitivity)         0.8           0.66667       1.0           \n",
      "F2(F2 score)                                                      0.90909       0.55556       1.0           \n",
      "FDR(False discovery rate)                                         0.33333       0.0           0.0           \n",
      "FN(False negative/miss/type 2 error)                              0             1             0             \n",
      "FNR(Miss rate or false negative rate)                             0.0           0.5           0.0           \n",
      "FOR(False omission rate)                                          0.0           0.2           0.0           \n",
      "FP(False positive/type 1 error/false alarm)                       1             0             0             \n",
      "FPR(Fall-out or false positive rate)                              0.25          0.0           0.0           \n",
      "G(G-measure geometric mean of precision and sensitivity)          0.8165        0.70711       1.0           \n",
      "GI(Gini index)                                                    0.75          0.5           1.0           \n",
      "GM(G-mean geometric mean of specificity and sensitivity)          0.86603       0.70711       1.0           \n",
      "IBA(Index of balanced accuracy)                                   0.9375        0.25          1.0           \n",
      "ICSI(Individual classification success index)                     0.66667       0.5           1.0           \n",
      "IS(Information score)                                             1.0           1.58496       1.58496       \n",
      "J(Jaccard index)                                                  0.66667       0.5           1.0           \n",
      "LS(Lift score)                                                    2.0           3.0           3.0           \n",
      "MCC(Matthews correlation coefficient)                             0.70711       0.63246       1.0           \n",
      "MCCI(Matthews correlation coefficient interpretation)             Strong        Moderate      Very Strong   \n",
      "MCEN(Modified confusion entropy)                                  0.26416       0.25          0             \n",
      "MK(Markedness)                                                    0.66667       0.8           1.0           \n",
      "N(Condition negative)                                             4             4             4             \n",
      "NLR(Negative likelihood ratio)                                    0.0           0.5           0.0           \n",
      "NLRI(Negative likelihood ratio interpretation)                    Good          Negligible    Good          \n",
      "NPV(Negative predictive value)                                    1.0           0.8           1.0           \n",
      "OC(Overlap coefficient)                                           1.0           1.0           1.0           \n",
      "OOC(Otsuka-Ochiai coefficient)                                    0.8165        0.70711       1.0           \n",
      "OP(Optimized precision)                                           0.69048       0.5           1.0           \n",
      "P(Condition positive or support)                                  2             2             2             \n",
      "PLR(Positive likelihood ratio)                                    4.0           None          None          \n",
      "PLRI(Positive likelihood ratio interpretation)                    Poor          None          None          \n",
      "POP(Population)                                                   6             6             6             \n",
      "PPV(Precision or positive predictive value)                       0.66667       1.0           1.0           \n",
      "PRE(Prevalence)                                                   0.33333       0.33333       0.33333       \n",
      "Q(Yule Q - coefficient of colligation)                            None          None          None          \n",
      "QI(Yule Q interpretation)                                         None          None          None          \n",
      "RACC(Random accuracy)                                             0.16667       0.05556       0.11111       \n",
      "RACCU(Random accuracy unbiased)                                   0.17361       0.0625        0.11111       \n",
      "TN(True negative/correct rejection)                               3             4             4             \n",
      "TNR(Specificity or true negative rate)                            0.75          1.0           1.0           \n",
      "TON(Test outcome negative)                                        3             5             4             \n",
      "TOP(Test outcome positive)                                        3             1             2             \n",
      "TP(True positive/hit)                                             2             1             2             \n",
      "TPR(Sensitivity, recall, hit rate, or true positive rate)         1.0           0.5           1.0           \n",
      "Y(Youden index)                                                   0.75          0.5           1.0           \n",
      "dInd(Distance index)                                              0.25          0.5           0.0           \n",
      "sInd(Similarity index)                                            0.82322       0.64645       1.0           \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(cm2)"
   ]
  }
 ],
 "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.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
