{"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":"gpu","dataSources":[{"sourceId":10038,"databundleVersionId":180088,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\nimport json\nimport matplotlib.pyplot as plt\nfrom sklearn import model_selection, metrics\nimport lightgbm as lgb\nimport optuna\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn import metrics\nimport joblib\nimport category_encoders as ce\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-08-08T16:19:23.267638Z","iopub.execute_input":"2024-08-08T16:19:23.267910Z","iopub.status.idle":"2024-08-08T16:19:29.155703Z","shell.execute_reply.started":"2024-08-08T16:19:23.267887Z","shell.execute_reply":"2024-08-08T16:19:29.154693Z"},"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 = 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 = 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=30_000)\n\n     ","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:19:32.860941Z","iopub.execute_input":"2024-08-08T16:19:32.861923Z","iopub.status.idle":"2024-08-08T16:19:44.225078Z","shell.execute_reply.started":"2024-08-08T16:19:32.861872Z","shell.execute_reply":"2024-08-08T16:19:44.224259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:19:49.849460Z","iopub.execute_input":"2024-08-08T16:19:49.850169Z","iopub.status.idle":"2024-08-08T16:19:49.879389Z","shell.execute_reply.started":"2024-08-08T16:19:49.850137Z","shell.execute_reply":"2024-08-08T16:19:49.878490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['totals_transactionRevenue'].describe()","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:19:52.242673Z","iopub.execute_input":"2024-08-08T16:19:52.243392Z","iopub.status.idle":"2024-08-08T16:19:52.257298Z","shell.execute_reply.started":"2024-08-08T16:19:52.243357Z","shell.execute_reply":"2024-08-08T16:19:52.256364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['totals_transactionRevenue']\nX = df.drop(columns='totals_transactionRevenue')\n\n\nX_train, X_test, y_train, y_test = train_test_split(X, y,\n                                                    train_size=0.8,\n                                                    random_state=42)\n\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train,\n                                                  train_size=0.6/0.8,\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-08-08T16:19:54.552957Z","iopub.execute_input":"2024-08-08T16:19:54.553317Z","iopub.status.idle":"2024-08-08T16:19:54.775347Z","shell.execute_reply.started":"2024-08-08T16:19:54.553289Z","shell.execute_reply":"2024-08-08T16:19:54.774428Z"},"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\nnum_transformer = Pipeline(\n    steps=[(\"imputer\", SimpleImputer(strategy=\"median\"))]\n)\n\ncat_transformer = Pipeline(\n    steps=[(\"imputer\", SimpleImputer(strategy=\"most_frequent\")),\n           (\"encoder\", ce.BinaryEncoder())]  # Використання BinaryEncoder\n)\n\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-08-08T16:19:58.881145Z","iopub.execute_input":"2024-08-08T16:19:58.881492Z","iopub.status.idle":"2024-08-08T16:19:59.048531Z","shell.execute_reply.started":"2024-08-08T16:19:58.881466Z","shell.execute_reply":"2024-08-08T16:19:59.047655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = preprocessor.fit_transform(X_train)\n\nX_val = preprocessor.transform(X_val)\nX_test = preprocessor.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:20:02.598786Z","iopub.execute_input":"2024-08-08T16:20:02.600033Z","iopub.status.idle":"2024-08-08T16:20:07.587428Z","shell.execute_reply.started":"2024-08-08T16:20:02.599987Z","shell.execute_reply":"2024-08-08T16:20:07.586373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def objective(trial):\n  \"\"\"\n  Функція objective для Optuna, яка навчає модель LightGBMRegressor\n  на тренувальному наборі даних та оцінює її на тестовому наборі.\n\n  Args:\n      trial (optuna.Trial): Об'єкт Optuna, що представляє один набір гіперпараметрів.\n\n  Returns:\n      float: Значення метрики оцінки (R2).\n  \"\"\"\n\n\n  params = {\n        'objective': 'mse',\n        'n_estimators': 500,\n        'learning_rate': 0.01,\n        'verbose': -1,\n        'boosting_type': trial.suggest_categorical('boosting_type', ['gbdt', 'dart']),\n        'lambda_l1': trial.suggest_float('lambda_l1', 1e-3, 10.0, log=True),\n        'lambda_l2': trial.suggest_float('lambda_l2', 1e-3, 10.0, log=True),\n        'num_leaves': trial.suggest_int('num_leaves', 20, 60),\n        'feature_fraction': trial.suggest_float('feature_fraction', 0.4, 1.0),\n        'bagging_fraction': trial.suggest_float('bagging_fraction', 0.4, 1.0),\n        'bagging_freq': trial.suggest_int('bagging_freq', 1, 7),\n        'min_child_samples': trial.suggest_int('min_child_samples', 5, 100),\n    }\n\n\n  model = lgb.LGBMRegressor(**params)\n\n\n  model.fit(X_train, y_train)\n\n\n  y_pred = model.predict(X_val)\n  r2 = metrics.r2_score(y_val, y_pred)\n\n  return r2","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:32:39.011270Z","iopub.execute_input":"2024-08-08T16:32:39.011686Z","iopub.status.idle":"2024-08-08T16:32:39.020427Z","shell.execute_reply.started":"2024-08-08T16:32:39.011653Z","shell.execute_reply":"2024-08-08T16:32:39.019307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optuna.logging.set_verbosity(optuna.logging.WARNING) # щоб не виводило інформацію про trial\n\nstudy = optuna.create_study(direction='maximize')\nstudy.optimize(objective, n_trials=10)\n\nprint(f\"найкраща метрика: {study.best_value}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:32:42.694016Z","iopub.execute_input":"2024-08-08T16:32:42.694884Z","iopub.status.idle":"2024-08-08T16:33:52.275077Z","shell.execute_reply.started":"2024-08-08T16:32:42.694848Z","shell.execute_reply":"2024-08-08T16:33:52.274035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_params = study.best_params\n\nmodel = lgb.LGBMRegressor(**best_params,\n                          objective = 'mse',\n                          n_estimators = 500,\n                          verbose=-1)\n\nmodel.fit(X_train, y_train,\n          eval_set=[(X_test, y_test), (X_train, y_train)],\n          eval_metric='mae')","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:11.709571Z","iopub.execute_input":"2024-08-08T16:34:11.710288Z","iopub.status.idle":"2024-08-08T16:34:24.088516Z","shell.execute_reply.started":"2024-08-08T16:34:11.710252Z","shell.execute_reply":"2024-08-08T16:34:24.087621Z"},"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)\n     \n\nall_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-08-08T16:34:26.940138Z","iopub.execute_input":"2024-08-08T16:34:26.940609Z","iopub.status.idle":"2024-08-08T16:34:27.425588Z","shell.execute_reply.started":"2024-08-08T16:34:26.940538Z","shell.execute_reply":"2024-08-08T16:34:27.424630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optuna.visualization.plot_param_importances(study)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:33.706049Z","iopub.execute_input":"2024-08-08T16:34:33.706756Z","iopub.status.idle":"2024-08-08T16:34:34.021698Z","shell.execute_reply.started":"2024-08-08T16:34:33.706726Z","shell.execute_reply":"2024-08-08T16:34:34.020829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb.plot_importance(model)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:38.546011Z","iopub.execute_input":"2024-08-08T16:34:38.546362Z","iopub.status.idle":"2024-08-08T16:34:41.226449Z","shell.execute_reply.started":"2024-08-08T16:34:38.546337Z","shell.execute_reply":"2024-08-08T16:34:41.225558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb.plot_metric(model,\n                metric='l1')","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:45.629696Z","iopub.execute_input":"2024-08-08T16:34:45.630551Z","iopub.status.idle":"2024-08-08T16:34:45.962506Z","shell.execute_reply.started":"2024-08-08T16:34:45.630506Z","shell.execute_reply":"2024-08-08T16:34:45.961585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Pipeline(\n    [(\"prep\", preprocessor),\n     (\"model\", model)\n     ]\n)\n\nmodel","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:53.712328Z","iopub.execute_input":"2024-08-08T16:34:53.713162Z","iopub.status.idle":"2024-08-08T16:34:53.826437Z","shell.execute_reply.started":"2024-08-08T16:34:53.713127Z","shell.execute_reply":"2024-08-08T16:34:53.825598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joblib.dump(model, 'my_model.pkl')\n\nload_model = joblib.load('my_model.pkl')\nload_model","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:56.451470Z","iopub.execute_input":"2024-08-08T16:34:56.451851Z","iopub.status.idle":"2024-08-08T16:34:56.873280Z","shell.execute_reply.started":"2024-08-08T16:34:56.451822Z","shell.execute_reply":"2024-08-08T16:34:56.872288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = df.sample(1)\n\nmodel.predict(data)","metadata":{"execution":{"iopub.status.busy":"2024-08-08T16:34:59.455224Z","iopub.execute_input":"2024-08-08T16:34:59.455879Z","iopub.status.idle":"2024-08-08T16:34:59.589529Z","shell.execute_reply.started":"2024-08-08T16:34:59.455846Z","shell.execute_reply":"2024-08-08T16:34:59.588601Z"},"trusted":true},"execution_count":null,"outputs":[]}]}