Skip to content

Commit

Permalink
deploy: dc6d256
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Dec 24, 2023
1 parent f5ab4da commit 66e5cef
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 1 deletion.
81 changes: 81 additions & 0 deletions _sources/book/machinelearning/modeltraining.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
55 changes: 55 additions & 0 deletions _sources/book/machinelearning/timeseries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
59 changes: 59 additions & 0 deletions book/machinelearning/modeltraining.html
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#model-ensembling-with-combo">5.5.23. Model Ensembling with <code class="docutils literal notranslate"><span class="pre">combo</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#residual-plots-with-yellowbrick">5.5.24. Residual Plots with <code class="docutils literal notranslate"><span class="pre">yellowbrick</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#powerful-and-distributed-hyperparameter-optimization-with-ray-tune">5.5.25. Powerful and Distributed Hyperparameter Optimization with <code class="docutils literal notranslate"><span class="pre">ray.tune</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#use-pytorch-with-scikit-learn-api-with-skorch">5.5.26. Use PyTorch with scikit-learn API with <code class="docutils literal notranslate"><span class="pre">skorch</span></code></a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -1503,6 +1504,63 @@ <h2><span class="section-number">5.5.25. </span>Powerful and Distributed Hyperpa
</div>
</div>
</section>
<section id="use-pytorch-with-scikit-learn-api-with-skorch">
<h2><span class="section-number">5.5.26. </span>Use PyTorch with scikit-learn API with <code class="docutils literal notranslate"><span class="pre">skorch</span></code><a class="headerlink" href="#use-pytorch-with-scikit-learn-api-with-skorch" title="Permalink to this heading">#</a></h2>
<p>PyTorch and scikit-learn are one of the most popular libraries for ML/DL.</p>
<p>So, why not combine PyTorch with scikit-learn?</p>
<p>Try <code class="docutils literal notranslate"><span class="pre">skorch</span></code>!</p>
<p><code class="docutils literal notranslate"><span class="pre">skorch</span></code> is a high-level library for PyTorch that provides a scikit-learn-compatible neural network module.</p>
<p>It allows you to use the simple scikit-learn interface for PyTorch.</p>
<p>Therefore you can integrate PyTorch models into scikit-learn workflows.</p>
<p>See below for an example.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>pip<span class="w"> </span>install<span class="w"> </span>skorch
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">torch</span> <span class="kn">import</span> <span class="n">nn</span>
<span class="kn">from</span> <span class="nn">skorch</span> <span class="kn">import</span> <span class="n">NeuralNetClassifier</span>
<span class="kn">from</span> <span class="nn">sklearn.pipeline</span> <span class="kn">import</span> <span class="n">Pipeline</span>
<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="kn">import</span> <span class="n">StandardScaler</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_units</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">nonlin</span><span class="o">=</span><span class="n">nn</span><span class="o">.</span><span class="n">ReLU</span><span class="p">()):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

<span class="bp">self</span><span class="o">.</span><span class="n">dense</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="n">num_units</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">nonlin</span> <span class="o">=</span> <span class="n">nonlin</span>
<span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="n">num_units</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">softmax</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Softmax</span><span class="p">(</span><span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">X</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nonlin</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span><span class="n">X</span><span class="p">))</span>
<span class="n">X</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dropout</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
<span class="n">X</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">X</span><span class="p">))</span>
<span class="k">return</span> <span class="n">X</span>

<span class="n">net</span> <span class="o">=</span> <span class="n">NeuralNetClassifier</span><span class="p">(</span>
<span class="n">MyModule</span><span class="p">,</span>
<span class="n">max_epochs</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>
<span class="n">lr</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span>
<span class="n">iterator_train__shuffle</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="p">)</span>

<span class="n">pipe</span> <span class="o">=</span> <span class="n">Pipeline</span><span class="p">([</span>
<span class="p">(</span><span class="s1">&#39;scale&#39;</span><span class="p">,</span> <span class="n">StandardScaler</span><span class="p">()),</span>
<span class="p">(</span><span class="s1">&#39;net&#39;</span><span class="p">,</span> <span class="n">net</span><span class="p">),</span>
<span class="p">])</span>

<span class="n">pipe</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">y_proba</span> <span class="o">=</span> <span class="n">pipe</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -1597,6 +1655,7 @@ <h2><span class="section-number">5.5.25. </span>Powerful and Distributed Hyperpa
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#model-ensembling-with-combo">5.5.23. Model Ensembling with <code class="docutils literal notranslate"><span class="pre">combo</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#residual-plots-with-yellowbrick">5.5.24. Residual Plots with <code class="docutils literal notranslate"><span class="pre">yellowbrick</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#powerful-and-distributed-hyperparameter-optimization-with-ray-tune">5.5.25. Powerful and Distributed Hyperparameter Optimization with <code class="docutils literal notranslate"><span class="pre">ray.tune</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#use-pytorch-with-scikit-learn-api-with-skorch">5.5.26. Use PyTorch with scikit-learn API with <code class="docutils literal notranslate"><span class="pre">skorch</span></code></a></li>
</ul>
</nav></div>

Expand Down
38 changes: 38 additions & 0 deletions book/machinelearning/timeseries.html
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-cross-validation-with-tscv">5.7.3. More Cross-Validation with <code class="docutils literal notranslate"><span class="pre">tscv</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#time-series-forecasting-with-machine-learning-with-mlforecast">5.7.4. Time Series Forecasting with Machine Learning with <code class="docutils literal notranslate"><span class="pre">mlforecast</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#lightning-fast-time-series-forecasting-with-statsforecast">5.7.5. Lightning Fast Time Series Forecasting with <code class="docutils literal notranslate"><span class="pre">statsforecast</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#time-series-with-polars-backend-with-functime">5.7.6. Time Series with Polars Backend with <code class="docutils literal notranslate"><span class="pre">functime</span></code></a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -584,6 +585,42 @@ <h2><span class="section-number">5.7.5. </span>Lightning Fast Time Series Foreca
</div>
</div>
</section>
<section id="time-series-with-polars-backend-with-functime">
<h2><span class="section-number">5.7.6. </span>Time Series with Polars Backend with <code class="docutils literal notranslate"><span class="pre">functime</span></code><a class="headerlink" href="#time-series-with-polars-backend-with-functime" title="Permalink to this heading">#</a></h2>
<p>Fast time-series forecasting with <code class="docutils literal notranslate"><span class="pre">functime</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">functime</span></code> is a Python library for time series forecasting and feature extraction, built with Polars.</p>
<p>Since it uses lazy Polars dataframes, <code class="docutils literal notranslate"><span class="pre">functime</span></code> speeds up forecasting and feature engineering.</p>
<p>Backtesting, cross-validation splitters and metrics are included too.</p>
<p>It even comes with a LLM agent to analyze and describe your forecasts.</p>
<p>Check it out!</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span>!pip install functime
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">polars</span> <span class="k">as</span> <span class="nn">pl</span>
<span class="kn">from</span> <span class="nn">functime.cross_validation</span> <span class="kn">import</span> <span class="n">train_test_split</span>
<span class="kn">from</span> <span class="nn">functime.forecasting</span> <span class="kn">import</span> <span class="n">linear_model</span>
<span class="kn">from</span> <span class="nn">functime.metrics</span> <span class="kn">import</span> <span class="n">mase</span>

<span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">pipe</span><span class="p">(</span><span class="n">train_test_split</span><span class="p">(</span><span class="n">test_size</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>

<span class="n">forecaster</span> <span class="o">=</span> <span class="n">linear_model</span><span class="p">(</span><span class="n">freq</span><span class="o">=</span><span class="s2">&quot;1mo&quot;</span><span class="p">,</span> <span class="n">lags</span><span class="o">=</span><span class="mi">24</span><span class="p">)</span>
<span class="n">forecaster</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">y_train</span><span class="p">)</span>
<span class="n">y_pred</span> <span class="o">=</span> <span class="n">forecaster</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">fh</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="n">y_pred</span> <span class="o">=</span> <span class="n">linear_model</span><span class="p">(</span><span class="n">freq</span><span class="o">=</span><span class="s2">&quot;1mo&quot;</span><span class="p">,</span> <span class="n">lags</span><span class="o">=</span><span class="mi">24</span><span class="p">)(</span><span class="n">y</span><span class="o">=</span><span class="n">y_train</span><span class="p">,</span> <span class="n">fh</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="n">scores</span> <span class="o">=</span> <span class="n">mase</span><span class="p">(</span><span class="n">y_true</span><span class="o">=</span><span class="n">y_test</span><span class="p">,</span> <span class="n">y_pred</span><span class="o">=</span><span class="n">y_pred</span><span class="p">,</span> <span class="n">y_train</span><span class="o">=</span><span class="n">y_train</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -658,6 +695,7 @@ <h2><span class="section-number">5.7.5. </span>Lightning Fast Time Series Foreca
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#more-cross-validation-with-tscv">5.7.3. More Cross-Validation with <code class="docutils literal notranslate"><span class="pre">tscv</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#time-series-forecasting-with-machine-learning-with-mlforecast">5.7.4. Time Series Forecasting with Machine Learning with <code class="docutils literal notranslate"><span class="pre">mlforecast</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#lightning-fast-time-series-forecasting-with-statsforecast">5.7.5. Lightning Fast Time Series Forecasting with <code class="docutils literal notranslate"><span class="pre">statsforecast</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#time-series-with-polars-backend-with-functime">5.7.6. Time Series with Polars Backend with <code class="docutils literal notranslate"><span class="pre">functime</span></code></a></li>
</ul>
</nav></div>

Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

0 comments on commit 66e5cef

Please sign in to comment.