{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-16T15:36:12.054327Z","iopub.execute_input":"2022-03-16T15:36:12.054809Z","iopub.status.idle":"2022-03-16T15:37:38.487433Z","shell.execute_reply.started":"2022-03-16T15:36:12.054773Z","shell.execute_reply":"2022-03-16T15:37:38.486750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"IMPORTING ALL THE RELEVANT LIBRARIES\n* MADE USE OF KERAS PROMINENTLY","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport pickle \nimport shutil \nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:38.488880Z","iopub.execute_input":"2022-03-16T15:37:38.489123Z","iopub.status.idle":"2022-03-16T15:37:39.274375Z","shell.execute_reply.started":"2022-03-16T15:37:38.489088Z","shell.execute_reply":"2022-03-16T15:37:39.273596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the above mentioned code we have used PICKLE AND SHUTIL which i have described below:\n1. PICKLE - serializing and deserializing a Python object structure - coverting it into a byte stream - process called pickling\n2.  SHUTIL - provides many functions of high-level operations on files and collections of files","metadata":{}},{"cell_type":"code","source":"from sklearn.datasets import load_files\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:39.275484Z","iopub.execute_input":"2022-03-16T15:37:39.275914Z","iopub.status.idle":"2022-03-16T15:37:39.583439Z","shell.execute_reply.started":"2022-03-16T15:37:39.275879Z","shell.execute_reply":"2022-03-16T15:37:39.582718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.layers import Dense\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:39.585565Z","iopub.execute_input":"2022-03-16T15:37:39.585830Z","iopub.status.idle":"2022-03-16T15:37:44.328342Z","shell.execute_reply.started":"2022-03-16T15:37:39.585787Z","shell.execute_reply":"2022-03-16T15:37:44.327487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.utils.vis_utils import plot_model\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.applications.vgg16 import VGG16\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.330101Z","iopub.execute_input":"2022-03-16T15:37:44.330773Z","iopub.status.idle":"2022-03-16T15:37:44.354995Z","shell.execute_reply.started":"2022-03-16T15:37:44.330731Z","shell.execute_reply":"2022-03-16T15:37:44.354011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.356363Z","iopub.execute_input":"2022-03-16T15:37:44.357293Z","iopub.status.idle":"2022-03-16T15:37:44.361903Z","shell.execute_reply.started":"2022-03-16T15:37:44.357132Z","shell.execute_reply":"2022-03-16T15:37:44.361098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PYTHON IMAGING LIBRARY - PIL, IT ADDS IMAGE PROCESSING TO PYTHON","metadata":{}},{"cell_type":"code","source":"\nfrom PIL import ImageFile \nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.364125Z","iopub.execute_input":"2022-03-16T15:37:44.365308Z","iopub.status.idle":"2022-03-16T15:37:44.377400Z","shell.execute_reply.started":"2022-03-16T15:37:44.365270Z","shell.execute_reply":"2022-03-16T15:37:44.376685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Below we have used the OS MODULE - it helps in connecting/interacting  with the operating system.\nos.path.join() that we have used below is a method in Python  which is used to join one or more path components intelligently.\n\n\n\n\n\n\n\n\n","metadata":{}},{"cell_type":"code","source":"datalink = \"../input/state-farm-distracted-driver-detection/imgs\"\ntest_data = os.path.join(datalink,\"test\")\ntrain_data = os.path.join(datalink,\"train\")","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.378374Z","iopub.execute_input":"2022-03-16T15:37:44.378628Z","iopub.status.idle":"2022-03-16T15:37:44.394928Z","shell.execute_reply.started":"2022-03-16T15:37:44.378595Z","shell.execute_reply":"2022-03-16T15:37:44.394137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csvlink = os.path.join(os.getcwd(),\"csv_files\")\nmodelpath = os.path.join(os.getcwd(),\"model\",\"vgg16\")\npicklepath = os.path.join(os.getcwd(),\"pickle\")\ntestcsv = os.path.join(os.getcwd(),\"csv_files\",\"test.csv\")\ntraincsv = os.path.join(os.getcwd(),\"csv_files\",\"train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.396980Z","iopub.execute_input":"2022-03-16T15:37:44.397866Z","iopub.status.idle":"2022-03-16T15:37:44.411749Z","shell.execute_reply.started":"2022-03-16T15:37:44.397827Z","shell.execute_reply":"2022-03-16T15:37:44.410994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists(test_data):\n    print(\"Testing data -> does not exists\")\nif not os.path.exists(train_data):\n    print(\"Training data -> does not exists\")\nif not os.path.exists(modelpath):\n    print(\"Model path -> does not exists\")\n    os.makedirs(modelpath)\n    print(\"Model path created\")\n    \nelse:\n    shutil.rmtree(modelpath)\n    os.makedirs(modelpath)\nif not os.path.exists(picklepath):\n    os.makedirs(picklepath)\nif not os.path.exists(csvlink):\n    os.makedirs(csvlink)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.416135Z","iopub.execute_input":"2022-03-16T15:37:44.416480Z","iopub.status.idle":"2022-03-16T15:37:44.429520Z","shell.execute_reply.started":"2022-03-16T15:37:44.416447Z","shell.execute_reply":"2022-03-16T15:37:44.428374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LENGTH\nprint(len(train_data))\nprint(len(traincsv))\n\nprint(len(test_data))\nprint(len(testcsv))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.430887Z","iopub.execute_input":"2022-03-16T15:37:44.432024Z","iopub.status.idle":"2022-03-16T15:37:44.441363Z","shell.execute_reply.started":"2022-03-16T15:37:44.431986Z","shell.execute_reply":"2022-03-16T15:37:44.440665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"path. isdir() -> method in Python is used to check whether the specified path is an existing directory or not. If a path has a symbolic link linking to a directory it will return \"TRUE\"","metadata":{}},{"cell_type":"code","source":"def csvfunc(datalink,filename):\n    class_names = os.listdir(datalink)\n    data = list()\n    if(os.path.isdir(os.path.join(datalink,class_names[0]))):\n        for class_name in class_names:\n            file_names = os.listdir(os.path.join(datalink,class_name))\n            for file in file_names:\n                data.append({\n                    \"Filename\":os.path.join(datalink,class_name,file),\n                    \"ClassName\":class_name\n                })\n    else:\n        class_name = \"test\"\n        file_names = os.listdir(datalink)\n        for file in file_names:\n            data.append(({\n                \"FileName\":os.path.join(datalink,file),\n                \"ClassName\":class_name\n            }))\n    data = pd.DataFrame(data)\n    data.to_csv(os.path.join(os.getcwd(),\"csv_files\",filename),index=False)\n\ncsvfunc(train_data,\"train.csv\")\ncsvfunc(test_data,\"test.csv\")\ndata_train = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"train.csv\"))\ndata_test = pd.read_csv(os.path.join(os.getcwd(),\"csv_files\",\"test.csv\"))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:44.448146Z","iopub.execute_input":"2022-03-16T15:37:44.448632Z","iopub.status.idle":"2022-03-16T15:37:45.870145Z","shell.execute_reply.started":"2022-03-16T15:37:44.448594Z","shell.execute_reply":"2022-03-16T15:37:45.869427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Details of the images are mentioned below ","metadata":{}},{"cell_type":"code","source":"imgdetails = '../input/state-farm-distracted-driver-detection/'\ndriver_imgs_list = pd.read_csv(os.path.join(imgdetails,'driver_imgs_list.csv'))\ndriver_imgs_list.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:45.874488Z","iopub.execute_input":"2022-03-16T15:37:45.877355Z","iopub.status.idle":"2022-03-16T15:37:45.923127Z","shell.execute_reply.started":"2022-03-16T15:37:45.877312Z","shell.execute_reply":"2022-03-16T15:37:45.922444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = pd.read_csv(traincsv)\ndata_test = pd.read_csv(testcsv)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:45.924364Z","iopub.execute_input":"2022-03-16T15:37:45.924618Z","iopub.status.idle":"2022-03-16T15:37:46.046521Z","shell.execute_reply.started":"2022-03-16T15:37:45.924585Z","shell.execute_reply":"2022-03-16T15:37:46.045779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visual representation\ndriver_imgs_list.groupby('classname')['img'].count().sort_values().plot(kind='box')\nplt.ylabel('Images range')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:46.047735Z","iopub.execute_input":"2022-03-16T15:37:46.048196Z","iopub.status.idle":"2022-03-16T15:37:46.247237Z","shell.execute_reply.started":"2022-03-16T15:37:46.048160Z","shell.execute_reply":"2022-03-16T15:37:46.246359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import *\nfrom keras.utils.np_utils import to_categorical\n\n\nlabels_list = list(set(data_train['ClassName'].values.tolist()))\nlabels_id = {label_name:id for id,label_name in enumerate(labels_list)}\nprint(labels_id)\ndata_train['ClassName'].replace(labels_id,inplace=True)\n\nlabels = to_categorical(data_train['ClassName'])\nprint(labels.shape)\n\nwith open(os.path.join(picklepath,\"labels_list_vgg16.pkl\"),\"wb\") as handle:\n    pickle.dump(labels_id,handle)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:46.248556Z","iopub.execute_input":"2022-03-16T15:37:46.248789Z","iopub.status.idle":"2022-03-16T15:37:46.282843Z","shell.execute_reply.started":"2022-03-16T15:37:46.248755Z","shell.execute_reply":"2022-03-16T15:37:46.282076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Below we have printed out the size of each image","metadata":{}},{"cell_type":"code","source":"from PIL import Image \nimg = Image.open('../input/state-farm-distracted-driver-detection/imgs/test/img_1.jpg')\nprint(img.size)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:46.284219Z","iopub.execute_input":"2022-03-16T15:37:46.284477Z","iopub.status.idle":"2022-03-16T15:37:46.290839Z","shell.execute_reply.started":"2022-03-16T15:37:46.284443Z","shell.execute_reply":"2022-03-16T15:37:46.289738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* load_img - loads RGB image as PIL.Image type\n* img_to_array - converts tge PIL image type to 3d tensor with a shape.\n*  expand_dims -  convert 3D tensor to 4D tensor with shape (1, 64, 64, 3) and return 4D tensor","metadata":{}},{"cell_type":"code","source":"xtrain,xtest,ytrain,ytest = train_test_split(data_train.iloc[:,0],labels,test_size = 0.2,random_state=42)\ndef path_to_tensor(img_path):\n    \n    img = image.load_img(img_path, target_size=(64, 64))\n    \n    x = image.img_to_array(img)\n\n    return np.expand_dims(x, axis=0)\n\ndef paths_to_tensor(img_paths):\n    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]\n    return np.vstack(list_of_tensors)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:46.291959Z","iopub.execute_input":"2022-03-16T15:37:46.292159Z","iopub.status.idle":"2022-03-16T15:37:46.305640Z","shell.execute_reply.started":"2022-03-16T15:37:46.292134Z","shell.execute_reply":"2022-03-16T15:37:46.304823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Pre-Process the data for Keras","metadata":{}},{"cell_type":"code","source":"ImageFile.LOAD_TRUNCATED_IMAGES = True                 \n\ntrain_tensors = paths_to_tensor(xtrain).astype('float32')/255 - 0.5\nvalid_tensors = paths_to_tensor(xtest).astype('float32')/255 - 0.5","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:37:46.307435Z","iopub.execute_input":"2022-03-16T15:37:46.308250Z","iopub.status.idle":"2022-03-16T15:40:34.787034Z","shell.execute_reply.started":"2022-03-16T15:37:46.308195Z","shell.execute_reply":"2022-03-16T15:40:34.786281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = VGG16(include_top=False)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:40:34.788387Z","iopub.execute_input":"2022-03-16T15:40:34.788658Z","iopub.status.idle":"2022-03-16T15:40:38.544505Z","shell.execute_reply.started":"2022-03-16T15:40:34.788625Z","shell.execute_reply":"2022-03-16T15:40:38.543743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = VGG16(include_top=False)\ntrain_vgg16 = model.predict(train_tensors,verbose=1)\nvalid_vgg16 = model.predict(valid_tensors,verbose=1)\ntrain_features = train_vgg16[0]\nvalid_features = valid_vgg16[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:40:38.545627Z","iopub.execute_input":"2022-03-16T15:40:38.545946Z","iopub.status.idle":"2022-03-16T15:40:52.606205Z","shell.execute_reply.started":"2022-03-16T15:40:38.545908Z","shell.execute_reply":"2022-03-16T15:40:52.605438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nVGG16_model = Sequential()\nVGG16_model.add(GlobalAveragePooling2D(input_shape=train_features.shape))\nVGG16_model.add(Dense(10, activation='softmax', kernel_initializer='glorot_normal'))\nVGG16_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:40:52.612054Z","iopub.execute_input":"2022-03-16T15:40:52.614153Z","iopub.status.idle":"2022-03-16T15:40:52.672522Z","shell.execute_reply.started":"2022-03-16T15:40:52.614112Z","shell.execute_reply":"2022-03-16T15:40:52.671783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = os.path.join(modelpath,\"distracted-{epoch:02d}-{val_accuracy:.2f}.hdf5\")\ncheckpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max',period=1)\ncallbacks_list = [checkpoint]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:40:52.674251Z","iopub.execute_input":"2022-03-16T15:40:52.674756Z","iopub.status.idle":"2022-03-16T15:40:52.683152Z","shell.execute_reply.started":"2022-03-16T15:40:52.674719Z","shell.execute_reply":"2022-03-16T15:40:52.681979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history = VGG16_model.fit(train_vgg16,ytrain,validation_data = (valid_vgg16, ytest),epochs=200, batch_size=16, shuffle=True,callbacks=callbacks_list)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:40:52.684273Z","iopub.execute_input":"2022-03-16T15:40:52.685616Z","iopub.status.idle":"2022-03-16T15:50:37.127970Z","shell.execute_reply.started":"2022-03-16T15:40:52.685577Z","shell.execute_reply":"2022-03-16T15:50:37.127190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(model_history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(model_history.history['val_loss'], color='r', label=\"validation loss\")\nax1.set_xticks(np.arange(1, 400, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(model_history.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(model_history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, 400, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:50:37.129241Z","iopub.execute_input":"2022-03-16T15:50:37.129487Z","iopub.status.idle":"2022-03-16T15:50:43.467021Z","shell.execute_reply.started":"2022-03-16T15:50:37.129454Z","shell.execute_reply":"2022-03-16T15:50:43.466353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_confusion_matrix(confusion_matrix, class_names, figsize = (10,7), fontsize=14):\n    df_cm = pd.DataFrame(\n        confusion_matrix, index=class_names, columns=class_names, \n    )\n    fig = plt.figure(figsize=figsize)\n    try:\n        heatmap = sns.heatmap(df_cm, annot=True, fmt=\"d\")\n    except ValueError:\n        raise ValueError(\"Confusion matrix values must be integers.\")\n    heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right', fontsize=fontsize)\n    heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right', fontsize=fontsize)\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    fig.savefig(os.path.join(modelpath,\"confusion_matrix.png\"))\n    return fig","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:50:43.468371Z","iopub.execute_input":"2022-03-16T15:50:43.468840Z","iopub.status.idle":"2022-03-16T15:50:43.477319Z","shell.execute_reply.started":"2022-03-16T15:50:43.468803Z","shell.execute_reply":"2022-03-16T15:50:43.476651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_heatmap(n_labels, n_predictions, class_names):\n    labels = n_labels \n    predictions = n_predictions \n\n    matrix = confusion_matrix(labels.argmax(axis=1),predictions.argmax(axis=1))\n    row_sum = np.sum(matrix, axis = 1)\n    w, h = matrix.shape\n\n    c_m = np.zeros((w, h))\n\n    for i in range(h):\n        c_m[i] = matrix[i] * 100 / row_sum[i]\n\n    c = c_m.astype(dtype = np.uint8)\n\n    \n    heatmap = print_confusion_matrix(c, class_names, figsize=(18,10), fontsize=20)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:50:43.478681Z","iopub.execute_input":"2022-03-16T15:50:43.478978Z","iopub.status.idle":"2022-03-16T15:50:43.488914Z","shell.execute_reply.started":"2022-03-16T15:50:43.478940Z","shell.execute_reply":"2022-03-16T15:50:43.488052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = list()\nfor name,idx in labels_id.items():\n    class_names.append(name)\n# print(class_names)\nypred = VGG16_model.predict(valid_vgg16,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:50:43.490073Z","iopub.execute_input":"2022-03-16T15:50:43.490427Z","iopub.status.idle":"2022-03-16T15:50:43.708676Z","shell.execute_reply.started":"2022-03-16T15:50:43.490392Z","shell.execute_reply":"2022-03-16T15:50:43.707919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Visual representation using heatmap","metadata":{}},{"cell_type":"code","source":"print_heatmap(ytest,ypred,class_names)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:50:43.709917Z","iopub.execute_input":"2022-03-16T15:50:43.710157Z","iopub.status.idle":"2022-03-16T15:50:44.492907Z","shell.execute_reply.started":"2022-03-16T15:50:43.710124Z","shell.execute_reply":"2022-03-16T15:50:44.492225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred_class = np.argmax(ypred,axis=1)\nytest = np.argmax(ytest,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:50:44.494148Z","iopub.execute_input":"2022-03-16T15:50:44.494574Z","iopub.status.idle":"2022-03-16T15:50:44.499418Z","shell.execute_reply.started":"2022-03-16T15:50:44.494521Z","shell.execute_reply":"2022-03-16T15:50:44.498760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"FORMULAS OF PRECISION. RECALL\n* precision tp / (tp + fp)\n* recall: tp / (tp + fn)\n* f1: 2 tp / (2 tp + fp + fn)","metadata":{}},{"cell_type":"code","source":"accuracy = accuracy_score(ytest,ypred_class)\nprint('Accuracy -  %f' % accuracy)\n\nprecision = precision_score(ytest, ypred_class,average='weighted')\nprint('Precision -  %f' % precision)\n\nrecall = recall_score(ytest,ypred_class,average='weighted')\nprint('Recall -  %f' % recall)\n\nf1 = f1_score(ytest,ypred_class,average='weighted')\nprint('F1 score -  %f' % f1)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:11:30.222988Z","iopub.execute_input":"2022-03-16T16:11:30.223244Z","iopub.status.idle":"2022-03-16T16:11:30.241837Z","shell.execute_reply.started":"2022-03-16T16:11:30.223214Z","shell.execute_reply":"2022-03-16T16:11:30.241129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}