{"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":"# 1 Loading Libraries and Data","metadata":{}},{"cell_type":"markdown","source":"# 1.1 Loading libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras import regularizers\nimport gc","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:44:19.028331Z","iopub.execute_input":"2022-08-08T12:44:19.028816Z","iopub.status.idle":"2022-08-08T12:44:25.345143Z","shell.execute_reply.started":"2022-08-08T12:44:19.028769Z","shell.execute_reply":"2022-08-08T12:44:25.344203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1.2 Loading Data","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv(\"../input/training/train.csv.gzip\",compression=\"gzip\",dtype=\"float16\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:44:25.347007Z","iopub.execute_input":"2022-08-08T12:44:25.347370Z","iopub.status.idle":"2022-08-08T12:47:11.273760Z","shell.execute_reply.started":"2022-08-08T12:44:25.347341Z","shell.execute_reply":"2022-08-08T12:47:11.272539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1.3 Data size and structure","metadata":{}},{"cell_type":"markdown","source":"Here we understant the shape of the table. We found that we have an extra column in the data set. So we need to remove it.","metadata":{}},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:11.275113Z","iopub.execute_input":"2022-08-08T12:47:11.275389Z","iopub.status.idle":"2022-08-08T12:47:11.283651Z","shell.execute_reply.started":"2022-08-08T12:47:11.275362Z","shell.execute_reply":"2022-08-08T12:47:11.282848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:11.285330Z","iopub.execute_input":"2022-08-08T12:47:11.285608Z","iopub.status.idle":"2022-08-08T12:47:11.321893Z","shell.execute_reply.started":"2022-08-08T12:47:11.285581Z","shell.execute_reply":"2022-08-08T12:47:11.321151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"When we save the dataframe as csv the datatypes are not maintained. So we need to reassign the data types. ","metadata":{}},{"cell_type":"code","source":"train[\"target\"]=train[\"target\"].astype(\"int8\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:11.322979Z","iopub.execute_input":"2022-08-08T12:47:11.323261Z","iopub.status.idle":"2022-08-08T12:47:11.920716Z","shell.execute_reply.started":"2022-08-08T12:47:11.323237Z","shell.execute_reply":"2022-08-08T12:47:11.919962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drop the extra added column","metadata":{}},{"cell_type":"code","source":"train.drop(\"Unnamed: 0\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:11.922035Z","iopub.execute_input":"2022-08-08T12:47:11.922377Z","iopub.status.idle":"2022-08-08T12:47:15.892218Z","shell.execute_reply.started":"2022-08-08T12:47:11.922342Z","shell.execute_reply":"2022-08-08T12:47:15.891502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:15.893209Z","iopub.execute_input":"2022-08-08T12:47:15.893911Z","iopub.status.idle":"2022-08-08T12:47:15.924957Z","shell.execute_reply.started":"2022-08-08T12:47:15.893865Z","shell.execute_reply":"2022-08-08T12:47:15.924162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1.4 Reduce the size of Data","metadata":{}},{"cell_type":"code","source":"for col in train.columns:\n    if col not in ['customer_ID', 'target']:\n        train[col]=train[col].astype(\"float16\")\ntrain[\"target\"]=train[\"target\"].astype(\"int8\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:15.926142Z","iopub.execute_input":"2022-08-08T12:47:15.926835Z","iopub.status.idle":"2022-08-08T12:47:16.921157Z","shell.execute_reply.started":"2022-08-08T12:47:15.926791Z","shell.execute_reply":"2022-08-08T12:47:16.920441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2 Splitting the Data ","metadata":{}},{"cell_type":"code","source":"train_y=train[\"target\"].copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:16.922287Z","iopub.execute_input":"2022-08-08T12:47:16.923249Z","iopub.status.idle":"2022-08-08T12:47:16.928707Z","shell.execute_reply.started":"2022-08-08T12:47:16.923208Z","shell.execute_reply":"2022-08-08T12:47:16.927834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x=train.loc[:,train.columns!=\"target\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:16.930943Z","iopub.execute_input":"2022-08-08T12:47:16.931468Z","iopub.status.idle":"2022-08-08T12:47:20.310562Z","shell.execute_reply.started":"2022-08-08T12:47:16.931439Z","shell.execute_reply":"2022-08-08T12:47:20.309884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:20.311567Z","iopub.execute_input":"2022-08-08T12:47:20.311787Z","iopub.status.idle":"2022-08-08T12:47:20.315742Z","shell.execute_reply.started":"2022-08-08T12:47:20.311747Z","shell.execute_reply":"2022-08-08T12:47:20.315035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Get a glimps of train_x and train_y","metadata":{}},{"cell_type":"code","source":"train_y","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:20.316869Z","iopub.execute_input":"2022-08-08T12:47:20.317076Z","iopub.status.idle":"2022-08-08T12:47:20.332520Z","shell.execute_reply.started":"2022-08-08T12:47:20.317052Z","shell.execute_reply":"2022-08-08T12:47:20.331695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:20.333770Z","iopub.execute_input":"2022-08-08T12:47:20.334063Z","iopub.status.idle":"2022-08-08T12:47:20.466105Z","shell.execute_reply.started":"2022-08-08T12:47:20.334030Z","shell.execute_reply":"2022-08-08T12:47:20.465316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Free the memory space","metadata":{}},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:20.467657Z","iopub.execute_input":"2022-08-08T12:47:20.467973Z","iopub.status.idle":"2022-08-08T12:47:20.636232Z","shell.execute_reply.started":"2022-08-08T12:47:20.467935Z","shell.execute_reply":"2022-08-08T12:47:20.635586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3 Training a model","metadata":{}},{"cell_type":"markdown","source":"Important link for hyperparameter tuning (https://medium.com/mini-distill/effect-of-batch-size-on-training-dynamics-21c14f7a716e)","metadata":{}},{"cell_type":"code","source":"# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T12:47:20.637382Z","iopub.execute_input":"2022-08-08T12:47:20.638092Z","iopub.status.idle":"2022-08-08T12:47:26.560969Z","shell.execute_reply.started":"2022-08-08T12:47:20.638063Z","shell.execute_reply":"2022-08-08T12:47:26.560391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# instantiating the model in the strategy scope creates the model on the TPU\nwith tpu_strategy.scope():\n    model=keras.Sequential([layers.Dense(units=50,activation=\"relu\",input_shape=[169]),layers.Dropout(0.2),layers.BatchNormalization(),layers.Dense(units=25,activation=\"relu\"),layers.BatchNormalization(),layers.Dense(units=25,activation=\"relu\"),layers.Dense(units=1,activation=\"sigmoid\")])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:31:09.796470Z","iopub.execute_input":"2022-08-08T15:31:09.796785Z","iopub.status.idle":"2022-08-08T15:31:10.124701Z","shell.execute_reply.started":"2022-08-08T15:31:09.796753Z","shell.execute_reply":"2022-08-08T15:31:10.123670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\",optimizer=keras.optimizers.Adam(learning_rate=0.001))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:31:10.735263Z","iopub.execute_input":"2022-08-08T15:31:10.735564Z","iopub.status.idle":"2022-08-08T15:31:10.770548Z","shell.execute_reply.started":"2022-08-08T15:31:10.735534Z","shell.execute_reply":"2022-08-08T15:31:10.769692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(\n    monitor='val_loss',\n    min_delta=0.001, # minimium amount of change to count as an improvement\n    patience=20, # how many epochs to wait before stopping\n    restore_best_weights=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:31:11.697378Z","iopub.execute_input":"2022-08-08T15:31:11.697685Z","iopub.status.idle":"2022-08-08T15:31:11.703062Z","shell.execute_reply.started":"2022-08-08T15:31:11.697654Z","shell.execute_reply":"2022-08-08T15:31:11.701890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x=train_x,y=train_y,batch_size=40000,epochs=200,validation_split=0.3,use_multiprocessing=True,class_weight={0:1,1:1},callbacks=[early_stopping],shuffle=True,validation_batch_size=17000)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:31:12.543064Z","iopub.execute_input":"2022-08-08T15:31:12.543416Z","iopub.status.idle":"2022-08-08T15:33:57.603469Z","shell.execute_reply.started":"2022-08-08T15:31:12.543380Z","shell.execute_reply":"2022-08-08T15:33:57.602542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:33:57.605540Z","iopub.execute_input":"2022-08-08T15:33:57.605794Z","iopub.status.idle":"2022-08-08T15:33:57.884205Z","shell.execute_reply.started":"2022-08-08T15:33:57.605758Z","shell.execute_reply":"2022-08-08T15:33:57.883280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:33:57.886267Z","iopub.execute_input":"2022-08-08T15:33:57.886666Z","iopub.status.idle":"2022-08-08T15:33:57.895249Z","shell.execute_reply.started":"2022-08-08T15:33:57.886627Z","shell.execute_reply":"2022-08-08T15:33:57.894169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_x\ndel train_y","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.306029Z","iopub.status.idle":"2022-08-08T07:12:43.306989Z","shell.execute_reply.started":"2022-08-08T07:12:43.306728Z","shell.execute_reply":"2022-08-08T07:12:43.306756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.308110Z","iopub.status.idle":"2022-08-08T07:12:43.308545Z","shell.execute_reply.started":"2022-08-08T07:12:43.308321Z","shell.execute_reply":"2022-08-08T07:12:43.308345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we free up space as training data is not needed now.","metadata":{}},{"cell_type":"markdown","source":"# 4 Load the test data","metadata":{}},{"cell_type":"code","source":"test=pd.read_csv(\"../input/test-dataset/test_1.csv.gzip\",compression=\"gzip\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.310254Z","iopub.status.idle":"2022-08-08T07:12:43.310685Z","shell.execute_reply.started":"2022-08-08T07:12:43.310427Z","shell.execute_reply":"2022-08-08T07:12:43.310451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.312284Z","iopub.status.idle":"2022-08-08T07:12:43.312876Z","shell.execute_reply.started":"2022-08-08T07:12:43.312606Z","shell.execute_reply":"2022-08-08T07:12:43.312654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.314175Z","iopub.status.idle":"2022-08-08T07:12:43.314536Z","shell.execute_reply.started":"2022-08-08T07:12:43.314350Z","shell.execute_reply":"2022-08-08T07:12:43.314372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in test.columns:\n    if col not in ['customer_ID', 'target']:\n        test[col]=test[col].astype(\"float16\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.316140Z","iopub.status.idle":"2022-08-08T07:12:43.316525Z","shell.execute_reply.started":"2022-08-08T07:12:43.316340Z","shell.execute_reply":"2022-08-08T07:12:43.316363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5 Split the test data","metadata":{}},{"cell_type":"code","source":"customerID=test[\"customer_ID\"].copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.317557Z","iopub.status.idle":"2022-08-08T07:12:43.317901Z","shell.execute_reply.started":"2022-08-08T07:12:43.317739Z","shell.execute_reply":"2022-08-08T07:12:43.317755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test[\"customer_ID\"]\ndel test[\"Unnamed: 0\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.318872Z","iopub.status.idle":"2022-08-08T07:12:43.319226Z","shell.execute_reply.started":"2022-08-08T07:12:43.319024Z","shell.execute_reply":"2022-08-08T07:12:43.319038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.322113Z","iopub.status.idle":"2022-08-08T07:12:43.322438Z","shell.execute_reply.started":"2022-08-08T07:12:43.322278Z","shell.execute_reply":"2022-08-08T07:12:43.322293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5 Fit the Data","metadata":{}},{"cell_type":"code","source":"target=pd.DataFrame(model.predict(x=test))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.324274Z","iopub.status.idle":"2022-08-08T07:12:43.324752Z","shell.execute_reply.started":"2022-08-08T07:12:43.324535Z","shell.execute_reply":"2022-08-08T07:12:43.324555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output=pd.concat([customerID,target],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.325851Z","iopub.status.idle":"2022-08-08T07:12:43.326510Z","shell.execute_reply.started":"2022-08-08T07:12:43.326316Z","shell.execute_reply":"2022-08-08T07:12:43.326339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.columns=[\"customer_ID\",\"target\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.327772Z","iopub.status.idle":"2022-08-08T07:12:43.328182Z","shell.execute_reply.started":"2022-08-08T07:12:43.327934Z","shell.execute_reply":"2022-08-08T07:12:43.327966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv(\"output1.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T07:12:43.330162Z","iopub.status.idle":"2022-08-08T07:12:43.330499Z","shell.execute_reply.started":"2022-08-08T07:12:43.330322Z","shell.execute_reply":"2022-08-08T07:12:43.330345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}