{"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":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import json\nimport pandas as pd\n\n\ndef 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 = pd.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    \n    df.drop(columns=['customDimensions', 'hits'], inplace=True)\n    \n#     df['totals_pageviews'].fillna(0, inplace=True)\n#     df['totals_transactionRevenue'].fillna(0, inplace=True)\n    df = df.fillna({'totals_pageviews':0,\n                    'totals_transactionRevenue':0,\n                    'totals_pageviews':0})\n    \n    df['device_isMobile'] = df['device_isMobile'].astype(int)\n    df['totals_transactionRevenue'] = df['totals_transactionRevenue'].astype(int)\n    \n    cat_col = df.select_dtypes(include='object').columns\n    \n    df[cat_col] = df[cat_col].astype(str)\n    \n    return df\n\n\ndf = load_df('/kaggle/input/ga-customer-revenue-prediction/train_v2.csv', nrows=100_000)","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:07:22.515710Z","iopub.execute_input":"2024-10-09T18:07:22.516119Z","iopub.status.idle":"2024-10-09T18:08:01.796438Z","shell.execute_reply.started":"2024-10-09T18:07:22.516087Z","shell.execute_reply":"2024-10-09T18:08:01.795390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:08:01.798687Z","iopub.execute_input":"2024-10-09T18:08:01.799131Z","iopub.status.idle":"2024-10-09T18:08:02.190729Z","shell.execute_reply.started":"2024-10-09T18:08:01.799091Z","shell.execute_reply":"2024-10-09T18:08:02.189462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:08:02.192458Z","iopub.execute_input":"2024-10-09T18:08:02.192916Z","iopub.status.idle":"2024-10-09T18:08:02.883861Z","shell.execute_reply.started":"2024-10-09T18:08:02.192877Z","shell.execute_reply":"2024-10-09T18:08:02.882668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['totals_transactionRevenue']\nX = df.drop(columns='totals_transactionRevenue')","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:08:02.886302Z","iopub.execute_input":"2024-10-09T18:08:02.886673Z","iopub.status.idle":"2024-10-09T18:08:03.064460Z","shell.execute_reply.started":"2024-10-09T18:08:02.886643Z","shell.execute_reply":"2024-10-09T18:08:03.063306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.describe()","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:08:04.005139Z","iopub.execute_input":"2024-10-09T18:08:04.006148Z","iopub.status.idle":"2024-10-09T18:08:04.024852Z","shell.execute_reply.started":"2024-10-09T18:08:04.006102Z","shell.execute_reply":"2024-10-09T18:08:04.023725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y,\n                                                    test_size=0.1,\n                                                    random_state=42)\n\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train,\n                                                  train_size=0.8 / 0.9,\n                                                  random_state=42)\n\nprint(f'Train: {X_train.shape[0]} samples \\t {round(X_train.shape[0] / X.shape[0] * 100)}%')\nprint(f'Test:  {X_test.shape[0]} samples \\t {round(X_test.shape[0] / X.shape[0] * 100)}%')\nprint(f'Val:   {X_val.shape[0]} samples \\t {round(X_val.shape[0] / X.shape[0] * 100)}%')","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:10:59.928609Z","iopub.execute_input":"2024-10-09T18:10:59.929411Z","iopub.status.idle":"2024-10-09T18:11:01.695816Z","shell.execute_reply.started":"2024-10-09T18:10:59.929376Z","shell.execute_reply":"2024-10-09T18:11:01.694700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OrdinalEncoder, OneHotEncoder","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:11:01.697658Z","iopub.execute_input":"2024-10-09T18:11:01.697983Z","iopub.status.idle":"2024-10-09T18:11:01.872513Z","shell.execute_reply.started":"2024-10-09T18:11:01.697954Z","shell.execute_reply":"2024-10-09T18:11:01.871369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_columns = X.select_dtypes(include=\"number\").columns\ncat_columns = X.select_dtypes(include=\"object\").columns\n\n# числові ознаки\nnum_transformer = Pipeline(\n    steps=[(\"imputer\", SimpleImputer(strategy=\"median\"))]\n    )\n\n# категоріальні ознаки\ncat_transformer = Pipeline(\n    steps=[(\"imputer\", SimpleImputer(strategy=\"most_frequent\")),\n           (\"encoder\", OrdinalEncoder(handle_unknown='use_encoded_value',\n                                     unknown_value=-1))])\n\n# об'єднання двох pipeline\npreprocessor = ColumnTransformer(\n    transformers=[\n        (\"num\", num_transformer, num_columns),\n        (\"cat\", cat_transformer, cat_columns),\n    ]\n)\n\npreprocessor.set_output(transform='pandas')\npreprocessor","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:11:01.873875Z","iopub.execute_input":"2024-10-09T18:11:01.874264Z","iopub.status.idle":"2024-10-09T18:11:02.393526Z","shell.execute_reply.started":"2024-10-09T18:11:01.874221Z","shell.execute_reply":"2024-10-09T18:11:02.392383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = preprocessor.fit_transform(X_train)\nX_test = preprocessor.transform(X_test)\nX_val = preprocessor.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:11:18.822539Z","iopub.execute_input":"2024-10-09T18:11:18.823348Z","iopub.status.idle":"2024-10-09T18:11:27.534124Z","shell.execute_reply.started":"2024-10-09T18:11:18.823312Z","shell.execute_reply":"2024-10-09T18:11:27.532717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q optuna","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:11:27.536019Z","iopub.execute_input":"2024-10-09T18:11:27.536479Z","iopub.status.idle":"2024-10-09T18:11:44.203713Z","shell.execute_reply.started":"2024-10-09T18:11:27.536448Z","shell.execute_reply":"2024-10-09T18:11:44.202129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn import metrics\n\n\ndef objective(trial):\n    # make model\n    model = lgb.LGBMRegressor(\n        n_estimators = 20,\n        verbose = -1,\n        max_depth = trial.suggest_int('max_depth', 4, 20),\n        num_leaves = trial.suggest_int('num_leaves', 5, 100),\n        lambda_l1 = trial.suggest_float('lambda_l1', 1e-6, 1, log=True),\n        lambda_l2 = trial.suggest_float('lambda_l2', 1e-6, 1, log=True),\n        objective = trial.suggest_categorical('objective', ['mse', 'mae', 'huber']),\n        alpha = trial.suggest_float('alpha', 1e9, 1e10, log=True)\n    )\n    \n    # train\n    model.fit(X_train, y_train)\n    \n    # metric\n    y_pred = model.predict(X_val)\n    r2 = metrics.r2_score(y_val, y_pred)\n    \n    return r2\n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:12:34.748714Z","iopub.execute_input":"2024-10-09T18:12:34.749103Z","iopub.status.idle":"2024-10-09T18:12:35.926964Z","shell.execute_reply.started":"2024-10-09T18:12:34.749075Z","shell.execute_reply":"2024-10-09T18:12:35.925856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import optuna\n\nstudy = optuna.create_study(direction='maximize')\nstudy.optimize(objective, n_trials=100)","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:14:23.783877Z","iopub.execute_input":"2024-10-09T18:14:23.784755Z","iopub.status.idle":"2024-10-09T18:15:50.201004Z","shell.execute_reply.started":"2024-10-09T18:14:23.784717Z","shell.execute_reply":"2024-10-09T18:15:50.199230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study.best_params","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:17:25.851235Z","iopub.execute_input":"2024-10-09T18:17:25.851688Z","iopub.status.idle":"2024-10-09T18:17:25.864649Z","shell.execute_reply.started":"2024-10-09T18:17:25.851654Z","shell.execute_reply":"2024-10-09T18:17:25.863079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = study.best_params\n\nmodel = lgb.LGBMRegressor(\n    **params,\n    verbose=-1,\n    n_estimators=30\n)\n\nmodel.fit(X_train, y_train,\n          eval_set=[(X_val, y_val), (X_train, y_train)],\n          eval_metric='mae')\n\n\nlgb.plot_metric(model,\n                metric='l1')","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:22:08.563902Z","iopub.execute_input":"2024-10-09T18:22:08.564348Z","iopub.status.idle":"2024-10-09T18:22:10.033564Z","shell.execute_reply.started":"2024-10-09T18:22:08.564305Z","shell.execute_reply":"2024-10-09T18:22:10.032240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_metrics(y_true, y_pred, name=\"model\"):\n  df = pd.DataFrame()\n\n  df.loc[\"MAE\", name] = metrics.mean_absolute_error(y_true, y_pred)\n  df.loc[\"RMSE\", name] = metrics.mean_squared_error(y_true, y_pred) ** 0.5\n  df.loc[\"R2\", name] = metrics.r2_score(y_true, y_pred)\n\n  return df.round(2)","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:21:05.163306Z","iopub.execute_input":"2024-10-09T18:21:05.163706Z","iopub.status.idle":"2024-10-09T18:21:05.170986Z","shell.execute_reply.started":"2024-10-09T18:21:05.163677Z","shell.execute_reply":"2024-10-09T18:21:05.169776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_metrics = pd.DataFrame()\n\nall_metrics[\"train\"] = get_metrics(y_train, model.predict(X_train))\nall_metrics[\"test\"] = get_metrics(y_test, model.predict(X_test))\n\nall_metrics","metadata":{"execution":{"iopub.status.busy":"2024-10-09T18:21:15.221664Z","iopub.execute_input":"2024-10-09T18:21:15.222065Z","iopub.status.idle":"2024-10-09T18:21:15.366123Z","shell.execute_reply.started":"2024-10-09T18:21:15.222035Z","shell.execute_reply":"2024-10-09T18:21:15.364995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}