{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b429d931", "metadata": {}, "outputs": [], "source": [ "!wget https://raw.githubusercontent.com/mlzoo/multi-step-regression-tutorials/main/Raotbl6.csv\n", "# !pip install lightgbm" ] }, { "cell_type": "code", "execution_count": null, "id": "cd78d743", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import lightgbm as lgb\n", "from sklearn.metrics import mean_squared_error\n", "from sklearn.multioutput import MultiOutputRegressor\n", "\n", "df = pd.read_csv('Raotbl6.csv')\n", "df['date'] = pd.to_datetime(df['date'])\n", "df.index = df['date']\n", "df.drop('date', axis=1, inplace=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "cf306d1f", "metadata": {}, "outputs": [], "source": [ "# make future targets\n", "for i in range(12):\n", " df['rgnp_{}'.format(i)] = df['rgnp'].shift(-i-1)\n", "\n", "df.dropna(inplace=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "58042d5f", "metadata": {}, "outputs": [], "source": [ "targets = [item for item in df.columns if 'rgnp_' in item]\n", "\n", "X_train = df.drop(targets, axis=1)[: int(len(df) * 0.8)]\n", "y_train = df[targets][: int(len(df) * 0.8)]\n", "\n", "X_test = df.drop(targets, axis=1)[int(len(df) * 0.8) :]\n", "y_test = df[targets][int(len(df) * 0.8) :]\n", "\n", "model = MultiOutputRegressor(lgb.LGBMRegressor(objective='regression')).fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": null, "id": "6b0617b2", "metadata": {}, "outputs": [], "source": [ "pred = pd.DataFrame(model.predict(X_test), columns=targets)\n", "pred.index = y_test.index\n", "(y_test - pred).abs().mean()" ] } ], "metadata": { "kernelspec": { "display_name": "conda_pytorch_p38", "language": "python", "name": "conda_pytorch_p38" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }