{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":104178,"databundleVersionId":12535956,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:24:25.303246Z","iopub.execute_input":"2025-05-31T13:24:25.303716Z","iopub.status.idle":"2025-05-31T13:24:25.985413Z","shell.execute_reply.started":"2025-05-31T13:24:25.303686Z","shell.execute_reply":"2025-05-31T13:24:25.984231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:31:02.980412Z","iopub.execute_input":"2025-05-31T13:31:02.980734Z","iopub.status.idle":"2025-05-31T13:31:02.985473Z","shell.execute_reply.started":"2025-05-31T13:31:02.980708Z","shell.execute_reply":"2025-05-31T13:31:02.984321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/benin-national-ai-olympiad-selection/train.csv')\ntest = pd.read_csv('/kaggle/input/benin-national-ai-olympiad-selection/test.csv')\nsubmission = pd.read_csv('/kaggle/input/benin-national-ai-olympiad-selection/sample_submission.csv')\ntrain.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:37:33.922873Z","iopub.execute_input":"2025-05-31T13:37:33.923275Z","iopub.status.idle":"2025-05-31T13:38:33.770528Z","shell.execute_reply.started":"2025-05-31T13:37:33.923245Z","shell.execute_reply":"2025-05-31T13:38:33.769565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.target.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:38:33.772302Z","iopub.execute_input":"2025-05-31T13:38:33.772658Z","iopub.status.idle":"2025-05-31T13:38:33.786986Z","shell.execute_reply.started":"2025-05-31T13:38:33.772628Z","shell.execute_reply":"2025-05-31T13:38:33.785950Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# dimensions\nprint('Train Set:', train.shape)\nprint('Test Set :', test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:38:33.788001Z","iopub.execute_input":"2025-05-31T13:38:33.788282Z","iopub.status.idle":"2025-05-31T13:38:33.810369Z","shell.execute_reply.started":"2025-05-31T13:38:33.788259Z","shell.execute_reply":"2025-05-31T13:38:33.809359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info(verbose=True, show_counts=True) # le type de chaque colonne","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:39:30.745375Z","iopub.execute_input":"2025-05-31T13:39:30.746092Z","iopub.status.idle":"2025-05-31T13:39:31.685068Z","shell.execute_reply.started":"2025-05-31T13:39:30.746058Z","shell.execute_reply":"2025-05-31T13:39:31.684233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# La liste des features\nfeatures = train.columns.tolist()\nprint(features)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:53:28.755991Z","iopub.execute_input":"2025-05-31T13:53:28.756973Z","iopub.status.idle":"2025-05-31T13:53:28.762862Z","shell.execute_reply.started":"2025-05-31T13:53:28.756928Z","shell.execute_reply":"2025-05-31T13:53:28.761928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# On enleve ID et target de la liste\nfeatures.remove('id')\nfeatures.remove('target')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:53:37.324541Z","iopub.execute_input":"2025-05-31T13:53:37.324895Z","iopub.status.idle":"2025-05-31T13:53:37.329313Z","shell.execute_reply.started":"2025-05-31T13:53:37.324869Z","shell.execute_reply":"2025-05-31T13:53:37.328371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(features)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:53:42.899758Z","iopub.execute_input":"2025-05-31T13:53:42.900094Z","iopub.status.idle":"2025-05-31T13:53:42.905311Z","shell.execute_reply.started":"2025-05-31T13:53:42.900070Z","shell.execute_reply":"2025-05-31T13:53:42.904496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# decrire les features\ntrain[features].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:53:47.742445Z","iopub.execute_input":"2025-05-31T13:53:47.742772Z","iopub.status.idle":"2025-05-31T13:54:01.527065Z","shell.execute_reply.started":"2025-05-31T13:53:47.742746Z","shell.execute_reply":"2025-05-31T13:54:01.526100Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(train[features], train['target'], test_size=0.3, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:54:50.134415Z","iopub.execute_input":"2025-05-31T13:54:50.134736Z","iopub.status.idle":"2025-05-31T13:54:56.043176Z","shell.execute_reply.started":"2025-05-31T13:54:50.134712Z","shell.execute_reply":"2025-05-31T13:54:56.042219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import roc_auc_score, roc_curve, auc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:59:19.224874Z","iopub.execute_input":"2025-05-31T13:59:19.225257Z","iopub.status.idle":"2025-05-31T13:59:19.229692Z","shell.execute_reply.started":"2025-05-31T13:59:19.225227Z","shell.execute_reply":"2025-05-31T13:59:19.228681Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = LogisticRegression(random_state=42)\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:58:06.664083Z","iopub.execute_input":"2025-05-31T13:58:06.665113Z","iopub.status.idle":"2025-05-31T13:58:34.886201Z","shell.execute_reply.started":"2025-05-31T13:58:06.665077Z","shell.execute_reply":"2025-05-31T13:58:34.885245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_proba = model.predict_proba(X_test)\ny_pred_proba_positive_class = y_pred_proba[:, 1] # Probabilities for class 1\nauc_score = roc_auc_score(y_test, y_pred_proba_positive_class)\nprint(f\"\\nAUC Score: {auc_score:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T13:59:29.078227Z","iopub.execute_input":"2025-05-31T13:59:29.078542Z","iopub.status.idle":"2025-05-31T13:59:29.592341Z","shell.execute_reply.started":"2025-05-31T13:59:29.078519Z","shell.execute_reply":"2025-05-31T13:59:29.591405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# --- 6. Calculate ROC Curve points ---\n# The roc_curve function returns false positive rates (fpr), true positive rates (tpr), and thresholds.\nfpr, tpr, thresholds = roc_curve(y_test, y_pred_proba_positive_class)\n\n# Calculate the Area Under the Curve (AUC) using the fpr and tpr.\n# This is an alternative way to get the AUC, same as roc_auc_score result.\nroc_auc_from_curve = auc(fpr, tpr)\nprint(f\"AUC calculated from ROC curve points: {roc_auc_from_curve:.4f}\")\n\n\n# --- 7. Plot ROC Curve (Optional) ---\nplt.figure(figsize=(8, 6))\nplt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {auc_score:.2f})')\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--', label='Chance level (AUC = 0.5)') # Diagonal line for random classifier\nplt.xlim([0.0, 1.0])\nplt.ylim([0.0, 1.05])\nplt.xlabel('False Positive Rate (FPR)')\nplt.ylabel('True Positive Rate (TPR)')\nplt.title('Receiver Operating Characteristic (ROC) Curve')\nplt.legend(loc=\"lower right\")\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T14:00:07.259710Z","iopub.execute_input":"2025-05-31T14:00:07.260061Z","iopub.status.idle":"2025-05-31T14:00:07.690023Z","shell.execute_reply.started":"2025-05-31T14:00:07.260028Z","shell.execute_reply":"2025-05-31T14:00:07.688990Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_proba = model.predict_proba(test[features])\ntest_proba_positive_class = test_proba[:, 1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T14:01:21.214923Z","iopub.execute_input":"2025-05-31T14:01:21.215810Z","iopub.status.idle":"2025-05-31T14:01:22.102131Z","shell.execute_reply.started":"2025-05-31T14:01:21.215776Z","shell.execute_reply":"2025-05-31T14:01:22.101225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T14:01:52.259144Z","iopub.execute_input":"2025-05-31T14:01:52.260769Z","iopub.status.idle":"2025-05-31T14:01:52.275356Z","shell.execute_reply.started":"2025-05-31T14:01:52.260725Z","shell.execute_reply":"2025-05-31T14:01:52.274382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission['target'] = test_proba_positive_class","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T14:02:15.347186Z","iopub.execute_input":"2025-05-31T14:02:15.347599Z","iopub.status.idle":"2025-05-31T14:02:15.354073Z","shell.execute_reply.started":"2025-05-31T14:02:15.347570Z","shell.execute_reply":"2025-05-31T14:02:15.353094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-31T14:04:32.486099Z","iopub.execute_input":"2025-05-31T14:04:32.486960Z","iopub.status.idle":"2025-05-31T14:04:33.791505Z","shell.execute_reply.started":"2025-05-31T14:04:32.486927Z","shell.execute_reply":"2025-05-31T14:04:33.790719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}