{"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)\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\"))\nimport json\nfrom pandas.io.json import json_normalize\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"034802f0f52b45867c93b70bad974c4b566703a8"},"cell_type":"code","source":"def load_df(csv_path, nrows=None):\n    USE_COLUMNS = [\n        'channelGrouping', 'date', 'device', 'fullVisitorId', 'geoNetwork',\n        'socialEngagementType', 'totals', 'trafficSource', 'visitId',\n        'visitNumber', 'visitStartTime', 'customDimensions'\n        #'hits'\n    ]\n    JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    \n    df = pd.read_csv(csv_path, \n                     converters={column: json.loads for column in JSON_COLUMNS}, \n                     dtype={'fullVisitorId': 'str'}, # Important!!\n                     nrows=nrows, usecols=USE_COLUMNS)\n    \n    for column in JSON_COLUMNS:\n        column_as_df = json_normalize(df[column])\n        column_as_df.columns = [f\"{column}.{subcolumn}\" for subcolumn in column_as_df.columns]\n        df = df.drop(column, axis=1).merge(column_as_df, right_index=True, left_index=True)\n    print(f\"Loaded {os.path.basename(csv_path)}. Shape: {df.shape}\")\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49becfef9df38d8c624c9beb63516e43d4bfba1c"},"cell_type":"code","source":"%%time\ntrain_df = load_df(\"../input/train_v2.csv\")\ntest_df = load_df(\"../input/test_v2.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb3925b993ec872982ed4312aa47443365bfcd85"},"cell_type":"code","source":"print(train_df.columns.difference(test_df.columns))\nprint(test_df.columns.difference(train_df.columns))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"440f1b1d5ae9c3eca8f4b688216b82a22424a370"},"cell_type":"code","source":"train_df = train_df.drop(labels=['trafficSource.campaignCode'],axis=1)\ntrain_df = train_df.drop(labels=['customDimensions','totals.totalTransactionRevenue'],axis=1)\ntest_df = test_df.drop(labels=['customDimensions','totals.transactionRevenue','totals.totalTransactionRevenue'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"01389cf426a5c531a072d26812b13afc3f76ab94"},"cell_type":"code","source":"#Converting the datatype of date field\ntrain_df['date']= pd.to_datetime(train_df['date'],format='%Y%m%d')\ntest_df['date']= pd.to_datetime(test_df['date'],format='%Y%m%d')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"e1fcaa062f7e5206c7e98193e13c3d12df4269a6"},"cell_type":"code","source":"#We need to predict totals.transactionRevenue. Lets explore that variable\ntrain_df['totals.transactionRevenue'] = train_df['totals.transactionRevenue'].astype('float')\nprint('Total records: ', len(train_df), 'customers doesnt contribute for revenue: ',train_df['totals.transactionRevenue'].isna().sum(), \\\n      'customers contributing revenue: ',len(train_df) - train_df['totals.transactionRevenue'].isna().sum())\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"892e62cbc9b34abf1d61966d6a5b3db5a762483e"},"cell_type":"markdown","source":"Purely an imbalanced dataset"},{"metadata":{"trusted":true,"_uuid":"b0768d3df6b2b1e8322a471de13a649be513e1c6"},"cell_type":"code","source":"#Lets start with data cleaning\n#Find any unique value columns\nprint('Features with one unique values are :')\nprint(train_df.columns[train_df.nunique()==1])\nnew_df = train_df.drop(labels=train_df.columns[train_df.nunique()==1],axis=1)\ntest_df = test_df.drop(labels=train_df.columns[train_df.nunique()==1],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true,"scrolled":false,"_uuid":"ac909d267b305f2a1043a37df7ca4ac823a547fd"},"cell_type":"code","source":"print(new_df.shape)\npd.options.display.max_columns=new_df.shape[1]\nprint(new_df.info())\nnew_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a530c136aae357902f696b41fd0e781185f4c27f"},"cell_type":"markdown","source":"Based on Initial glance of data we can drop following fields while exploring\n* sessionId - Unique number for a session\n* visitId - Nth visit fullVisitorId is visiting the store \n* visitStartTime - Time of Visit \n"},{"metadata":{"trusted":true,"_uuid":"834e8754321c5ee0d3d89a772c4dc2575c5cea22"},"cell_type":"code","source":"\ngeofields = ['geoNetwork.city','geoNetwork.continent','geoNetwork.country','geoNetwork.metro','geoNetwork.networkDomain','geoNetwork.region','geoNetwork.subContinent']\n\nfor fields in geofields:\n    temp_df = new_df.groupby(by=fields).size().sort_values(ascending=False).head(10)\n    print(temp_df)\n    print('*'*30)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44947e39f2a93925e8e102f550533d4c6140ee89"},"cell_type":"code","source":"temp = new_df.groupby(by=['fullVisitorId']).agg({'visitId':'count','totals.transactionRevenue':'sum'})\nprint(temp.corr())\ntemp.plot(kind='scatter',x='visitId',y='totals.transactionRevenue')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c64125105704f9f313f03e6952e16885e06092b1"},"cell_type":"markdown","source":"No of visits doesn't have much relation with transactionRevenue"},{"metadata":{"trusted":true,"_uuid":"5a557540d71c12e507bda3b30618a65ad0e10f84"},"cell_type":"code","source":"#Lets analyze how the visits and transactionRevenue trend with date\nplot_df = new_df.groupby(by=['date']).agg({'fullVisitorId':'count','totals.transactionRevenue':'sum'}).reset_index()\n\nfig = plt.figure(figsize=(15,8))\nplt.subplot(2,1,1)\nplt.plot(pd.to_datetime(plot_df['date']),plot_df['totals.transactionRevenue'])\nplt.ylabel('Revenue')\nplt.xticks(rotation=90)\n\nplt.subplot(2,1,2)\nplt.plot(pd.to_datetime(plot_df['date']),plot_df['fullVisitorId'])\nplt.ylabel('Visits')\nplt.xticks(rotation=90);\n\nfor ax in fig.axes:\n    plt.sca(ax)\n    plt.xticks(rotation=90)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28b2f29cfa863c2857396e0952504fcddfc85f7b"},"cell_type":"markdown","source":"Numer of visits drasitically increases from October and reduces in December. But that didnt contribute much on revenue. As only one year of data available, we cannot identify any YoY pattern of data."},{"metadata":{"trusted":true,"_uuid":"f02b5f0b414bf38c727d11f6950f0d5515b3f206"},"cell_type":"code","source":"#Weekly pattern\n\nplot_df = new_df.groupby(by=['date']).agg({'fullVisitorId':'count','totals.transactionRevenue':'sum'})\n\nplot_df = plot_df.resample('W').mean()\n\nplt.figure(figsize=(15,8))\nplt.subplot(2,1,1)\nplt.plot(plot_df.index,plot_df['totals.transactionRevenue'])\nplt.ylabel('Revenue')\n\nplt.subplot(2,1,2)\nplt.plot(plot_df.index,plot_df['fullVisitorId'])\nplt.ylabel('Visits');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6da040337fbb08efea236af862328b9718fedff"},"cell_type":"code","source":"plot_df = new_df.groupby(by=['fullVisitorId']).agg({'channelGrouping':'count','totals.transactionRevenue':'sum'})\n\nplt.figure(figsize=(10,8))\nplt.subplot(2,1,1)\nplt.hist(np.log1p(plot_df[plot_df['totals.transactionRevenue']>0]['totals.transactionRevenue']))\n\nplt.subplot(2,1,2)\nplt.hist(np.log1p(plot_df[plot_df['totals.transactionRevenue']>0]['totals.transactionRevenue']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b2f8d95560074c83fb57361e7ed7fcfb87413718"},"cell_type":"code","source":"#new_df = new_df.drop(labels=['sessionId','visitId','visitStartTime'])\nnew_df.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd11632c1290bf0d8a5dc705bf3c7d39333e796e"},"cell_type":"code","source":"new_df['totals.transactionRevenue'].fillna(value=0,inplace=True)\ntransaction_df = new_df[new_df['totals.transactionRevenue']>0.0]\nnontransaction_df = new_df[new_df['totals.transactionRevenue']<=0]\nprint(transaction_df.shape, nontransaction_df.shape)\n#new_df['totals.transactionRevenue']>0.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"297130e70937993b3fc706526d7d628d134c08be","scrolled":false},"cell_type":"code","source":"def getplot(df1, df2, groupfield):\n    fig =plt.figure(figsize=(15,8))\n    plt.subplot(2,1,1)\n    plot_df = df1.groupby(by=[groupfield])['totals.transactionRevenue'].size().reset_index()\n    plot_df = plot_df.sort_values(by=['totals.transactionRevenue'], ascending=False).head(10)\n    plt1 = plt.bar(plot_df[groupfield],plot_df['totals.transactionRevenue'])\n\n    plt.subplot(2,1,2)\n    plot_df = df2.groupby(by=[groupfield])['totals.transactionRevenue'].size().reset_index()\n    plot_df = plot_df.sort_values(by=['totals.transactionRevenue'], ascending=False).head(10)\n    plt2 = plt.bar(plot_df[groupfield],plot_df['totals.transactionRevenue'])\n    for ax in fig.axes:\n        plt.sca(ax)\n        plt.xticks(rotation=90)\n    #fig.tight_layout()\n    return plt1, plt2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8623cc52322b6ffaaadff2c0f1d88bc1bf58a5ff"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.country');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca63b2f28775bd460a95b514142a14647548621f"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.city');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8634b7f7fe43cc6d2ab849b9eb346bb415ba2e58"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.continent');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be76315e7878ae7cf4342b8b1dd4a69becdd2228"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.metro');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ce14b0cd674e6800f68dc5485c9a628b403c079"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.networkDomain');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cbc1253c62cac7f7f4b5826b3decc0f17225c5a"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.region');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d646ccff394b063ae1d179e8a2af112dce4f55d6"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.subContinent');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a0ab6e1da973d2067df8f7105ff331facd463bc"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'channelGrouping');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6db0b9f9e615779fe28723d4bfe10eab553c754a"},"cell_type":"markdown","source":"Lets drop labels other than city and subcontinent. As city is the lower granular level in geo. As most values in city is 'not available' lets keep another variable subcontinent as an additional field.\n"},{"metadata":{"trusted":true,"_uuid":"36eefb87510913f0a4ee684f420f9eec153dafed"},"cell_type":"code","source":"new_df1 = new_df.drop(labels=['geoNetwork.region','geoNetwork.networkDomain','geoNetwork.metro','geoNetwork.continent','geoNetwork.country','visitId','visitStartTime'],axis=1)\ntest_df1 = test_df.drop(labels=['geoNetwork.region','geoNetwork.networkDomain','geoNetwork.metro','geoNetwork.continent','geoNetwork.country','visitId','visitStartTime'],axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"beb1ad518c62c00abc43a0883f6e358d9edfcb43"},"cell_type":"markdown","source":"Only one year of data is available. With Date or Month we couldn't find any relation. Lets drop it. \ndevice.isMobile is a duplicate variable as the detail is covered in deviceCategory\n"},{"metadata":{"trusted":true,"_uuid":"d70df2e94eb567ded735135bcc5202e06def610b"},"cell_type":"code","source":"new_df1 = new_df1.drop(labels=['date','device.isMobile'], axis=1)\ntest_df1 = test_df1.drop(labels=['date','device.isMobile'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49d795b080e81c52e37c246f1ab814e69c00fdf7"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'device.browser');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ba2fe8cfd712b89cc532e28e6dcabd28591b708f"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'device.deviceCategory');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26ab87260fd69f83c925b27f77749c17e9222e9f"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'device.operatingSystem');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae0281c22cfd3fee69a43b33a054befedc24a783"},"cell_type":"code","source":"transaction_df['trafficSource.adContent'].fillna('NA',inplace=True)\ngetplot(transaction_df,nontransaction_df,'trafficSource.adContent')\n#NA values seems to contribute more for revenue :p","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9aa67ac2cca91adef43b2c45d5235ba62df1600a"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'trafficSource.source')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55a90a34fd8abf7dabee01c98878db7d7acd2955"},"cell_type":"code","source":"new_df1 = new_df1.drop(labels=['trafficSource.adwordsClickInfo.adNetworkType', 'trafficSource.adwordsClickInfo.gclId', 'trafficSource.adwordsClickInfo.page', 'trafficSource.adwordsClickInfo.slot', 'trafficSource.medium', 'trafficSource.medium', 'trafficSource.referralPath','trafficSource.source'],axis=1)\ntest_df1 = test_df1.drop(labels=['trafficSource.adwordsClickInfo.adNetworkType', 'trafficSource.adwordsClickInfo.gclId', 'trafficSource.adwordsClickInfo.page', 'trafficSource.adwordsClickInfo.slot', 'trafficSource.medium', 'trafficSource.medium', 'trafficSource.referralPath','trafficSource.source'],axis=1)\nprint(new_df1.shape)\nnew_df1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70c62329d7dfaf64214457cbc8aae8809f4ac520"},"cell_type":"code","source":"new_df1.fillna('0',inplace=True)\ntest_df1.fillna('0',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"285a02accf28ef48bbcac373b8a1d7dd9650ab68"},"cell_type":"code","source":"new_df1.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd9f5ce9dc521aa9b0a06276bb9ba6921142a484"},"cell_type":"code","source":"new_df1['trafficSource.adContent'].fillna('Noadcontent',inplace=True)\nnew_df1['trafficSource.keyword'].fillna('NA',inplace=True)\ntest_df1['trafficSource.adContent'].fillna('Noadcontent',inplace=True)\ntest_df1['trafficSource.keyword'].fillna('NA',inplace=True)\n\nfor columns in ['totals.sessionQualityDim','totals.timeOnSite','totals.transactions']:\n    new_df1[columns].fillna('0',inplace=True)\n    new_df1[columns] = new_df1[columns].astype('int')\n    test_df1[columns].fillna('0',inplace=True)\n    test_df1[columns] = test_df1[columns].astype('int')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6f6a1030cc2f368fe69a4edac4d6e0105611326"},"cell_type":"code","source":"new_df1.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c15456eb326070babd692e4838695d1bf8378baa"},"cell_type":"code","source":"new_df1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"878321fc7a57f66844c73b12ef2f73a7065c10e7"},"cell_type":"code","source":"def convert_category_todummies(df,field):\n    #print('Processing ', field)\n    dummy_df = pd.get_dummies(df[field])\n    df = pd.concat([df,dummy_df],axis=1)\n    df.drop(labels=[field],axis=1,inplace=True)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3846fe16916afc5bcedef66a8421023346ced73"},"cell_type":"code","source":"def convert_category_tolevel(df,field):\n    df[field],index = pd.factorize(df[field])\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3bd8bf7aedb80691a3371b44d7768406030115b2"},"cell_type":"code","source":"#test_df1=test_df1.drop(labels=['totals.transactionRevenue'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d7a3b7ab99d46e4615204d3f9615b6e61f62123"},"cell_type":"code","source":"#Data cleaning\ntrain_size = new_df1.shape[0]\nmerged_df = pd.concat([new_df1,test_df1])\nmerged_df['totals.pageviews']=merged_df['totals.pageviews'].astype('int')\nmerged_df['totals.hits']=merged_df['totals.pageviews'].astype('int')\nmerged_df = merged_df.drop(labels=['trafficSource.keyword'],axis=1)\nprint('Before: ', merged_df.shape)\ncolumns = merged_df.columns\nfor fields in columns:\n    if merged_df[fields].dtype == 'object' and fields not in ['fullVisitorId','method']:\n        print('Unique values for ', fields, len(merged_df[fields].unique()), merged_df[fields].unique())\n        if len(merged_df[fields].unique()) > 40:\n            print('Level conversion')\n            merged_df[fields] = convert_category_tolevel(merged_df,fields)\n            merged_df[fields] = merged_df[fields].astype('int')\n        else:\n            print('One hot conversion')\n            merged_df = convert_category_todummies(merged_df,fields)\n            #merged_df = merged_df.drop(labels=[fields],axis=1)\nprint('After: ', merged_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"902d22317a90ebdb2fed91447d98f017e273196c"},"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\n\nscaler1 = MinMaxScaler()\n\n#merged_df1 = merged_df.groupby(by=['fullVisitorId']).mean()\nmerged_df['totals.transactionRevenue'] = np.log1p(merged_df['totals.transactionRevenue'])\n\n\nnew_df1 = merged_df.iloc[:train_size]\ntest_df1 = merged_df[train_size:]\n#new_df1 = new_df1.drop(labels=['method'],axis=1)\ntest_df1 = test_df1.drop(labels=['totals.transactionRevenue'],axis=1)\nprint(new_df1.shape, test_df1.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c48b5ea0145add9548e0d13686d4c75809e67064"},"cell_type":"code","source":"import gc\ndel train_df, new_df, test_df,merged_df, transaction_df, nontransaction_df, plot_df\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8c88e4017e42e6eae45f79476c3a0ea18ebe54c"},"cell_type":"code","source":"#we need to predict log revenue per customer. Lets group by full visitor id\ntrain_x = new_df1.groupby(by=['fullVisitorId']).mean()\ndel new_df1\ngc.collect()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7bc29b32ca4dfa9a899e69065ddc8a4bd45a1d9"},"cell_type":"code","source":"train_y = train_x['totals.transactionRevenue']\ntrain_x = train_x.drop(labels=['totals.transactionRevenue'],axis=1)\nscaled_x = scaler1.fit_transform(train_x.values)\n#train_y = np.log1p(train_y)\ntrain_x = pd.DataFrame(scaled_x, columns=train_x.columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76f1815802449d5a61ae3b47f70ea3c59ba10991"},"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(train_x, train_y, test_size=0.15, random_state=1)\n\nlgb_train_data = lgb.Dataset(X_train, label=y_train)\nlgb_val_data = lgb.Dataset(X_val, label=y_val)\n\nparams = {\n        \"objective\" : \"regression\",\n        \"metric\" : \"rmse\",\n        #\"num_leaves\" : 40,\n        \"max_depth\" : 10,\n        \"boosting\" : \"gbdt\",\n        \"learning_rate\" : 0.0025,\n        \"bagging_fraction\" : 0.7,\n        \"feature_fraction\" : 0.7,\n        \"bagging_frequency\" : 6,\n        \"bagging_seed\" : 42,\n        \"seed\": 42}\nmodel = lgb.train(params, lgb_train_data, \n                      num_boost_round=5000,\n                      valid_sets=[lgb_train_data, lgb_val_data],\n                      early_stopping_rounds=100,\n                      verbose_eval=500)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f54ae721dea4ffaec8da5adb85f0a7f18b49956"},"cell_type":"code","source":"test_x = test_df1.groupby(by=['fullVisitorId']).mean()\nvisitorid = test_x.index\nscaled_test = scaler1.transform(test_x)\ntest_x = pd.DataFrame(scaled_test,columns=test_x.columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2a9f9376af9299db94623d99c69b11d3c1d4f83"},"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\ny_pred_train = model.predict(X_train, num_iteration=model.best_iteration)\ny_pred_val = model.predict(X_val, num_iteration=model.best_iteration)\ny_pred_submit = model.predict(test_x, num_iteration=model.best_iteration)\n\nprint(f\"LGBM: RMSE val: {np.sqrt(mean_squared_error(y_val, y_pred_val))}  - RMSE train: {np.sqrt(mean_squared_error(y_train, y_pred_train))}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1ba6cf128f2f5c5d43bda72f22c328126061fe0","scrolled":false},"cell_type":"code","source":"plt.style.use('ggplot')\nlgb.plot_importance(model,max_num_features=15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"faf508e9a09d98fef8e7fd6bb70a43438ee97fe0"},"cell_type":"code","source":"#submission = pd.DataFrame({'fullVisitorId':visitorid,'PredictedLogRevenue':y_pred_submit})\n#submission['fullVisitorId']= submission['fullVisitorId'].astype(str)\n#submission['PredictedLogRevenue']=submission['PredictedLogRevenue'].apply(lambda x: 0 if x<0 else x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c52c52b8a31083123e002cb8fe4ad0e6660b4b7"},"cell_type":"code","source":"#submission.to_csv('submission1.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"32be3b67d07fbca2833f7fac02f5a53dafcbab36"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import LSTM, Bidirectional, Dropout\nfrom keras.callbacks import ReduceLROnPlateau\n\nX_train = X_train.values\nX_val = X_val.values\ny_train = y_train.values\ny_val = y_val.values\nX_train = X_train.reshape(X_train.shape[0],1,X_train.shape[1])\nX_val = X_val.reshape(X_val.shape[0],1,X_val.shape[1])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3babbddc99d21c19f24fb6b1c406a2e44700b4fa"},"cell_type":"code","source":"#model1 = Sequential()\n#model1.add(Bidirectional(LSTM(200,recurrent_dropout=0.2, input_shape=(X_train.shape[1],X_train.shape[2]), kernel_initializer='lecun_normal', return_sequences=True)))\n#model1.add(Dropout(0.2))\n#model1.add(Bidirectional(LSTM(120,recurrent_dropout=0.2, kernel_initializer='lecun_normal')))\n#model1.add(Dropout(0.2))\n#model1.add(Dense(50,activation='sigmoid'))\n#model1.add(Dropout(0.2))\n#model1.add(Dense(20,activation='elu'))\n#model1.add(Dense(1,activation='linear'))\n#model1.compile(loss='mse', optimizer='adam')\n\n\n#history = model1.fit(X_train, y_train, epochs=2, batch_size=64, validation_data=(X_val, y_val), verbose=1, shuffle=False)\n#plt.plot(history.history['loss'], label='train')\n#plt.plot(history.history['val_loss'], label='test')\n#plt.legend()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c1411cabd43428ea1fafabd971766df9f05e9e2"},"cell_type":"code","source":"#test_x = test_x.values\n#test_x = test_x.reshape((test_x.shape[0],1,test_x.shape[1]))\n\n#y_pred_train = model1.predict(X_train)\n#y_pred_val = model1.predict(X_val)\n#y_pred_submit = model1.predict(test_x)\n\n#print(f\"LSTM: RMSE val: {np.sqrt(mean_squared_error(y_val, y_pred_val))}  - RMSE train: {np.sqrt(mean_squared_error(y_train, y_pred_train))}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f7bcb910abe9c93c58ca0dc9dae20b40925e21c"},"cell_type":"code","source":"from keras.layers import Input\nfrom keras.models import Model\n\ninputs = Input(shape=(1,70))\nx = Bidirectional(LSTM(200,recurrent_dropout=0.2, kernel_initializer='lecun_normal', return_sequences=True))(inputs)\nx = Bidirectional(LSTM(120,recurrent_dropout=0.2, kernel_initializer='lecun_normal'))(x)\nx = Dense(50, activation='sigmoid')(x)\nx = Dropout(0.1)(x)\nx = Dense(20,activation='elu')(x)\noutput = Dense(1,activation='linear')(x)\n\nmodel2 = Model(inputs=inputs, outputs=output)\nmodel2.compile(loss='mse', optimizer='adam')\nmodel2.fit(X_train, y_train, epochs=4, batch_size=64, validation_data=(X_val, y_val), verbose=1, shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60093069090dc4f10b2e63ebb2ec3de94a8257c0"},"cell_type":"code","source":"test_x = test_x.values\ntest_x = test_x.reshape((test_x.shape[0],1,test_x.shape[1]))\n\ny_pred_train = model2.predict(X_train)\ny_pred_val = model2.predict(X_val)\ny_pred_submit = model2.predict(test_x)\n\nprint(f\"LSTM: RMSE val: {np.sqrt(mean_squared_error(y_val, y_pred_val))}  - RMSE train: {np.sqrt(mean_squared_error(y_train, y_pred_train))}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"488481454dcf4cc7df602069e88287374adf172f"},"cell_type":"code","source":"submission = pd.DataFrame({'fullVisitorId':visitorid,'PredictedLogRevenue':np.squeeze(y_pred_submit)})\nsubmission['fullVisitorId']= submission['fullVisitorId'].astype(str)\nsubmission['PredictedLogRevenue']=submission['PredictedLogRevenue'].apply(lambda x: 0 if x<0 else x)\n\nsubmission.to_csv('submission2.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"50ef8f8957bd72e93fd41742826dff32a918be04"},"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}