{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":89688,"databundleVersionId":10362377,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"! pip install -q etna \"pandas<2.0\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:37:32.935062Z","iopub.execute_input":"2024-12-03T06:37:32.935793Z","iopub.status.idle":"2024-12-03T06:38:08.163563Z","shell.execute_reply.started":"2024-12-03T06:37:32.935751Z","shell.execute_reply":"2024-12-03T06:38:08.162432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndata = \"/kaggle/input/forecasting-future-ts-hse/train.parquet\"\n\ndf = pd.read_parquet(data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:38:08.165708Z","iopub.execute_input":"2024-12-03T06:38:08.166058Z","iopub.status.idle":"2024-12-03T06:38:14.436027Z","shell.execute_reply.started":"2024-12-03T06:38:08.166025Z","shell.execute_reply":"2024-12-03T06:38:14.435115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"freqs = df.freq.unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:38:14.437240Z","iopub.execute_input":"2024-12-03T06:38:14.437689Z","iopub.status.idle":"2024-12-03T06:38:14.443064Z","shell.execute_reply.started":"2024-12-03T06:38:14.437656Z","shell.execute_reply":"2024-12-03T06:38:14.442035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"freq_x_horizon = {\n'10T': 48,\n'H': 48,\n'5T': 48,\n'W-SUN': 13,\n'10S': 60,\n'D': 14,\n'W-FRI': 8,\n'M': 12,\n'Q-DEC': 8,\n'15T': 48,\n'W-THU': 8,\n'A-DEC': 6,\n'W-WED': 8,\n'W-TUE': 8\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:38:14.444258Z","iopub.execute_input":"2024-12-03T06:38:14.444603Z","iopub.status.idle":"2024-12-03T06:38:14.457247Z","shell.execute_reply.started":"2024-12-03T06:38:14.444572Z","shell.execute_reply":"2024-12-03T06:38:14.456383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from etna.models import CatBoostMultiSegmentModel\nfrom etna.models import NaiveModel\nfrom etna.pipeline import Pipeline\nfrom etna.transforms import DateFlagsTransform\nfrom etna.transforms import SegmentEncoderTransform\nfrom etna.datasets import TSDataset\n\nfrom copy import deepcopy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T06:38:14.459299Z","iopub.execute_input":"2024-12-03T06:38:14.459625Z","iopub.status.idle":"2024-12-03T06:38:19.466353Z","shell.execute_reply.started":"2024-12-03T06:38:14.459593Z","shell.execute_reply":"2024-12-03T06:38:19.465523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"forecast\"] = None","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T07:08:40.282844Z","iopub.status.idle":"2024-12-03T07:08:40.283393Z","shell.execute_reply.started":"2024-12-03T07:08:40.283129Z","shell.execute_reply":"2024-12-03T07:08:40.283155Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for freq in freqs:\n\n    \n    df_view = deepcopy(df[df.freq == freq])\n    print(df_view.head())\n    horizon = freq_x_horizon[freq]\n\n\n    start_date = df_view[\"start\"].max()\n    max_len = df_view[\"target\"].apply(len).max()\n    dt_range = pd.date_range(start=start_date, periods=max_len, freq=freq)\n\n    df_view[\"timestamp\"] = df_view.target.apply(lambda x: dt_range[-len(x):])\n\n\n    df_train = (\n        df_view\n        .explode([\"timestamp\", \"target\"])\n        [[\"id\", \"target\",\"timestamp\"]]\n        .rename(columns={\n            \"id\": \"segment\",\n            \"target\": \"target\",\n            \"timestamp\": \"timestamp\"\n        })\n    )\n\n    df_train[\"target\"] = df_train[\"target\"].astype(\"float\")\n\n    transforms = []\n\n    model = NaiveModel()\n    \n    pipeline = Pipeline(model=model, transforms=transforms, horizon=horizon)\n    \n    ts = TSDataset(df_train, freq=freq)\n    \n    pipeline.fit(deepcopy(ts))\n\n    ts_forecast = pipeline.forecast(ts)\n    \n    dict_forecast = (\n        ts_forecast\n        .to_pandas(features=[\"target\"])\n        .droplevel(level=\"feature\", axis=1)\n        .to_dict(orient=\"list\")\n    )\n    \n    for idx, i in enumerate(df_view.index):\n        df.at[i, \"forecast\"] = dict_forecast[df.at[i, \"id\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T07:08:54.521330Z","iopub.execute_input":"2024-12-03T07:08:54.521754Z","iopub.status.idle":"2024-12-03T07:13:41.878195Z","shell.execute_reply.started":"2024-12-03T07:08:54.521717Z","shell.execute_reply":"2024-12-03T07:13:41.876969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[[\"id\", \"forecast\"]].to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T07:13:41.880092Z","iopub.execute_input":"2024-12-03T07:13:41.880443Z","iopub.status.idle":"2024-12-03T07:13:42.140519Z","shell.execute_reply.started":"2024-12-03T07:13:41.880407Z","shell.execute_reply":"2024-12-03T07:13:42.139289Z"}},"outputs":[],"execution_count":null}]}