{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport matplotlib\n%matplotlib inline\n\nimport seaborn as sns\nsns.set()\n\nimport lightgbm as lgb\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a571188677ba2eaa1ed5f2a8a63d99149842d82e"},"cell_type":"code","source":"import os\nos.listdir(\"../input/gstoreextractedjson/\")","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/gstoreextractedjson/train_cleaned.csv\", \n                    dtype={\"fullVisitorId\": \"str\", \"sessionId\": \"str\"}, \n                    parse_dates=[\"date\"])\ntest = pd.read_csv(\"../input/gstoreextractedjson/test_cleaned.csv\", \n                    dtype={\"fullVisitorId\": \"str\", \"sessionId\": \"str\"}, \n                    parse_dates=[\"date\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39388b136f9da5f348474abf4b40e064b283adf6"},"cell_type":"code","source":"train.shape, test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c63e6d7e5360168676392aef50ba03c128885b3c"},"cell_type":"code","source":"target = train.loc[:,'totals.transactionRevenue'].values\ndel train[\"totals.transactionRevenue\"]\ntrain.shape, test.shape, target.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"599238ce35409ea2234eaa061b0a20c9b131e5ed"},"cell_type":"code","source":"num_cols = ['totals.bounces', \n            'totals.hits', \n            'totals.newVisits', \n            'totals.pageviews']\ncat_cols = ['channelGrouping', \n            'device.browser', \n            'device.deviceCategory', \n            'device.operatingSystem', \n            'geoNetwork.city', \n            'geoNetwork.continent', \n            'geoNetwork.country', \n            'geoNetwork.metro', \n            'geoNetwork.networkDomain', \n            'geoNetwork.region', \n            'geoNetwork.subContinent', \n            'trafficSource.adContent', \n            'trafficSource.adwordsClickInfo.adNetworkType', \n            'trafficSource.adwordsClickInfo.gclId', \n            'trafficSource.adwordsClickInfo.slot', \n            'trafficSource.campaign', \n            'trafficSource.keyword', \n            'trafficSource.medium', \n            'trafficSource.referralPath', \n            'trafficSource.source', \n            'device.isMobile', \n            'trafficSource.adwordsClickInfo.isVideoAd', \n            'trafficSource.adwordsClickInfo.page', \n            'trafficSource.isTrueDirect']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e51d559e59fd824f56c55314c211413525bbae34"},"cell_type":"code","source":"features = num_cols+cat_cols\nX_train, X_val, y_train, y_val = train_test_split(train[features], target, test_size=0.2, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2812dbb56178b3fb26673b2bb9cc290fb7a86b96"},"cell_type":"code","source":"def rmse(y_true, y_pred):\n    return round(np.sqrt(mean_squared_error(y_true, y_pred)), 5)\n\ndef run_lgb(X_train, y_train, X_val, y_val, X_test):\n    data_train = lgb.Dataset(X_train, label=y_train)\n    data_val = lgb.Dataset(X_val, label=y_val)\n    param = {\n         'objective':'regression',\n         'metric': 'rmse',\n         'learning_rate':0.005,\n         'num_leaves':40,\n         'min_data_in_leaf':150,\n         'max_depth':10,\n         'bagging_fraction':0.6,\n         'feature_fraction':0.6,\n         'bagging_frequency': 6,\n         'verbosity':-1,\n         'random_state': 42}\n    model = lgb.train(param, data_train, valid_sets=[data_train, data_val], num_boost_round=5000, early_stopping_rounds=100,\n                  verbose_eval=200)\n    pred_y_test = model.predict(X_test, num_iteration=model.best_iteration)\n    \n    return pred_y_test, model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b3fe67057c53af547e4923f91266a0a46f02691"},"cell_type":"code","source":"pred_test, model = run_lgb(X_train, y_train, X_val, y_val, test[features])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df70a0544c6ec8f2a43b5026190be2c9e2362329"},"cell_type":"code","source":"pred_test = model.predict(test[num_cols + cat_cols], num_iteration=model.best_iteration)\npred_test[pred_test<0]=0\nsubmission = test[['fullVisitorId']].copy()\nsubmission.loc[:,'PredictedLogRevenue'] = pred_test\ngrouped_test = submission[['fullVisitorId', 'PredictedLogRevenue']].groupby('fullVisitorId').sum().reset_index()\ngrouped_test.to_csv(\"submit.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"401bd89a3d3b9bb32f73ceb1f1ecc3bdaba35b7e"},"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.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}