{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Bibliothèques","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold, train_test_split\n\nimport lightgbm as lgb\n\nimport time\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:20:35.249303Z","iopub.execute_input":"2023-01-08T11:20:35.249890Z","iopub.status.idle":"2023-01-08T11:20:35.257392Z","shell.execute_reply.started":"2023-01-08T11:20:35.249848Z","shell.execute_reply":"2023-01-08T11:20:35.256058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Charger des ensembles de données","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/emsi-5iir-ds/train.csv\")\ntest = pd.read_csv(\"../input/emsi-5iir-ds/test.csv\")\nsubmission = pd.read_csv(\"../input/emsi-5iir-ds/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:21:54.903577Z","iopub.execute_input":"2023-01-08T11:21:54.904079Z","iopub.status.idle":"2023-01-08T11:22:12.292371Z","shell.execute_reply.started":"2023-01-08T11:21:54.904042Z","shell.execute_reply":"2023-01-08T11:22:12.291131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploration des données","metadata":{}},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:22:50.478912Z","iopub.execute_input":"2023-01-08T11:22:50.479337Z","iopub.status.idle":"2023-01-08T11:22:50.517762Z","shell.execute_reply.started":"2023-01-08T11:22:50.479304Z","shell.execute_reply":"2023-01-08T11:22:50.516665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:23:16.210125Z","iopub.execute_input":"2023-01-08T11:23:16.210729Z","iopub.status.idle":"2023-01-08T11:23:16.262460Z","shell.execute_reply.started":"2023-01-08T11:23:16.210684Z","shell.execute_reply":"2023-01-08T11:23:16.261297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Train data set: {train.shape[0]} rows and {train.shape[1]} columns')\nprint(f'Test data set: {test.shape[0]} rows and {test.shape[1]} columns') ","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:23:41.307616Z","iopub.execute_input":"2023-01-08T11:23:41.308113Z","iopub.status.idle":"2023-01-08T11:23:41.314997Z","shell.execute_reply.started":"2023-01-08T11:23:41.308076Z","shell.execute_reply":"2023-01-08T11:23:41.314031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Missing values in the train dataset: {train.isna().sum().sum()}')\nprint(f'Missing values in the test dataset: {test.isna().sum().sum()}')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:23:55.391986Z","iopub.execute_input":"2023-01-08T11:23:55.392433Z","iopub.status.idle":"2023-01-08T11:23:55.554447Z","shell.execute_reply.started":"2023-01-08T11:23:55.392397Z","shell.execute_reply":"2023-01-08T11:23:55.552837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.nunique().sort_values(ascending = True)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:24:10.233269Z","iopub.execute_input":"2023-01-08T11:24:10.233778Z","iopub.status.idle":"2023-01-08T11:24:11.655503Z","shell.execute_reply.started":"2023-01-08T11:24:10.233741Z","shell.execute_reply":"2023-01-08T11:24:11.653771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:24:24.151528Z","iopub.execute_input":"2023-01-08T11:24:24.152296Z","iopub.status.idle":"2023-01-08T11:24:24.274958Z","shell.execute_reply.started":"2023-01-08T11:24:24.152236Z","shell.execute_reply":"2023-01-08T11:24:24.273223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe().T","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:24:39.285546Z","iopub.execute_input":"2023-01-08T11:24:39.285950Z","iopub.status.idle":"2023-01-08T11:24:40.509029Z","shell.execute_reply.started":"2023-01-08T11:24:39.285919Z","shell.execute_reply":"2023-01-08T11:24:40.507934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"Training features:","metadata":{}},{"cell_type":"code","source":"TARGET = 'target'\nFeatures = [col for col in train.columns if col != TARGET]\nprint('Training data colummn names:', Features)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:26:05.151931Z","iopub.execute_input":"2023-01-08T11:26:05.152703Z","iopub.status.idle":"2023-01-08T11:26:05.161820Z","shell.execute_reply.started":"2023-01-08T11:26:05.152608Z","shell.execute_reply":"2023-01-08T11:26:05.160140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Encoding Categorical Features","metadata":{}},{"cell_type":"code","source":"label_cols = [\"f_27\"]\ndef label_encoder(train,test,columns):\n    for col in columns:\n        train[col] = LabelEncoder().fit_transform(train[col])\n        test[col] =  LabelEncoder().fit_transform(test[col])\n    return train, test\n\ntrain ,test = label_encoder(train,test ,label_cols)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:27:11.163256Z","iopub.execute_input":"2023-01-08T11:27:11.163703Z","iopub.status.idle":"2023-01-08T11:27:17.443289Z","shell.execute_reply.started":"2023-01-08T11:27:11.163671Z","shell.execute_reply":"2023-01-08T11:27:17.441977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"Feats_ignore = ['target', 'id', 'f_27']\nFeatures = [col for col in train.columns if col not in Feats_ignore]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:27:52.822189Z","iopub.execute_input":"2023-01-08T11:27:52.822621Z","iopub.status.idle":"2023-01-08T11:27:52.828330Z","shell.execute_reply.started":"2023-01-08T11:27:52.822587Z","shell.execute_reply":"2023-01-08T11:27:52.827064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[Features]\ny = train[TARGET]\ntest = test.drop(columns=[\"id\",\"f_27\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:28:09.755278Z","iopub.execute_input":"2023-01-08T11:28:09.755695Z","iopub.status.idle":"2023-01-08T11:28:10.032277Z","shell.execute_reply.started":"2023-01-08T11:28:09.755660Z","shell.execute_reply":"2023-01-08T11:28:10.030389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Créer un classificateur LGBM","metadata":{}},{"cell_type":"code","source":"SEED = 2022","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:28:33.016515Z","iopub.execute_input":"2023-01-08T11:28:33.016964Z","iopub.status.idle":"2023-01-08T11:28:33.021893Z","shell.execute_reply.started":"2023-01-08T11:28:33.016929Z","shell.execute_reply":"2023-01-08T11:28:33.020883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_eval, y_train, y_eval = train_test_split(X, \n                                                    y, \n                                                    test_size = 0.20, \n                                                    random_state = SEED)\nprint(\"Train/Eval Sizes : \", X_train.shape, X_eval.shape, y_train.shape, y_eval.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:28:44.207467Z","iopub.execute_input":"2023-01-08T11:28:44.208217Z","iopub.status.idle":"2023-01-08T11:28:44.717852Z","shell.execute_reply.started":"2023-01-08T11:28:44.208180Z","shell.execute_reply":"2023-01-08T11:28:44.716657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit_params = {\"early_stopping_rounds\":100, \n            \"eval_metric\" : 'auc', \n            \"eval_set\" : [(X_eval,y_eval)],\n            'verbose': 1000,\n           }","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:28:54.687929Z","iopub.execute_input":"2023-01-08T11:28:54.688331Z","iopub.status.idle":"2023-01-08T11:28:54.694977Z","shell.execute_reply.started":"2023-01-08T11:28:54.688300Z","shell.execute_reply":"2023-01-08T11:28:54.693354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_model = lgb.LGBMClassifier (\n                                n_estimators = 5000,\n                                max_depth = 11,\n                                num_leaves = 15,\n                                learning_rate = 0.05,\n                                subsample = 0.9,\n                                colsample_bytree = 0.7,\n                                random_state = SEED )","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:29:10.951073Z","iopub.execute_input":"2023-01-08T11:29:10.951554Z","iopub.status.idle":"2023-01-08T11:29:10.959086Z","shell.execute_reply.started":"2023-01-08T11:29:10.951518Z","shell.execute_reply":"2023-01-08T11:29:10.957449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_model.fit(X, y, **fit_params)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:29:27.370606Z","iopub.execute_input":"2023-01-08T11:29:27.371038Z","iopub.status.idle":"2023-01-08T11:35:39.908533Z","shell.execute_reply.started":"2023-01-08T11:29:27.370981Z","shell.execute_reply":"2023-01-08T11:35:39.907343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prédire les résultats","metadata":{}},{"cell_type":"code","source":"lgb_predictions = lgb_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T11:37:04.512644Z","iopub.execute_input":"2023-01-08T11:37:04.513076Z","iopub.status.idle":"2023-01-08T11:38:16.889686Z","shell.execute_reply.started":"2023-01-08T11:37:04.513042Z","shell.execute_reply":"2023-01-08T11:38:16.888256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Soumettre à Kaggle","metadata":{}},{"cell_type":"code","source":"submission[TARGET] = lgb_predictions\nsubmission.to_csv(\"submission.csv\",index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T12:03:01.319687Z","iopub.execute_input":"2023-01-08T12:03:01.320583Z","iopub.status.idle":"2023-01-08T12:03:02.084321Z","shell.execute_reply.started":"2023-01-08T12:03:01.320543Z","shell.execute_reply":"2023-01-08T12:03:02.083113Z"},"trusted":true},"execution_count":null,"outputs":[]}]}