{"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":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.235837Z","iopub.execute_input":"2022-07-11T11:07:18.237431Z","iopub.status.idle":"2022-07-11T11:07:18.245735Z","shell.execute_reply.started":"2022-07-11T11:07:18.237375Z","shell.execute_reply":"2022-07-11T11:07:18.244255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.248677Z","iopub.execute_input":"2022-07-11T11:07:18.249094Z","iopub.status.idle":"2022-07-11T11:07:18.263668Z","shell.execute_reply.started":"2022-07-11T11:07:18.249059Z","shell.execute_reply":"2022-07-11T11:07:18.262610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')\ntest = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.265707Z","iopub.execute_input":"2022-07-11T11:07:18.266719Z","iopub.status.idle":"2022-07-11T11:07:18.332128Z","shell.execute_reply.started":"2022-07-11T11:07:18.266665Z","shell.execute_reply":"2022-07-11T11:07:18.330511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.336639Z","iopub.execute_input":"2022-07-11T11:07:18.337325Z","iopub.status.idle":"2022-07-11T11:07:18.365947Z","shell.execute_reply.started":"2022-07-11T11:07:18.337279Z","shell.execute_reply":"2022-07-11T11:07:18.364995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.369468Z","iopub.execute_input":"2022-07-11T11:07:18.370766Z","iopub.status.idle":"2022-07-11T11:07:18.394779Z","shell.execute_reply.started":"2022-07-11T11:07:18.370700Z","shell.execute_reply":"2022-07-11T11:07:18.393401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.397122Z","iopub.execute_input":"2022-07-11T11:07:18.397540Z","iopub.status.idle":"2022-07-11T11:07:18.418877Z","shell.execute_reply.started":"2022-07-11T11:07:18.397507Z","shell.execute_reply":"2022-07-11T11:07:18.416706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.420809Z","iopub.execute_input":"2022-07-11T11:07:18.421368Z","iopub.status.idle":"2022-07-11T11:07:18.449881Z","shell.execute_reply.started":"2022-07-11T11:07:18.421314Z","shell.execute_reply":"2022-07-11T11:07:18.448668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.451570Z","iopub.execute_input":"2022-07-11T11:07:18.451969Z","iopub.status.idle":"2022-07-11T11:07:18.475954Z","shell.execute_reply.started":"2022-07-11T11:07:18.451930Z","shell.execute_reply":"2022-07-11T11:07:18.474128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['CryoSleep'] = train['CryoSleep'].replace({True: 1, False: 0})\ntrain['VIP'] = train['VIP'].replace({True: 1, False: 0})\ntrain['Transported'] = train['Transported'].replace({True: 1, False: 0})","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:18.477475Z","iopub.execute_input":"2022-07-11T11:07:18.477866Z","iopub.status.idle":"2022-07-11T11:07:18.512692Z","shell.execute_reply.started":"2022-07-11T11:07:18.477830Z","shell.execute_reply":"2022-07-11T11:07:18.511081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Cabin_class'] = \"\"\ntrain['Cabin_number']= \"\"\ntrain['Cabin_extension'] = \"\"\nfor i in range(0,len(train['Cabin'])):\n    if not pd.isna(train['Cabin'][i]):\n        train['Cabin_class'][i] = train['Cabin'][i].split('/')[0]\n        train['Cabin_number'][i] = train['Cabin'][i].split('/')[1]\n        train['Cabin_extension'][i] = train['Cabin'][i].split('/')[2]","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-11T11:07:18.514500Z","iopub.execute_input":"2022-07-11T11:07:18.515055Z","iopub.status.idle":"2022-07-11T11:07:27.367786Z","shell.execute_reply.started":"2022-07-11T11:07:18.515003Z","shell.execute_reply":"2022-07-11T11:07:27.366544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.369450Z","iopub.execute_input":"2022-07-11T11:07:27.369824Z","iopub.status.idle":"2022-07-11T11:07:27.399532Z","shell.execute_reply.started":"2022-07-11T11:07:27.369789Z","shell.execute_reply":"2022-07-11T11:07:27.397259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop('Name', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.401641Z","iopub.execute_input":"2022-07-11T11:07:27.402483Z","iopub.status.idle":"2022-07-11T11:07:27.412721Z","shell.execute_reply.started":"2022-07-11T11:07:27.402430Z","shell.execute_reply":"2022-07-11T11:07:27.411443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['HomePlanet'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.414691Z","iopub.execute_input":"2022-07-11T11:07:27.415499Z","iopub.status.idle":"2022-07-11T11:07:27.425759Z","shell.execute_reply.started":"2022-07-11T11:07:27.415443Z","shell.execute_reply":"2022-07-11T11:07:27.424636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Destination'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.430401Z","iopub.execute_input":"2022-07-11T11:07:27.430781Z","iopub.status.idle":"2022-07-11T11:07:27.442130Z","shell.execute_reply.started":"2022-07-11T11:07:27.430748Z","shell.execute_reply":"2022-07-11T11:07:27.440853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['HomePlanet'] = train['HomePlanet'].replace({'Europa': 0, 'Earth': 1,'Mars': 2})\ntrain['Destination'] = train['Destination'].replace({'TRAPPIST-1e': 0, 'PSO J318.5-22': 1,'55 Cancri e':2})","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.444064Z","iopub.execute_input":"2022-07-11T11:07:27.444875Z","iopub.status.idle":"2022-07-11T11:07:27.470088Z","shell.execute_reply.started":"2022-07-11T11:07:27.444819Z","shell.execute_reply":"2022-07-11T11:07:27.469191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop('Cabin', axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.471692Z","iopub.execute_input":"2022-07-11T11:07:27.472320Z","iopub.status.idle":"2022-07-11T11:07:27.479931Z","shell.execute_reply.started":"2022-07-11T11:07:27.472283Z","shell.execute_reply":"2022-07-11T11:07:27.478971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.481925Z","iopub.execute_input":"2022-07-11T11:07:27.482695Z","iopub.status.idle":"2022-07-11T11:07:27.509304Z","shell.execute_reply.started":"2022-07-11T11:07:27.482657Z","shell.execute_reply":"2022-07-11T11:07:27.508195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\ndef encoding(column):\n    df_1 = train.copy()\n\n    original = df_1\n    mask = df_1[column].isnull()\n\n    df_1 = df_1.astype(str).apply(LabelEncoder().fit_transform)\n    train[column] = df_1.where(~mask, original)[column]\nencoding('Cabin_class')\nencoding('Cabin_extension')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.510886Z","iopub.execute_input":"2022-07-11T11:07:27.511564Z","iopub.status.idle":"2022-07-11T11:07:27.875623Z","shell.execute_reply.started":"2022-07-11T11:07:27.511524Z","shell.execute_reply":"2022-07-11T11:07:27.874218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.877402Z","iopub.execute_input":"2022-07-11T11:07:27.877791Z","iopub.status.idle":"2022-07-11T11:07:27.904739Z","shell.execute_reply.started":"2022-07-11T11:07:27.877754Z","shell.execute_reply":"2022-07-11T11:07:27.903371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Cabin_number'] = pd.to_numeric(train['Cabin_number'])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.906584Z","iopub.execute_input":"2022-07-11T11:07:27.907631Z","iopub.status.idle":"2022-07-11T11:07:27.921717Z","shell.execute_reply.started":"2022-07-11T11:07:27.907587Z","shell.execute_reply":"2022-07-11T11:07:27.920658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.linear_model import LinearRegression\n# def train_on_nan(column):\n#     regressor = LinearRegression()\n#     x_train = train[train[column].isna() == False].drop(['PassengerId',column,'Transported'], axis=1).values\n#     y_train = train[column].dropna().values\n#     regressor.fit(x_train.reshape(-1, 1), y_train)\n#     for i in range(0, len(train[column])):\n#         if pd.isna(train[column][i]):\n#             train[column][i] = regressor.predict([[train.iloc[[i]].values]])\n#         else:\n#             pass\ncolumns = ['HomePlanet','CryoSleep','Destination','Age','VIP','RoomService','FoodCourt','ShoppingMall','Spa','VRDeck','Cabin_class','Cabin_number','Cabin_extension']\nfor col in columns:\n    train[col] = train[col].fillna(value=train[col].mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:27.926125Z","iopub.execute_input":"2022-07-11T11:07:27.926543Z","iopub.status.idle":"2022-07-11T11:07:27.943694Z","shell.execute_reply.started":"2022-07-11T11:07:27.926508Z","shell.execute_reply":"2022-07-11T11:07:27.942518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['CryoSleep'] = test['CryoSleep'].replace({True: 1, False: 0})\ntest['VIP'] = test['VIP'].replace({True: 1, False: 0})\n\ntest['Cabin_class'] = \"\"\ntest['Cabin_number']= \"\"\ntest['Cabin_extension'] = \"\"\nfor i in range(0,len(test['Cabin'])):\n    if not pd.isna(test['Cabin'][i]):\n        test['Cabin_class'][i] = test['Cabin'][i].split('/')[0]\n        test['Cabin_number'][i] = test['Cabin'][i].split('/')[1]\n        test['Cabin_extension'][i] = test['Cabin'][i].split('/')[2]\n\ntest = test.drop('Name', axis=1)\ntest = test.drop('Cabin', axis=1)\n\ntest['HomePlanet'] = test['HomePlanet'].replace({'Europa': 0, 'Earth': 1,'Mars': 2})\ntest['Destination'] = test['Destination'].replace({'TRAPPIST-1e': 0, 'PSO J318.5-22': 1,'55 Cancri e':2})\n\nfrom sklearn.preprocessing import LabelEncoder\ndef encoding(column):\n    df_1 = test.copy()\n\n    original = df_1\n    mask = df_1[column].isnull()\n\n    df_1 = df_1.astype(str).apply(LabelEncoder().fit_transform)\n    test[column] = df_1.where(~mask, original)[column]\nencoding('Cabin_class')\nencoding('Cabin_extension')\n\ntest['Cabin_number'] = pd.to_numeric(test['Cabin_number'])\n\ncolumns = ['HomePlanet','CryoSleep','Destination','Age','VIP','RoomService','FoodCourt','ShoppingMall','Spa','VRDeck','Cabin_class','Cabin_number','Cabin_extension']\nfor col in columns:\n    test[col] = test[col].fillna(value=test[col].mean())","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-11T11:07:27.946171Z","iopub.execute_input":"2022-07-11T11:07:27.947152Z","iopub.status.idle":"2022-07-11T11:07:32.338009Z","shell.execute_reply.started":"2022-07-11T11:07:27.947101Z","shell.execute_reply":"2022-07-11T11:07:32.336747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop(['Transported','PassengerId'], axis=1).values\ny_train = train['Transported'].values","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:32.340441Z","iopub.execute_input":"2022-07-11T11:07:32.341061Z","iopub.status.idle":"2022-07-11T11:07:32.350340Z","shell.execute_reply.started":"2022-07-11T11:07:32.341012Z","shell.execute_reply":"2022-07-11T11:07:32.349229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test.drop(['PassengerId'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:32.352209Z","iopub.execute_input":"2022-07-11T11:07:32.352574Z","iopub.status.idle":"2022-07-11T11:07:32.361173Z","shell.execute_reply.started":"2022-07-11T11:07:32.352540Z","shell.execute_reply":"2022-07-11T11:07:32.360162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.DataFrame()\npred['PassengerId'] = test['PassengerId']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:32.362838Z","iopub.execute_input":"2022-07-11T11:07:32.363820Z","iopub.status.idle":"2022-07-11T11:07:32.374509Z","shell.execute_reply.started":"2022-07-11T11:07:32.363706Z","shell.execute_reply":"2022-07-11T11:07:32.373376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from hyperopt import STATUS_OK, Trials, fmin, hp, tpe\nspace={'max_depth': hp.quniform(\"max_depth\", 3, 18, 1),\n        'gamma': hp.uniform ('gamma', 1,9),\n        'reg_alpha' : hp.quniform('reg_alpha', 40,180,1),\n        'reg_lambda' : hp.uniform('reg_lambda', 0,1),\n        'colsample_bytree' : hp.uniform('colsample_bytree', 0.5,1),\n        'min_child_weight' : hp.quniform('min_child_weight', 0, 10, 1),\n        'n_estimators': 180,\n        'seed': 0\n    }","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:32.376025Z","iopub.execute_input":"2022-07-11T11:07:32.376596Z","iopub.status.idle":"2022-07-11T11:07:32.384484Z","shell.execute_reply.started":"2022-07-11T11:07:32.376563Z","shell.execute_reply":"2022-07-11T11:07:32.383290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(model):\n    model.fit(X_train,y_train)\n    print('nom du modèle : ',model)\n    pred['Transported'] = model.predict(X_test)\n    pred['Transported'] = pred['Transported'].replace({1: True, 0: False})\n    #pred.to_csv('Subbmission_7.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:32.385815Z","iopub.execute_input":"2022-07-11T11:07:32.386447Z","iopub.status.idle":"2022-07-11T11:07:32.400695Z","shell.execute_reply.started":"2022-07-11T11:07:32.386407Z","shell.execute_reply":"2022-07-11T11:07:32.399796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom xgboost import XGBClassifier\nfrom lightgbm import LGBMClassifier\n\nm1=LogisticRegression()\nm2=SVC()\nm3=DecisionTreeClassifier(max_depth=6)\nm4=RandomForestClassifier(max_samples=0.9)\nm5=KNeighborsClassifier(n_neighbors=5)\nm6=XGBClassifier(\n                    n_estimators =space['n_estimators'], max_depth = space['max_depth'], gamma = space['gamma'],\n                    reg_alpha = space['reg_alpha'],min_child_weight=space['min_child_weight'],\n                    colsample_bytree=space['colsample_bytree'])\nm7=LGBMClassifier(max_depth=6, random_state=314, silent=True, metric='None', n_jobs=6)\n\nmodels=[m7]\n\nfor model in models:\n    evaluate(model)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:07:32.402243Z","iopub.execute_input":"2022-07-11T11:07:32.403210Z","iopub.status.idle":"2022-07-11T11:07:33.310936Z","shell.execute_reply.started":"2022-07-11T11:07:32.403159Z","shell.execute_reply":"2022-07-11T11:07:33.309763Z"},"trusted":true},"execution_count":null,"outputs":[]}]}