{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport lightgbm as lgb\nfrom lightgbm.sklearn import LGBMRegressor\npd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom pandas.io.json import json_normalize\nfrom sklearn import metrics \nimport json\nimport feather\nimport matplotlib.pyplot as plt\n%matplotlib inline\nplt.style.use('fivethirtyeight')\nfrom sklearn.model_selection import KFold,StratifiedKFold\n\nfrom sklearn import preprocessing\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"#Developed a function to flatten columns having JSON objects.\n#Added new columns having key values of JSON Objects as columns\nimport pandas as pd\nimport os\nfrom pandas.io.json import json_normalize\nimport json\ndef load_df(csv_path='../input/ga-customer-revenue-prediction/train.csv', nrows=None):\n    COLUMNS_HAVING_JSON = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    df_json_columns = pd.read_csv(csv_path, \n                     converters={column: json.loads for column in COLUMNS_HAVING_JSON}, \n                     dtype={'fullVisitorId': 'str'}, # SHOULD CHANGE\n                     nrows=nrows)\n    for column in COLUMNS_HAVING_JSON:\n        df_column = json_normalize(df_json_columns[column])\n        df_column.columns = [f\"{column}_{key}\" for key in df_column.columns]\n        df_json_columns = df_json_columns.drop(column, axis=1)\n        df_json_columns = df_json_columns.merge(df_column, right_index=True, left_index=True)\n    print(f\"Loaded {os.path.basename(csv_path)}. Shape: {df_json_columns.shape}\")\n    return df_json_columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"336eebb5b7501706d90b66197be58e24195409da"},"cell_type":"code","source":"training_df = load_df(\"../input/ga-customer-revenue-prediction/train.csv\")\ntest_df = load_df(\"../input/ga-customer-revenue-prediction/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"206f185dde0f0d7bb4aa98031d65c9a58cb3c6ed","scrolled":false},"cell_type":"code","source":"training_df.to_feather('train_data.feather')\ntest_df.to_feather('test_data.feather')\nprint(\"Loaded data in feather file\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cba841622718acac545f2949d73e4f04faa3a01","scrolled":true},"cell_type":"code","source":"df = pd.read_feather('train_data.feather')\ntest_df = pd.read_feather('test_data.feather')\nprint(\"Loaded from feather file\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"944e90ca010087fd75c9e5bd99e4aa7d8065a2ba","scrolled":false},"cell_type":"code","source":"#Removed columns having constant and null values in test and training data\nconstant_valued_columns = [c for c in df.columns if df[c].nunique(dropna=False)==1]\nnon_relevant = [\"visitNumber\", \"date\", \"fullVisitorId\", \"sessionId\", \"visitId\", \"visitStartTime\"]\ndf = df.drop(constant_valued_columns, axis=1)\ntest_df = test_df.drop(constant_valued_columns, axis=1)\n## non relevant columns\ndf=df.drop(['trafficSource_campaignCode'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c279ad830babe55c2a93246976eb8bcac571414","scrolled":true},"cell_type":"code","source":"def convert_totals_columns_to_float(df,isTrain=True):\n    numeric_cols_to_transform = ['totals_hits'\n                ,'totals_pageviews'\n                ,'totals_newVisits'\n                ,'totals_bounces']\n    if isTrain == True:\n        df[numeric_cols_to_transform] = df[numeric_cols_to_transform].fillna(0)\n        df['totals_transactionRevenue'] = df['totals_transactionRevenue'].fillna(0)\n    else:\n        df[numeric_cols_to_transform] = df[numeric_cols_to_transform].fillna(0) \n    for col in numeric_cols_to_transform:\n        df[col] = df[col].astype('float32')\n    if isTrain == True:\n         df['totals_transactionRevenue'] = df['totals_transactionRevenue'].astype('float32')\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a09edeb18f935507c07f5b3e7f05a67aa93b71cd","scrolled":false},"cell_type":"code","source":"df['totals_transactionRevenue'] = df['totals_transactionRevenue'].astype('float32')\ndf=df.drop(['trafficSource_campaignCode'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2adb6124e9bd85a751d325c0fd72d01b571d53fe","scrolled":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_continent')['totals_transactionRevenue'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\ndf_plot.plot(kind='bar',colors=my_colors,grid='false')\nplt.title('Mean revenue by continent')\nplt.ylabel('Mean Revenue')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9de1d038dd2c9360c6d6d38c51688ad720edbd8c"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_subContinent')['totals_transactionRevenue'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\ndf_plot.plot(kind='bar',grid='false')\nplt.title('Mean revenue by subContinent');\nplt.ylabel('Mean Revenue')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f0975809796057596cd0f3afe7bcf0936eb3151"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_continent')['totals_transactionRevenue'].agg(['size','count'])\ndf_plot.columns = [\"Size\",\"Count\"]\ndf_plot_count = df_plot.groupby('geoNetwork_continent')['Count'].mean().sort_index()\ndf_plot_count.plot(kind='bar',grid='false')\nplt.title('Total Non-Zero Count of Transaction_Revenue Vs Continent');\nplt.ylabel('Count')\ndf.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28b8113038646cdddbfb4099827e45a5ef27d596"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_subContinent')['totals_transactionRevenue'].count()\ndf_plot.plot(kind='bar',grid='false')\nplt.title('Total Non-Zero Count of Transaction Revenue Vs SubContinent')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b881c1ab17f38faca57ae2ac5b0d22b37a6a205e"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_continent')['totals_transactionRevenue'].size()\ndf_plot.plot(kind='bar',grid='false')\nplt.title('Size of Transaction Revenue Vs Continent')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"68b09530f78b110f50b04e6d321c9fb0f6054874"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_subContinent')['totals_transactionRevenue'].size()\ndf_plot.plot(kind='bar',grid='false')\nplt.title('Total Count of Transaction Revenue Vs SubContinent')\nplt.ylabel('Count')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"620302a2dbf626f677864f50332e225821b93a12"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_continent')['fullVisitorId'].count()\ndf_plot.plot(kind='bar',grid='false')\nplt.title('Visits Per Continent')\nplt.ylabel('Visits')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"47739bc04d0c12f5e70e540a52bdc98fb9b4bef5"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('geoNetwork_subContinent')['fullVisitorId'].count()\ndf_plot.plot(kind='bar',grid='false')\nplt.title('Visits Per subContinent')\nplt.ylabel('Visits')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94016861342f6ee41bd1a46a785c33fc5a541f38"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('device_operatingSystem')['totals_transactionRevenue'].agg(['size','count','mean'])\ndf_plot.head()\ndf_plot.columns = [\"Size\", \"Count\", \"Mean\"]\n\ndf_plot_count = df_plot.groupby('device_operatingSystem')['Size'].mean()\ndf_plot_count.plot(kind='bar',grid='false')\nplt.title('Total Count of Transaction Revenue Vs Operating System')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b34c5a87af003fbe6456b9c17b41c8cc53d3b4bc"},"cell_type":"code","source":"df_plot_non_revenue_count = df_plot.groupby('device_operatingSystem')['Count'].mean()\ndf_plot_non_revenue_count.plot(kind='bar',grid='false')\nplt.title('Total Non-Zero of Transaction Revenue Count Vs Operating System')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8d53cbdf8eff3ee7b691f49085e240d1eeb4f133"},"cell_type":"code","source":"df_plot_mean = df_plot.groupby('device_operatingSystem')['Mean'].mean()\ndf_plot_mean.plot(kind='bar',grid='false')\nplt.title('Mean of Transaction Revenue Count Vs Operating System')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f90787fa861ccd0430c26b4e97b2943b5566c025"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('device_deviceCategory')['totals_transactionRevenue'].agg(['size','count'])\ndf_plot.head()\ndf_plot.columns = [\"Size\", \"Count\"]\n\ndf_plot_count = df_plot.groupby('device_deviceCategory')['Size'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\n\ndf_plot_count.plot(kind='bar',grid='false',colors=my_colors)\nplt.title('Total Count of Transaction Revenue Vs Device Category')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e11d3165df7a6b105a721d69754da9a3ec0aed49"},"cell_type":"code","source":"df_plot_non_zero_count = df_plot.groupby('device_deviceCategory')['Count'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\n\ndf_plot_non_zero_count.plot(kind='bar',grid='false',colors=my_colors)\nplt.title('Total Non-Zero Count of Transaction Revenue Vs Device Category')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8eb39088fbccc91cfb3909cf62adbc35c9e9ef5"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('device_browser')['totals_transactionRevenue'].agg(['size','count','mean'])\ndf_plot.head()\ndf_plot.columns = [\"Size\", \"Count\",\"Mean\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7fe566d795ec4c525792031777630abbaa35b39a"},"cell_type":"code","source":"df_plot_count = df_plot.groupby('device_browser')['Size'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\ndf_plot = df_plot.sort_values([\"Size\"], ascending=False)\ndf_plot_count.plot(kind='bar',grid='false',figsize=(20,5))\nplt.title('Total Count of Transaction Revenue Vs Device Browser')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"127d22d9b6684074ab45336efe8e1aa91d8c7e93"},"cell_type":"code","source":"df_plot_count = df_plot.groupby('device_browser')['Count'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\n\ndf_plot_count.plot(kind='bar',grid='false',figsize=(20,5))\nplt.title('Total Non Transaction Revenue Vs Device Browser')\nplt.ylabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c6bfe076d8d7644ec5fee2e653e6703e946d4e3"},"cell_type":"code","source":"df_plot_count = df_plot.groupby('device_browser')['Mean'].mean()\nmy_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df_plot))]\n\ndf_plot_count.plot(kind='bar',grid='false',figsize=(20,5))\nplt.title('Mean of Transaction Revenue Vs Device Browser')\nplt.ylabel('Mean')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bdadbf9eb3502ce5852cc5a4abd730342350ced"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('totals_hits')['totals_transactionRevenue'].agg(['size','count'])\ndf_plot.head()\ndf_plot.columns = [\"Size\", \"Count\"]\ndf_plot = df_plot.sort_values([\"Size\"], ascending=False)\ndf_plot1 = df_plot['Size'].head(60)\ndf_plot1.plot(kind='barh',grid='false',figsize=(9, 9),yticks=range(0,60))\nplt.title('Hits Vs Total Count of Transaction Revenue')\nplt.ylabel('Hits')\nplt.xlabel('Total Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a24970446751fa32b1f7fc750f6e3baa19eaa7b"},"cell_type":"code","source":"df_plot2 = df_plot['Count'].head(60)\ndf_plot2.plot(kind='barh',grid='false',figsize=(11, 11),yticks=range(0,60))\nplt.title('Hits Vs Non-Zero Revenue Transactions')\nplt.ylabel('Hits')\nplt.xlabel('Total Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc3dfef99da90eaa6d7d76391ab4bcd1ff05a62d"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nx = [{i:np.random.randint(1,5)} for i in range(10)]\ndf_plot = df.groupby('totals_pageviews')['totals_transactionRevenue'].agg(['size','count','mean'])\ndf_plot.head()\ndf_plot.columns = [\"Size\", \"Count\",\"Mean\"]\ndf_plot = df_plot.sort_values([\"Size\"], ascending=False)\ndf_plot_pv_count = df_plot['Size'].head(60)\ndf_plot_pv_count.plot(kind='barh',grid='false',figsize=(9, 9))\nplt.title('PageViews Vs Total Count of Transactions')\nplt.ylabel('PageViews')\nplt.xlabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adb7a91d0d0091070b67c1901b700177f59a3cda"},"cell_type":"code","source":"df_plot_pv_count = df_plot['Count'].head(60)\ndf_plot_pv_count.plot(kind='barh',grid='false',figsize=(9, 9))\nplt.title('PageViews Total Vs Non-Zero Revenue Transactions')\nplt.ylabel('PageViews')\nplt.xlabel('Count')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f064c6ca5fcae6ce814c6ee6c4d728bdf197168"},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\ncategorical_columns = [c for c in df.columns if not c.startswith(\"total\")]\ncategorical_columns = [c for c in categorical_columns if c not in constant_valued_columns + non_relevant]\nfor c in categorical_columns:\n\n    le = LabelEncoder()\n    train_vals = list(df[c].values.astype(str))\n    test_vals = list(test_df[c].values.astype(str))\n    \n    le.fit(train_vals + test_vals)\n    \n    df[c] = le.transform(train_vals)\n    test_df[c] = le.transform(test_vals)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db3b5dc4286ed7711c3ab675e7a5cf93203536eb"},"cell_type":"code","source":"df = convert_totals_columns_to_float(df)\ntest_df = convert_totals_columns_to_float(test_df,False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"550889cfe6c467352cfedbcd49e979ec7bb0bded"},"cell_type":"code","source":"def normalize_numerical_columns(df, isTrain = True):\n    df[\"totals_hits\"] = df[\"totals_hits\"].astype(float)\n    df[\"totals_hits\"] = (df[\"totals_hits\"] - min(df[\"totals_hits\"])) / (max(df[\"totals_hits\"]) - min(df[\"totals_hits\"]))\n\n    df[\"totals_pageviews\"] = df[\"totals_pageviews\"].astype(float)\n    df[\"totals_pageviews\"] = (df[\"totals_pageviews\"] - min(df[\"totals_pageviews\"])) / (max(df[\"totals_pageviews\"]) - min(df[\"totals_pageviews\"]))\n    \n    if isTrain:\n        df[\"totals_transactionRevenue\"] = df[\"totals_transactionRevenue\"].fillna(0.0)\n    return df ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62d2654c2e435646870c95bbf5267b408814375c"},"cell_type":"code","source":"df = normalize_numerical_columns(df)\ntest_df = normalize_numerical_columns(test_df, isTrain = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04b629778507cbfb07b4f68123d2c9ec8b338331"},"cell_type":"code","source":"features = [c for c in df.columns if c not in constant_valued_columns + non_relevant]\nfeatures.remove(\"totals_transactionRevenue\")\ndf_new[\"totals_transactionRevenue\"] = np.log1p(df[\"totals_transactionRevenue\"].astype(float))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"128b805ad11a59eccb515428a754392047e3f661"},"cell_type":"code","source":"#\ndf_count_nonzero_tr = df.groupby('fullVisitorId')['totals_transactionRevenue'].agg(['count'])\ndf_count_nonzero_tr.columns = [\"Count\"]\ntotalNonZeroTransactionCount = df_count_nonzero_tr[\"Count\"].sum()\ndf_count_nonzero_tr['buying_probability'] = df_count_nonzero_tr[\"Count\"]/totalNonZeroTransactionCount\ndf_count_nonzero_tr=df_count_nonzero_tr.sort_values(by=['buying_probability'],ascending=False)\nprint(df_count_nonzero_tr.head(10))\n\nprobSum = df_count_nonzero_tr['buying_probability'].sum()\nprint(probSum)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05f855012b0211c885dc3d8b237ce8423147e09e"},"cell_type":"code","source":"train_x, valid_x, train_y, valid_y = train_test_split(df[features], df_new[\"totals_transactionRevenue\"], test_size=0.20, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb471b6aef9aa13e74af0f5bded8e0b06d366ffd"},"cell_type":"code","source":"import lightgbm as lgb \n\nlgb_params = {\"objective\" : \"regression\",\n        \"metric\" : \"rmse\", \n        \"num_leaves\" : 200,\n        \"min_child_samples\" : 100,\n        \"learning_rate\" : 0.05,\n        \"boosting_type\" : \"gbdt\",\n        \"bagging_fraction\" : 0.7,\n        \"feature_fraction\" : 0.5,\n        \"bagging_frequency\" : 5,\n        \"bagging_seed\" : 2018,\n        \"verbosity\" : -1}\n    \nlgb_train = lgb.Dataset(train_x, label=train_y)\nlgb_val = lgb.Dataset(valid_x, label=valid_y)\nmodel = lgb.train(lgb_params, lgb_train, 700, valid_sets=[lgb_val], early_stopping_rounds=150)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a63cf28a9221baade75054ec6dff3ef941bef9fb"},"cell_type":"code","source":"unimportant_features = [\"visitNumber\", \"date\", \"fullVisitorId\", \"sessionId\", \"visitId\", \"visitStartTime\"]\nfeatures = [c for c in df.columns if c not in constant_valued_columns + unimportant_features]\nprint (features)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e45b74682db9c0a533a6a952b6d963be9adc81b"},"cell_type":"code","source":"features = [c for c in df.columns if c not in constant_valued_columns + unimportant_features]\nfeatures.remove(\"totals_transactionRevenue\")\npreds = model.predict(test_df[features], num_iteration=model.best_iteration)\ntest_df[\"PredictedLogRevenue\"] = np.expm1(preds)\nresult = test_df.groupby(\"fullVisitorId\").agg({\"PredictedLogRevenue\" : \"sum\"}).reset_index()\nresult[\"PredictedLogRevenue\"] = np.log1p(result[\"PredictedLogRevenue\"])\nresult[\"PredictedLogRevenue\"] =  result[\"PredictedLogRevenue\"].apply(lambda x : 0.0 if x < 0 else x)\nresult[\"PredictedLogRevenue\"] = result[\"PredictedLogRevenue\"].fillna(0)\nresult.to_csv(\"baseline.csv\", index=False)\nprint(result.isnull().values.any())\nresult.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"193a3e79f855b51c6b5ca155a5ecea15f7b70442"},"cell_type":"code","source":"def calculate_rmse(df_new):\n    \n    temp_df = pd.DataFrame()\n    temp_df[\"totals_transactionRevenue\"] = np.log1p(df_new[\"totals_transactionRevenue\"].astype(float))\n    train_x, valid_x, train_y, valid_y = train_test_split(df_new[features], temp_df[\"totals_transactionRevenue\"], test_size=0.2, random_state=20)\n\n\n    lgb_params = {\"objective\" : \"regression\", \"metric\" : \"rmse\",\n                  \"num_leaves\" : 50, \"learning_rate\" : 0.02, \n                  \"bagging_fraction\" : 0.75, \"feature_fraction\" : 0.8, \"bagging_frequency\" : 9}\n\n\n    lgb_train = lgb.Dataset(train_x, label=train_y)\n    lgb_val = lgb.Dataset(valid_x, label=valid_y)\n    model = lgb.train(lgb_params, lgb_train, 400, valid_sets=[lgb_val], early_stopping_rounds=100, verbose_eval=200)\n    \n    prediction = model.predict(valid_x, num_iteration=model.best_iteration)\n    rmse = np.sqrt(metrics.mean_squared_error(prediction, valid_y))    \n    return rmse","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60b7a7773b8b8acec0a50cf658f9443f9aaa4665"},"cell_type":"code","source":"#To compute p-value\ntesting_cols = ['trafficSource_campaign','totals.pageviews','trafficSource.adContent']\nfor cols in testing_cols:\n    df_new = df.copy()\n    count = 0\n    i=1\n    while i < 500: #No of iterations\n        df_new[cols] = np.random.permutation(df_new[cols])   \n        if calculate_rmse(df_new)<bestValue:\n            count=count+1\n    print(count)\n    print(\"For \",cols,\" number of times RMSE is lesser than base is : \",count)","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}