{"cells":[{"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)\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.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv', index_col='id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for col in train_data.columns:\n    print(col, train_data[col].nunique())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Drop columns with high cardinality"},{"metadata":{"trusted":true},"cell_type":"code","source":"categorical_columns = [col for col in train_data.columns if train_data[col].dtype == object]\ncolumns_with_high_cardinality = [col for col in categorical_columns if train_data[col].nunique() > 30]\n\ntrain_data.drop(columns_with_high_cardinality, axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ny = train_data['target']\ntrain_data.drop(['target'], axis=1, inplace=True)\ntrain_X, valid_X, train_y, valid_y = train_test_split(train_data, y, train_size=0.8, test_size=0.2, random_state=0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"Impute missing values"},{"metadata":{"trusted":true},"cell_type":"code","source":"for col in train_X.columns:\n    mode = train_X[col].mode()[0]\n    train_X[col].fillna(mode, inplace=True)\n    valid_X[col].fillna(mode, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_X","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"OneHot-encode columns with low cardinality"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\n\ncolumns_oh_encode = [col for col in train_X.columns if train_X[col].dtype == object]\n\noh_encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)\n\ncolumn_transformer = ColumnTransformer(transformers=[\n    ('onehot', oh_encoder, columns_oh_encode)\n])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"XGBClassifier"},{"metadata":{"trusted":true},"cell_type":"code","source":"from xgboost import XGBClassifier\nfrom sklearn.pipeline import Pipeline\n\nmodel = XGBClassifier(n_estimators=100, random_state=0, n_jobs=-1)\n\npipeline = Pipeline(steps=[\n    ('transformer', column_transformer),\n    ('model', model)\n])\n\npipeline.fit(train_X, train_y)\nprediction = pipeline.predict_proba(valid_X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\nscore = roc_auc_score(valid_y, prediction[:,1])\nprint(score)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"Predict the test data."},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv', index_col='id')\n\ntest_data.drop(columns_with_high_cardinality, axis=1, inplace=True)\n\n#\nfor col in train_data.columns:\n    mode = train_data[col].mode()[0]\n    train_data[col].fillna(mode, inplace=True)\n    test_data[col].fillna(mode, inplace=True)\n\n#\npipeline.fit(train_data, y)\nprediction_test = pipeline.predict_proba(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output = pd.DataFrame({\n    'id': test_data.index,\n    'target': prediction_test[:,1]\n})\noutput.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":1}