{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport tensorflow as tf\nimport keras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-24T10:53:33.622751Z","iopub.execute_input":"2024-02-24T10:53:33.624110Z","iopub.status.idle":"2024-02-24T10:53:50.680060Z","shell.execute_reply.started":"2024-02-24T10:53:33.624069Z","shell.execute_reply":"2024-02-24T10:53:50.678641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"KLD = keras.losses.KLDivergence()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:53:50.682167Z","iopub.execute_input":"2024-02-24T10:53:50.682862Z","iopub.status.idle":"2024-02-24T10:53:50.689502Z","shell.execute_reply.started":"2024-02-24T10:53:50.682826Z","shell.execute_reply":"2024-02-24T10:53:50.688418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Example**\nfrom: https://www.tensorflow.org/api_docs/python/tf/keras/losses/KLDivergence\n\nloss = y_true * log(y_true / y_pred)","metadata":{}},{"cell_type":"code","source":"y_true = [[0, 1], [0, 0]]\ny_pred = [[0.6, 0.4], [0.4, 0.6]]\nkl = tf.keras.losses.KLDivergence()\nkl(y_true, y_pred).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:53:50.690846Z","iopub.execute_input":"2024-02-24T10:53:50.691217Z","iopub.status.idle":"2024-02-24T10:53:50.915758Z","shell.execute_reply.started":"2024-02-24T10:53:50.691186Z","shell.execute_reply":"2024-02-24T10:53:50.914280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from private https://www.kaggle.com/code/jedrzejak/modifications-notebook\n\ndf = pd.read_csv('/kaggle/input/hms-harmful-brain-activity-classification/train.csv')\nTARGETS = ['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']\n\naggregation = {\n    'spectrogram_id': 'first',\n    'spectrogram_label_offset_seconds': ['min', 'max'],\n    'patient_id': 'first',\n    'expert_consensus': 'first'}\n\nfor target in TARGETS:\n    aggregation[target] = 'sum'\n\ntrain = df\\\n.groupby('eeg_id')\\\n.agg(aggregation)\\\n.reset_index()\n\ntrain.columns = ['eeg_id', 'spec_id', 'min', 'max', 'patient_id', 'target'] + TARGETS\ny_data = train[TARGETS].values\n\ny_true = y_data / y_data.sum(axis=1, keepdims=True)\n\ny_true","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:53:57.909207Z","iopub.execute_input":"2024-02-24T10:53:57.909660Z","iopub.status.idle":"2024-02-24T10:53:58.171568Z","shell.execute_reply.started":"2024-02-24T10:53:57.909626Z","shell.execute_reply":"2024-02-24T10:53:58.170274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Playing with metric","metadata":{}},{"cell_type":"markdown","source":"**When y_pred = y_true**","metadata":{}},{"cell_type":"code","source":"y_pred = y_true\nkl(y_true, y_pred).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:53:59.803537Z","iopub.execute_input":"2024-02-24T10:53:59.803982Z","iopub.status.idle":"2024-02-24T10:53:59.838026Z","shell.execute_reply.started":"2024-02-24T10:53:59.803950Z","shell.execute_reply":"2024-02-24T10:53:59.836765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**When y_pred = y_true* 0.1**","metadata":{}},{"cell_type":"code","source":"y_pred = y_true * 0.1\nkl(y_true, y_pred).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:54:02.915421Z","iopub.execute_input":"2024-02-24T10:54:02.915876Z","iopub.status.idle":"2024-02-24T10:54:02.929851Z","shell.execute_reply.started":"2024-02-24T10:54:02.915844Z","shell.execute_reply":"2024-02-24T10:54:02.928336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**When y_pred = y_true * 0.8**","metadata":{}},{"cell_type":"code","source":"y_pred = y_true * 0.8\nkl(y_true, y_pred).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:54:04.021594Z","iopub.execute_input":"2024-02-24T10:54:04.022317Z","iopub.status.idle":"2024-02-24T10:54:04.036704Z","shell.execute_reply.started":"2024-02-24T10:54:04.022283Z","shell.execute_reply":"2024-02-24T10:54:04.035456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**When y_pred = y_true * 0.7**","metadata":{}},{"cell_type":"code","source":"y_pred = y_true * 0.7\nkl(y_true, y_pred).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:54:05.093390Z","iopub.execute_input":"2024-02-24T10:54:05.093813Z","iopub.status.idle":"2024-02-24T10:54:05.108958Z","shell.execute_reply.started":"2024-02-24T10:54:05.093783Z","shell.execute_reply":"2024-02-24T10:54:05.107725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**When y_pred = y_true.mean()**","metadata":{}},{"cell_type":"code","source":"mean= y_true.mean()\ny_pred = mean\nkl(y_true, y_pred).numpy()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:54:06.680707Z","iopub.execute_input":"2024-02-24T10:54:06.681928Z","iopub.status.idle":"2024-02-24T10:54:06.697583Z","shell.execute_reply.started":"2024-02-24T10:54:06.681879Z","shell.execute_reply":"2024-02-24T10:54:06.696191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Target","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nclass_names = ['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']\n\nnum_classes = y_true.shape[1]\nfor i in range(num_classes):\n    plt.figure(figsize=(6, 4))\n    plt.hist(y_true[:, i], bins=20, alpha=0.75)\n    plt.title(f\"Histogram for class '{class_names[i]}'\")\n    plt.xlabel(\"Value\")\n    plt.ylabel(\"Number of Occurrences\")\n    plt.grid(True)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:54:07.813560Z","iopub.execute_input":"2024-02-24T10:54:07.814135Z","iopub.status.idle":"2024-02-24T10:54:09.442359Z","shell.execute_reply.started":"2024-02-24T10:54:07.814090Z","shell.execute_reply":"2024-02-24T10:54:09.441033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_proportions = np.mean(y_true, axis=0)\n\n# Create pie chart\nplt.figure(figsize=(8, 6))\nplt.pie(class_proportions, labels=class_names, autopct='%1.1f%%', startangle=140)\nplt.title(\"Proportion Distribution of Target Classes\")\nplt.axis('equal') \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-24T10:54:10.836730Z","iopub.execute_input":"2024-02-24T10:54:10.837186Z","iopub.status.idle":"2024-02-24T10:54:11.014701Z","shell.execute_reply.started":"2024-02-24T10:54:10.837155Z","shell.execute_reply":"2024-02-24T10:54:11.013463Z"},"trusted":true},"execution_count":null,"outputs":[]}]}