{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Analyze your model performance by confusion matrix"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# inference results\ndf_train = pd.read_csv(\"../input/inference5folds/train_inference_5folds.csv\")\n\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# read logits\nlogits = np.array([df_train[\"logits0\"], df_train[\"logits1\"], df_train[\"logits2\"], df_train[\"logits3\"] ,df_train[\"logits4\"]]).T\nlogits","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# accuracy for 5-fold\nnp.sum((df_train.label.values==logits.argmax(1)))/(len(df_train))*100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# get df for incorrect predictions\ndf_train[df_train[\"label\"]!=logits.argmax(1)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ncm = confusion_matrix(df_train[\"label\"], logits.argmax(1))\n\nprint(cm)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, val in enumerate(cm):\n    print(\"for class {}: accuracy: {}\".format(i, val[i]/sum(val)*100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, val in enumerate(cm[:-1]):\n    print(\"for class {}: possibility to mistake for healthy: {}\".format(i, val[4]/val[i]*100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nsns.heatmap(cm)\nplt.savefig('.sklearn_confusion_matrix.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# Calculate P, R, f1"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import classification_report\n\nprint(classification_report(df_train[\"label\"], logits.argmax(1)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}