{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":17000,"databundleVersionId":895292,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# https://www.youtube.com/watch?v=EATAM3BOD_E\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom sklearn import preprocessing #label encoding\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.models import Model, load_model\nfrom tensorflow.keras import callbacks\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras import utils","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-17T17:21:21.874461Z","iopub.execute_input":"2024-10-17T17:21:21.874892Z","iopub.status.idle":"2024-10-17T17:21:21.881490Z","shell.execute_reply.started":"2024-10-17T17:21:21.874854Z","shell.execute_reply":"2024-10-17T17:21:21.880106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/cat-in-the-dat-ii/train.csv\")\ntest = pd.read_csv(\"../input/cat-in-the-dat-ii/test.csv\")\nsample = pd.read_csv(\"../input/cat-in-the-dat-ii/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:21:24.963906Z","iopub.execute_input":"2024-10-17T17:21:24.964313Z","iopub.status.idle":"2024-10-17T17:21:28.808263Z","shell.execute_reply.started":"2024-10-17T17:21:24.964272Z","shell.execute_reply":"2024-10-17T17:21:28.807127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:11.713820Z","iopub.execute_input":"2024-10-17T17:23:11.714231Z","iopub.status.idle":"2024-10-17T17:23:11.739993Z","shell.execute_reply.started":"2024-10-17T17:23:11.714191Z","shell.execute_reply":"2024-10-17T17:23:11.738845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head() # test set dosen't have the \"target\" column","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:14.513746Z","iopub.execute_input":"2024-10-17T17:23:14.514167Z","iopub.status.idle":"2024-10-17T17:23:14.541068Z","shell.execute_reply.started":"2024-10-17T17:23:14.514127Z","shell.execute_reply":"2024-10-17T17:23:14.539087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.loc[:,'target']= -1 # Add fake target as \"-1\" because I want to combine training and test together.","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:19.204431Z","iopub.execute_input":"2024-10-17T17:23:19.204869Z","iopub.status.idle":"2024-10-17T17:23:19.211368Z","shell.execute_reply.started":"2024-10-17T17:23:19.204825Z","shell.execute_reply":"2024-10-17T17:23:19.210048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.concat([train,test]).reset_index(drop=True)\ndata.shape, train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:13.239085Z","iopub.execute_input":"2024-10-17T17:24:13.239512Z","iopub.status.idle":"2024-10-17T17:24:14.360007Z","shell.execute_reply.started":"2024-10-17T17:24:13.239471Z","shell.execute_reply":"2024-10-17T17:24:14.358976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [f for f in train.columns if f not in [\"id\",\"target\"]] # Not include ID and target columns","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:24.904022Z","iopub.execute_input":"2024-10-17T17:23:24.904503Z","iopub.status.idle":"2024-10-17T17:23:24.909946Z","shell.execute_reply.started":"2024-10-17T17:23:24.904450Z","shell.execute_reply":"2024-10-17T17:23:24.908809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:27.629054Z","iopub.execute_input":"2024-10-17T17:23:27.629484Z","iopub.status.idle":"2024-10-17T17:23:27.654554Z","shell.execute_reply.started":"2024-10-17T17:23:27.629444Z","shell.execute_reply":"2024-10-17T17:23:27.653398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# orginally from Youtube\nfor feat in features:\n    lbl_enc = preprocessing.LabelEncoder()  # label encoder\n    data.loc[:, feat] = lbl_enc.fit_transform(data[feat].astype(str).fillna(\"-1\").values) # fitting it and transforming everything at once ","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:39.934288Z","iopub.execute_input":"2024-10-17T17:23:39.935213Z","iopub.status.idle":"2024-10-17T17:23:49.716605Z","shell.execute_reply.started":"2024-10-17T17:23:39.935152Z","shell.execute_reply":"2024-10-17T17:23:49.715470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()   # The result looks the same as the youtube video","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:23:53.459163Z","iopub.execute_input":"2024-10-17T17:23:53.459564Z","iopub.status.idle":"2024-10-17T17:23:53.484790Z","shell.execute_reply.started":"2024-10-17T17:23:53.459527Z","shell.execute_reply":"2024-10-17T17:23:53.483644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()   # However, many variables as \"object\" after transforming, which leads to errors at the end model.fit()","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:01.594046Z","iopub.execute_input":"2024-10-17T17:24:01.594467Z","iopub.status.idle":"2024-10-17T17:24:02.400527Z","shell.execute_reply.started":"2024-10-17T17:24:01.594427Z","shell.execute_reply":"2024-10-17T17:24:02.399396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ChatGPT \"Here’s a slightly modified and clearer version of the code, along with some improvements:\"\nfor feat in features:\n    lbl_enc = preprocessing.LabelEncoder()  # Initialize the LabelEncoder\n    # Fit and transform the data, replacing NaNs with \"-1\"\n    data[feat] = lbl_enc.fit_transform(data[feat].fillna(\"-1\").astype(str))","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:18.264211Z","iopub.execute_input":"2024-10-17T17:24:18.264643Z","iopub.status.idle":"2024-10-17T17:24:26.619920Z","shell.execute_reply.started":"2024-10-17T17:24:18.264602Z","shell.execute_reply":"2024-10-17T17:24:26.618765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()  # The result looks a little different but it's all good","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:29.363843Z","iopub.execute_input":"2024-10-17T17:24:29.364246Z","iopub.status.idle":"2024-10-17T17:24:29.388639Z","shell.execute_reply.started":"2024-10-17T17:24:29.364207Z","shell.execute_reply":"2024-10-17T17:24:29.387434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()  # the datatype of all variables now changed to int64","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:32.163758Z","iopub.execute_input":"2024-10-17T17:24:32.164199Z","iopub.status.idle":"2024-10-17T17:24:32.204331Z","shell.execute_reply.started":"2024-10-17T17:24:32.164157Z","shell.execute_reply":"2024-10-17T17:24:32.203244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = data[data.target !=-1].reset_index(drop = True)\ntest = data[data.target == -1].reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:35.319021Z","iopub.execute_input":"2024-10-17T17:24:35.319442Z","iopub.status.idle":"2024-10-17T17:24:35.691604Z","shell.execute_reply.started":"2024-10-17T17:24:35.319402Z","shell.execute_reply":"2024-10-17T17:24:35.690556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:37.999062Z","iopub.execute_input":"2024-10-17T17:24:38.000085Z","iopub.status.idle":"2024-10-17T17:24:38.006998Z","shell.execute_reply.started":"2024-10-17T17:24:38.000036Z","shell.execute_reply":"2024-10-17T17:24:38.005724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a model\ndef get_model(df, categorical_columns):\n    inputs = []\n    outputs = []\n    for c in categorical_columns:\n        num_unique_vals = int(df[c].nunique())   # number of unique values in each categorical column\n        embed_dim = int(min(np.ceil(num_unique_vals / 2),50))    # what's the dimension of the embedding that you want for specific column (e.g. 35->18)\n        inp = layers.Input(shape=(1,))\n        out = layers.Embedding(num_unique_vals + 1, embed_dim, name = c)(inp)\n    # apply dropout here\n        out = layers.Reshape(target_shape=(embed_dim, ))(out)\n        inputs.append(inp)\n        outputs.append(out)\n    # regularization\n    x = layers.Concatenate()(outputs)\n    x = layers.Dense(300, activation = 'relu')(x)\n    x = layers.Dropout(0.3)(x)\n    y = layers.Dense(1, activation = 'sigmoid')(x)\n    model = Model(inputs = inputs, outputs = y)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:40.283961Z","iopub.execute_input":"2024-10-17T17:24:40.284385Z","iopub.status.idle":"2024-10-17T17:24:40.292677Z","shell.execute_reply.started":"2024-10-17T17:24:40.284317Z","shell.execute_reply":"2024-10-17T17:24:40.291513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_model(train,features)","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:43.279412Z","iopub.execute_input":"2024-10-17T17:24:43.279913Z","iopub.status.idle":"2024-10-17T17:24:43.552100Z","shell.execute_reply.started":"2024-10-17T17:24:43.279866Z","shell.execute_reply":"2024-10-17T17:24:43.550928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_model(train,features).summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:45.639173Z","iopub.execute_input":"2024-10-17T17:24:45.639613Z","iopub.status.idle":"2024-10-17T17:24:45.989186Z","shell.execute_reply.started":"2024-10-17T17:24:45.639572Z","shell.execute_reply":"2024-10-17T17:24:45.988088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model(train,features)\nmodel.compile(loss = 'binary_crossentropy',optimizer = 'adam')","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:51.884039Z","iopub.execute_input":"2024-10-17T17:24:51.884452Z","iopub.status.idle":"2024-10-17T17:24:52.138553Z","shell.execute_reply.started":"2024-10-17T17:24:51.884416Z","shell.execute_reply":"2024-10-17T17:24:52.137497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[train.head(10).loc[:,f].values for f in features]","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:54.238973Z","iopub.execute_input":"2024-10-17T17:24:54.239428Z","iopub.status.idle":"2024-10-17T17:24:54.253664Z","shell.execute_reply.started":"2024-10-17T17:24:54.239386Z","shell.execute_reply":"2024-10-17T17:24:54.252537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit([train.loc[:, f].values for f in features], train.target.values)\n\n# Loss: A scalar value that we attempt to minimize during our training of the model. \n# The lower the loss, the closer our predictions are to the true labels.","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:24:57.018996Z","iopub.execute_input":"2024-10-17T17:24:57.019425Z","iopub.status.idle":"2024-10-17T17:26:30.130183Z","shell.execute_reply.started":"2024-10-17T17:24:57.019383Z","shell.execute_reply":"2024-10-17T17:26:30.128925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape  \n# The video shows \"train on 600000 samples.\" Why it's 18750 here? \n# The Tensorflow has been changed/upgraded. The batchsize is here 32. \n# Batch size refers to the number of samples before updating weights.\n# epoch is the amount of times it works through the whole training\n\n#For instance, let's say you have 1050 training samples and you want to set up a batch_size equal to 100. \n# The algorithm takes the first 100 samples (from 1st to 100th) from the training dataset and trains the network. \n# Next, it takes the second 100 samples (from 101st to 200th) and trains the network again. \n# We can keep doing this procedure until we have propagated all samples through the network.","metadata":{"execution":{"iopub.status.busy":"2024-10-17T17:27:12.499034Z","iopub.execute_input":"2024-10-17T17:27:12.500083Z","iopub.status.idle":"2024-10-17T17:27:12.506783Z","shell.execute_reply.started":"2024-10-17T17:27:12.500033Z","shell.execute_reply":"2024-10-17T17:27:12.505736Z"},"trusted":true},"execution_count":null,"outputs":[]}]}