{"cells":[{"metadata":{"_uuid":"1da96117ac22b9abd66a771ed0606e5356aa81fb"},"cell_type":"markdown","source":"## Confusion matrix and Miss-Classification table"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport itertools\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"40d3a5f3780a8d27da5a9bce0f196bd740847d0a"},"cell_type":"code","source":"!wget 'https://www.dropbox.com/s/6b1ervg883khysg/train_true.csv'\n!wget 'https://www.dropbox.com/s/y2g55iywusndq7h/train_preds.csv'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ab663344946de204c04fe522cf37a2b1ded39f91"},"cell_type":"markdown","source":"### Load predictions"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_true = pd.read_csv('train_true.csv')\ntrain_preds = pd.read_csv('train_preds.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23fbbb22aa0e4dd1bec55fd210e73b5d0daac0c2"},"cell_type":"code","source":"labels  = ['Nucleoplasm','Nuclear membrane','Nucleoli',\n'Nucleoli fibrillar center','Nuclear speckles','Nuclear bodies',\n'Endoplasmic reticulum ','Golgi apparatus','Peroxisomes',\n'Endosomes','Lysosomes','Intermediate filaments ',\n'Actin filaments','Focal adhesion sites','Microtubules',\n'Microtubule ends','Cytokinetic bridge','Mitotic spindle',\n'Microtubule organizing center','Centrosome','Lipid droplets',\n'Plasma membrane','Cell junctions','Mitochondria',\n'Aggresome','Cytosol','Cytoplasmic bodies', 'Rods & rings']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76b1f9e283a09dc1047e6fb7792b619ca51a0ce5"},"cell_type":"code","source":"yt = train_true.values[:, 1:]\nyp = (train_preds.values[:, 1:] > 0).astype('int')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"19eff803d31e8e3850bbeb0331e14a194b77d7a1"},"cell_type":"markdown","source":"### Confusion matrix"},{"metadata":{"trusted":true,"_uuid":"4bb9fa050bd37418e6f38bac9a2560339fb9df6a"},"cell_type":"code","source":"# based on https://www.kaggle.com/nikolaikopernik/confusion-matrix\ndef confusion_matrix(yt, yp, labels):\n    instcount = yt.shape[0]\n    n_classes = len(labels)\n    fp = ((yt - yp) < 0).sum(axis = 0)\n    fn = ((yt - yp) > 0).sum(axis = 0)\n    tp = (yt*yp).sum(axis = 0)\n    tn = ((yt==0)*(yp==0)).sum(axis = 0)\n    mtx = np.vstack([tp/(tp + fn), fn/(tp + fn), tn/(tn + fp), fp/(tn + fp)]).T\n    plt.figure(num=None, figsize=(5, 15), dpi=100, facecolor='w', edgecolor='k')\n    plt.imshow(mtx, interpolation='nearest',cmap='Blues')\n    plt.title(\"Confusion matrix\")\n    tick_marks = np.arange(n_classes)\n    plt.xticks(np.arange(4), ['1 - 1','1 - 0','0 - 0','0 - 1'])\n    plt.yticks(tick_marks, labels)\n    for i, j in itertools.product(range(n_classes), range(4)):\n        plt.text(j, i, round(mtx[i][j],2), horizontalalignment=\"center\")\n\n    plt.ylabel('labels')\n    plt.xlabel('True-Predicted')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d56be90103b08bb58ebb6ebe183706219e519c4e"},"cell_type":"code","source":"confusion_matrix(yt, yp, labels)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b7e593813222bcf16b229a749ff67d1ea3f67277"},"cell_type":"markdown","source":"### Miss-Classification table"},{"metadata":{"trusted":true,"_uuid":"5bc0f87559b37409e3ed5adb943d1ab3e97a2d51"},"cell_type":"code","source":"def miss_classification(yt, yp, classes, percent=False, multi_label=True):\n    '''\n    Params:\n        yt, yp : binary numpy array\n        classes : list of names classes\n        percent : display persent of miss classification\n        multi_label : target can have multy labels\n    '''\n\n    n_classes = len(classes)\n    fp = ((yt - yp) < 0).astype('int')\n    fn = ((yt - yp) > 0).astype('int')\n    mtc = (fn.T @ fp)\n    if multi_label:\n        mtc = np.hstack([mtc, (fp.sum(axis = 0) - mtc.sum(axis = 0)).clip(0)[:,None]])\n        classes.append('As extra class')\n    if percent:\n        mtc = (mtc / mtc.sum(axis = 1)[:,None])\n        mtc = np.nan_to_num(mtc).clip(0,1) * 100\n        \n    plt.figure(num=None, figsize=(12, 12), dpi=100, facecolor='w', edgecolor='k')\n    plt.imshow(mtc, interpolation='nearest',cmap='Blues')\n    plt.title(\"Miss-Classification table\")\n\n    plt.xticks(np.arange(mtc.shape[1]), classes, rotation=90)\n    plt.yticks(np.arange(mtc.shape[0]), classes, rotation=0)\n    \n    for i in range(mtc.shape[0]):\n        for j in range(mtc.shape[1]):\n            plt.text(j, i, int(mtc[i][j]), horizontalalignment=\"center\")\n    plt.ylabel('labels')\n    plt.xlabel('Miss classed')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"838ec48e8d65cbe950accbbe2215415b410a6c00"},"cell_type":"markdown","source":"### Total count of miss classed"},{"metadata":{"trusted":true,"_uuid":"aa3d6bc0f941a19db6c229c7d607088b832e7c2d"},"cell_type":"code","source":"miss_classification(yt, yp, labels)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"977363fb76ec68e5e21952f67fa92940265a7164"},"cell_type":"markdown","source":"**Percet of miss classed class to class**\nExample: Nuclear speckles clasified as Nucleoplasm in 59% of all missclassed "},{"metadata":{"trusted":true,"_uuid":"a76bdb9cc87ac6a7996fb67dbff7ff505c2d78dc"},"cell_type":"code","source":"miss_classification(yt, yp, labels, percent=True)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}