{"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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport xgboost as xgb\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import OrdinalEncoder\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":{},"cell_type":"markdown","source":"Functions"},{"metadata":{"trusted":true},"cell_type":"code","source":"def is_null(df):\n    dic={}\n    dic['count'] = df.isnull().sum()\n    dic['fraction'] = df.isnull().sum()/len(train)\n    return pd.DataFrame(dic)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Importing Data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv')\ntest = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"EDA (train)"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\nenc = OrdinalEncoder()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.bin_0.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.bin_2.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.bin_3.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.bin_4.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.bin_1.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.ord_0.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.ord_1.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.ord_2.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.ord_3.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.ord_4.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.ord_5.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"is_null(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#bin_3 ---- 'Y', 'N'\n#bin_4 ---- 'Y', 'N'\n#ord_1 ---- 'Novice', 'Expert', 'Contributor' , 'Master','Grandmaster'\n#ord_2 ---- 'Freezing', 'Warm', 'Cold', 'Boiling Hot', 'Hot', 'Lava Hot'\n#ord_3 ---- 'n', 'a','m','c','h','o','b','e','k','i','d','f','g','j','l'\n#ord_4 ----'N' ,'P' ,'Y'  ,'A'  ,'R'  ,'U' ,'M'  ,'X'   ,'C'   ,'H'  ,'Q'   ,'T'  ,'O'  ,'B'  ,'E'  ,'K'  ,'I' ,'D'  ,'F'  ,'W' ,'Z'  ,'S' ,'G','V','J','L'\n#ord_5 ----- 'Fl','DN','Sz' ........","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As ord_3, ord_4,ord_5 have no apparent order I'll treat them like categorical variables. As I don't know what most of the variables represent I'm just going to do some basic cleaning and fit a model and see what happens."},{"metadata":{},"cell_type":"markdown","source":"EDA (test)"},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.describe()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Label Encoding the data"},{"metadata":{"trusted":true},"cell_type":"code","source":"Y = train['target']\ntrain =train.astype('category')\ntrain['target'] = Y\n\ntest[\"target\"] = -1\ndata = pd.concat([train, test]).reset_index(drop=True)\n\nfeatures = [x for x in train.columns if x not in [\"id\", \"target\",'ord_1','ord_2']]\n\nfor feat in features:\n    lbl_enc = LabelEncoder()\n    data[feat] = lbl_enc.fit_transform(data[feat].fillna(\"-1\").astype(str))\n    \n\no_enc = OrdinalEncoder()\ndata[['ord_1','ord_2']] = o_enc.fit_transform(data[['ord_1','ord_2']].fillna(\"-1\").astype(str))\n\ntest = data[data.target == -1]\ntrain = data[data.target != -1]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Create train and test sets"},{"metadata":{"trusted":true},"cell_type":"code","source":"target = train['target']\ntrain.drop(['id','target'],axis = 1,inplace = True)\n\nfrom sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(train,target,train_size = .85,random_state = 42,stratify = Y)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Training classifier"},{"metadata":{"trusted":true},"cell_type":"code","source":"xg_reg1 = xgb.XGBClassifier( objective='reg:logistic',learning_rate = 0.1,\n                max_depth = 20, reg_alpha = 15, n_estimators = 1000,booster = 'dart',\n            tree_method = 'hist')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xg_reg1.fit(x_train,y_train,eval_set= [(x_train,y_train), (x_test, y_test)],\n    eval_metric=\"auc\",early_stopping_rounds=5,verbose=True)\nxg_reg1.save_model('xgb.model')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Predicting values for submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"key = test['id'].reset_index(drop = True)\ntest.drop(['id','target'],axis = 1,inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"key.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = pd.Series(xg_reg1.predict_proba(test)[:,-1])\n\nsub = pd.concat([key,pred],axis = 1,ignore_index = True)\n\nsub.columns = ['id','target']\n \nsub.to_csv('submission.csv',index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}