{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":203900450,"sourceType":"kernelVersion"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install tsai","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:12:20.093945Z","iopub.execute_input":"2024-12-28T15:12:20.094323Z","iopub.status.idle":"2024-12-28T15:14:54.881226Z","shell.execute_reply.started":"2024-12-28T15:12:20.094278Z","shell.execute_reply":"2024-12-28T15:14:54.880245Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!git clone https://github.com/timeseriesAI/tsai","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:14:54.882542Z","iopub.execute_input":"2024-12-28T15:14:54.882768Z","iopub.status.idle":"2024-12-28T15:15:37.996828Z","shell.execute_reply.started":"2024-12-28T15:14:54.88275Z","shell.execute_reply":"2024-12-28T15:15:37.995957Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%cd tsai\n!pip install -e .[dev]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:15:37.998568Z","iopub.execute_input":"2024-12-28T15:15:37.998792Z","iopub.status.idle":"2024-12-28T15:16:03.020688Z","shell.execute_reply.started":"2024-12-28T15:15:37.998774Z","shell.execute_reply":"2024-12-28T15:16:03.019573Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tsai.basics import *\n\nX, y, splits = get_regression_data('AppliancesEnergy', split_data=False)\ntfms = [None, TSRegression()]\nbatch_tfms = TSStandardize(by_sample=True)\nreg = TSRegressor(X, y, splits=splits, path='models', arch=\"TSTPlus\", tfms=tfms, batch_tfms=batch_tfms, metrics=rmse, cbs=ShowGraph(), verbose=True)\nreg.fit_one_cycle(100, 3e-4)\nreg.export(\"reg.pkl\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:16:03.021773Z","iopub.execute_input":"2024-12-28T15:16:03.022007Z","iopub.status.idle":"2024-12-28T15:16:49.312214Z","shell.execute_reply.started":"2024-12-28T15:16:03.021988Z","shell.execute_reply":"2024-12-28T15:16:49.311534Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport polars as pl\nimport numpy as np\nimport gc\nfrom matplotlib import pyplot as plt\n\ntraining = pl.scan_parquet(\n    f\"/kaggle/input/js24-preprocessing-create-lags/training.parquet/\"\n).collect().to_pandas().iloc[-100_000:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:51:39.213116Z","iopub.execute_input":"2024-12-28T15:51:39.213474Z","iopub.status.idle":"2024-12-28T15:52:14.083225Z","shell.execute_reply.started":"2024-12-28T15:51:39.213445Z","shell.execute_reply":"2024-12-28T15:52:14.073409Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T14:32:06.528958Z","iopub.execute_input":"2024-12-28T14:32:06.529533Z","iopub.status.idle":"2024-12-28T14:32:06.535527Z","shell.execute_reply.started":"2024-12-28T14:32:06.529499Z","shell.execute_reply":"2024-12-28T14:32:06.534482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training = training.drop(\"id\", axis=1).fillna(-6)\ntraining[\"date_id\"] = [float(f'{training[\"date_id\"].iloc[i]}.{training[\"time_id\"].iloc[i]}') for i in range(len(training))]\ntraining = training.drop(\"time_id\", axis=1)\n\nX = np.array(training.drop(\"responder_6\", axis=1)).reshape(len(training), 101, 1)\ny = np.array(training[\"responder_6\"])\nX.shape, y.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:52:14.087269Z","iopub.execute_input":"2024-12-28T15:52:14.087581Z","iopub.status.idle":"2024-12-28T15:52:16.747972Z","shell.execute_reply.started":"2024-12-28T15:52:14.087558Z","shell.execute_reply":"2024-12-28T15:52:16.746877Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"splits = [list(range(int(len(X)*0.8))), list(range(int(len(X)*0.8), len(X), 1))]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:24:06.996562Z","iopub.execute_input":"2024-12-28T15:24:06.996778Z","iopub.status.idle":"2024-12-28T15:24:07.037196Z","shell.execute_reply.started":"2024-12-28T15:24:06.996761Z","shell.execute_reply":"2024-12-28T15:24:07.036161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tfms = [None, TSRegression()]\nbatch_tfms = TSStandardize(by_sample=True)\nreg = TSRegressor(X, y, splits=splits, path='models', arch=\"TSTPlus\", tfms=tfms, batch_tfms=batch_tfms, metrics=rmse, cbs=ShowGraph(), verbose=True)\nreg.fit_one_cycle(15, 3e-4)\nreg.export(\"reg.pkl\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:25:43.554615Z","iopub.execute_input":"2024-12-28T15:25:43.555094Z","iopub.status.idle":"2024-12-28T15:51:31.106841Z","shell.execute_reply.started":"2024-12-28T15:25:43.555059Z","shell.execute_reply":"2024-12-28T15:51:31.10528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ts = training\nts.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:54:50.396412Z","iopub.execute_input":"2024-12-28T15:54:50.396793Z","iopub.status.idle":"2024-12-28T15:54:50.402781Z","shell.execute_reply.started":"2024-12-28T15:54:50.396769Z","shell.execute_reply":"2024-12-28T15:54:50.401785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X, y = SlidingWindow(window_len=60, horizon=3)(ts)\n\n# Split the data into training and validation sets\nsplits = TimeSplitter(valid_size=0.2, fcst_horizon=3)(y)  # 80% train, 20% validation\n\n# Check the shapes\nprint(f\"Input (X) shape: {X.shape}\")  # Expected: (samples, 60, 3)\nprint(f\"Target (y) shape: {y.shape}\")  # Expected: (samples, 3, 3)\nprint(f\"Train indices: {splits[0][:5]}\")  # Example train indices\nprint(f\"Validation indices: {splits[1][:5]}\")  # Example valid indices","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:55:05.111584Z","iopub.execute_input":"2024-12-28T15:55:05.111915Z","iopub.status.idle":"2024-12-28T15:55:08.01972Z","shell.execute_reply.started":"2024-12-28T15:55:05.111893Z","shell.execute_reply":"2024-12-28T15:55:08.018291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define transformations\ntfms = [None, TSRegression()]\nbatch_tfms = TSStandardize(by_sample=True)\n\n# Train a time series regressor\nreg = TSRegressor(X, y, splits=splits, path='models',\n                  arch=\"TSTPlus\", tfms=tfms, batch_tfms=batch_tfms, \n                  metrics=rmse, cbs=ShowGraph(), verbose=True)\n\n# Train the model\nreg.fit_one_cycle(15, 3e-4)\n\n# Export the trained model\nreg.export(\"multi_var_model.pkl\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:55:22.89944Z","iopub.execute_input":"2024-12-28T15:55:22.89976Z","iopub.status.idle":"2024-12-28T16:03:08.599012Z","shell.execute_reply.started":"2024-12-28T15:55:22.899737Z","shell.execute_reply":"2024-12-28T16:03:08.597926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the trained model\nmodel = load_learner(\"multi_var_model.pkl\")\n\n# Use the last 60 time steps from your dataset as input\nlast_data = ts[-60:]  # Shape: (60, 3)\nlast_data = np.expand_dims(last_data, axis=0)  # Add batch dimension: (1, 60, 3)\n\n# Predict the next 3 steps\npredictions = model.predict(last_data)\nprint(\"Predicted values:\", predictions)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ts = get_forecasting_time_series(\"Sunspots\").values\nX, y = SlidingWindow(60, horizon=3)(ts)\nsplits = TimeSplitter(235, fcst_horizon=3)(y) \ntfms = [None, TSForecasting()]\nbatch_tfms = TSStandardize()\nfcst = TSForecaster(X, y, splits=splits, path='models', tfms=tfms, batch_tfms=batch_tfms, bs=512, arch=\"TSTPlus\", metrics=mae, cbs=ShowGraph())\nfcst.fit_one_cycle(50, 1e-3)\nfcst.export(\"fcst.pkl\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}