{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-15T23:40:17.578950Z","iopub.execute_input":"2022-08-15T23:40:17.579833Z","iopub.status.idle":"2022-08-15T23:40:17.618193Z","shell.execute_reply.started":"2022-08-15T23:40:17.579673Z","shell.execute_reply":"2022-08-15T23:40:17.616620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/ga-customer-revenue-prediction/sample_submission.csv')\nsub2 = pd.read_csv('/kaggle/input/ga-customer-revenue-prediction/sample_submission_v2.csv')\ndisplay(sub, sub2)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:15:25.665721Z","iopub.execute_input":"2022-08-15T12:15:25.666185Z","iopub.status.idle":"2022-08-15T12:15:26.591472Z","shell.execute_reply.started":"2022-08-15T12:15:25.666133Z","shell.execute_reply":"2022-08-15T12:15:26.590355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/ga-customer-revenue-prediction/train_v2.csv')\ntest = pd.read_csv('/kaggle/input/ga-customer-revenue-prediction/test_v2.csv')\ndisplay(train, test)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:15:30.978038Z","iopub.execute_input":"2022-08-15T12:15:30.979401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:02:38.988788Z","iopub.execute_input":"2022-08-15T12:02:38.989164Z","iopub.status.idle":"2022-08-15T12:02:39.340347Z","shell.execute_reply.started":"2022-08-15T12:02:38.989131Z","shell.execute_reply":"2022-08-15T12:02:39.339455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/input\")","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:02:39.341724Z","iopub.execute_input":"2022-08-15T12:02:39.342030Z","iopub.status.idle":"2022-08-15T12:02:39.353363Z","shell.execute_reply.started":"2022-08-15T12:02:39.342002Z","shell.execute_reply":"2022-08-15T12:02:39.351646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nfrom pandas.io.json import json_normalize\nimport matplotlib.pyplot as plt\nimport seaborn as sns\ncolor = sns.color_palette()\n\n%matplotlib inline\n\nfrom plotly import tools\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\n\nfrom sklearn import model_selection, preprocessing, metrics\nimport lightgbm as lgb\n\npd.options.mode.chained_assignment = None","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:30:05.000841Z","iopub.execute_input":"2022-08-15T12:30:05.001267Z","iopub.status.idle":"2022-08-15T12:30:07.434745Z","shell.execute_reply.started":"2022-08-15T12:30:05.001233Z","shell.execute_reply":"2022-08-15T12:30:07.433713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"../input/ga-customer-revenue-prediction","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nfrom pandas.io.json import json_normalize\n\ndef load_df(csv_path='../input/train.csv', nrows=None, skiprows):\n    JSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource'] # 'customDimensions', 'hits'\n    \n    df = pd.read_csv(csv_path, \n                     converters={column: json.loads for column in JSON_COLUMNS}, \n                     dtype={'fullVisitorId': 'str'}, # Important!!\n                     skiprows=skiprows,\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\n\n# print(os.listdir(\"../input/ga-customer-revenue-prediction\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:50:13.555397Z","iopub.execute_input":"2022-08-15T12:50:13.556633Z","iopub.status.idle":"2022-08-15T12:50:13.571027Z","shell.execute_reply.started":"2022-08-15T12:50:13.556553Z","shell.execute_reply":"2022-08-15T12:50:13.568872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('../input/ga-customer-revenue-prediction')\nos.listdir()","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:44:08.527487Z","iopub.execute_input":"2022-08-15T12:44:08.527918Z","iopub.status.idle":"2022-08-15T12:44:08.549833Z","shell.execute_reply.started":"2022-08-15T12:44:08.527883Z","shell.execute_reply":"2022-08-15T12:44:08.548195Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain_df = load_df('train_v2.csv', nrows=100000)\ntest_df = load_df(\"test_v2.csv\", nrows=100000)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:54:56.319522Z","iopub.execute_input":"2022-08-15T12:54:56.321109Z","iopub.status.idle":"2022-08-15T12:55:55.542037Z","shell.execute_reply.started":"2022-08-15T12:54:56.321052Z","shell.execute_reply":"2022-08-15T12:55:55.540775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns=99\ndisplay(train_df, test_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T13:05:05.389895Z","iopub.execute_input":"2022-08-15T13:05:05.391653Z","iopub.status.idle":"2022-08-15T13:05:05.529901Z","shell.execute_reply.started":"2022-08-15T13:05:05.391578Z","shell.execute_reply":"2022-08-15T13:05:05.528542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:02:50.670683Z","iopub.status.idle":"2022-08-15T12:02:50.671112Z","shell.execute_reply.started":"2022-08-15T12:02:50.670921Z","shell.execute_reply":"2022-08-15T12:02:50.670941Z"}}},{"cell_type":"raw","source":"train_df.to_csv('/kaggle/working/train_df.csv', index=0)\ntest_df.to_csv('/kaggle/working/test_df.csv', index=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:02:50.672058Z","iopub.status.idle":"2022-08-15T12:02:50.672459Z","shell.execute_reply.started":"2022-08-15T12:02:50.672234Z","shell.execute_reply":"2022-08-15T12:02:50.672251Z"}}},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/gstore-unnested/train_df.csv')\ntest_df = pd.read_csv('/kaggle/input/gstore-unnested/test_df.csv')\ndisplay(train_df, test_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:03:11.367444Z","iopub.execute_input":"2022-08-15T12:03:11.367857Z","iopub.status.idle":"2022-08-15T12:03:49.178073Z","shell.execute_reply.started":"2022-08-15T12:03:11.367821Z","shell.execute_reply":"2022-08-15T12:03:49.176869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_list = list(train_df.columns)\ndevice_cols = [x.split('.')[1] for x in col_list if 'device' in x]\nfor x in device_cols:\n    print(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:04:07.110525Z","iopub.execute_input":"2022-08-15T12:04:07.110908Z","iopub.status.idle":"2022-08-15T12:04:07.117843Z","shell.execute_reply.started":"2022-08-15T12:04:07.110877Z","shell.execute_reply":"2022-08-15T12:04:07.116677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# geoNetwork\ngeoNetwork_cols = [x.split('.')[1] for x in col_list if 'geoNetwork' in x]\nfor x in geoNetwork_cols:\n    print(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:04:07.408384Z","iopub.execute_input":"2022-08-15T12:04:07.409155Z","iopub.status.idle":"2022-08-15T12:04:07.416054Z","shell.execute_reply.started":"2022-08-15T12:04:07.409115Z","shell.execute_reply":"2022-08-15T12:04:07.414714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# totals\ntotals_cols = [x.split('.')[1] for x in col_list if 'totals' in x]\nfor x in totals_cols:\n    print(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:02:50.679099Z","iopub.status.idle":"2022-08-15T12:02:50.679684Z","shell.execute_reply.started":"2022-08-15T12:02:50.679481Z","shell.execute_reply":"2022-08-15T12:02:50.679501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trafficSource\ncol_list = list(train_df.columns)\ntrafficSource_cols = [x.split('.')[1] for x in col_list if 'trafficSource' in x]\nfor x in trafficSource_cols:\n    print(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-15T12:02:50.680739Z","iopub.status.idle":"2022-08-15T12:02:50.681130Z","shell.execute_reply.started":"2022-08-15T12:02:50.680940Z","shell.execute_reply":"2022-08-15T12:02:50.680959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}