{"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":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport json\nimport gc\n\nfrom scipy.stats import uniform\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom sklearn.svm import SVR, LinearSVR\nfrom sklearn.decomposition import PCA\nfrom sklearn.metrics import mean_squared_error\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.model_selection import train_test_split, RandomizedSearchCV","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:47:14.200439Z","iopub.execute_input":"2023-02-23T06:47:14.200895Z","iopub.status.idle":"2023-02-23T06:47:15.022655Z","shell.execute_reply.started":"2023-02-23T06:47:14.200786Z","shell.execute_reply":"2023-02-23T06:47:15.021476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_csv(csv_path, nrows=None, skiprows=None):\n    json_cols = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    df = pd.read_csv(csv_path,\n                     converters = {col: json.loads for col in json_cols},\n                     dtype={'fullVisitorId': 'str'},\n                     nrows=nrows,\n                     skiprows=skiprows)\n    \n    for col in json_cols:\n        normalized_col_df = pd.json_normalize(df[col])\n        df = pd.concat([df.drop(columns=col), normalized_col_df], axis=1)\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:47:15.024963Z","iopub.execute_input":"2023-02-23T06:47:15.025693Z","iopub.status.idle":"2023-02-23T06:47:15.033606Z","shell.execute_reply.started":"2023-02-23T06:47:15.025642Z","shell.execute_reply":"2023-02-23T06:47:15.032451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = load_csv('/kaggle/input/ga-customer-revenue-prediction/train_v2.csv', nrows=120_000)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:47:15.035315Z","iopub.execute_input":"2023-02-23T06:47:15.036167Z","iopub.status.idle":"2023-02-23T06:48:03.525754Z","shell.execute_reply.started":"2023-02-23T06:47:15.036116Z","shell.execute_reply":"2023-02-23T06:48:03.524516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_up(df, train=False, valid=False):\n    if train or valid:\n        df = df.drop(columns='transactionRevenue') # This field is deprecated. Use \"totals.totalTransactionRevenue\" instead\n        df[\"totalTransactionRevenue\"] = df['totalTransactionRevenue'].fillna(\"0\").astype(float)\n    \n    df[\"pageviews\"] = df[\"pageviews\"].fillna(0).astype(int)\n    df[\"newVisits\"] = df[\"newVisits\"].fillna(0).astype(int)\n    df[\"sessionQualityDim\"] = df[\"sessionQualityDim\"].fillna(\"1\").astype(int)\n    return df\n\ntrain_df = clean_up(train_df, train=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:03.528880Z","iopub.execute_input":"2023-02-23T06:48:03.529863Z","iopub.status.idle":"2023-02-23T06:48:04.935106Z","shell.execute_reply.started":"2023-02-23T06:48:03.529811Z","shell.execute_reply":"2023-02-23T06:48:04.933592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"country_counts = train_df[[\"campaign\"]].groupby(\"campaign\").size().sort_values(ascending=False)[1:6]\ncountry_counts = country_counts.reset_index()\ncountry_counts.columns = [\"campaign\", \"count\"]\n\nplt.figure(figsize=(18,4))\nsns.barplot(data=country_counts, x=\"campaign\", y=\"count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:04.936621Z","iopub.execute_input":"2023-02-23T06:48:04.936984Z","iopub.status.idle":"2023-02-23T06:48:05.251554Z","shell.execute_reply.started":"2023-02-23T06:48:04.936947Z","shell.execute_reply":"2023-02-23T06:48:05.250192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"country_counts = train_df[[\"metro\"]].groupby(\"metro\").size().sort_values(ascending=False)[:10]\ncountry_counts = country_counts.reset_index()\ncountry_counts.columns = [\"metro\", \"count\"]\n\nplt.figure(figsize=(18,4))\nsns.barplot(data=country_counts, x=\"metro\", y=\"count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:05.253086Z","iopub.execute_input":"2023-02-23T06:48:05.253532Z","iopub.status.idle":"2023-02-23T06:48:05.588199Z","shell.execute_reply.started":"2023-02-23T06:48:05.253488Z","shell.execute_reply":"2023-02-23T06:48:05.587024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_countries = train_df[[\"country\"]].groupby(\"country\").size().sort_values(ascending=False)[:20].index.to_numpy()\ntrain_df_top_countries = train_df[train_df[\"country\"].isin(top_countries)]\n\nplt.figure(figsize=(18,4))\nsns.stripplot(data=train_df_top_countries, x=\"country\", y=\"totalTransactionRevenue\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:05.589600Z","iopub.execute_input":"2023-02-23T06:48:05.589966Z","iopub.status.idle":"2023-02-23T06:48:06.840583Z","shell.execute_reply.started":"2023-02-23T06:48:05.589931Z","shell.execute_reply":"2023-02-23T06:48:06.838945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_extraction(df):\n    agg_df = df.groupby(\"fullVisitorId\")[\"newVisits\", \"pageviews\"].agg(\n        newVisits_sum = (\"newVisits\", \"sum\"),\n        pageviews_sum = (\"pageviews\", \"sum\"),\n        pageviews_mean = (\"pageviews\", \"mean\"),\n        pageviews_std = (\"pageviews\", \"std\"),\n    ).reset_index()\n    agg_df[\"pageviews_std\"] = agg_df[\"pageviews_std\"].fillna(0)\n    df = pd.merge(df, agg_df, on=\"fullVisitorId\", suffixes=(\"\", \"\"))\n    return df\n\ntrain_df = feature_extraction(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:06.842426Z","iopub.execute_input":"2023-02-23T06:48:06.843494Z","iopub.status.idle":"2023-02-23T06:48:09.236074Z","shell.execute_reply.started":"2023-02-23T06:48:06.843440Z","shell.execute_reply":"2023-02-23T06:48:09.234879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18,4))\nsns.stripplot(data=train_df, x=\"pageviews_std\", y=\"totalTransactionRevenue\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:09.237289Z","iopub.execute_input":"2023-02-23T06:48:09.238093Z","iopub.status.idle":"2023-02-23T06:48:27.075880Z","shell.execute_reply.started":"2023-02-23T06:48:09.238057Z","shell.execute_reply":"2023-02-23T06:48:27.075002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:27.079767Z","iopub.execute_input":"2023-02-23T06:48:27.080313Z","iopub.status.idle":"2023-02-23T06:48:27.689336Z","shell.execute_reply.started":"2023-02-23T06:48:27.080269Z","shell.execute_reply":"2023-02-23T06:48:27.688067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(columns=\"totalTransactionRevenue\")\ny = train_df[\"totalTransactionRevenue\"].astype(float)\n\nprint(X.shape, y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:27.690570Z","iopub.execute_input":"2023-02-23T06:48:27.690901Z","iopub.status.idle":"2023-02-23T06:48:27.837299Z","shell.execute_reply.started":"2023-02-23T06:48:27.690870Z","shell.execute_reply":"2023-02-23T06:48:27.836439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# initialize with training dataset, apply it on test set as well\none_hot_encoder = OneHotEncoder(handle_unknown='ignore')\npca = PCA(n_components=24)\ndef get_features(df, train=False):\n    df = df[[\"pageviews\",\n             \"isMobile\",\n             \"channelGrouping\",\n             \"country\",\n             \"metro\",\n             \"newVisits_sum\"\n            ]]\n    \n    cat_cols = [\"channelGrouping\", \"country\", \"metro\"]\n    if train:\n        encoded_df = one_hot_encoder.fit_transform(df[cat_cols]).toarray()\n        encoded_df = pca.fit_transform(encoded_df)\n    else:\n        encoded_df = one_hot_encoder.transform(df[cat_cols]).toarray()\n        encoded_df = pca.transform(encoded_df)\n    \n    encoded_df = pd.DataFrame(encoded_df, index=df.index) # columns=pca.get_feature_names_out(),\n    df = pd.concat([df.drop(columns=cat_cols), encoded_df], axis=1)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:27.838393Z","iopub.execute_input":"2023-02-23T06:48:27.839446Z","iopub.status.idle":"2023-02-23T06:48:27.848300Z","shell.execute_reply.started":"2023-02-23T06:48:27.839413Z","shell.execute_reply":"2023-02-23T06:48:27.847268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_valid_df():\n    val_df = load_csv('/kaggle/input/ga-customer-revenue-prediction/train_v2.csv',\n                      nrows=60000, skiprows=range(1, 120000))\n    val_df = clean_up(val_df, valid=True)\n    val_df = feature_extraction(val_df)\n    \n    X_valid = val_df.drop(columns=\"totalTransactionRevenue\")\n    y_valid = val_df[\"totalTransactionRevenue\"]\n    \n    return X_valid, y_valid","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:27.850635Z","iopub.execute_input":"2023-02-23T06:48:27.851881Z","iopub.status.idle":"2023-02-23T06:48:27.864390Z","shell.execute_reply.started":"2023-02-23T06:48:27.851834Z","shell.execute_reply":"2023-02-23T06:48:27.863377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = get_features(X, train=True)\nprint(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:27.866476Z","iopub.execute_input":"2023-02-23T06:48:27.866957Z","iopub.status.idle":"2023-02-23T06:48:31.493296Z","shell.execute_reply.started":"2023-02-23T06:48:27.866913Z","shell.execute_reply":"2023-02-23T06:48:31.492100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# {'epsilon': 0.75, 'C': 2}\n# dual=False, loss='squared_epsilon_insensitive'\nmodel = LinearSVR(epsilon=0.75)\n# distributions = dict(C=[0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3, 3.25, 3.5, 3.75, 4],\n#                      epsilon=[0, 0.25, 0.5, 0.75, 1])\n# clf = RandomizedSearchCV(model, distributions, random_state=0)\n# clf.fit(X_train, y)\n# print(clf.best_params_)\n\nmodel.fit(X_train, y)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:48:31.495309Z","iopub.execute_input":"2023-02-23T06:48:31.495770Z","iopub.status.idle":"2023-02-23T06:49:13.726909Z","shell.execute_reply.started":"2023-02-23T06:48:31.495722Z","shell.execute_reply":"2023-02-23T06:49:13.725602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ndef valid_score(model):\n    X_valid, y_valid = load_valid_df()\n    \n    result_valid = X_valid[[\"fullVisitorId\"]]\n    result_valid[\"Revenue\"] = y_valid\n    result_valid = result_valid.groupby(\"fullVisitorId\").sum().reset_index()\n    result_valid[\"Log_Revenue\"] = np.log(result_valid[\"Revenue\"] + 1)\n    \n    X_valid_features = get_features(X_valid)\n    predictions_valid = model.predict(X_valid_features)\n#     predictions_valid[predictions_valid < 0] = 0\n    \n    submission_valid = X_valid[[\"fullVisitorId\"]]\n    submission_valid[\"Predicted_Revenue\"] = predictions_valid\n    submission_valid = submission_valid.groupby(\"fullVisitorId\").sum().reset_index()\n    submission_valid[\"Log_Predicted_Revenue\"] = np.log(submission_valid[\"Predicted_Revenue\"] + 1)\n    \n    rmse = np.sqrt(mean_squared_error(result_valid[\"Log_Revenue\"], submission_valid[\"Log_Predicted_Revenue\"]))\n    return rmse\n\n# print(\"validation score: \", valid_score(model))","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:24:59.146944Z","iopub.execute_input":"2023-02-23T06:24:59.147335Z","iopub.status.idle":"2023-02-23T06:24:59.156223Z","shell.execute_reply.started":"2023-02-23T06:24:59.147303Z","shell.execute_reply":"2023-02-23T06:24:59.155116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_df\ndel X\ndel y\ndel X_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:24:59.363529Z","iopub.execute_input":"2023-02-23T06:24:59.363979Z","iopub.status.idle":"2023-02-23T06:24:59.901517Z","shell.execute_reply.started":"2023-02-23T06:24:59.363940Z","shell.execute_reply":"2023-02-23T06:24:59.900102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = load_csv('/kaggle/input/ga-customer-revenue-prediction/test_v2.csv')\ntest_df = clean_up(test_df)\ntest_df = feature_extraction(test_df)\n\nprint(test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:24:59.904079Z","iopub.execute_input":"2023-02-23T06:24:59.904425Z","iopub.status.idle":"2023-02-23T06:28:32.457815Z","shell.execute_reply.started":"2023-02-23T06:24:59.904395Z","shell.execute_reply":"2023-02-23T06:28:32.455986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nX_test = get_features(test_df)\npredictions = model.predict(X_test)\n# predictions[predictions < 0] = 0\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:28:32.459540Z","iopub.execute_input":"2023-02-23T06:28:32.459983Z","iopub.status.idle":"2023-02-23T06:28:39.378820Z","shell.execute_reply.started":"2023-02-23T06:28:32.459941Z","shell.execute_reply":"2023-02-23T06:28:39.375269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test.columns)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:28:39.382178Z","iopub.execute_input":"2023-02-23T06:28:39.384403Z","iopub.status.idle":"2023-02-23T06:28:39.395691Z","shell.execute_reply.started":"2023-02-23T06:28:39.384322Z","shell.execute_reply":"2023-02-23T06:28:39.393274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = test_df[[\"fullVisitorId\"]]\nsubmission[\"PredictedLogRevenue\"] = np.log(predictions + 1)\nsubmission = submission.groupby('fullVisitorId').sum().reset_index()\nsubmission.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:28:39.397577Z","iopub.execute_input":"2023-02-23T06:28:39.398682Z","iopub.status.idle":"2023-02-23T06:28:40.107981Z","shell.execute_reply.started":"2023-02-23T06:28:39.398633Z","shell.execute_reply":"2023-02-23T06:28:40.107094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:28:40.109320Z","iopub.execute_input":"2023-02-23T06:28:40.110409Z","iopub.status.idle":"2023-02-23T06:28:40.776933Z","shell.execute_reply.started":"2023-02-23T06:28:40.110373Z","shell.execute_reply":"2023-02-23T06:28:40.775828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_df\ndel X_test\ndel submission\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T06:28:40.778869Z","iopub.execute_input":"2023-02-23T06:28:40.779326Z","iopub.status.idle":"2023-02-23T06:28:42.546865Z","shell.execute_reply.started":"2023-02-23T06:28:40.779291Z","shell.execute_reply":"2023-02-23T06:28:42.545535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}