{"cells":[{"metadata":{"_uuid":"dde849f09fb2e5749b6c2f1a00bee9751a238a95"},"cell_type":"markdown","source":"- <a href='#0'>0. Introduction</a>  \n- <a href='#1'>1. Get the Data</a>\n- <a href='#2'>2. Check the Data</a>\n    - <a href='#2-1'>2.1 Remove useless columns</a>\n    - <a href='#2-2'>2.2 Check missing features</a>\n- <a href='#3'> 3. Explore the data</a>\n    - <a href='#3-1'>3.1 Y label</a>\n    - <a href='#3-2'>3.2 Device group</a>\n    - <a href='#3-3'>3.3 GeoNetwork group</a>\n    - <a href='#3-4'>3.4 Totals group</a>\n    - <a href='#3-5'>3.5 TrafficSource group</a>\n    - <a href='#3-6'>3.6 Others</a>\n- <a href='#4'> 4. Baseline model</a>\n    - <a href='#4-1'>4.1 Pre-processing</a>\n    - <a href='#4-2'>4.2 Modeling</a>\n    - <a href='#4-3'>4.3 Feature importance</a>"},{"metadata":{"_uuid":"86913fe83da3f3527afdd709a8877dd1743cf2af"},"cell_type":"markdown","source":"## <a id='0'>0. Introduction</a>\nThe 80/20 rule has proven true for many businesses–only a small percentage of customers produce most of the revenue. As such, marketing teams are challenged to make appropriate investments in promotional strategies.\n\nRStudio, the developer of free and open tools for R and enterprise-ready products for teams to scale and share work, has partnered with Google Cloud and Kaggle to demonstrate the business impact that thorough data analysis can have.\n\nIn this competition, you’re challenged to analyze a Google Merchandise Store customer dataset to predict revenue per customer. Hopefully, the outcome will be more actionable operational changes and a better use of marketing budgets for those companies who choose to use data analysis on top of GA data.\n\nSubmissions are scored on the root mean squared error(RMSE). For each *fullVisitorId* in the test set, you must predict the natural log of their total revenue in PredictedLogRevenue. We are predicting the natural log of the sum of all transactions per user. For every user in the test set, the target is:\n\n$$y_{user} = \\sum_{i=1}^ntransaction_{useri}$$\n\n$$target_{user} = ln(y_{user}+1)$$"},{"metadata":{"_uuid":"d0f3deafd6ada48b7c440f4b66b9a8855a2278ee"},"cell_type":"markdown","source":"## <a id='1'>1. Get the Data</a>"},{"metadata":{"trusted":true,"_uuid":"5b712e4a30c9cb82a3308310ed1b841dd4073349"},"cell_type":"code","source":"import os\nimport time\nimport gc\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n# data manipulation\nimport json\nfrom pandas.io.json import json_normalize\nimport numpy as np\nimport pandas as pd\n# plot\nimport matplotlib.pyplot as plt\nimport seaborn as sns\ncolor = sns.color_palette()\n# model\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import mean_squared_error\nimport lightgbm as lgb","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1fb3720db7c4b48ffbf6af5e8ed71db1db23ec19"},"cell_type":"markdown","source":"Thanks [this kernel](https://www.kaggle.com/julian3833/1-quick-start-read-csv-and-flatten-json-fields/notebook) by [Julian](https://www.kaggle.com/julian3833) for handling the columns with JSON data. "},{"metadata":{"trusted":true,"_uuid":"495abadac148ab9d1ca7fdeef22fda710a05ce45"},"cell_type":"code","source":"%%time\ndef load_df(csv_path='../input/train.csv', nrows=None):\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)\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\ntrain = load_df('../input/train.csv')\ntest = load_df('../input/test.csv')\nsub = pd.read_csv('../input/sample_submission.csv')\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee4a812178bee82294704062a3be21e29cd42d47"},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2ad8fecdb5729e638788a805d34179b047f137d"},"cell_type":"markdown","source":"## <a id='2'>2. Check the Data</a>"},{"metadata":{"_uuid":"28bd30ef859f9c305364c5a9d3499a2826eeb6a2"},"cell_type":"markdown","source":"### <a id='2-1'>2.1 Remove useless columns</a>\n**The fields in train set but not test set**"},{"metadata":{"trusted":true,"_uuid":"8f552474cffa3fc2224595f8766f81efb877446b"},"cell_type":"code","source":"set(train.columns).difference(set(test.columns))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c05b1209e5a4b23b837bea14cb975b1236fa26c6"},"cell_type":"markdown","source":"**The fields with constant value**"},{"metadata":{"trusted":true,"_uuid":"03b954b88e87fb6f4f5dbebce45f44e5ce393402"},"cell_type":"code","source":"cons_col = [i for i in train.columns if train[i].nunique(dropna=False)==1]\ncons_col","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca54755802d3a505716d5eb848282185bfab0cc8"},"cell_type":"code","source":"train = train.drop(cons_col + ['trafficSource.campaignCode'], axis=1)\ntest = test.drop(cons_col, axis=1)\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdeec805480c2218bc3205e0372dcef94a388e3e"},"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"947c4fff3557cf9da0e921e291a30ed2b5d2c3cf"},"cell_type":"markdown","source":"### <a id='2-2'>2.2 Check missing values</a>"},{"metadata":{"trusted":true,"_uuid":"47216d90598c4aaf8c2a5d79f68a26aa9a963311"},"cell_type":"code","source":"def find_missing(data):\n    # number of missing values\n    count_missing = data.isnull().sum().values\n    # total records\n    total = data.shape[0]\n    # percentage of missing\n    ratio_missing = count_missing/total\n    # return a dataframe to show: feature name, # of missing and % of missing\n    return pd.DataFrame(data={'missing_count':count_missing, 'missing_ratio':ratio_missing}, index=data.columns.values)\ntrain_missing = find_missing(train)\ntest_missing = find_missing(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"839c7ea568981a6214785a1688a11a91bdca0e5e"},"cell_type":"code","source":"train_missing.reset_index()[['index', 'missing_ratio']]\\\n    .merge(test_missing.reset_index()[['index', 'missing_ratio']], on='index', how='left')\\\n    .rename(columns={'index':'columns', 'missing_ratio_x':'train_missing_ratio', 'missing_ratio_y':'test_missing_ratio'})\\\n    .sort_values(['train_missing_ratio', 'test_missing_ratio'], ascending=False)\\\n    .query('train_missing_ratio>0')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7be5a29bc3e01eac2cb1f63f20331a35547444e5"},"cell_type":"markdown","source":"## <a id='3'>3. Explore the Data</a>"},{"metadata":{"trusted":true,"_uuid":"71362c054c612cd9fb4378080b923a70e632c237"},"cell_type":"code","source":"if test.fullVisitorId.nunique() == len(sub):\n    print('Till now, the number of fullVisitorId is equal to the rows in submission. Everything goes well!')\nelse:\n    print('Check it again')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a6333af4f3b4a322bffce3d9e636b856f85c4b96"},"cell_type":"markdown","source":"- **fullVisitorId**: A unique identifier for each user of the Google Merchandise Store.\n- **visitId**: An identifier for this session. This is only unique to the user. For a completely unique ID, you should use a combination of fullVisitorId and visitId.\n- **sessionId**:  *fullVisitorId_visitId*; A unique identifier for this visit to the store.\n- **date** : The date on which the user visited the Store.\n- **visitNumber**: The session number for this user. If this is the first session, then this is set to 1.\n- **visitStartTime**: The timestamp (expressed as POSIX time).\n- **socialEngagementType**: Engagement type, either \"Socially Engaged\" or \"Not Socially Engaged\".\n- **channelGrouping** : The channel via which the user came to the Store. 'Organic Search', 'Referral', 'Paid Search', 'Affiliates', 'Direct', 'Display', 'Social' or 'Other'.\n- **device** : The specifications for the device used to access the Store. **It includes 16 variables, 4 of which are useful. **\n- **geoNetwork**: This section contains information about the geography of the user. **It includes 11 variables, 7 of which are useful. **\n- **totals**: This section contains aggregate values across the session. **It includes 6 variables, 5 of which are useful**. **Specially, 'totals.transactionRevenue' is the target for modeling**\n- **trafficSource**: This section contains information about the Traffic Source from which the session originated. **It includes 14 variables, 12 of which are useful. **"},{"metadata":{"_uuid":"89fecd8b391cbd16c0ab16e94ebd1e1640fc504d"},"cell_type":"markdown","source":"###  <a id='3-1'>3.1 Y label</a>\n#### The distribution of 'transactionRevenue'"},{"metadata":{"trusted":true,"_uuid":"491eaeeabbc7071ad75712c94eca305e6b307579"},"cell_type":"code","source":"y = np.nan_to_num(np.array([float(i) for i in train['totals.transactionRevenue']]))\nprint('The ratio of customers with transaction revenue is', str((y != 0).mean()))   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"877dc82d84980956b081e7f00c3ec3eea45d37a5"},"cell_type":"code","source":"plt.figure(figsize=[12, 6])\nsns.distplot(y[y!=0])\nplt.xlabel('transactionRevenue')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"79ddac3b6e0d93f965695067b944485014568d21"},"cell_type":"markdown","source":"#### The distribution of 'Target'"},{"metadata":{"trusted":true,"_uuid":"4a08f6fc9d2c4adabb0a0e1be88585f75a34ca82"},"cell_type":"code","source":"train[\"totals.transactionRevenue\"] = train[\"totals.transactionRevenue\"].astype('float')\ntarget = np.log1p(train.groupby(\"fullVisitorId\")[\"totals.transactionRevenue\"].sum())\nprint('The ratio of customers with transaction revenue is', str((target != 0).mean()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b91b8bfa4a882823ff7030e02dc75728ed7d7e8c"},"cell_type":"code","source":"plt.figure(figsize=[12, 6])\nsns.distplot(target[target!=0])\nplt.xlabel('Target')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f824980a48121f2579e169770f3a871f7f566953"},"cell_type":"markdown","source":"###  <a id='3-2'>3.2 Device group</a>"},{"metadata":{"trusted":true,"_uuid":"ca90650f89cf9f00ed5ae54f2cfab414d424d7d7"},"cell_type":"code","source":"def plot_categorical(data, col, size=[8 ,4], xlabel_angle=0, title='', max_cat = None):\n    '''use this for ploting the count of categorical features'''\n    plotdata = data[col].value_counts() / len(data)\n    if max_cat != None:\n        plotdata = plotdata[max_cat[0]:max_cat[1]]\n    plt.figure(figsize = size)\n    sns.barplot(x = plotdata.index, y=plotdata.values)\n    plt.title(title)\n    if xlabel_angle!=0: \n        plt.xticks(rotation=xlabel_angle)\n    plt.show()\nplot_categorical(data=train, col='device.browser', size=[8 ,4], xlabel_angle=20, title='Device - Browser', max_cat=[0, 6])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d9fe26e68a5daeceeb9cc700c58d197844a0fbb4"},"cell_type":"code","source":"plot_categorical(data=train, col='device.deviceCategory', size=[8 ,4], xlabel_angle=0, title='Device - Category')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8df39362cb40d05c73fcb89edc21c0e00c68b9e"},"cell_type":"code","source":"plot_categorical(data=train, col='device.operatingSystem', size=[8 ,4], xlabel_angle=30, \n                 title='Device - Operating System', max_cat = [0, 7])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"547cdec4c0da651cf3f88ea011690cfad3cfc29a"},"cell_type":"markdown","source":"###  <a id='3-3'>3.3 GeoNetwork group</a>"},{"metadata":{"trusted":true,"_uuid":"f000ffa76bcaa7f8cb5f1a698ce6ac269646cc0f"},"cell_type":"code","source":"plot_categorical(data=train, col='geoNetwork.city', size=[12 ,4], xlabel_angle=30, \n                 title='GeoNetwork - City', max_cat = [1, 20])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca7593bd46b70598c3c585c99783ef83cd28e933"},"cell_type":"code","source":"plot_categorical(data=train, col='geoNetwork.country', size=[12 ,4], xlabel_angle=30, \n                 title='GeoNetwork - Country', max_cat = [0, 20])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e492c84767ca918505f4134390aa769667d648a7"},"cell_type":"code","source":"plot_categorical(data=train, col='geoNetwork.region', size=[12 ,4], xlabel_angle=30, \n                 title='GeoNetwork - Region', max_cat = [1, 20])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"403ec36bb450fa263e6bed10d6cb01de8256e2a6"},"cell_type":"code","source":"plot_categorical(data=train, col='geoNetwork.metro', size=[12 ,4], xlabel_angle=90, \n                 title='GeoNetwork - metro', max_cat = [2, 20])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"724385060741d0dc3c7d1fc16695091832b49d1c"},"cell_type":"code","source":"plot_categorical(data=train, col='geoNetwork.subContinent', size=[8 ,4], xlabel_angle=30, \n                 title='GeoNetwork - SubContinent', max_cat = [0, 10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"512640d23b5f31f4de7a885c1bdd1f9ece94164a"},"cell_type":"code","source":"plot_categorical(data=train, col='geoNetwork.continent', size=[8 ,4], xlabel_angle=30, \n                 title='GeoNetwork - Continent')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5f09d66efff2833e069e470455e12e1991c6fcb9"},"cell_type":"markdown","source":"###  <a id='3-4'>3.4 Totals group</a>"},{"metadata":{"trusted":true,"_uuid":"77f470ec2f34b5ed3e61f4adeeb7c3f0751c3520"},"cell_type":"code","source":"train['totals.bounces'] = train['totals.bounces'].fillna('0')\nplot_categorical(data=train, col='totals.bounces', size=[8 ,4], xlabel_angle=0, title='Totals - Bounces')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6882bc760ced178ab798df2e29213dbcfc792ed6"},"cell_type":"code","source":"train['totals.newVisits'] = train['totals.newVisits'].fillna('0')\nplot_categorical(data=train, col='totals.newVisits', size=[8 ,4], xlabel_angle=0, title='Totals - NewVisits')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04dd192bd06ae3f39dc3ab1429485ad09aecea78"},"cell_type":"code","source":"plt.figure(figsize=[12, 6])\nsns.distplot(train['totals.hits'].astype('float'), kde=True,bins=30)\nplt.xlabel('totals.hits')\nplt.title('Total - Hits')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"871c5ae50ac3933b4b39c13405344591da704f9d"},"cell_type":"code","source":"plt.figure(figsize=[12, 6])\nsns.distplot(train['totals.pageviews'].astype('float').fillna(0))\nplt.xlabel('totals.pageviews')\nplt.title('Total - Pageviews')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9e8ba550cc7aa8858ac519b9a44e56e55eeb4386"},"cell_type":"markdown","source":"###  <a id='3-5'>3.5 TrafficSource group</a>"},{"metadata":{"trusted":true,"_uuid":"d76cc80b8ad9f61e7b03cc4a721ec9e045eb0725"},"cell_type":"code","source":"plot_categorical(data=train, col='trafficSource.adContent', size=[10 ,4], xlabel_angle=30, \n                 title='TrafficSource - AdContent', max_cat = [0, 10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04e97d71f6e10f2e4c4fc76f2d65344fbc13310f"},"cell_type":"code","source":"plot_categorical(data=train, col='trafficSource.medium', size=[10 ,4], xlabel_angle=30, \n                 title='TrafficSource - medium')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"79e8e14d9f5800031d1fb7cfb0b1503996769b14"},"cell_type":"markdown","source":"###  <a id='3-6'>3.6  Others</a>\n**Channel Grouping**"},{"metadata":{"trusted":true,"_uuid":"05fffe7a744092c51bb0489dcc5c994dc6ac858c"},"cell_type":"code","source":"plot_categorical(data=train, col='channelGrouping', size=[10 ,4], xlabel_angle=30, \n                 title='Channel Grouping')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b127dd630067dfaceae6648fdbce14a58542cc9"},"cell_type":"markdown","source":"**Visit Number **"},{"metadata":{"trusted":true,"_uuid":"bdedb5438eb10b37ce1a3d1a3f67d5c2018fe14f"},"cell_type":"code","source":"a = train.groupby(\"fullVisitorId\")[\"visitNumber\"].max()\nplt.figure(figsize=[12, 6])\nsns.distplot(a)\nplt.xlabel('VisitNumber')\nplt.title('Visit Number')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"96d5d3b64a72b289e807d87e4337ffceb963f8ce"},"cell_type":"markdown","source":"**Date**"},{"metadata":{"trusted":true,"_uuid":"4afcee1b77125bca825a3a0f94ac55203c102d55"},"cell_type":"code","source":"plt.figure(figsize=[12, 6])\nsns.distplot(train['date'])\nplt.xlabel('Date')\nplt.title('Date')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"140c4af64f962ba58fc0161f4bc1c2c2e529feab"},"cell_type":"markdown","source":"## <a id='4'>4. Baseline Model</a>\n###  <a id='4-1'>4.1 Pre-processing</a>\n#### Index and Target"},{"metadata":{"trusted":true,"_uuid":"e0d4942398570b795ecf063864111d586257a905"},"cell_type":"code","source":"train_idx = train.fullVisitorId\ntest_idx = test.fullVisitorId\ntrain[\"totals.transactionRevenue\"] = train[\"totals.transactionRevenue\"].astype('float').fillna(0)\ntrain_y = train[\"totals.transactionRevenue\"]\ntrain_target = np.log1p(train.groupby(\"fullVisitorId\")[\"totals.transactionRevenue\"].sum())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"99bd7408ee58d8b557e59424ebc26a8b77fd2f71"},"cell_type":"markdown","source":"#### Pre-processing: label encoder"},{"metadata":{"trusted":true,"_uuid":"e10f2b1cea2d3cb024798871c082522324f3cfff"},"cell_type":"code","source":"train.drop(['fullVisitorId', 'sessionId', 'visitId'], axis = 1, inplace = True)\ntest.drop(['fullVisitorId', 'sessionId', 'visitId'], axis = 1, inplace = True)\nnum_col = [\"totals.hits\", \"totals.pageviews\", \"visitNumber\", \"visitStartTime\", 'totals.bounces',  'totals.newVisits']\nfor i in num_col:\n    train[i] = train[i].astype('float').fillna(0)\n    test[i] = test[i].astype('float').fillna(0)\ncat_col = [e for e in train.columns.tolist() if e not in num_col]\ncat_col.remove('date')\ncat_col.remove('totals.transactionRevenue')\nfor i in cat_col:\n    lab_en = LabelEncoder()\n    train[i] = train[i].fillna('not known')\n    test[i] = test[i].fillna('not known')\n    lab_en.fit(list(train[i].astype('str')) + list(test[i].astype('str')))\n    train[i] = lab_en.transform(list(train[i].astype('str')))\n    test[i] = lab_en.transform(test[i].astype('str'))\n    print('finish', i)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ac913dc6d80864e2446f724bfedecc84f32c403f"},"cell_type":"markdown","source":"###  <a id='4-2'>4.2 Modeling</a>\n#### train valid split"},{"metadata":{"trusted":true,"_uuid":"214f9240922722c51913c989088b2649af22a270"},"cell_type":"code","source":"y_train = np.log1p(train[\"totals.transactionRevenue\"])\nx_train = train.drop([\"totals.transactionRevenue\"], axis=1)\nx_test = test.copy()\nprint(x_train.shape)\nprint(x_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"81005255f16be9e2ebfbe275321f1a2b28a3cffc"},"cell_type":"markdown","source":"**Be careful! In a typical time series application, we can't use latter records to predict previous one.**\n\nHowever till now, I am not sure we should treat this problem as a time-series one or not. Feel free to comment and discuss. \n\nBTW, if you want to conduct time-series split, see [sklearn document](http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html) for more information."},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"c5b9c36f5b1f808dee3176cf730d0886974d3e7e"},"cell_type":"code","source":"folds = KFold(n_splits=5,random_state=6)\noof_preds = np.zeros(x_train.shape[0])\nsub_preds = np.zeros(x_test.shape[0])\n\nstart = time.time()\nvalid_score = 0\nfor n_fold, (trn_idx, val_idx) in enumerate(folds.split(x_train, y_train)):\n    trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]\n    val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]    \n    \n    train_data = lgb.Dataset(data=trn_x, label=trn_y)\n    valid_data = lgb.Dataset(data=val_x, label=val_y)\n    \n    params = {\"objective\" : \"regression\", \"metric\" : \"rmse\", 'n_estimators':10000, 'early_stopping_rounds':100,\n              \"num_leaves\" : 30, \"learning_rate\" : 0.01, \"bagging_fraction\" : 0.9,\n              \"feature_fraction\" : 0.3, \"bagging_seed\" : 0}\n    \n    lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000) \n    \n    oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)\n    oof_preds[oof_preds<0] = 0\n    sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration) / folds.n_splits\n    sub_pred[sub_pred<0] = 0 # should be greater or equal to 0\n    sub_preds += sub_pred\n    print('Fold %2d RMSE : %.6f' % (n_fold + 1, np.sqrt(mean_squared_error(val_y, oof_preds[val_idx]))))\n    valid_score += np.sqrt(mean_squared_error(val_y, oof_preds[val_idx]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe3f91749a4b1b4f812c8c567f9b71b754c6f4e4"},"cell_type":"code","source":"print('Session-level CV-score:', str(round(valid_score/folds.n_splits,4)))\nprint(' ')\ntrain_pred = pd.DataFrame({\"fullVisitorId\":train_idx})\ntrain_pred[\"PredictedLogRevenue\"] = np.expm1(oof_preds)\ntrain_pred = train_pred.groupby(\"fullVisitorId\")[\"PredictedLogRevenue\"].sum().reset_index()\ntrain_pred.columns = [\"fullVisitorId\", \"PredictedLogRevenue\"]\ntrain_pred[\"PredictedLogRevenue\"] = np.log1p(train_pred[\"PredictedLogRevenue\"])\ntrain_rmse = np.sqrt(mean_squared_error(train_target, train_pred['PredictedLogRevenue']))\nprint('User-level score:', str(round(train_rmse, 4)))\nprint(' ')\nend = time.time()\nprint('training time:', str(round((end - start)/60)), 'mins')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b0e1cd529fb441ce0f2a9ee5f31e28adc20146b"},"cell_type":"code","source":"test_pred = pd.DataFrame({\"fullVisitorId\":test_idx})\ntest_pred[\"PredictedLogRevenue\"] = np.expm1(sub_preds)\ntest_pred = test_pred.groupby(\"fullVisitorId\")[\"PredictedLogRevenue\"].sum().reset_index()\ntest_pred.columns = [\"fullVisitorId\", \"PredictedLogRevenue\"]\ntest_pred[\"PredictedLogRevenue\"] = np.log1p(test_pred[\"PredictedLogRevenue\"])\ntest_pred.to_csv(\"lgb_base_model.csv\", index=False) # submission","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"447529ac685f339a17eefd2a344c3c72f66d970a"},"cell_type":"markdown","source":"###  <a id='4-3'>4.3 Feature importance</a>"},{"metadata":{"trusted":true,"_uuid":"5fac0a130e977b4f7cb4dfff4efa5a49da708ffb"},"cell_type":"code","source":"lgb.plot_importance(lgb_model, height=0.5, max_num_features=20, ignore_zero = False, figsize = (12,6), importance_type ='gain')\nplt.show()","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}