{"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":"# Importing libraries\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\nimport pandas as pd\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.impute import SimpleImputer\nfrom copy import deepcopy\n\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\n","metadata":{"id":"0_ePz4unFA9p","execution":{"iopub.status.busy":"2022-08-07T00:52:39.007059Z","iopub.execute_input":"2022-08-07T00:52:39.007775Z","iopub.status.idle":"2022-08-07T00:52:39.648917Z","shell.execute_reply.started":"2022-08-07T00:52:39.007735Z","shell.execute_reply":"2022-08-07T00:52:39.647780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading data","metadata":{}},{"cell_type":"code","source":"sample = pd.read_csv('../input/tabular-playground-series-aug-2022/sample_submission.csv')\ndata_test = pd.read_csv('../input/tabular-playground-series-aug-2022/test.csv')\ndata_train = pd.read_csv('../input/tabular-playground-series-aug-2022/train.csv')","metadata":{"id":"fRZgN2vhFs7y","execution":{"iopub.status.busy":"2022-08-07T00:53:08.627341Z","iopub.execute_input":"2022-08-07T00:53:08.627763Z","iopub.status.idle":"2022-08-07T00:53:08.903665Z","shell.execute_reply.started":"2022-08-07T00:53:08.627732Z","shell.execute_reply":"2022-08-07T00:53:08.902642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.head(4)","metadata":{"id":"--zbPHb4423a","outputId":"fa124a83-3960-471d-d2fd-7e71616cb0dc","execution":{"iopub.status.busy":"2022-08-07T00:53:30.533949Z","iopub.execute_input":"2022-08-07T00:53:30.534433Z","iopub.status.idle":"2022-08-07T00:53:30.564361Z","shell.execute_reply.started":"2022-08-07T00:53:30.534393Z","shell.execute_reply":"2022-08-07T00:53:30.563449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Columns on the dataset","metadata":{"execution":{"iopub.status.busy":"2022-08-07T00:54:01.246806Z","iopub.execute_input":"2022-08-07T00:54:01.247194Z","iopub.status.idle":"2022-08-07T00:54:01.253875Z","shell.execute_reply.started":"2022-08-07T00:54:01.247164Z","shell.execute_reply":"2022-08-07T00:54:01.252395Z"}}},{"cell_type":"code","source":"print(data_test.columns)\nprint(data_train.columns)","metadata":{"id":"fttq_uTjGpHf","outputId":"8c09b24d-e6d9-4c29-bd26-d835c72ce178","execution":{"iopub.status.busy":"2022-08-07T00:54:09.723206Z","iopub.execute_input":"2022-08-07T00:54:09.723605Z","iopub.status.idle":"2022-08-07T00:54:09.729711Z","shell.execute_reply.started":"2022-08-07T00:54:09.723574Z","shell.execute_reply":"2022-08-07T00:54:09.728768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train['failure'].value_counts()","metadata":{"id":"NuIO62YNGpr8","outputId":"9b5b3875-30f1-4178-ffc6-b8437ffaa0e1","execution":{"iopub.status.busy":"2022-08-07T00:54:13.726092Z","iopub.execute_input":"2022-08-07T00:54:13.727021Z","iopub.status.idle":"2022-08-07T00:54:13.741702Z","shell.execute_reply.started":"2022-08-07T00:54:13.726982Z","shell.execute_reply":"2022-08-07T00:54:13.740731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, we visualize the differents values from dataset to verify if exist any null value and fixed. We can see that exist some values with this problem. For example, from loading to measurement_17 we can use \"fillna\" with some characterics how 'mean', 'median' or 'mode'.","metadata":{}},{"cell_type":"code","source":"data_train.isnull().sum(axis=0)[data_train.isnull().sum(axis=0)!=0]","metadata":{"id":"7IAWwbHVHS9q","outputId":"dc1aa2ca-d865-40bd-a2e5-9d6149b8c3fc","execution":{"iopub.status.busy":"2022-08-07T00:59:50.947368Z","iopub.execute_input":"2022-08-07T00:59:50.947802Z","iopub.status.idle":"2022-08-07T00:59:50.969789Z","shell.execute_reply.started":"2022-08-07T00:59:50.947771Z","shell.execute_reply":"2022-08-07T00:59:50.968831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train[data_train.isnull().any(axis=1)].head(2)","metadata":{"id":"F07zJz815oUe","outputId":"4a989a65-2558-4331-a558-0337227a2a6d","execution":{"iopub.status.busy":"2022-08-07T00:59:56.606266Z","iopub.execute_input":"2022-08-07T00:59:56.606715Z","iopub.status.idle":"2022-08-07T00:59:56.639190Z","shell.execute_reply.started":"2022-08-07T00:59:56.606652Z","shell.execute_reply":"2022-08-07T00:59:56.638164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cleanup data (Impute and fill NaN values)","metadata":{}},{"cell_type":"code","source":"def clean(data):\n  \n  le = LabelEncoder()\n  \n  # Replace str by numbers\n  data = deepcopy(data)\n  cols = [f\"attribute_{i}\" for i in range(0, 2)] + [\"product_code\"]\n  for col in cols:\n      data[col] = le.fit_transform(data[col])\n      #print(le.classes_)\n  \n  data = data.drop(['id', 'product_code'], axis=1)\n  \n  # None values\n  imputer = SimpleImputer(strategy='mean')\n  final_data = pd.DataFrame(imputer.fit_transform(data))\n\n  final_data.columns = data.columns\n\n  return final_data\n  \ntrain = clean(data_train)\ntest = clean(data_test)\n\ntrain.head(5)\n","metadata":{"id":"B4IDLMJz53wB","outputId":"d2cac3a3-6cc8-4596-f2fe-48c96441274d","execution":{"iopub.status.busy":"2022-08-07T01:01:07.746558Z","iopub.execute_input":"2022-08-07T01:01:07.746971Z","iopub.status.idle":"2022-08-07T01:01:07.874286Z","shell.execute_reply.started":"2022-08-07T01:01:07.746939Z","shell.execute_reply":"2022-08-07T01:01:07.873329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['failure'].value_counts()","metadata":{"id":"qfPy2ipgkG3j","outputId":"1f2e67f9-52ab-45ee-e4e5-ebb74c29bc96","execution":{"iopub.status.busy":"2022-08-07T01:01:12.965897Z","iopub.execute_input":"2022-08-07T01:01:12.966638Z","iopub.status.idle":"2022-08-07T01:01:12.978674Z","shell.execute_reply.started":"2022-08-07T01:01:12.966593Z","shell.execute_reply":"2022-08-07T01:01:12.977311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data and test and train\n# Train\nX = train.drop('failure', axis=1).to_numpy()\nY = train['failure'].astype(np.int32)\n\ntrain_X, val_X, train_y, val_y = train_test_split(X, Y, test_size=0.2, shuffle=False)\ntrain_y = train_y.to_numpy()\nval_y = val_y.to_numpy()\n\nval_y = val_y.reshape(val_y.shape[0], 1)\ntrain_y = train_y.reshape(train_y.shape[0], 1)","metadata":{"id":"kz3dBuTiTHpB","execution":{"iopub.status.busy":"2022-08-07T01:01:28.606478Z","iopub.execute_input":"2022-08-07T01:01:28.607670Z","iopub.status.idle":"2022-08-07T01:01:28.620403Z","shell.execute_reply.started":"2022-08-07T01:01:28.607625Z","shell.execute_reply":"2022-08-07T01:01:28.619223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"train_X shape: {train_X.shape}\")\nprint(f\"val_X shape: {val_X.shape}\")\nprint(f\"train_y shape: {train_y.shape}\")\nprint(f\"val_y shape: {val_y.shape}\")","metadata":{"id":"cbmHy9zjTawO","outputId":"e89fba80-1332-4bcc-d77f-d1e52b613aa9","execution":{"iopub.status.busy":"2022-08-07T01:01:30.546605Z","iopub.execute_input":"2022-08-07T01:01:30.547294Z","iopub.status.idle":"2022-08-07T01:01:30.553030Z","shell.execute_reply.started":"2022-08-07T01:01:30.547259Z","shell.execute_reply":"2022-08-07T01:01:30.551698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X[3].astype(np.int32)","metadata":{"id":"h4lauAV6jEan","outputId":"75a7429b-7270-4d40-c46f-2f9d75eadd5b","execution":{"iopub.status.busy":"2022-08-07T01:01:34.018085Z","iopub.execute_input":"2022-08-07T01:01:34.019445Z","iopub.status.idle":"2022-08-07T01:01:34.029271Z","shell.execute_reply.started":"2022-08-07T01:01:34.019389Z","shell.execute_reply":"2022-08-07T01:01:34.027938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model\ndef Model():\n  input = tfl.Input(shape=(23))\n  x = input\n  x = tfl.BatchNormalization()(x)\n  x_shortcut = tfl.Dense(units=23, activation='relu', kernel_initializer='he_uniform')(x)\n    \n  x = tfl.Dropout(0.2)(x_shortcut)\n  x = tfl.Dense(units=32, activation='relu', kernel_initializer='he_uniform')(x)\n\n  output = tfl.Dense(units=1, activation='sigmoid')(x)\n\n  model = tf.keras.Model(inputs=input, outputs=output)\n\n\n  model.compile(optimizer=tf.keras.optimizers.Adam(),\n                loss=tf.keras.losses.binary_crossentropy,\n                metrics=[\"accuracy\"])\n  return model\n\nmodel = Model()\n\n#train_X = train_X/np.linalg.norm(train_X, axis=0)\n#val_X = val_X/np.linalg.norm(val_X, axis=0)\n\n#model.summary()\n\nmodel.fit(train_X, train_y, validation_data=(val_X, val_y), epochs=15, batch_size=128)","metadata":{"id":"snUoGx1clM47","outputId":"f47a0c36-b8c1-4c89-e7b0-f51ba5f3f246","execution":{"iopub.status.busy":"2022-08-07T01:12:31.927713Z","iopub.execute_input":"2022-08-07T01:12:31.928114Z","iopub.status.idle":"2022-08-07T01:12:41.309660Z","shell.execute_reply.started":"2022-08-07T01:12:31.928084Z","shell.execute_reply":"2022-08-07T01:12:41.308415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" a = model.predict(val_X)\n b = model.predict(train_X)","metadata":{"id":"TPd19t1Vllr2","execution":{"iopub.status.busy":"2022-08-07T01:12:43.866145Z","iopub.execute_input":"2022-08-07T01:12:43.867422Z","iopub.status.idle":"2022-08-07T01:12:44.775965Z","shell.execute_reply.started":"2022-08-07T01:12:43.867368Z","shell.execute_reply":"2022-08-07T01:12:44.774613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(a[np.argmax(a)])\nprint(val_y[np.argmax(a)])","metadata":{"id":"7I3A3lIvwHLy","outputId":"ead468a3-2994-47ff-eabc-2f88be346484","execution":{"iopub.status.busy":"2022-08-07T01:12:45.838383Z","iopub.execute_input":"2022-08-07T01:12:45.838993Z","iopub.status.idle":"2022-08-07T01:12:45.848384Z","shell.execute_reply.started":"2022-08-07T01:12:45.838960Z","shell.execute_reply":"2022-08-07T01:12:45.847014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Save data","metadata":{}},{"cell_type":"code","source":"pred = np.squeeze(model.predict(test))\nid = data_test['id']\n\npd.DataFrame({'id': id, 'failure': pred}).to_csv(\"./submission.csv\", index=False)\n","metadata":{"id":"0Ri1xB1eNfWc","execution":{"iopub.status.busy":"2022-08-07T01:06:02.646467Z","iopub.execute_input":"2022-08-07T01:06:02.646862Z","iopub.status.idle":"2022-08-07T01:06:03.871572Z","shell.execute_reply.started":"2022-08-07T01:06:02.646832Z","shell.execute_reply":"2022-08-07T01:06:03.870682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}