{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Cat in dat II\n### This is the cat in dat II competition where you must use 24 features for binary classification. Credit to Jian Yang for inspiration on this notebook."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom deeptables.models.deeptable import ModelConfig, DeepTable\nfrom tensorflow.keras.utils import plot_model\nfrom scipy.stats import zscore\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.\nX = pd.read_csv(f'../input/cat-in-the-dat-ii/train.csv')\ny = X['target']\ntest = pd.read_csv(f'../input/cat-in-the-dat-ii/test.csv')\n\ncatID = test['id']\n\nX = X.drop('id', axis=1)\nX = X.drop('target', axis=1)\ntest = test.drop('id', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ord_order = [\n    [1.0, 2.0, 3.0],\n    ['Novice', 'Contributer', 'Expert', 'Master', 'Grandmaster'],\n    ['Freezing', 'Cold', 'Warm', 'Hot', 'Boiling Hot', 'Lava Hot']\n]\n\nfor i in range(1, 3):\n    ord_order_dict = {i : j for j, i in enumerate(ord_order[i])}\n    X[f'ord_{i}_en'] = X[f'ord_{i}'].fillna('NULL').map(ord_order_dict)\n    test[f'ord_{i}_en'] = test[f'ord_{i}'].fillna('NULL').map(ord_order_dict)\n\nfor i in range(3, 6):\n    ord_order_dict = {i : j for j, i in enumerate(sorted(list(set(list(X[f'ord_{i}'].dropna().unique()) + list(test[f'ord_{i}'].dropna().unique())))))}\n    X[f'ord_{i}_en'] = X[f'ord_{i}'].fillna('NULL').map(ord_order_dict)\n    test[f'ord_{i}_en'] =  test[f'ord_{i}'].fillna('NULL').map(ord_order_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cat_cols = [i for i in X.columns if '_en' not in i]\nX[cat_cols] = X[cat_cols].astype('category')\ntest[cat_cols] = test[cat_cols].astype('category')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Deeptables\n### The following model is an ensemble of neural networks called deeptables. Its parameters are defined through ModelConfig."},{"metadata":{"trusted":true},"cell_type":"code","source":"config = ModelConfig(\n    dnn_params={\n        'hidden_units':((200, 0.3, True),(200, 0.3, True),), #hidden_units\n        'dnn_activation':'relu',\n    },\n    embeddings_output_dim=20,\n    nets =['linear','cin_nets','dnn_nets'],\n    output_use_bias = False,\n    cin_params={\n       'cross_layer_size': (200, 200),\n       'use_bias': True,\n       'direct': True\n    },\n)\nmodel = DeepTable(config=config)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"oof_proba, eval_proba, test_prob = model.fit_cross_validation(\n    X, y, X_eval=None, X_test=test, num_folds=3, stratified=False, iterators=None, batch_size=128, epochs=1, verbose=1, callbacks=[], n_jobs=1\n)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Neural network architecture\n### I don't know about you, but I think that observing the insides of a neural network is pretty cool. Here's mine."},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_model(model.get_model().model,rankdir='TB')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv(f'../input/cat-in-the-dat-ii/sample_submission.csv')\nsubmission['target'] = test_prob\nsubmission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}