{"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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"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":"b5118c0003c732e7e2592f2dcbc5c3ab7a7e3b35"},"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":"a5cacbce0a66123aa183a605289fcc5840da27b4","scrolled":true},"cell_type":"code","source":"train_columnsNames = train_df.columns.values\ntest_columnsNames = test_df.columns.values","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":false,"_kg_hide-output":false,"trusted":true,"_uuid":"4d2f58ceee49cc844d113b37cca22b33664e1c3d"},"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":"fbcafef4c6c4ba0b432c8e00fe730e2afebd9feb"},"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,"_uuid":"f4a27138e83f7ec7c3a758ed45f0fb9efdd73657"},"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":{"trusted":true,"_uuid":"1b91be04a47b6814c7e3aa9ebfbc9ba091d29e97"},"cell_type":"code","source":"#We need to predict totals.transactionRevenue. \n#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())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17dc968abf44f30ff7cd0f73f07c0cd5718cc6ee"},"cell_type":"code","source":"#Lets start with data cleaning\n#Find any unique value columns\n\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],\n                      axis = 1)\ntest_df = test_df.drop(labels = train_df.columns[train_df.nunique() == 1],\n                      axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"631bc9c0305cc009d7143c4f7ca48daedef90118"},"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":"3e13c8550f02a5df718cec999a0ffed90aae44a2"},"cell_type":"markdown","source":"Based on Initial glance of data we can drop following fields while exploring\n\n    sessionId - Unique number for a session\n    visitId - Nth visit fullVisitorId is visiting the store\n    visitStartTime - Time of Visit"},{"metadata":{"trusted":true,"_uuid":"344826932342155ec6db5f74e41169c0319685ef"},"cell_type":"code","source":"geofields = ['geoNetwork.city','geoNetwork.continent',\n             'geoNetwork.country','geoNetwork.metro',\n             'geoNetwork.networkDomain','geoNetwork.region',\n             '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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ab672c0d736f12bb4bb5bcb3ff9a16ddbe516307"},"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":{"_uuid":"9ae051266cc4a3b04a9cde06123e8f94f2ee6ea2"},"cell_type":"markdown","source":"No of visits doesn't have much relation with transactionRevenue"},{"metadata":{"trusted":true,"_uuid":"be5bd85a22963d61841b589fd0fa413010a22d9c"},"cell_type":"code","source":"#Lets analyze how the visits and transactionRevenue trend with date\n#Yearly pattern\n\nplot_df = new_df.groupby(by=['date']).agg({'fullVisitorId':'count',\n                                        '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')\n#plt.xticks(rotation=90)\n\nplt.subplot(2,1,2)\nplt.plot(pd.to_datetime(plot_df['date']),plot_df['fullVisitorId'])\nplt.ylabel('Visits')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"625e126157aea79f6d21e3d7390d31712d5d0d3e"},"cell_type":"markdown","source":"Number 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":"29eec0c31513178c60554e8c869e875a42baabc6"},"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":"e32e59a07847eb34a7be2b11b9e37472e0141ccc"},"cell_type":"code","source":"new_df.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e502d9c3cdabcef1101af70493166360e990c92"},"cell_type":"code","source":"#Fill na in transactionRevenue with 0\nnew_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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2fd443dde46b3d7d90e6875f5331c3563d49030"},"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":"976b3e5e8ab5831de2062e6c50a18bc061f415dd"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.country');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed8838ca8b794782c419b86a69ee3ac38d5e5733"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.city');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3f695447207960921dc70d2b1e7dc093cd87fc4"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.continent');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"25d7df154f75068c35c612668f840012f7c2df59"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.metro');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"805893dd536d2c985916bccd82def56a79af12f1"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.networkDomain');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cb446198a91ade7802e81e15c39d59a62aaf64d"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'geoNetwork.subContinent');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8eeb59fa96299905f7e9938bf01e2d112d48f3d"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'channelGrouping');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0689fe8a6238bcf5c0b9895d6820ed88fdb3a49a"},"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."},{"metadata":{"trusted":true,"_uuid":"095510722a6c793fed2a3970fe62336391e7da91"},"cell_type":"code","source":"new_df1 = new_df.drop(labels=['geoNetwork.region','geoNetwork.networkDomain','geoNetwork.metro',\n                              'geoNetwork.continent','geoNetwork.country','visitId','visitStartTime'],\n                      axis=1)\ntest_df1 = test_df.drop(labels=['geoNetwork.region','geoNetwork.networkDomain','geoNetwork.metro',\n                                'geoNetwork.continent','geoNetwork.country','visitId','visitStartTime'],\n                        axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e07b05e4e50ec2f72c6595d001c6cb8608a2dcf2"},"cell_type":"code","source":"#device.isMobile is a duplicate variable as the detail is covered in deviceCategory\n\nnew_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":"fbafae214f2bfa0616114af03229e16ea2cb5401"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'device.browser');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"924f8a1d7c097cca96c4510aa95e1838490f1914"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'device.deviceCategory');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e65fe3fcd86dbe26e17eef3767f8bb874eb4173a"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'device.operatingSystem');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"963c80de40e602b8d75e5c34bb856c48b2b0074c"},"cell_type":"code","source":"transaction_df['trafficSource.adContent'].fillna('NA',inplace=True)\n#getplot(transaction_df,nontransaction_df,'trafficSource.adContent')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"948760992e5c454f7b59f70e012658ec3717ba6f"},"cell_type":"code","source":"getplot(transaction_df,nontransaction_df,'trafficSource.source')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc8f977f2e681fb1ab077c41103d495f4d2cbcb5"},"cell_type":"code","source":"new_df1 = new_df1.drop(labels=['trafficSource.adwordsClickInfo.adNetworkType', 'trafficSource.adwordsClickInfo.gclId',\n                               'trafficSource.adwordsClickInfo.page', 'trafficSource.adwordsClickInfo.slot',\n                               'trafficSource.medium', 'trafficSource.medium', 'trafficSource.referralPath','trafficSource.source'],\n                       axis=1)\ntest_df1 = test_df1.drop(labels=['trafficSource.adwordsClickInfo.adNetworkType', 'trafficSource.adwordsClickInfo.gclId',\n                                 'trafficSource.adwordsClickInfo.page', 'trafficSource.adwordsClickInfo.slot',\n                                 'trafficSource.medium', 'trafficSource.medium', 'trafficSource.referralPath','trafficSource.source'],\n                         axis=1)\nprint(new_df1.shape)\nnew_df1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"685d09305a44a93dccf51f2569ec0fc36e5a2524"},"cell_type":"code","source":"new_df1.fillna('0',inplace=True)\ntest_df1.fillna('0',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40c42c10ffd14c7ff464a6ab5f641e5e15e9b527"},"cell_type":"code","source":"new_df1.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eacb055fadaf20d1bc5bb4ebf430584884b00b3a"},"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',\n                '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":"1f14d96bac7cd0c204da076c7d316bc68952d4c3"},"cell_type":"code","source":"new_df1.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0cfc45a13305fc521683ef517daddb158c30db0a"},"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":"9cb65b0ed0475b0423fd9b4cf35cbce3cc0bf0d4"},"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":"34ea990fc66fd634ff905299ffb40aba350bde45"},"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.hits'].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":"1bf2823e3c26b65c5fb226fa500f0d9ea7d572d8"},"cell_type":"code","source":"#merged_df1 = merged_df.groupby(by=['fullVisitorId']).mean()\nmerged_df['totals.transactionRevenue'] = np.log1p(merged_df['totals.transactionRevenue'])\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":"0f6b878e2a56b8ae517f981a4cee322dc2eed3d9"},"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":"93287dbb92943882d79904d063cd77952845c286"},"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()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96eeb5295eada9878a71bb5a0deec57c81af27b4"},"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nscaler1 = MinMaxScaler()\n\ntrain_y = train_x['totals.transactionRevenue']\ntrain_x = train_x.drop(labels=['totals.transactionRevenue'],axis=1)\nscaled_x = scaler1.fit_transform(train_x.values)\ntrain_x = pd.DataFrame(scaled_x, columns=train_x.columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"508d64c9e3f271e215fb7d283bf4d4559e1a29d1"},"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}\nlgbModel = 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":"1a1fbc3a0b275cfb0a8602de74e691c137d4d629"},"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":"2e3f3d1b888b61f4d0f6e428c6076c3ac3abfd42"},"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\n\ny_pred_train = lgbModel.predict(X_train, num_iteration=model.best_iteration)\ny_pred_val = lgbModel.predict(X_val, num_iteration=model.best_iteration)\ny_pred_submit = lgbModel.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":"9ecb4c44e788db0f8c38e5dd02ceecc8c1938ec5"},"cell_type":"code","source":"plt.style.use('ggplot')\nlgb.plot_importance(lgbModel,max_num_features=15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"203d121008648247fbbf9211143e2f86466a7410"},"cell_type":"code","source":"'''#Now lets try some neural networks :) :D LSTM\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import LSTM, Bidirectional, Dropout\nfrom keras.callbacks import ReduceLROnPlateau\n\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(train_x, train_y, test_size=0.15, random_state=1, )\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])'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c2c6b18fc675c3e26b6f5f82d0b1bff7e20dac95"},"cell_type":"code","source":"'''X_train.shape, X_val.shape'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3556dc50a3eee7d2541ea0fecda2751587e7c859","scrolled":true},"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\nlstm = Model(inputs=inputs, outputs=output)\nlstm.compile(loss='mse', optimizer='adam')\nlstm.fit(X_train, y_train, epochs=10, batch_size=64, validation_data=(X_val, y_val), verbose=1, shuffle=False)'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26399a0107f37d6f8fc40d6675bf7935983a18e8"},"cell_type":"code","source":"'''from keras.utils.vis_utils import plot_model\nfrom IPython.display import SVG\nfrom keras.utils.vis_utils import model_to_dot\nplot_model(lstm, to_file='lstm.png', show_shapes = True, show_layer_names = True)\nSVG(model_to_dot(lstm).create(prog='dot', format='svg'))'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7126bc69dfc698f4fd8be947bc8afd8b444539b2"},"cell_type":"code","source":"'''from sklearn.metrics import mean_squared_error\n\ntest_x = test_x.values\ntest_x = test_x.reshape((test_x.shape[0],1,test_x.shape[1]))\n\ny_pred_train = lstm.predict(X_train)\ny_pred_val = lstm.predict(X_val)\ny_pred_submit = lstm.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":"aaa5ec8ce5b588ca84c7c2ff9ffea70e664c9017"},"cell_type":"code","source":"'''from keras.models import load_model\n\nlstm.save('LSTM.h5')'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f605286faa14be0b6a50158e6ed6f8034b83fd5"},"cell_type":"code","source":"'''model = load_model('LSTM.h5')'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"afbf8f9b5f79520fbcdf212c63bbd098dfb4c430"},"cell_type":"code","source":"'''lstm.summary()'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3dbe9b2357a953b013483c5b1753b8dbfb03755"},"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('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34dffecdaa8fc9ce3f71e044553e625d489c5e20"},"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}