{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30635,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set(style=\"whitegrid\")\ntrain = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/train.csv\")\n\ntrain.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-11T02:47:30.251399Z","iopub.execute_input":"2024-01-11T02:47:30.251866Z","iopub.status.idle":"2024-01-11T02:47:31.774312Z","shell.execute_reply.started":"2024-01-11T02:47:30.251826Z","shell.execute_reply":"2024-01-11T02:47:31.772966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows, columns = train.shape\nrows, columns","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:47:31.909683Z","iopub.execute_input":"2024-01-11T02:47:31.910096Z","iopub.status.idle":"2024-01-11T02:47:31.918965Z","shell.execute_reply.started":"2024-01-11T02:47:31.910064Z","shell.execute_reply":"2024-01-11T02:47:31.917266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_info = train.info()\ntrain_info","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:47:34.015880Z","iopub.execute_input":"2024-01-11T02:47:34.016450Z","iopub.status.idle":"2024-01-11T02:47:34.054938Z","shell.execute_reply.started":"2024-01-11T02:47:34.016400Z","shell.execute_reply":"2024-01-11T02:47:34.054104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_columns = train.select_dtypes(include=['object', 'category']).columns\ncategorical_summary = train[categorical_columns].describe()\ncategorical_summary","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:47:44.616201Z","iopub.execute_input":"2024-01-11T02:47:44.616679Z","iopub.status.idle":"2024-01-11T02:47:44.650984Z","shell.execute_reply.started":"2024-01-11T02:47:44.616638Z","shell.execute_reply":"2024-01-11T02:47:44.649675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(set(train['expert_consensus'].unique()))","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:47:53.220570Z","iopub.execute_input":"2024-01-11T02:47:53.220987Z","iopub.status.idle":"2024-01-11T02:47:53.235442Z","shell.execute_reply.started":"2024-01-11T02:47:53.220952Z","shell.execute_reply":"2024-01-11T02:47:53.234196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.countplot(data=train, x='expert_consensus')\nplt.title('Distribution of Expert Consensus')\nplt.xlabel('Expert Consensus')\nplt.ylabel('Count')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:47:59.543094Z","iopub.execute_input":"2024-01-11T02:47:59.543550Z","iopub.status.idle":"2024-01-11T02:47:59.985761Z","shell.execute_reply.started":"2024-01-11T02:47:59.543512Z","shell.execute_reply":"2024-01-11T02:47:59.984720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = ['seizure_vote', 'lpd_vote', 'gpd_vote', 'lrda_vote', 'grda_vote', 'other_vote']\n\nplt.figure(figsize=(15, 10))\nfor i, column in enumerate(targets, 1):\n    plt.subplot(2, 4, i)\n    sns.histplot(train[column], kde=False, bins=30)\n    plt.title(column)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:11.034080Z","iopub.execute_input":"2024-01-11T02:48:11.034568Z","iopub.status.idle":"2024-01-11T02:48:14.261650Z","shell.execute_reply.started":"2024-01-11T02:48:11.034530Z","shell.execute_reply":"2024-01-11T02:48:14.260180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlation_targets = train[targets].corr()\nplt.figure(figsize=(12, 8))\nsns.heatmap(correlation_targets, annot=True, cmap='coolwarm', fmt=\".2f\")\nplt.title('Correlation Matrix of Vote Columns')\nplt.show()\n\nplt.figure(figsize=(12, 10))\nfor i, column in enumerate(targets, 1):\n    plt.subplot(3, 2, i)\n    sns.violinplot(data=train, x='expert_consensus', y=column)\n    plt.title(f'Distribution of {column} by Expert Consensus')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:18.173625Z","iopub.execute_input":"2024-01-11T02:48:18.174021Z","iopub.status.idle":"2024-01-11T02:48:24.882756Z","shell.execute_reply.started":"2024-01-11T02:48:18.173991Z","shell.execute_reply":"2024-01-11T02:48:24.881672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"offset_stats = train[['eeg_label_offset_seconds', 'spectrogram_label_offset_seconds']].describe()\n\nplt.figure(figsize=(12, 6))\nsns.histplot(train['eeg_label_offset_seconds'], bins=30, kde=True)\nplt.title('Distribution of EEG Label Offset Seconds')\nplt.xlabel('EEG Label Offset Seconds')\nplt.ylabel('Count')\nplt.show()\n\nplt.figure(figsize=(12, 6))\nsns.histplot(train['spectrogram_label_offset_seconds'], bins=30, kde=True)\nplt.title('Distribution of Spectrogram Label Offset Seconds')\nplt.xlabel('Spectrogram Label Offset Seconds')\nplt.ylabel('Count')\nplt.show()\n\noffset_stats","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:28.543219Z","iopub.execute_input":"2024-01-11T02:48:28.544224Z","iopub.status.idle":"2024-01-11T02:48:30.553561Z","shell.execute_reply.started":"2024-01-11T02:48:28.544166Z","shell.execute_reply":"2024-01-11T02:48:30.552105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_eegs = len(train['eeg_id'].unique())\ntotal_eegs","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:40.241643Z","iopub.execute_input":"2024-01-11T02:48:40.242040Z","iopub.status.idle":"2024-01-11T02:48:40.253631Z","shell.execute_reply.started":"2024-01-11T02:48:40.242000Z","shell.execute_reply":"2024-01-11T02:48:40.252704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_eeg_label_offset_seconds = sorted(list(train['eeg_label_offset_seconds'].unique()))\nlen(all_eeg_label_offset_seconds), str(all_eeg_label_offset_seconds[0:5]), str(all_eeg_label_offset_seconds[-5:])","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:46.294775Z","iopub.execute_input":"2024-01-11T02:48:46.295264Z","iopub.status.idle":"2024-01-11T02:48:46.306859Z","shell.execute_reply.started":"2024-01-11T02:48:46.295226Z","shell.execute_reply":"2024-01-11T02:48:46.305568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_spectrogram_label_offset_seconds = sorted(list(train['spectrogram_label_offset_seconds'].unique()))\nlen(all_spectrogram_label_offset_seconds), str(all_spectrogram_label_offset_seconds[0:5]), str(all_spectrogram_label_offset_seconds[-5:])","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:50.800360Z","iopub.execute_input":"2024-01-11T02:48:50.800849Z","iopub.status.idle":"2024-01-11T02:48:50.813443Z","shell.execute_reply.started":"2024-01-11T02:48:50.800811Z","shell.execute_reply":"2024-01-11T02:48:50.811879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg_sub_id_count_per_eeg_id = train.groupby('eeg_id')['eeg_sub_id'].nunique()\nspectrogram_sub_id_count_per_spectrogram_id = train.groupby('spectrogram_id')['spectrogram_sub_id'].nunique()\n\nplt.figure(figsize=(12, 6))\nsns.histplot(eeg_sub_id_count_per_eeg_id, bins=50, kde=True)\nplt.title('EEG Sub-ID Count per EEG ID')\nplt.xlabel('Count of EEG Sub-ID per EEG ID')\nplt.ylabel('Frequency')\nplt.show()\n\nplt.figure(figsize=(12, 6))\nsns.histplot(spectrogram_sub_id_count_per_spectrogram_id, bins=50, kde=True)\nplt.title('Spectrogram Sub-ID Count per Spectrogram ID')\nplt.xlabel('Count of Spectrogram Sub-ID per Spectrogram ID')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:48:56.279497Z","iopub.execute_input":"2024-01-11T02:48:56.279991Z","iopub.status.idle":"2024-01-11T02:48:57.509530Z","shell.execute_reply.started":"2024-01-11T02:48:56.279948Z","shell.execute_reply":"2024-01-11T02:48:57.508657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vote_counts_by_consensus = train.groupby('expert_consensus')[targets].sum()\n\nplt.figure(figsize=(12, 8))\nvote_counts_by_consensus.plot(kind='bar', stacked=True)\nplt.title('Overall Vote Counts by Expert Consensus')\nplt.xlabel('Expert Consensus')\nplt.ylabel('Total Votes')\nplt.xticks(rotation=45)\nplt.legend(title='Vote Types')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:03.941385Z","iopub.execute_input":"2024-01-11T02:49:03.941840Z","iopub.status.idle":"2024-01-11T02:49:04.413252Z","shell.execute_reply.started":"2024-01-11T02:49:03.941805Z","shell.execute_reply":"2024-01-11T02:49:04.411971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cumulative_votes = train.groupby('eeg_label_offset_seconds')[targets].sum().cumsum().reset_index()\n\nplt.figure(figsize=(12, 8))\nfor column in targets:\n    plt.plot(cumulative_votes['eeg_label_offset_seconds'], cumulative_votes[column], label=column)\n\nplt.title('Vote Counts Over EEG Label Offset Seconds')\nplt.xlabel('EEG Label Offset Seconds')\nplt.ylabel('Total Votes')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:07.785387Z","iopub.execute_input":"2024-01-11T02:49:07.786938Z","iopub.status.idle":"2024-01-11T02:49:08.270167Z","shell.execute_reply.started":"2024-01-11T02:49:07.786887Z","shell.execute_reply":"2024-01-11T02:49:08.268717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cumulative_votes = train.groupby('spectrogram_label_offset_seconds')[targets].sum().cumsum().reset_index()\n\nplt.figure(figsize=(12, 8))\nfor column in targets:\n    plt.plot(cumulative_votes['spectrogram_label_offset_seconds'], cumulative_votes[column], label=column)\n\nplt.title('Vote Counts Over Spectrogram Offset Seconds')\nplt.xlabel('EEG Label Offset Seconds')\nplt.ylabel('Total Votes')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:13.942165Z","iopub.execute_input":"2024-01-11T02:49:13.943570Z","iopub.status.idle":"2024-01-11T02:49:14.422199Z","shell.execute_reply.started":"2024-01-11T02:49:13.943502Z","shell.execute_reply":"2024-01-11T02:49:14.420938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_data = train.sort_values(by=['eeg_id', 'eeg_sub_id'])\n\nsorted_data['offset_difference'] = sorted_data.groupby('eeg_id')['eeg_label_offset_seconds'].diff()\n\noffset_differences = sorted_data['offset_difference'].dropna()\n\noffset_difference_stats = offset_differences.describe()\n\nplt.figure(figsize=(12, 6))\nsns.histplot(offset_differences, bins=30, kde=True)\nplt.title('Offset Differences within EEG IDs')\nplt.xlabel('Offset Difference (Seconds)')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:19.255755Z","iopub.execute_input":"2024-01-11T02:49:19.256189Z","iopub.status.idle":"2024-01-11T02:49:20.287137Z","shell.execute_reply.started":"2024-01-11T02:49:19.256149Z","shell.execute_reply":"2024-01-11T02:49:20.286052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_data = train.sort_values(by=['spectrogram_id', 'spectrogram_sub_id'])\n\nsorted_data['offset_difference'] = sorted_data.groupby('spectrogram_id')['spectrogram_label_offset_seconds'].diff()\n\noffset_differences = sorted_data['offset_difference'].dropna()\n\noffset_difference_stats = offset_differences.describe()\n\nplt.figure(figsize=(12, 6))\nsns.histplot(offset_differences, bins=30, kde=True)\nplt.title('Offset Differences within Spectrogram IDs')\nplt.xlabel('Offset Difference (Seconds)')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:23.172230Z","iopub.execute_input":"2024-01-11T02:49:23.172642Z","iopub.status.idle":"2024-01-11T02:49:24.327094Z","shell.execute_reply.started":"2024-01-11T02:49:23.172609Z","shell.execute_reply":"2024-01-11T02:49:24.326260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_patients = train['patient_id'].sample(20, random_state=1).values\nsample_data = train[train['patient_id'].isin(sample_patients)]\n\nfor i, vote_type in enumerate(targets, 1):\n    plt.figure(figsize=(15, 10))\n    sns.boxplot(x='patient_id', y=vote_type, data=sample_data)\n    plt.title(f'Distribution of {vote_type} for Selected Patients')\n    plt.xlabel('Patient ID')\n    plt.ylabel(f'{vote_type} Count')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:28.237662Z","iopub.execute_input":"2024-01-11T02:49:28.238106Z","iopub.status.idle":"2024-01-11T02:49:32.573158Z","shell.execute_reply.started":"2024-01-11T02:49:28.238070Z","shell.execute_reply":"2024-01-11T02:49:32.571843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\n\n\nfor i, patient_id in enumerate(sample_patients, 1):\n    plt.figure(figsize=(15, 10))\n    patient_data = train[train['patient_id'] == patient_id]\n    correlation_matrix = patient_data[targets].corr()\n    sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=\".2f\")\n    plt.title(f'Correlation of Votes for Patient ID {patient_id}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:36.201833Z","iopub.execute_input":"2024-01-11T02:49:36.202243Z","iopub.status.idle":"2024-01-11T02:49:46.481351Z","shell.execute_reply.started":"2024-01-11T02:49:36.202209Z","shell.execute_reply":"2024-01-11T02:49:46.480195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_votes_per_eeg = train.groupby('eeg_id')[targets].sum().sum(axis=1)\nnormalized_votes = train.groupby('eeg_id')[targets].sum().div(total_votes_per_eeg, axis=0)\nmean_vote_ratio = normalized_votes.mean()\nmean_vote_ratio","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:46.483187Z","iopub.execute_input":"2024-01-11T02:49:46.484188Z","iopub.status.idle":"2024-01-11T02:49:46.524542Z","shell.execute_reply.started":"2024-01-11T02:49:46.484153Z","shell.execute_reply":"2024-01-11T02:49:46.523447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gap = 1 - sum([round(v,6) for _, v in mean_vote_ratio.items()])\nprint(gap)\nmean_vote_ratio['other_vote'] += gap","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:48.081097Z","iopub.execute_input":"2024-01-11T02:49:48.081555Z","iopub.status.idle":"2024-01-11T02:49:48.088500Z","shell.execute_reply.started":"2024-01-11T02:49:48.081517Z","shell.execute_reply":"2024-01-11T02:49:48.087274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum([round(v,5) for _, v in mean_vote_ratio.items()])","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:49:53.842838Z","iopub.execute_input":"2024-01-11T02:49:53.843355Z","iopub.status.idle":"2024-01-11T02:49:53.851461Z","shell.execute_reply.started":"2024-01-11T02:49:53.843319Z","shell.execute_reply":"2024-01-11T02:49:53.850402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/hms-harmful-brain-activity-classification/sample_submission.csv\")\nfor target in targets:\n    sub[target] = mean_vote_ratio[target]\nsub","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:50:01.355799Z","iopub.execute_input":"2024-01-11T02:50:01.356229Z","iopub.status.idle":"2024-01-11T02:50:01.377106Z","shell.execute_reply.started":"2024-01-11T02:50:01.356193Z","shell.execute_reply":"2024-01-11T02:50:01.375801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-11T02:50:11.621066Z","iopub.execute_input":"2024-01-11T02:50:11.621442Z","iopub.status.idle":"2024-01-11T02:50:11.630170Z","shell.execute_reply.started":"2024-01-11T02:50:11.621413Z","shell.execute_reply":"2024-01-11T02:50:11.629015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}