From 66e5cef6a099d6708d0252f86d85f3f6a4eae971 Mon Sep 17 00:00:00 2001 From: baniasbaabe Date: Sun, 24 Dec 2023 12:30:32 +0000 Subject: [PATCH] deploy: dc6d256edb26a61a34a2091638629e9219398d0c --- .../book/machinelearning/modeltraining.ipynb | 81 +++++++++++++++++++ .../book/machinelearning/timeseries.ipynb | 55 +++++++++++++ book/machinelearning/modeltraining.html | 59 ++++++++++++++ book/machinelearning/timeseries.html | 38 +++++++++ searchindex.js | 2 +- 5 files changed, 234 insertions(+), 1 deletion(-) diff --git a/_sources/book/machinelearning/modeltraining.ipynb b/_sources/book/machinelearning/modeltraining.ipynb index 21bc867..bbf373f 100644 --- a/_sources/book/machinelearning/modeltraining.ipynb +++ b/_sources/book/machinelearning/modeltraining.ipynb @@ -1644,6 +1644,87 @@ "results = tuner.fit()\n", "print(results.get_best_result(metric=\"mean_accuracy\", mode=\"max\").config)" ] + }, + { + "cell_type": "markdown", + "id": "62334c73", + "metadata": {}, + "source": [ + "## Use PyTorch with scikit-learn API with `skorch`" + ] + }, + { + "cell_type": "markdown", + "id": "4ac45c78", + "metadata": {}, + "source": [ + "PyTorch and scikit-learn are one of the most popular libraries for ML/DL.\n", + "\n", + "So, why not combine PyTorch with scikit-learn?\n", + "\n", + "Try `skorch`!\n", + "\n", + "`skorch` is a high-level library for PyTorch that provides a scikit-learn-compatible neural network module.\n", + "\n", + "It allows you to use the simple scikit-learn interface for PyTorch.\n", + "\n", + "Therefore you can integrate PyTorch models into scikit-learn workflows.\n", + "\n", + "See below for an example." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "09a00fe5", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install skorch" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad9ed770", + "metadata": {}, + "outputs": [], + "source": [ + "from torch import nn\n", + "from skorch import NeuralNetClassifier\n", + "from sklearn.pipeline import Pipeline\n", + "from sklearn.preprocessing import StandardScaler\n", + "\n", + "class MyModule(nn.Module):\n", + " def __init__(self, num_units=10, nonlin=nn.ReLU()):\n", + " super().__init__()\n", + "\n", + " self.dense = nn.Linear(20, num_units)\n", + " self.nonlin = nonlin\n", + " self.output = nn.Linear(num_units, 2)\n", + " self.softmax = nn.Softmax(dim=-1)\n", + "\n", + " def forward(self, X, **kwargs):\n", + " X = self.nonlin(self.dense(X))\n", + " X = self.dropout(X)\n", + " X = self.softmax(self.output(X))\n", + " return X\n", + "\n", + "net = NeuralNetClassifier(\n", + " MyModule,\n", + " max_epochs=10,\n", + " lr=0.1,\n", + " iterator_train__shuffle=True,\n", + ")\n", + "\n", + "pipe = Pipeline([\n", + " ('scale', StandardScaler()),\n", + " ('net', net),\n", + "])\n", + "\n", + "pipe.fit(X, y)\n", + "y_proba = pipe.predict_proba(X)" + ] } ], "metadata": { diff --git a/_sources/book/machinelearning/timeseries.ipynb b/_sources/book/machinelearning/timeseries.ipynb index ec94296..16a0af1 100644 --- a/_sources/book/machinelearning/timeseries.ipynb +++ b/_sources/book/machinelearning/timeseries.ipynb @@ -252,6 +252,61 @@ "sf.fit(df)\n", "sf.predict(h=12, level=[95])" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Time Series with Polars Backend with `functime`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Fast time-series forecasting with `functime`.\n", + "\n", + "`functime` is a Python library for time series forecasting and feature extraction, built with Polars.\n", + "\n", + "Since it uses lazy Polars dataframes, `functime` speeds up forecasting and feature engineering.\n", + "\n", + "Backtesting, cross-validation splitters and metrics are included too.\n", + "\n", + "It even comes with a LLM agent to analyze and describe your forecasts.\n", + "\n", + "Check it out!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "!pip install functime" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import polars as pl\n", + "from functime.cross_validation import train_test_split\n", + "from functime.forecasting import linear_model\n", + "from functime.metrics import mase\n", + "\n", + "y_train, y_test = y.pipe(train_test_split(test_size=3))\n", + "\n", + "forecaster = linear_model(freq=\"1mo\", lags=24)\n", + "forecaster.fit(y=y_train)\n", + "y_pred = forecaster.predict(fh=3)\n", + "\n", + "y_pred = linear_model(freq=\"1mo\", lags=24)(y=y_train, fh=3)\n", + "\n", + "scores = mase(y_true=y_test, y_pred=y_pred, y_train=y_train)" + ] } ], "metadata": { diff --git a/book/machinelearning/modeltraining.html b/book/machinelearning/modeltraining.html index 9c10433..00f8d46 100644 --- a/book/machinelearning/modeltraining.html +++ b/book/machinelearning/modeltraining.html @@ -440,6 +440,7 @@

Contents

  • 5.5.23. Model Ensembling with combo
  • 5.5.24. Residual Plots with yellowbrick
  • 5.5.25. Powerful and Distributed Hyperparameter Optimization with ray.tune
  • +
  • 5.5.26. Use PyTorch with scikit-learn API with skorch
  • @@ -1503,6 +1504,63 @@

    5.5.25. Powerful and Distributed Hyperpa +
    +

    5.5.26. Use PyTorch with scikit-learn API with skorch#

    +

    PyTorch and scikit-learn are one of the most popular libraries for ML/DL.

    +

    So, why not combine PyTorch with scikit-learn?

    +

    Try skorch!

    +

    skorch is a high-level library for PyTorch that provides a scikit-learn-compatible neural network module.

    +

    It allows you to use the simple scikit-learn interface for PyTorch.

    +

    Therefore you can integrate PyTorch models into scikit-learn workflows.

    +

    See below for an example.

    +
    +
    +
    !pip install skorch
    +
    +
    +
    +
    +
    +
    +
    from torch import nn
    +from skorch import NeuralNetClassifier
    +from sklearn.pipeline import Pipeline
    +from sklearn.preprocessing import StandardScaler
    +
    +class MyModule(nn.Module):
    +    def __init__(self, num_units=10, nonlin=nn.ReLU()):
    +        super().__init__()
    +
    +        self.dense = nn.Linear(20, num_units)
    +        self.nonlin = nonlin
    +        self.output = nn.Linear(num_units, 2)
    +        self.softmax = nn.Softmax(dim=-1)
    +
    +    def forward(self, X, **kwargs):
    +        X = self.nonlin(self.dense(X))
    +        X = self.dropout(X)
    +        X = self.softmax(self.output(X))
    +        return X
    +
    +net = NeuralNetClassifier(
    +    MyModule,
    +    max_epochs=10,
    +    lr=0.1,
    +    iterator_train__shuffle=True,
    +)
    +
    +pipe = Pipeline([
    +    ('scale', StandardScaler()),
    +    ('net', net),
    +])
    +
    +pipe.fit(X, y)
    +y_proba = pipe.predict_proba(X)
    +
    +
    +
    +
    +