{"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":"!pip install tensorflow==2.6.0\n!pip install keras==2.6.*","metadata":{"execution":{"iopub.status.busy":"2021-11-12T17:39:43.205544Z","iopub.execute_input":"2021-11-12T17:39:43.206157Z","iopub.status.idle":"2021-11-12T17:39:58.346452Z","shell.execute_reply.started":"2021-11-12T17:39:43.206120Z","shell.execute_reply":"2021-11-12T17:39:58.345579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport numpy as np\nimport sklearn\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-12T17:40:07.908656Z","iopub.execute_input":"2021-11-12T17:40:07.909361Z","iopub.status.idle":"2021-11-12T17:40:08.538694Z","shell.execute_reply.started":"2021-11-12T17:40:07.909322Z","shell.execute_reply":"2021-11-12T17:40:08.537925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2021-11-12T17:40:11.487467Z","iopub.execute_input":"2021-11-12T17:40:11.488305Z","iopub.status.idle":"2021-11-12T17:40:11.499072Z","shell.execute_reply.started":"2021-11-12T17:40:11.488264Z","shell.execute_reply":"2021-11-12T17:40:11.498312Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:10.351305Z","iopub.execute_input":"2021-10-14T17:05:10.352077Z","iopub.status.idle":"2021-10-14T17:05:13.445282Z","shell.execute_reply.started":"2021-10-14T17:05:10.352031Z","shell.execute_reply":"2021-10-14T17:05:13.44454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:13.479827Z","iopub.execute_input":"2021-10-14T17:05:13.480036Z","iopub.status.idle":"2021-10-14T17:05:13.485105Z","shell.execute_reply.started":"2021-10-14T17:05:13.48001Z","shell.execute_reply":"2021-10-14T17:05:13.484457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop('id',inplace = True,axis = 1)\nlabels = train['target']\nfeatures = train.drop('target',inplace = False,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:16.452255Z","iopub.execute_input":"2021-10-14T17:05:16.452998Z","iopub.status.idle":"2021-10-14T17:05:16.671632Z","shell.execute_reply.started":"2021-10-14T17:05:16.452953Z","shell.execute_reply":"2021-10-14T17:05:16.670889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in features.isnull().sum().index:\n    features[col] = features[col].fillna('missing',inplace = False)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:19.473865Z","iopub.execute_input":"2021-10-14T17:05:19.47412Z","iopub.status.idle":"2021-10-14T17:05:21.690936Z","shell.execute_reply.started":"2021-10-14T17:05:19.474089Z","shell.execute_reply":"2021-10-14T17:05:21.690195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str_cols = features.select_dtypes(include = object).columns","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:22.472091Z","iopub.execute_input":"2021-10-14T17:05:22.472337Z","iopub.status.idle":"2021-10-14T17:05:23.005708Z","shell.execute_reply.started":"2021-10-14T17:05:22.472307Z","shell.execute_reply":"2021-10-14T17:05:23.004959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_ds(features,labels):\n    X_train,X_test,y_train,y_test = train_test_split(features,labels,test_size = 0.2)\n    X_train = {name:tf.constant(value.map(str).to_numpy().reshape(-1,1)) for name,value in dict(X_train).items()}\n    X_test = {name:tf.constant(value.map(str).to_numpy().reshape(-1,1)) for name,value in dict(X_test).items()}\n    return (X_train,y_train.to_numpy().reshape(-1,1)),(X_test,y_test.to_numpy().reshape(-1,1))","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:29.062226Z","iopub.execute_input":"2021-10-14T17:05:29.062648Z","iopub.status.idle":"2021-10-14T17:05:29.074022Z","shell.execute_reply.started":"2021-10-14T17:05:29.062605Z","shell.execute_reply":"2021-10-14T17:05:29.072212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a,b = make_ds(features,labels)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:05:32.051129Z","iopub.execute_input":"2021-10-14T17:05:32.051394Z","iopub.status.idle":"2021-10-14T17:05:36.800281Z","shell.execute_reply.started":"2021-10-14T17:05:32.051361Z","shell.execute_reply":"2021-10-14T17:05:36.799517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_lookup(data,col,vocab):\n    lookup = layers.StringLookup(vocabulary = vocab)\n    return lookup","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:23:00.841975Z","iopub.execute_input":"2021-10-14T17:23:00.842617Z","iopub.status.idle":"2021-10-14T17:23:00.847447Z","shell.execute_reply.started":"2021-10-14T17:23:00.842565Z","shell.execute_reply":"2021-10-14T17:23:00.846673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = []\noutputs = []\nfor col in str_cols:\n    inp = keras.Input(shape = (1,),name = col,dtype = tf.string)\n    l = make_lookup(a[0],col,list(features[col].unique()))\n    in_dim = len(l.get_vocabulary())\n    out_dim = max(len(l.get_vocabulary()),50)\n    x = l(inp)\n    x = layers.Embedding(input_dim = in_dim,\n                        output_dim = out_dim)(x)\n    x = layers.Reshape(target_shape = (out_dim,))(x)\n    inputs.append(inp)\n    outputs.append(x)\nx = layers.Concatenate()(outputs)\n\nx = layers.Dense(100,activation = 'relu')(x)\nx = layers.Dropout(0.2)(x)\nx = layers.Dense(100,activation = 'relu')(x)\nx = layers.Dropout(0.2)(x)\nx = layers.Dense(100,activation = 'relu')(x)\nx = layers.Dense(1,activation = 'sigmoid')(x)\nmodel = keras.Model(inputs,x)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:23:18.252597Z","iopub.execute_input":"2021-10-14T17:23:18.252875Z","iopub.status.idle":"2021-10-14T17:23:20.058481Z","shell.execute_reply.started":"2021-10-14T17:23:18.252842Z","shell.execute_reply":"2021-10-14T17:23:20.057727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.utils.plot_model(model,show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:24:31.966018Z","iopub.execute_input":"2021-10-14T17:24:31.966281Z","iopub.status.idle":"2021-10-14T17:24:33.729797Z","shell.execute_reply.started":"2021-10-14T17:24:31.966249Z","shell.execute_reply":"2021-10-14T17:24:33.727355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = keras.losses.BinaryCrossentropy(),optimizer = 'adam',metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:25:46.764552Z","iopub.execute_input":"2021-10-14T17:25:46.765224Z","iopub.status.idle":"2021-10-14T17:25:46.810247Z","shell.execute_reply.started":"2021-10-14T17:25:46.765178Z","shell.execute_reply":"2021-10-14T17:25:46.809519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(a[0],a[1])","metadata":{"execution":{"iopub.status.busy":"2021-10-14T17:25:59.530012Z","iopub.execute_input":"2021-10-14T17:25:59.530338Z","iopub.status.idle":"2021-10-14T18:04:24.469746Z","shell.execute_reply.started":"2021-10-14T17:25:59.530303Z","shell.execute_reply":"2021-10-14T18:04:24.468981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(b[0],b[1])","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:04:24.473876Z","iopub.execute_input":"2021-10-14T18:04:24.47587Z","iopub.status.idle":"2021-10-14T18:06:47.152732Z","shell.execute_reply.started":"2021-10-14T18:04:24.475814Z","shell.execute_reply":"2021-10-14T18:06:47.152014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features = test.drop('id',inplace = False,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:12:36.503802Z","iopub.execute_input":"2021-10-14T18:12:36.504509Z","iopub.status.idle":"2021-10-14T18:12:36.564929Z","shell.execute_reply.started":"2021-10-14T18:12:36.504465Z","shell.execute_reply":"2021-10-14T18:12:36.563925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in test_features.isnull().sum().index:\n    test_features[col] = test_features[col].fillna('missing',inplace = False)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:13:26.381983Z","iopub.execute_input":"2021-10-14T18:13:26.382255Z","iopub.status.idle":"2021-10-14T18:13:27.846303Z","shell.execute_reply.started":"2021-10-14T18:13:26.382225Z","shell.execute_reply":"2021-10-14T18:13:27.84559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:13:42.223533Z","iopub.execute_input":"2021-10-14T18:13:42.224237Z","iopub.status.idle":"2021-10-14T18:13:42.908735Z","shell.execute_reply.started":"2021-10-14T18:13:42.2242Z","shell.execute_reply":"2021-10-14T18:13:42.907905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = {name:tf.constant(value.map(str).to_numpy().reshape(-1,1)) for name,value in dict(test_features).items()}","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:15:11.286943Z","iopub.execute_input":"2021-10-14T18:15:11.287211Z","iopub.status.idle":"2021-10-14T18:15:13.910916Z","shell.execute_reply.started":"2021-10-14T18:15:11.287181Z","shell.execute_reply":"2021-10-14T18:15:13.910167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:15:51.97556Z","iopub.execute_input":"2021-10-14T18:15:51.976256Z","iopub.status.idle":"2021-10-14T18:30:14.297951Z","shell.execute_reply.started":"2021-10-14T18:15:51.976216Z","shell.execute_reply":"2021-10-14T18:30:14.297174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(test['id'],columns = ['id'])","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:37:24.852306Z","iopub.execute_input":"2021-10-14T18:37:24.853053Z","iopub.status.idle":"2021-10-14T18:37:24.861887Z","shell.execute_reply.started":"2021-10-14T18:37:24.853013Z","shell.execute_reply":"2021-10-14T18:37:24.86107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['target'] = pd.Series(tf.squeeze(pred))","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:38:37.237882Z","iopub.execute_input":"2021-10-14T18:38:37.238572Z","iopub.status.idle":"2021-10-14T18:38:37.246234Z","shell.execute_reply.started":"2021-10-14T18:38:37.238536Z","shell.execute_reply":"2021-10-14T18:38:37.245389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T18:44:08.8654Z","iopub.execute_input":"2021-10-14T18:44:08.865684Z","iopub.status.idle":"2021-10-14T18:44:09.993328Z","shell.execute_reply.started":"2021-10-14T18:44:08.865655Z","shell.execute_reply":"2021-10-14T18:44:09.992582Z"},"trusted":true},"execution_count":null,"outputs":[]}]}