{"cells":[{"metadata":{"trusted":true,"_uuid":"9bf64ce027e483ba81b814f35ef7d30a574f811f","collapsed":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd \n\npd.set_option('precision', 5)\npd.set_option('display.float_format', lambda x: '%.5f' % x)\n\n# Input data files are available in the \"../input/\" directory.\n\n\nimport os\nprint(os.listdir(\"../input\"))\n","execution_count":10,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f3b94f005cc51b4544d27fe367cfc8cfe662889","collapsed":true},"cell_type":"code","source":"tr = pd.read_csv('../input/train.csv')\nte = pd.read_csv('../input/test.csv')\nprint('train data shape is :', tr.shape)\nprint('test data shape is :', te.shape)","execution_count":18,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"feb8f07c6b097d965501a08a96ffbac36670a5f7"},"cell_type":"code","source":"data = pd.concat([tr, te], axis=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"dd9db01ba2a11a97bfc55e333c55c23ac9c2a588"},"cell_type":"code","source":"data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9faadd6b22dfc4ec961f3c8975c2f9082c0823cf"},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nimport lightgbm as lgb\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"2edb7f05e245feba463de6c85705d0771a68ea55"},"cell_type":"code","source":"data.activation_date = pd.to_datetime(data.activation_date)\ntr.activation_date = pd.to_datetime(tr.activation_date)\n\ndata['day_of_month'] = data.activation_date.apply(lambda x: x.day)\ndata['day_of_week'] = data.activation_date.apply(lambda x: x.weekday())\n\ntr['day_of_month'] = tr.activation_date.apply(lambda x: x.day)\ntr['day_of_week'] = tr.activation_date.apply(lambda x: x.weekday())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e59cf16bd100828f1bd14a31514981ff6d1009ea"},"cell_type":"code","source":"data['char_len_title'] = data.title.apply(lambda x: len(str(x)))\ndata['char_len_desc'] = data.description.apply(lambda x: len(str(x)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6e3ccda0af9b4972592d556ec326f9ab8c52c898"},"cell_type":"code","source":"agg_cols = ['region', 'city', 'parent_category_name', 'category_name',\n            'image_top_1', 'user_type','item_seq_number','day_of_month','day_of_week'];\nfor c in tqdm(agg_cols):\n    gp = tr.groupby(c)['deal_probability']\n    mean = gp.mean()\n    std  = gp.std()\n    data[c + '_deal_probability_avg'] = data[c].map(mean)\n    data[c + '_deal_probability_std'] = data[c].map(std)\n\nfor c in tqdm(agg_cols):\n    gp = tr.groupby(c)['price']\n    mean = gp.mean()\n    data[c + '_price_avg'] = data[c].map(mean)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c37177a602e778804cdac3179eda707570850983"},"cell_type":"code","source":"cate_cols = ['city',  'category_name', 'user_type',]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"733f9b81106ac70cf28ced16676f4312d9eb582d"},"cell_type":"code","source":"for c in cate_cols:\n    data[c] = LabelEncoder().fit_transform(data[c].values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b3f73435080fd69347233e349d6bf99e611fc42b"},"cell_type":"code","source":"new_data = data.drop(['user_id','description','image','parent_category_name','region',\n                      'item_id','param_1','param_2','param_3','title'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"7da2b81f7abfc633f39cce376208f725ac258174"},"cell_type":"code","source":"import gc\ndel data\ndel tr\ndel te\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1a493276d549edf624bf2d38bca2c8338a49a6b6"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"a9c3d6f0f454d96a71d568e7495f267b849b1625"},"cell_type":"code","source":"X = new_data.loc[new_data.activation_date<=pd.to_datetime('2017-04-07')]\nX_te = new_data.loc[new_data.activation_date>=pd.to_datetime('2017-04-08')]\n\ny = X['deal_probability']\nX = X.drop(['deal_probability','activation_date'],axis=1)\nX_tr, X_va, y_tr, y_va = train_test_split(X, y, test_size=0.2, random_state=2018)\nX_te = X_te.drop(['deal_probability','activation_date'],axis=1)\n\nprint(X_tr.shape, X_va.shape, X_te.shape)\n\n\ndel X\ndel y\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"09b544ac428d0b1cca66097c6e31e68e60b7269e"},"cell_type":"code","source":"# Create the LightGBM data containers\ntr_data = lgb.Dataset(X_tr, label=y_tr, categorical_feature=cate_cols)\nva_data = lgb.Dataset(X_va, label=y_va, categorical_feature=cate_cols, reference=tr_data)\ndel X_tr\ndel X_va\ndel y_tr\ndel y_va\ngc.collect()\n\n# Train the model\nparameters = {\n    'task': 'train',\n    'boosting_type': 'gbdt',\n    'objective': 'regression',\n    'metric': 'rmse',\n    'num_leaves': 31,\n    'learning_rate': 0.05,\n    'feature_fraction': 0.9,\n    'bagging_fraction': 0.8,\n    'bagging_freq': 5,\n    'verbose': 50\n}\n\n\nmodel = lgb.train(parameters,\n                  tr_data,\n                  valid_sets=va_data,\n                  num_boost_round=2000,\n                  early_stopping_rounds=120,\n                  verbose_eval=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d97f48b8de82d9eb679d36a8a906180fef088582"},"cell_type":"code","source":"y_pred = model.predict(X_te)\nsub = pd.read_csv('../input/sample_submission.csv')\nsub['deal_probability'] = y_pred\nsub['deal_probability'].clip(0.0, 1.0, inplace=True)\nsub.to_csv('lgb_with_mean_encode.csv', index=False)\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f1a09cd03382bc4fda01fb71f1323818914022bc"},"cell_type":"code","source":"lgb.plot_importance(model, importance_type='gain', figsize=(10,20))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"4fcca17a2d6493d4b514ad64ccb288473adbb618"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}