{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10038,"databundleVersionId":180088,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-07-16T16:03:03.286966Z","iopub.status.idle":"2024-07-16T16:03:03.291394Z","shell.execute_reply.started":"2024-07-16T16:03:03.291016Z","shell.execute_reply":"2024-07-16T16:03:03.291049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport json\nimport numpy as np \nimport pandas as pd\nfrom pandas import json_normalize\n\n\nimport seaborn as sns \nimport matplotlib.pyplot as plt\n\n\nfrom sklearn.preprocessing import LabelEncoder\n\n\nfrom sklearn.model_selection import TimeSeriesSplit, KFold, StratifiedKFold, train_test_split\n\n\nfrom sklearn.metrics import mean_squared_error\n\nimport lightgbm as lgb\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\npd.options.mode.chained_assignment = None\npd.options.display.max_columns = 999","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.292573Z","iopub.status.idle":"2024-07-16T16:03:03.293109Z","shell.execute_reply.started":"2024-07-16T16:03:03.292835Z","shell.execute_reply":"2024-07-16T16:03:03.292857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_df(csv_path='//kaggle//input//ga-customer-revenue-prediction//train_v2.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'},  \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","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.295408Z","iopub.status.idle":"2024-07-16T16:03:03.296215Z","shell.execute_reply.started":"2024-07-16T16:03:03.295908Z","shell.execute_reply":"2024-07-16T16:03:03.295934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = load_df('//kaggle//input//ga-customer-revenue-prediction//train_v2.csv', nrows=1000000)","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:04.929190Z","iopub.execute_input":"2024-07-16T16:03:04.930149Z","iopub.status.idle":"2024-07-16T16:08:22.771056Z","shell.execute_reply.started":"2024-07-16T16:03:04.930074Z","shell.execute_reply":"2024-07-16T16:08:22.769072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = load_df(\"/kaggle/input/ga-customer-revenue-prediction/test.csv\", nrows=1000000)","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:08:27.858899Z","iopub.execute_input":"2024-07-16T16:08:27.859362Z","iopub.status.idle":"2024-07-16T16:09:58.584738Z","shell.execute_reply.started":"2024-07-16T16:08:27.859318Z","shell.execute_reply":"2024-07-16T16:09:58.583329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.250190Z","iopub.status.idle":"2024-07-16T16:03:03.250600Z","shell.execute_reply.started":"2024-07-16T16:03:03.250409Z","shell.execute_reply":"2024-07-16T16:03:03.250426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.252212Z","iopub.status.idle":"2024-07-16T16:03:03.252634Z","shell.execute_reply.started":"2024-07-16T16:03:03.252444Z","shell.execute_reply":"2024-07-16T16:03:03.252461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"totals.transactionRevenue\"] = df_train[\"totals.transactionRevenue\"].astype('float')\ngdf = df_train.groupby(\"fullVisitorId\")[\"totals.transactionRevenue\"].sum().reset_index()\n\n\nlog_revenues = np.log1p(gdf['totals.transactionRevenue'])\n\n\nplt.figure(figsize=(10, 6)) \nplt.hist(log_revenues, bins=5, color='blue', alpha=0.7)  \nplt.title('Log of Total Transaction Revenue per User') \nplt.xlabel('Log of Revenue')  \nplt.ylabel('Number of Users')  \nplt.grid(True) \nplt.show()  ","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.254017Z","iopub.status.idle":"2024-07-16T16:03:03.254458Z","shell.execute_reply.started":"2024-07-16T16:03:03.254261Z","shell.execute_reply":"2024-07-16T16:03:03.254278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['date'] = pd.to_datetime(df_train['date'], format='%Y%m%d')\n\n\npivot_table = df_train.pivot_table(\n    values='totals.transactionRevenue', \n    index=df_train['date'].dt.to_period('M'), \n    aggfunc='sum'\n).reset_index()\n\n\nprint(pivot_table)","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.262648Z","iopub.status.idle":"2024-07-16T16:03:03.263102Z","shell.execute_reply.started":"2024-07-16T16:03:03.262880Z","shell.execute_reply":"2024-07-16T16:03:03.262901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplt.plot(pivot_table['date'].astype(str), pivot_table['totals.transactionRevenue'], marker='o')\nplt.title('Monthly Transaction Revenue')\nplt.xlabel('Month')\nplt.ylabel('Total Transaction Revenue')\nplt.xticks(rotation=45)\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.264833Z","iopub.status.idle":"2024-07-16T16:03:03.265270Z","shell.execute_reply.started":"2024-07-16T16:03:03.265051Z","shell.execute_reply":"2024-07-16T16:03:03.265067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['totals.newVisits'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.267609Z","iopub.status.idle":"2024-07-16T16:03:03.268019Z","shell.execute_reply.started":"2024-07-16T16:03:03.267839Z","shell.execute_reply":"2024-07-16T16:03:03.267856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['totals.newVisits']","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.269278Z","iopub.status.idle":"2024-07-16T16:03:03.269675Z","shell.execute_reply.started":"2024-07-16T16:03:03.269495Z","shell.execute_reply":"2024-07-16T16:03:03.269511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['totals.newVisits'].fillna(0, inplace=True)\n\n\ndf_train['totals.newVisits'] = df_train['totals.newVisits'].astype(int)\n\n\nnew_customers = df_train[df_train['totals.newVisits'] == 1]\n\nexisting_customers = df_train[df_train['totals.newVisits'] == 0]\n\n\nprint(\"Number of new customers:\", new_customers.shape[0])\nprint(\"Number of existing customers:\", existing_customers.shape[0])","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.271250Z","iopub.status.idle":"2024-07-16T16:03:03.272150Z","shell.execute_reply.started":"2024-07-16T16:03:03.271902Z","shell.execute_reply":"2024-07-16T16:03:03.271923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_customers_revenue = new_customers['totals.transactionRevenue'].sum()\n\nexisting_customers_revenue = existing_customers['totals.transactionRevenue'].sum()\n\n\nprint(new_customers_revenue)\nprint(existing_customers_revenue)","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.273488Z","iopub.status.idle":"2024-07-16T16:03:03.273865Z","shell.execute_reply.started":"2024-07-16T16:03:03.273690Z","shell.execute_reply":"2024-07-16T16:03:03.273705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['New Customers', 'Existing Customers']\nrevenues = [new_customers_revenue, existing_customers_revenue]\n\nplt.figure(figsize=(10, 6))\nplt.bar(labels, revenues, color=['blue', 'green'])\nplt.title('Total Transaction Revenue: New vs Existing Customers')\nplt.xlabel('Customer Type')\nplt.ylabel('Total Transaction Revenue')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.275852Z","iopub.status.idle":"2024-07-16T16:03:03.276328Z","shell.execute_reply.started":"2024-07-16T16:03:03.276107Z","shell.execute_reply":"2024-07-16T16:03:03.276132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['month'] = df_train['date'].dt.to_period('M')\n\n\nmonthly_new_customers_revenue = df_train[df_train['totals.newVisits'] == 1].groupby('month')['totals.transactionRevenue'].sum()\nmonthly_existing_customers_revenue = df_train[df_train['totals.newVisits'] == 0].groupby('month')['totals.transactionRevenue'].sum()\n\n\nmonthly_new_customers_revenue = monthly_new_customers_revenue.to_frame(name='new_customers_revenue').reset_index()\nmonthly_existing_customers_revenue = monthly_existing_customers_revenue.to_frame(name='existing_customers_revenue').reset_index()\n\n\nmonthly_new_customers_revenue['new_customers_revenue'].fillna(0, inplace=True)\nmonthly_existing_customers_revenue['existing_customers_revenue'].fillna(0, inplace=True)\n\n\nmonthly_revenue = pd.merge(monthly_new_customers_revenue, monthly_existing_customers_revenue, on='month', how='outer').fillna(0)\n\n\nplt.figure(figsize=(14, 8))\nplt.plot(monthly_revenue['month'].astype(str), monthly_revenue['new_customers_revenue'], marker='o', label='New Customers Revenue')\nplt.plot(monthly_revenue['month'].astype(str), monthly_revenue['existing_customers_revenue'], marker='o', label='Existing Customers Revenue')\nplt.title('Monthly New and Existing Customers Revenue')\nplt.xlabel('Month')\nplt.ylabel('Total Transaction Revenue')\nplt.xticks(rotation=45)\nplt.legend()\nplt.grid(True)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.277675Z","iopub.status.idle":"2024-07-16T16:03:03.278128Z","shell.execute_reply.started":"2024-07-16T16:03:03.277911Z","shell.execute_reply":"2024-07-16T16:03:03.277927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"channel_counts = df_train['channelGrouping'].value_counts()\nplt.figure(figsize=(10, 6))\nsns.barplot(x=channel_counts.index, y=channel_counts.values, palette='viridis')\nplt.title('Distribution of Channel Grouping')\nplt.xlabel('Channel Grouping')\nplt.ylabel('Count')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.279792Z","iopub.status.idle":"2024-07-16T16:03:03.280289Z","shell.execute_reply.started":"2024-07-16T16:03:03.279996Z","shell.execute_reply":"2024-07-16T16:03:03.280012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aggregations = {'fullVisitorId': 'count',\n                'totals.pageviews': 'count',\n                'totals.transactionRevenue': 'sum'\n}","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.281642Z","iopub.status.idle":"2024-07-16T16:03:03.282062Z","shell.execute_reply.started":"2024-07-16T16:03:03.281861Z","shell.execute_reply":"2024-07-16T16:03:03.281879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_channels = df_train.groupby('channelGrouping').agg(aggregations).sort_values(by='totals.transactionRevenue', ascending=False)\ndf_channels\n","metadata":{"execution":{"iopub.status.busy":"2024-07-16T16:03:03.284039Z","iopub.status.idle":"2024-07-16T16:03:03.284491Z","shell.execute_reply.started":"2024-07-16T16:03:03.284303Z","shell.execute_reply":"2024-07-16T16:03:03.284321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}