{"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 matplotlib.pyplot as plt # data Visualization\nimport seaborn as sns # data Visualization\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":"#Importing Datasets\ndf_train = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv')\ndf_test = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')\ndf_sub = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets check the target variable first"},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x=df_train['target'], data=df_train, palette='seismic')\nplt.title(\"TARGET DISTRIBUTION\", fontsize = 20)\nplt.xlabel(\"Target Values\", fontsize = 15)\nplt.ylabel(\"Count\", fontsize = 15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Form above graph we can see Target values are not balanced"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.sort_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = df_train['target']\ntest_id = df_test['id']\ndf_train.drop(['target', 'id'], axis=1, inplace=True)\ndf_test.drop('id', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Data Preprossing and Features Engeneering"},{"metadata":{"trusted":true},"cell_type":"code","source":"cat_feat_to_encode = df_train.columns.tolist()\nsmoothing=0.20\nimport category_encoders as ce\noof = pd.DataFrame([])\nfrom sklearn.model_selection import StratifiedKFold\nfor tr_idx, oof_idx in StratifiedKFold(n_splits=5, random_state= 1024, shuffle=True).split(df_train, y_train):\n    ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)\n    ce_target_encoder.fit(df_train.iloc[tr_idx, :], y_train.iloc[tr_idx])\n    oof = oof.append(ce_target_encoder.transform(df_train.iloc[oof_idx, :]), ignore_index=False)\nce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)\nce_target_encoder.fit(df_train, y_train)\ndf_train = oof.sort_index()\ndf_test = ce_target_encoder.transform(df_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets take our matrix of features of ml model"},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = df_train.iloc[:,:].values\nx_test = df_test.iloc[:,:].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import lightgbm as lgb\nclassifier = lgb.LGBMClassifier(**{\n                    'learning_rate': 0.05,\n                    'feature_fraction': 0.1,\n                    'min_data_in_leaf' : 12,\n                    'max_depth': 3,\n                    'reg_alpha': 1,\n                    'reg_lambda': 1,\n                    'objective': 'binary',\n                    'metric': 'auc',\n                    'n_jobs': -1,\n                    'n_estimators' : 5000,\n                    'feature_fraction_seed': 42,\n                    'bagging_seed': 42,\n                    'boosting_type': 'gbdt',\n                    'verbose': 1,\n                    'is_unbalance': True,\n                    'boost_from_average': False})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classifier.fit(x_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = classifier.predict_proba(x_test)[:,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Sumbmission the result\ndf_sub = pd.DataFrame()\ndf_sub['id'] = test_id\ndf_sub['target'] = y_pred\ndf_sub.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sub.head(20)","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":1}