{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.9","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30068,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# IMPORTING THE LIBRARIES","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport pickle\nimport numpy as np\nimport seaborn as sns\nfrom sklearn.datasets import load_files\nfrom keras.utils import np_utils\nimport matplotlib.pyplot as plt\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.models import Sequential\nfrom keras.utils.vis_utils import plot_model\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.utils import to_categorical\nfrom sklearn.metrics import confusion_matrix\nfrom keras.preprocessing import image                  \nfrom tqdm import tqdm\n\nimport seaborn as sns\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:18.488589Z","iopub.execute_input":"2024-11-24T09:29:18.488928Z","iopub.status.idle":"2024-11-24T09:29:18.494664Z","shell.execute_reply.started":"2024-11-24T09:29:18.488900Z","shell.execute_reply":"2024-11-24T09:29:18.493484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Pretty display for notebooks\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:18.496901Z","iopub.execute_input":"2024-11-24T09:29:18.497281Z","iopub.status.idle":"2024-11-24T09:29:18.514548Z","shell.execute_reply.started":"2024-11-24T09:29:18.497247Z","shell.execute_reply":"2024-11-24T09:29:18.513799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:18.515675Z","iopub.execute_input":"2024-11-24T09:29:18.516033Z","iopub.status.idle":"2024-11-24T09:29:19.634318Z","shell.execute_reply.started":"2024-11-24T09:29:18.516000Z","shell.execute_reply":"2024-11-24T09:29:19.633510Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Defining the train,test and model directories\n\nWe will create the directories for train,test and model training paths if not present","metadata":{}},{"cell_type":"code","source":"DATA_DIR = \"../input/state-farm-distracted-driver-detection/imgs\"\nTEST_DIR = os.path.join(DATA_DIR,\"test\")\nTRAIN_DIR = os.path.join(DATA_DIR,\"train\")\nMODEL_PATH = os.path.join(os.getcwd(),\"model\",\"self_trained\")\nPICKLE_DIR = os.path.join(os.getcwd(),\"pickle_files\")\nCSV_DIR = os.path.join(os.getcwd(),\"csv_files\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:19.636122Z","iopub.execute_input":"2024-11-24T09:29:19.636492Z","iopub.status.idle":"2024-11-24T09:29:19.641582Z","shell.execute_reply.started":"2024-11-24T09:29:19.636453Z","shell.execute_reply":"2024-11-24T09:29:19.640830Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if not os.path.exists(TEST_DIR):\n    print(\"Testing data does not exists\")\nif not os.path.exists(TRAIN_DIR):\n    print(\"Training data does not exists\")\nif not os.path.exists(MODEL_PATH):\n    print(\"Model path does not exists\")\n    os.makedirs(MODEL_PATH)\n    print(\"Model path created\")\nif not os.path.exists(PICKLE_DIR):\n    os.makedirs(PICKLE_DIR)\nif not os.path.exists(CSV_DIR):\n    os.makedirs(CSV_DIR)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:19.644673Z","iopub.execute_input":"2024-11-24T09:29:19.644978Z","iopub.status.idle":"2024-11-24T09:29:19.692895Z","shell.execute_reply.started":"2024-11-24T09:29:19.644954Z","shell.execute_reply":"2024-11-24T09:29:19.692363Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"markdown","source":"We will create a csv file having the location of the files present for training and test images and their associated class if present so that it is easily traceable.","metadata":{}},{"cell_type":"code","source":"def create_csv(DATA_DIR,filename):\n    class_names = os.listdir(DATA_DIR)\n    data = list()\n    if(os.path.isdir(os.path.join(DATA_DIR,class_names[0]))):\n        for class_name in class_names:\n            file_names = os.listdir(os.path.join(DATA_DIR,class_name))\n            for file in file_names:\n                data.append({\n                    \"Filename\":os.path.join(DATA_DIR,class_name,file),\n                    \"ClassName\":class_name\n                })\n    else:\n        class_name = \"test\"\n        file_names = os.listdir(DATA_DIR)\n        for file in file_names:\n            data.append(({\n                \"FileName\":os.path.join(DATA_DIR,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\ncreate_csv(TRAIN_DIR,\"train.csv\")\ncreate_csv(TEST_DIR,\"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\"))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:19.695714Z","iopub.execute_input":"2024-11-24T09:29:19.695981Z","iopub.status.idle":"2024-11-24T09:29:20.345682Z","shell.execute_reply.started":"2024-11-24T09:29:19.695958Z","shell.execute_reply":"2024-11-24T09:29:20.345027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.346918Z","iopub.execute_input":"2024-11-24T09:29:20.347257Z","iopub.status.idle":"2024-11-24T09:29:20.359242Z","shell.execute_reply.started":"2024-11-24T09:29:20.347221Z","shell.execute_reply":"2024-11-24T09:29:20.358469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train['ClassName'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.360634Z","iopub.execute_input":"2024-11-24T09:29:20.360877Z","iopub.status.idle":"2024-11-24T09:29:20.374562Z","shell.execute_reply.started":"2024-11-24T09:29:20.360848Z","shell.execute_reply":"2024-11-24T09:29:20.373782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_train.describe()","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.375637Z","iopub.execute_input":"2024-11-24T09:29:20.375973Z","iopub.status.idle":"2024-11-24T09:29:20.406869Z","shell.execute_reply.started":"2024-11-24T09:29:20.375943Z","shell.execute_reply":"2024-11-24T09:29:20.406122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nnf = data_train['ClassName'].value_counts(sort=False)\nlabels = data_train['ClassName'].value_counts(sort=False).index.tolist()\ny = np.array(nf)\nwidth = 1/1.5\nN = len(y)\nx = range(N)\n\nfig = plt.figure(figsize=(20,15))\nay = fig.add_subplot(211)\n\nplt.xticks(x, labels, size=15)\nplt.yticks(size=15)\n\nay.bar(x, y, width, color=\"blue\")\n\nplt.title('Bar Chart',size=25)\nplt.xlabel('classname',size=15)\nplt.ylabel('Count',size=15)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.407699Z","iopub.execute_input":"2024-11-24T09:29:20.407894Z","iopub.status.idle":"2024-11-24T09:29:20.570442Z","shell.execute_reply.started":"2024-11-24T09:29:20.407874Z","shell.execute_reply":"2024-11-24T09:29:20.569678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.571462Z","iopub.execute_input":"2024-11-24T09:29:20.571670Z","iopub.status.idle":"2024-11-24T09:29:20.578799Z","shell.execute_reply.started":"2024-11-24T09:29:20.571649Z","shell.execute_reply":"2024-11-24T09:29:20.578037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.579825Z","iopub.execute_input":"2024-11-24T09:29:20.580136Z","iopub.status.idle":"2024-11-24T09:29:20.589442Z","shell.execute_reply.started":"2024-11-24T09:29:20.580105Z","shell.execute_reply":"2024-11-24T09:29:20.588691Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Observation:\n1. There are total 22424 training samples\n2. There are total 79726 testing samples\n3. The training dataset is equally balanced to a great extent and hence we need not do any downsampling of the data","metadata":{}},{"cell_type":"markdown","source":"## Converting into numerical values","metadata":{}},{"cell_type":"code","source":"labels_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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.590493Z","iopub.execute_input":"2024-11-24T09:29:20.590814Z","iopub.status.idle":"2024-11-24T09:29:20.613488Z","shell.execute_reply.started":"2024-11-24T09:29:20.590779Z","shell.execute_reply":"2024-11-24T09:29:20.612799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(os.path.join(os.getcwd(),\"pickle_files\",\"labels_list.pkl\"),\"wb\") as handle:\n    pickle.dump(labels_id,handle)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.614441Z","iopub.execute_input":"2024-11-24T09:29:20.614640Z","iopub.status.idle":"2024-11-24T09:29:20.617946Z","shell.execute_reply.started":"2024-11-24T09:29:20.614620Z","shell.execute_reply":"2024-11-24T09:29:20.617287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = to_categorical(data_train['ClassName'])\nprint(labels.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.618964Z","iopub.execute_input":"2024-11-24T09:29:20.619173Z","iopub.status.idle":"2024-11-24T09:29:20.629265Z","shell.execute_reply.started":"2024-11-24T09:29:20.619147Z","shell.execute_reply":"2024-11-24T09:29:20.628581Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Splitting into Train and Test sets","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nxtrain,xtest,ytrain,ytest = train_test_split(data_train.iloc[:,0],labels,test_size = 0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.630410Z","iopub.execute_input":"2024-11-24T09:29:20.630727Z","iopub.status.idle":"2024-11-24T09:29:20.640683Z","shell.execute_reply.started":"2024-11-24T09:29:20.630687Z","shell.execute_reply":"2024-11-24T09:29:20.640070Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Converting into 64*64 images \nYou can substitute 64,64 to 224,224 for better results only if ram is >32gb","metadata":{}},{"cell_type":"code","source":"\ndef path_to_tensor(img_path):\n    # loads RGB image as PIL.Image.Image type\n    img = image.load_img(img_path, target_size=(64, 64))\n    # convert PIL.Image.Image type to 3D tensor with shape (64, 64, 3)\n    x = image.img_to_array(img)\n    # convert 3D tensor to 4D tensor with shape (1, 64,64, 3) and return 4D tensor\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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.641678Z","iopub.execute_input":"2024-11-24T09:29:20.641892Z","iopub.status.idle":"2024-11-24T09:29:20.649641Z","shell.execute_reply.started":"2024-11-24T09:29:20.641871Z","shell.execute_reply":"2024-11-24T09:29:20.649027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfrom PIL import ImageFile                            \nImageFile.LOAD_TRUNCATED_IMAGES = True                 \n\n# pre-process the data for Keras\ntrain_tensors = paths_to_tensor(xtrain).astype('float32')/255 - 0.5\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:29:20.650539Z","iopub.execute_input":"2024-11-24T09:29:20.650753Z","iopub.status.idle":"2024-11-24T09:31:01.299303Z","shell.execute_reply.started":"2024-11-24T09:29:20.650732Z","shell.execute_reply":"2024-11-24T09:31:01.298483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"valid_tensors = paths_to_tensor(xtest).astype('float32')/255 - 0.5\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:31:01.300894Z","iopub.execute_input":"2024-11-24T09:31:01.301247Z","iopub.status.idle":"2024-11-24T09:31:26.884800Z","shell.execute_reply.started":"2024-11-24T09:31:01.301211Z","shell.execute_reply":"2024-11-24T09:31:26.884129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"##takes too much ram \n## run this if your ram is greater than 16gb \ntest_tensors = paths_to_tensor(data_test.iloc[:,0]).astype('float32')/255 - 0.5 ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:31:26.885884Z","iopub.execute_input":"2024-11-24T09:31:26.886091Z","iopub.status.idle":"2024-11-24T09:39:50.013273Z","shell.execute_reply.started":"2024-11-24T09:31:26.886070Z","shell.execute_reply":"2024-11-24T09:39:50.012630Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Defining the Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Input\nfrom tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.initializers import glorot_normal\n\nmobilenetv2_base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(64, 64, 3))\nfor layer in mobilenetv2_base.layers:\n    layer.trainable = False\n\nmodel = Sequential()\n\nmodel.add(mobilenetv2_base)\nmodel.add(Flatten())\nmodel.add(Dense(500, activation='relu', kernel_initializer=glorot_normal()))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax', kernel_initializer=glorot_normal()))\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:39:50.014359Z","iopub.execute_input":"2024-11-24T09:39:50.014629Z","iopub.status.idle":"2024-11-24T09:39:51.183615Z","shell.execute_reply.started":"2024-11-24T09:39:50.014593Z","shell.execute_reply":"2024-11-24T09:39:51.182854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_model(model,to_file=os.path.join(MODEL_PATH,\"model_distracted_driver.png\"),show_shapes=True,show_layer_names=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:39:51.184857Z","iopub.execute_input":"2024-11-24T09:39:51.185176Z","iopub.status.idle":"2024-11-24T09:39:51.744550Z","shell.execute_reply.started":"2024-11-24T09:39:51.185131Z","shell.execute_reply":"2024-11-24T09:39:51.743492Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:39:51.745991Z","iopub.execute_input":"2024-11-24T09:39:51.746299Z","iopub.status.idle":"2024-11-24T09:39:51.761220Z","shell.execute_reply.started":"2024-11-24T09:39:51.746270Z","shell.execute_reply":"2024-11-24T09:39:51.760530Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filepath = os.path.join(MODEL_PATH,\"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:39:51.762298Z","iopub.execute_input":"2024-11-24T09:39:51.762576Z","iopub.status.idle":"2024-11-24T09:39:51.771989Z","shell.execute_reply.started":"2024-11-24T09:39:51.762552Z","shell.execute_reply":"2024-11-24T09:39:51.771314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_history = model.fit(train_tensors,ytrain,validation_data = (valid_tensors, ytest),epochs=25, batch_size=40, shuffle=True,callbacks=callbacks_list)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:39:51.772917Z","iopub.execute_input":"2024-11-24T09:39:51.773164Z","iopub.status.idle":"2024-11-24T09:41:48.565130Z","shell.execute_reply.started":"2024-11-24T09:39:51.773141Z","shell.execute_reply":"2024-11-24T09:41:48.564402Z"}},"outputs":[],"execution_count":null},{"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, 25, 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, 25, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:48.566694Z","iopub.execute_input":"2024-11-24T09:41:48.567024Z","iopub.status.idle":"2024-11-24T09:41:49.000276Z","shell.execute_reply.started":"2024-11-24T09:41:48.566989Z","shell.execute_reply":"2024-11-24T09:41:48.999463Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Analysis\n\nFinding the Confusion matrix,Precision,Recall and F1 score to analyse the model thus created ","metadata":{}},{"cell_type":"code","source":"\ndef 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(MODEL_PATH,\"confusion_matrix.png\"))\n    return fig\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:49.001695Z","iopub.execute_input":"2024-11-24T09:41:49.002026Z","iopub.status.idle":"2024-11-24T09:41:49.008291Z","shell.execute_reply.started":"2024-11-24T09:41:49.001989Z","shell.execute_reply":"2024-11-24T09:41:49.007627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def print_heatmap(n_labels, n_predictions, class_names):\n    labels = n_labels #sess.run(tf.argmax(n_labels, 1))\n    predictions = n_predictions #sess.run(tf.argmax(n_predictions, 1))\n\n#     confusion_matrix = sess.run(tf.contrib.metrics.confusion_matrix(labels, predictions))\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)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:49.009457Z","iopub.execute_input":"2024-11-24T09:41:49.009657Z","iopub.status.idle":"2024-11-24T09:41:49.018516Z","shell.execute_reply.started":"2024-11-24T09:41:49.009638Z","shell.execute_reply":"2024-11-24T09:41:49.017828Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = list()\nfor name,idx in labels_id.items():\n    class_names.append(name)\nypred = model.predict(valid_tensors)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:49.019655Z","iopub.execute_input":"2024-11-24T09:41:49.019966Z","iopub.status.idle":"2024-11-24T09:41:50.455703Z","shell.execute_reply.started":"2024-11-24T09:41:49.019935Z","shell.execute_reply":"2024-11-24T09:41:50.454972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_heatmap(ytest,ypred,class_names)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:50.456813Z","iopub.execute_input":"2024-11-24T09:41:50.457022Z","iopub.status.idle":"2024-11-24T09:41:51.285072Z","shell.execute_reply.started":"2024-11-24T09:41:50.457001Z","shell.execute_reply":"2024-11-24T09:41:51.284328Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Precision Recall F1 Score","metadata":{}},{"cell_type":"code","source":"ypred_class = np.argmax(ypred,axis=1)\n# print(ypred_class[:10])\nytest = np.argmax(ytest,axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:51.286321Z","iopub.execute_input":"2024-11-24T09:41:51.286579Z","iopub.status.idle":"2024-11-24T09:41:51.290310Z","shell.execute_reply.started":"2024-11-24T09:41:51.286554Z","shell.execute_reply":"2024-11-24T09:41:51.289458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = accuracy_score(ytest,ypred_class)\nprint('Accuracy: %f' % accuracy)\n# precision tp / (tp + fp)\nprecision = precision_score(ytest, ypred_class,average='weighted')\nprint('Precision: %f' % precision)\n# recall: tp / (tp + fn)\nrecall = recall_score(ytest,ypred_class,average='weighted')\nprint('Recall: %f' % recall)\n# f1: 2 tp / (2 tp + fp + fn)\nf1 = f1_score(ytest,ypred_class,average='weighted')\nprint('F1 score: %f' % f1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-24T09:41:51.293206Z","iopub.execute_input":"2024-11-24T09:41:51.293494Z","iopub.status.idle":"2024-11-24T09:41:51.310988Z","shell.execute_reply.started":"2024-11-24T09:41:51.293457Z","shell.execute_reply":"2024-11-24T09:41:51.310331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}