{"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\nimport pandas as pd\nimport numpy as np\n\nglobal df, train_ID\n\ntrain = pd.read_csv('../input/spaceship-titanic/train.csv')\n\ntrain.info(),\n\ntrain_ID = train['PassengerId']\n\ndf = train\n    \nprint(df.head())\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.142438,"end_time":"2022-07-01T11:33:08.835007","exception":false,"start_time":"2022-07-01T11:33:08.692569","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.307769Z","iopub.execute_input":"2022-07-13T21:51:04.308219Z","iopub.status.idle":"2022-07-13T21:51:04.374510Z","shell.execute_reply.started":"2022-07-13T21:51:04.308186Z","shell.execute_reply":"2022-07-13T21:51:04.373274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\" champ manquant : \")\nprint(df.isnull().sum())","metadata":{"papermill":{"duration":0.043648,"end_time":"2022-07-01T11:33:08.906551","exception":false,"start_time":"2022-07-01T11:33:08.862903","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.376455Z","iopub.execute_input":"2022-07-13T21:51:04.376839Z","iopub.status.idle":"2022-07-13T21:51:04.393184Z","shell.execute_reply.started":"2022-07-13T21:51:04.376808Z","shell.execute_reply":"2022-07-13T21:51:04.392193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\nimputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')\nimputer.fit_transform(df[['HomePlanet','CryoSleep','Cabin','Destination','VIP','Name']])\n","metadata":{"papermill":{"duration":1.380658,"end_time":"2022-07-01T11:33:10.315056","exception":false,"start_time":"2022-07-01T11:33:08.934398","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.394820Z","iopub.execute_input":"2022-07-13T21:51:04.395399Z","iopub.status.idle":"2022-07-13T21:51:04.424926Z","shell.execute_reply.started":"2022-07-13T21:51:04.395365Z","shell.execute_reply":"2022-07-13T21:51:04.423460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Nettoyage des données numériques\ndf['RoomService'] = df['RoomService'].fillna(df['RoomService'].median())\ndf['Age'] = df['Age'].fillna(df['Age'].median())\ndf['FoodCourt'] = df['FoodCourt'].fillna(df['FoodCourt'].median())\ndf['ShoppingMall'] = df['ShoppingMall'].fillna(df['ShoppingMall'].median())\ndf['Spa'] = df['Spa'].fillna(df['Spa'].median())\ndf['VRDeck'] = df['VRDeck'].fillna(df['VRDeck'].median())\nprint(df.head())","metadata":{"papermill":{"duration":0.052609,"end_time":"2022-07-01T11:33:10.397008","exception":false,"start_time":"2022-07-01T11:33:10.344399","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.427863Z","iopub.execute_input":"2022-07-13T21:51:04.428209Z","iopub.status.idle":"2022-07-13T21:51:04.454026Z","shell.execute_reply.started":"2022-07-13T21:51:04.428181Z","shell.execute_reply":"2022-07-13T21:51:04.452609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df['HomePlanet'].value_counts())\nprint(df['CryoSleep'].value_counts())","metadata":{"papermill":{"duration":0.041752,"end_time":"2022-07-01T11:33:10.466795","exception":false,"start_time":"2022-07-01T11:33:10.425043","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.455469Z","iopub.execute_input":"2022-07-13T21:51:04.455891Z","iopub.status.idle":"2022-07-13T21:51:04.467347Z","shell.execute_reply.started":"2022-07-13T21:51:04.455861Z","shell.execute_reply":"2022-07-13T21:51:04.466241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Describe_data = df.describe()\nprint(Describe_data)","metadata":{"papermill":{"duration":0.056132,"end_time":"2022-07-01T11:33:10.551232","exception":false,"start_time":"2022-07-01T11:33:10.495100","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.468416Z","iopub.execute_input":"2022-07-13T21:51:04.468862Z","iopub.status.idle":"2022-07-13T21:51:04.506323Z","shell.execute_reply.started":"2022-07-13T21:51:04.468831Z","shell.execute_reply":"2022-07-13T21:51:04.504579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nstandardScalerData = ['Age','RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']\n\ndfStandardScalerData = df[standardScalerData]\n\nfor i in standardScalerData:\n    df[i] = df[i].fillna(0).astype(int)\n\ndf_scale = pd.DataFrame(StandardScaler().fit_transform(dfStandardScalerData), columns = dfStandardScalerData.columns)\nprint(df_scale)","metadata":{"papermill":{"duration":0.052692,"end_time":"2022-07-01T11:33:10.633075","exception":false,"start_time":"2022-07-01T11:33:10.580383","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.508657Z","iopub.execute_input":"2022-07-13T21:51:04.509183Z","iopub.status.idle":"2022-07-13T21:51:04.536240Z","shell.execute_reply.started":"2022-07-13T21:51:04.509128Z","shell.execute_reply":"2022-07-13T21:51:04.535240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\npassenger = ['CryoSleep','VIP']\ndf[passenger] = df[passenger].apply(LabelEncoder().fit_transform)\nprint(df.head())","metadata":{"papermill":{"duration":0.055306,"end_time":"2022-07-01T11:33:10.718086","exception":false,"start_time":"2022-07-01T11:33:10.662780","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.538081Z","iopub.execute_input":"2022-07-13T21:51:04.538545Z","iopub.status.idle":"2022-07-13T21:51:04.563318Z","shell.execute_reply.started":"2022-07-13T21:51:04.538483Z","shell.execute_reply":"2022-07-13T21:51:04.561840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=[\"HomePlanet\",\"Destination\"]\ndf_tansformed = pd.get_dummies(df[features])\n\ndf = df_tansformed.join(df)\n\ndf","metadata":{"papermill":{"duration":0.07871,"end_time":"2022-07-01T11:33:10.828911","exception":false,"start_time":"2022-07-01T11:33:10.750201","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.565072Z","iopub.execute_input":"2022-07-13T21:51:04.565905Z","iopub.status.idle":"2022-07-13T21:51:04.607983Z","shell.execute_reply.started":"2022-07-13T21:51:04.565856Z","shell.execute_reply":"2022-07-13T21:51:04.606692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#définition variable d'entrainement\n\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(df_scale, df['Transported'], test_size=0.33, random_state=42)\n\ninput_shape = [19]","metadata":{"papermill":{"duration":0.040713,"end_time":"2022-07-01T11:33:10.900014","exception":false,"start_time":"2022-07-01T11:33:10.859301","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.611351Z","iopub.execute_input":"2022-07-13T21:51:04.611814Z","iopub.status.idle":"2022-07-13T21:51:04.623240Z","shell.execute_reply.started":"2022-07-13T21:51:04.611702Z","shell.execute_reply":"2022-07-13T21:51:04.621672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\n# 1 neurone\nmodel = keras.Sequential([\n    layers.Dense(1, input_shape=input_shape),\n])\n\nw, b = model.weights\n\nprint(\"Weights\\n{}\\n\\nBias\\n{}\".format(w, b))","metadata":{"papermill":{"duration":7.222136,"end_time":"2022-07-01T11:33:29.063082","exception":false,"start_time":"2022-07-01T11:33:21.840946","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.625331Z","iopub.execute_input":"2022-07-13T21:51:04.625766Z","iopub.status.idle":"2022-07-13T21:51:04.649919Z","shell.execute_reply.started":"2022-07-13T21:51:04.625733Z","shell.execute_reply":"2022-07-13T21:51:04.648870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\n\n# visualisation du neurone\nmodel = keras.Sequential([\n    layers.Dense(1, input_shape=[1]),\n])\n\nx = tf.linspace(-1.0, 1.0, 100)\ny = model.predict(x)\n\nplt.figure(dpi=100)\nplt.plot(x, y, 'k')\nplt.xlim(-1, 1)\nplt.ylim(-1, 1)\nplt.xlabel(\"Input: x\")\nplt.ylabel(\"Target y\")\nw, b = model.weights # you could also use model.get_weights() here\nplt.title(\"Weight: {:0.2f}\\nBias: {:0.2f}\".format(w[0][0], b[0]))\nplt.show()","metadata":{"papermill":{"duration":0.60527,"end_time":"2022-07-01T11:33:29.706671","exception":false,"start_time":"2022-07-01T11:33:29.101401","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:04.651182Z","iopub.execute_input":"2022-07-13T21:51:04.651981Z","iopub.status.idle":"2022-07-13T21:51:04.980743Z","shell.execute_reply.started":"2022-07-13T21:51:04.651949Z","shell.execute_reply":"2022-07-13T21:51:04.979601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\n# 1 couche de 512 neurones\n\nmodel = keras.Sequential([\n    layers.Dense(units=512, activation='relu', input_shape=input_shape),\n    layers.Dense(units=1),\n])\nmodel = keras.Sequential([\n    layers.Dense(32, input_shape=[13]),\n    layers.Activation('relu'),\n    layers.Dense(32),\n    layers.Activation('relu'),\n    layers.Dense(1),\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T21:51:04.982216Z","iopub.execute_input":"2022-07-13T21:51:04.983064Z","iopub.status.idle":"2022-07-13T21:51:05.042239Z","shell.execute_reply.started":"2022-07-13T21:51:04.983022Z","shell.execute_reply":"2022-07-13T21:51:05.041035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\n# 3 couches de respectivement 512, 256, 128 neurones\nmodel = keras.Sequential([\n    layers.Dense(512, activation='relu', input_shape=input_shape),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(128, activation='relu'),    \n    layers.Dense(1),\n])\n\nactivation_layer = layers.Activation('relu')\n\nx = tf.linspace(-3.0, 3.0, 100)\ny = activation_layer(x) # once created, a layer is callable just like a function\n\nplt.figure(dpi=100)\nplt.plot(x, y)\nplt.xlim(-3, 3)\nplt.xlabel(\"Input\")\nplt.ylabel(\"Output\")\nplt.show()","metadata":{"papermill":{"duration":0.249285,"end_time":"2022-07-01T11:33:29.995713","exception":false,"start_time":"2022-07-01T11:33:29.746428","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-13T21:51:05.043735Z","iopub.execute_input":"2022-07-13T21:51:05.044075Z","iopub.status.idle":"2022-07-13T21:51:05.288087Z","shell.execute_reply.started":"2022-07-13T21:51:05.044045Z","shell.execute_reply":"2022-07-13T21:51:05.286895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import callbacks\n\n\nearly_stopping = callbacks.EarlyStopping(\n    patience=5,\n    min_delta=0.001,\n    restore_best_weights=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T22:08:08.827411Z","iopub.execute_input":"2022-07-13T22:08:08.827887Z","iopub.status.idle":"2022-07-13T22:08:08.834182Z","shell.execute_reply.started":"2022-07-13T22:08:08.827853Z","shell.execute_reply":"2022-07-13T22:08:08.833170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\n# ajout dropout et batch\nmodel = keras.Sequential([\n    layers.Dense(512, activation='relu', input_shape=[X_train.shape[1]]),\n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    layers.Dense(256, activation='relu'),\n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    layers.Dense(128, activation='relu'), \n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    layers.Dense(1),\n])\n\nmodel.compile(\n    optimizer='adam',\n    loss='mae',\n)\n\nhistory = model.fit(\n    X_train, y_train,\n    validation_data=(X_test, y_test),\n    batch_size=512,\n    epochs=100,\n    callbacks=[early_stopping],\n    \n)\n\n\n# Show the learning curves\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T22:12:02.984352Z","iopub.execute_input":"2022-07-13T22:12:02.984834Z","iopub.status.idle":"2022-07-13T22:12:24.923306Z","shell.execute_reply.started":"2022-07-13T22:12:02.984799Z","shell.execute_reply":"2022-07-13T22:12:24.921717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-07-13T22:02:00.298569Z","iopub.execute_input":"2022-07-13T22:02:00.299057Z","iopub.status.idle":"2022-07-13T22:02:13.578657Z","shell.execute_reply.started":"2022-07-13T22:02:00.299022Z","shell.execute_reply":"2022-07-13T22:02:13.576928Z"},"trusted":true},"execution_count":null,"outputs":[]}]}