{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom catboost import CatBoostClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split, cross_val_predict, StratifiedKFold\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.preprocessing import OrdinalEncoder, StandardScaler\nimport category_encoders as ce","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv', index_col='id')\ntest_df = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv', index_col='id')\ny_true = train_df['target'].squeeze().copy()\nidx = train_df.shape[0] # We should know where to cut train/test from full\nfull_df = pd.concat([train_df.drop('target', axis=1), test_df])\ndel train_df, test_df\nfull_df['n_of_mis_values'] = full_df.isna().sum(axis=1)\nfull_df['%_of_mis_values'] = (full_df['n_of_mis_values']/(len(full_df.columns) - 1))\nctb_X = full_df.copy()\n\ncat_feat_idxs = np.where(ctb_X.dtypes == 'object')[0]\nnum_feat_idxs = np.where(ctb_X.dtypes != 'object')[0]\n\nbin_cols = [f'bin_{i}' for i in range(0,5)]\nnom_cols = [f'nom_{i}' for i in range(0,10)]\nord_cols = [f'nom_{i}' for i in range(0,6)]\n\nctb = CatBoostClassifier(random_seed=17,\n                         silent=True,\n                         task_type=\"GPU\",\n                         border_count=254)\n\nlogit = LogisticRegression(random_state=2020, solver='lbfgs', max_iter=10000)\nrfc = RandomForestClassifier(random_state=2020, n_estimators=200, max_depth=30)\nscaler = StandardScaler()\nskf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2020)\n\n[ctb_X[col].fillna('MISSING_STRING', inplace=True) for col in ctb_X.columns[cat_feat_idxs]];\n[ctb_X[col].fillna(0, inplace=True) for col in ctb_X.columns[num_feat_idxs]];\nfull_df[bin_cols] = full_df[bin_cols].fillna(0)\nfull_df['day'] = full_df['day'].fillna(0)\nfull_df['month'] = full_df['month'].fillna(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_df[bin_cols] = full_df[bin_cols].replace({'F': 0, 'T': 1, 'N': 0, 'Y': 1})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"encoder = ce.TargetEncoder(cols=['nom_0', 'nom_1', 'nom_2',\n       'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9', 'ord_0',\n       'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5'], smoothing=0.2)\n\nencoder.fit(full_df[:idx], y_true)\n\nX = encoder.transform(full_df)\nX = scaler.fit_transform(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nmodels = [logit, rfc]\n\nmeta_mtrx = np.zeros(shape=(X[:idx].shape[0], len(models)))\nmeta_mtrx_test = np.zeros(shape=(X[idx:].shape[0], len(models)))\n\nfor i, model in enumerate(models):\n    meta_mtrx[:, i] = cross_val_predict(model, X=X[:idx], y=y_true, cv=skf)\n    model.fit(X[:idx], y_true)\n    meta_mtrx_test[:, i] = model.predict(X[idx:])\nmeta_mtrx","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_full = np.vstack([meta_mtrx, meta_mtrx_test])\nctb_X = pd.concat([ctb_X, pd.DataFrame(meta_full, columns=['meta_lr', 'meta_rfc'])], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nctb.fit(ctb_X[:idx], y_true, cat_features=cat_feat_idxs)\ny_pred = ctb.predict_proba(ctb_X[idx:])[:, 1]\nsubmission = pd.read_csv('../input/cat-in-the-dat-ii/sample_submission.csv', index_col='id')\nsubmission['target'] = y_pred\nsubmission.to_csv('catboost_submission.csv')","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}