{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, AdaBoostClassifier, AdaBoostRegressor, GradientBoostingClassifier, GradientBoostingRegressor, HistGradientBoostingClassifier, HistGradientBoostingRegressor \nfrom sklearn.svm import SVC, SVR\nfrom sklearn.metrics import accuracy_score, roc_auc_score, mean_absolute_percentage_error, mean_squared_error\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\n#import sklearn\n#sklearn.set_config(transform_output=\"pandas\")\n\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nimport tensorflow.keras.layers as L\nimport tensorflow.keras.models as M\nimport tensorflow.keras.callbacks as C\n\nimport lightgbm\nfrom lightgbm.sklearn import LGBMRegressor, LGBMClassifier\nfrom catboost import CatBoostRegressor, CatBoostClassifier, Pool\nimport xgboost as xgb\n\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-04-17T14:40:38.966023Z","iopub.status.idle":"2025-04-17T14:40:38.966218Z","shell.execute_reply.started":"2025-04-17T14:40:38.966124Z","shell.execute_reply":"2025-04-17T14:40:38.966132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nsub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.966825Z","iopub.status.idle":"2025-04-17T14:40:38.967012Z","shell.execute_reply.started":"2025-04-17T14:40:38.966921Z","shell.execute_reply":"2025-04-17T14:40:38.966929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.968044Z","iopub.status.idle":"2025-04-17T14:40:38.968244Z","shell.execute_reply.started":"2025-04-17T14:40:38.968151Z","shell.execute_reply":"2025-04-17T14:40:38.968159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"LABEL = \"label\"\nFEATURES = [c for c in df_train.columns if c != LABEL]\nX_Sub = df_test[FEATURES]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.969336Z","iopub.status.idle":"2025-04-17T14:40:38.969567Z","shell.execute_reply.started":"2025-04-17T14:40:38.969459Z","shell.execute_reply":"2025-04-17T14:40:38.969469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df_train[FEATURES]\ny = df_train[LABEL]\n\n\nX.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.970732Z","iopub.status.idle":"2025-04-17T14:40:38.971028Z","shell.execute_reply.started":"2025-04-17T14:40:38.970878Z","shell.execute_reply":"2025-04-17T14:40:38.970891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Je sépare mon dataframe pour avoir 25% de test et  75% d'entraînement\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.972132Z","iopub.status.idle":"2025-04-17T14:40:38.972446Z","shell.execute_reply.started":"2025-04-17T14:40:38.972281Z","shell.execute_reply":"2025-04-17T14:40:38.972293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Je prépare une liste de tout les modèles possibles afin de les tester par la suite\n# En ayant fait tout tourner le RandomClassifier avait une bonne accuracy, apres avoir tester sans \nfrom sklearn.ensemble import RandomForestClassifier\n\n\nmodels = {\n    #\"RandomForestClassifier\" : RandomForestClassifier()\n    #\"AdaBoost\": AdaBoostClassifier( ),\n    #\"GradientBoosting\": GradientBoostingClassifier(),\n    #\"HistGradientBoosting\": HistGradientBoostingClassifier(),\n    \"SVC\": SVC(probability=True),\n    #\"LightGBM\": LGBMClassifier(),\n    #\"CatBoost\": CatBoostClassifier(verbose=0),\n    #\"XGBoost\": xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:44:33.911443Z","iopub.execute_input":"2025-04-17T14:44:33.912084Z","iopub.status.idle":"2025-04-17T14:44:33.915981Z","shell.execute_reply.started":"2025-04-17T14:44:33.912063Z","shell.execute_reply":"2025-04-17T14:44:33.915258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = []\n\nfor name, model in models.items():\n    print(f\"Training: {name}\")\n    \n    model.fit(X_train, y_train)\n    y_pred = model.predict(X_test)\n\n    acc = accuracy_score(y_test, y_pred)\n    precision = precision_score(y_test, y_pred, average='macro')\n    recall = recall_score(y_test, y_pred, average='macro')\n    f1 = f1_score(y_test, y_pred, average='macro')\n\n    results.append({\n        \"Model\": name,\n        \"Accuracy\": acc,\n        \"Precision\": precision,\n        \"Recall\": recall,\n        \"F1 Score\": f1\n    })\n    \n    sub[\"Label\"] = model.predict(X_Sub)\n    sub.to_csv(\"submission.csv\", index=False)\n    \nresults_df = pd.DataFrame(results).sort_values(by=\"F1 Score\", ascending=False)\nprint(results_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.974840Z","iopub.status.idle":"2025-04-17T14:40:38.975064Z","shell.execute_reply.started":"2025-04-17T14:40:38.974953Z","shell.execute_reply":"2025-04-17T14:40:38.974964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nresults_df_plot = results_df.set_index(\"Model\")\n\nplt.figure(figsize=(12, 6))\nplt.style.use(\"seaborn-v0_8-muted\")\n\nresults_df_plot.plot(kind='bar', figsize=(12, 6))\nplt.title(\"Comparaison des modèles - métriques de performance\")\nplt.ylabel(\"Score\")\nplt.ylim(0, 1.05)\nplt.grid(True, axis='y', linestyle='--', alpha=0.7)\nplt.xticks(rotation=45)\nplt.legend(loc='lower right')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-17T14:40:38.975710Z","iopub.status.idle":"2025-04-17T14:40:38.975898Z","shell.execute_reply.started":"2025-04-17T14:40:38.975807Z","shell.execute_reply":"2025-04-17T14:40:38.975815Z"}},"outputs":[],"execution_count":null}]}