{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Transforming Target\n", "One of the common requirements is to transform the target variable itself. \n", "hcrystalball implements ``TargetTransformer`` transformer for such a cases, that internally scales input data and return un-scaled again." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "plt.style.use('seaborn')\n", "plt.rcParams['figure.figsize'] = [12, 6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from hcrystalball.utils import get_sales_data\n", "\n", "df = get_sales_data(n_dates=100, \n", " n_assortments=1, \n", " n_states=1, \n", " n_stores=1)\n", "X, y = pd.DataFrame(index=df.index), df['Sales']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from hcrystalball.wrappers import get_sklearn_wrapper\n", "from hcrystalball.compose import TSColumnTransformer\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.ensemble import RandomForestRegressor" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from hcrystalball.preprocessing import TargetTransformer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## With Wrapper" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rf_model = get_sklearn_wrapper(RandomForestRegressor)\n", "scaled = TargetTransformer(rf_model, StandardScaler())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(scaled.fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", " .merge(y, left_index=True, right_index=True, how='outer')\n", " .tail(50)\n", " .plot()\n", ");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## With Pipeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X['trend'] = np.arange(len(X))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add standard scaler to the trend and let the date column pass in raw form\n", "preprocessing = TSColumnTransformer(\n", " transformers=[ \n", " ('scaler', StandardScaler(), ['trend'])\n", " ]) \n", "# define random forest model\n", "rf_model = get_sklearn_wrapper(RandomForestRegressor)\n", "# glue it together\n", "sklearn_model_pipeline = Pipeline([\n", " ('preprocessing', preprocessing), \n", " ('model', rf_model)\n", "]) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scaled_pipeline = TargetTransformer(sklearn_model_pipeline, StandardScaler())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(scaled_pipeline.fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", " .merge(y, left_index=True, right_index=True, how='outer')\n", " .tail(50)\n", " .plot()\n", ");" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }