{"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 json\nimport tensorflow\nimport matplotlib.pyplot as plt\nimport datetime as datetime\nfrom datetime import timedelta, date\nimport seaborn as sns\nimport matplotlib.cm as CM\n%matplotlib inline\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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout,GRU\nfrom sklearn.linear_model import LinearRegression, SGDRegressor, LogisticRegression\nfrom sklearn.preprocessing import StandardScaler\nfrom xgboost import XGBRegressor\nfrom xgboost import plot_importance\nfrom sklearn.model_selection import train_test_split\n\n\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('../input/train.csv')\ntest_data = pd.read_csv('../input/test.csv',dtype={'fullVisitorId': str})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0146292d6713a6a4f7d6a9abe5ff9abef28f7b81"},"cell_type":"code","source":"submission = test_data[['fullVisitorId']].copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0787414dc0efad7e5ea33f7226d1e82c33bca70"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f8280ee8b59bf1bad973cf774fac878e9373787"},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ab28cfb7f3c68f0c65ed1be0cd53e66b9ed5105"},"cell_type":"code","source":"train_data[\"revenue\"] = pd.DataFrame(train_data.totals.apply(json.loads).tolist())[[\"transactionRevenue\"]].fillna(0)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1082026610f53b52e7e5e575a6e503c6bd5564c2"},"cell_type":"code","source":"train_data['date'] = train_data['date'].astype('str')\ntest_data['date'] = test_data['date'].astype('str')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4830fc7b0db044ee7970bdf25990ebfaa41fa83f"},"cell_type":"code","source":"tmp_geo_df_train = pd.DataFrame(train_data.geoNetwork.apply(json.loads).tolist())[[\"continent\",\"subContinent\",\"country\",\"city\"]]\ntmp_geo_df_test = pd.DataFrame(test_data.geoNetwork.apply(json.loads).tolist())[[\"continent\",\"subContinent\",\"country\",\"city\"]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d28f3be5c3bb3e2c3a32c7812ddf24e0de08d1aa"},"cell_type":"code","source":"tmp_device_df_train = pd.DataFrame(train_data.device.apply(json.loads).tolist())[[\"deviceCategory\",\"isMobile\"]]\ntmp_device_df_test = pd.DataFrame(test_data.device.apply(json.loads).tolist())[[\"deviceCategory\",\"isMobile\"]]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8335051f384eef34f51969a050bde31e5b4cc42f"},"cell_type":"code","source":"tmp_totals_df_train =  pd.DataFrame(train_data.totals.apply(json.loads).tolist())[['bounces', 'hits', 'newVisits', 'pageviews','visits']]\ntmp_totals_df_test =  pd.DataFrame(test_data.totals.apply(json.loads).tolist())[['bounces', 'hits', 'newVisits', 'pageviews','visits']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ea625487f3ae5428a60094e5ffdbd1c9682bfe9"},"cell_type":"code","source":"tmp_traffic_source_df_train = pd.DataFrame(train_data.trafficSource.apply(json.loads).tolist())[[ 'isTrueDirect', 'medium', 'source']]\ntmp_traffic_source_df_test = pd.DataFrame(test_data.trafficSource.apply(json.loads).tolist())[[ 'isTrueDirect', 'medium', 'source']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1200e647e5b9bd19e1aaaa0b65fb845d65a19b4"},"cell_type":"code","source":"train_data.drop(['device','geoNetwork','trafficSource','totals',],axis = 1,inplace=True)\ntest_data.drop(['device','geoNetwork','trafficSource','totals',],axis = 1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3089613f201d19fb53ee6ed5722b73185fea77ac"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc6747f1c65194a6ead356252387445ceab6d2e6"},"cell_type":"code","source":"train_data  = train_data.join(tmp_geo_df_train,how = \"outer\")\ntrain_data  = train_data.join(tmp_device_df_train,how = \"outer\")\ntrain_data  = train_data.join(tmp_traffic_source_df_train,how = \"outer\")\ntrain_data  = train_data.join(tmp_totals_df_train,how = \"outer\")\ntmp_device_df_train,tmp_geo_df_train,tmp_totals_df_train,tmp_traffic_source_df_train = None,None,None,None\ntest_data  = test_data.join(tmp_geo_df_test,how = \"outer\")\ntest_data  = test_data.join(tmp_device_df_test,how = \"outer\")\ntest_data  = test_data.join(tmp_traffic_source_df_test,how = \"outer\")\ntest_data  = test_data.join(tmp_totals_df_test,how = \"outer\")\ntmp_device_df_test,tmp_geo_df_test,tmp_totals_df_test,tmp_traffic_source_df_test = None,None,None,None\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0e4feb6703c12c8fff717f6ba67da3aa989abb4"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"058916784b8d6297a7c93ca15da46230be81414a"},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"910f82ddc7c991ebc91c50d5086a24292f7ac3fe"},"cell_type":"code","source":"train_data.drop(['date','fullVisitorId','sessionId','visitId','visitStartTime','source','city','country'],axis = 1,inplace=True)\ntest_data.drop(['date','fullVisitorId','sessionId','visitId','visitStartTime','source','city','country'],axis = 1,inplace=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"691fe6d6fd323ce6e232a43b3e141d3621335d5f"},"cell_type":"code","source":"train_data.isTrueDirect.fillna(False,inplace=True)\ntrain_data.newVisits.fillna(0,inplace=True)\ntrain_data.bounces.fillna(0,inplace=True)\ntrain_data.pageviews.fillna(0,inplace=True)\ntest_data.isTrueDirect.fillna(False,inplace=True)\ntest_data.newVisits.fillna(0,inplace=True)\ntest_data.bounces.fillna(0,inplace=True)\ntest_data.pageviews.fillna(0,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a35b73651f6529d9c6f917126fa9dd67ebcfccdb"},"cell_type":"code","source":"train_data.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b67adfb6e5ad94f5fdeb96af56e9910898cd70c3"},"cell_type":"code","source":"all_data = pd.concat((train_data,test_data))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d07dc765516530075a15cdedccb9a9f8c0d7a4ca"},"cell_type":"code","source":"categorical_columns = ['channelGrouping','deviceCategory','subContinent','socialEngagementType',\n                      'medium','isTrueDirect','isMobile','bounces','newVisits','continent']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7abb5b97ca7b8c43194f06764b37daa53dcc2bde"},"cell_type":"code","source":"for column in categorical_columns:\n    train_data[column] = train_data[column].astype('category', categories = all_data[column].unique())\n    test_data[column] = test_data[column].astype('category', categories = all_data[column].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94b8641959c40f1fd21552329a8ba6b4fa28f01d"},"cell_type":"code","source":"train_data = pd.get_dummies(train_data,columns=categorical_columns)\ntest_data = pd.get_dummies(test_data,columns=categorical_columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8bc2f0e41b40bfd9a27dd1ba56ae909fce4dd078"},"cell_type":"code","source":"train_data.shape,test_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29e883ef29211f40bdf4fe0ce9713f878a2ac4a0"},"cell_type":"code","source":"train_data.revenue = train_data.revenue.astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d9c8d7735a0190d7f87b806682d0d9706274fe0"},"cell_type":"code","source":"train_data.revenue = np.log1p(train_data.revenue)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b53091c5eca3326b42a9000fef2fb6533132f0ae"},"cell_type":"code","source":"X = train_data.drop('revenue',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3afa8ee478e08d8e0598ba71242bfeca4169135","scrolled":false},"cell_type":"code","source":"Y = train_data.revenue","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a4a1163cf9e97d354d96eb924580ffc0fea6b60"},"cell_type":"code","source":"X.hits = X.hits.astype('uint8')\nX.pageviews = X.pageviews.astype('uint8')\nX.visits = X.visits.astype('uint8')\ntest_data.hits = test_data.hits.astype('uint8')\ntest_data.pageviews = test_data.pageviews.astype('uint8')\ntest_data.visits = test_data.visits.astype('uint8')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11a278732a096698dcf3e16753d103b68da7f800"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"270e396f8239b0a772324eb3378e6244486e4968"},"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1a2c6e54c5202be26ff011236c431aa62620ff6"},"cell_type":"code","source":"model = XGBRegressor(n_estimators=100,\n                    learning_rate = .1,\n                    max_depth = 6,\n                    random_state=42,\n                    n_jobs = -1,\n                    early_stopping_rounds=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec7f04a705d250be5035f6e7c0315880d4e93c7b"},"cell_type":"code","source":"model.fit(\n    X_train, \n    y_train, \n    eval_metric=\"rmse\",\n    eval_set=[(X_test, y_test)],\n    verbose=True, )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c3d370cbdc8995ac36840af240d5bde49f09eb94"},"cell_type":"code","source":"predictions = model.predict(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0126a7675e87ec503a53b0befa0c8d54e6d27dbe"},"cell_type":"code","source":"predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d80ff6462b98b298928723016ba42f236ce3de77"},"cell_type":"code","source":"figsize=(10,10)\nfig, ax = plt.subplots(1,1,figsize=figsize)\nplot_importance(model, ax=ax,height = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5a144b86eb18871827c144b207ff5abec3f4bdaf"},"cell_type":"code","source":"submission['fullVisitorId'] = submission['fullVisitorId']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0eb69b54c3b677835a513a68ef37659f84292b1f"},"cell_type":"code","source":"submission['PredictedLogRevenue'] = predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"098ef8231f264c60dbef571c214461335c03e634"},"cell_type":"code","source":"submission[\"PredictedLogRevenue\"] = np.expm1(submission[\"PredictedLogRevenue\"])\nsubmission[\"PredictedLogRevenue\"] = submission[\"PredictedLogRevenue\"].apply(lambda x : 0.0 if x < 0 else x)\nsubmission[\"PredictedLogRevenue\"] = submission[\"PredictedLogRevenue\"].fillna(0.0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68d6819505a213158874b28d2ed790f4c296a453"},"cell_type":"code","source":"submission_sum = submission[['fullVisitorId', 'PredictedLogRevenue']].groupby('fullVisitorId').sum().reset_index()\nsubmission_sum[\"PredictedLogRevenue\"] = np.log1p(submission_sum[\"PredictedLogRevenue\"])\nsubmission_sum.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68aa04328483f49db40a8819aecfad06d0c2114a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3886d6bd324d148e8f11b20ef391015dd4e1d502"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"716063ca13d78016eefb4c02b299eacf70e8ea73"},"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}