Skip to content

Commit

Permalink
deploy: 59091fd
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Jan 15, 2024
1 parent d9c69b4 commit afb055e
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 1 deletion.
55 changes: 55 additions & 0 deletions _sources/book/jupyternotebook/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,61 @@
"source": [
"!nbstripout FILE.ipynb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bring LLMs Into Your Notebook with `jupyter-ai`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bring GenAI into your Jupyter Notebooks with `jupyter-ai`\n",
"\n",
"`jupyter-ai` lets you use LLMs from vendors like OpenAI, Huggingface and Anthropic within your Notebook cells.\n",
"\n",
"You can just ask for a code snippet and the result will be rendered into your Notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%env PROVIDER_API_KEY=YOUR_API_KEY_HERE"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install jupyter_ai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext jupyter_ai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%ai chatgpt\n",
"Provide a hello world function in Python"
]
}
],
"metadata": {
Expand Down
52 changes: 52 additions & 0 deletions _sources/book/machinelearning/modeltraining.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,58 @@
"model = timm.create_model('densenet121', pretrained=True)\n",
"output = model(torch.randn(2, 3, 224, 224))"
]
},
{
"cell_type": "markdown",
"id": "69feb793",
"metadata": {},
"source": [
"## Generate Guaranteed Prediction Intervals and Sets with `MAPIE`"
]
},
{
"cell_type": "markdown",
"id": "41226b58",
"metadata": {},
"source": [
"For quantifying uncertainties of your models, use MAPIE.\n",
"\n",
"`MAPIE` (Model Agnostic Prediction Interval Estimator) takes your sklearn-/tensorflow-/pytorch-compatible model and generate prediction intervals or sets with guaranteed coverage."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7abfe51",
"metadata": {},
"outputs": [],
"source": [
"!pip install mapie"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecf3cdf1",
"metadata": {},
"outputs": [],
"source": [
"from mapie.regression import MapieRegressor\n",
"import numpy as np\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.datasets import make_regression\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=59)\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)\n",
"regressor = LinearRegression()\n",
"\n",
"mapie_regressor = MapieRegressor(regressor)\n",
"mapie_regressor.fit(X_train, y_train)\n",
"\n",
"alpha = [0.05, 0.20]\n",
"y_pred, y_pis = mapie_regressor.predict(X_test, alpha=alpha)"
]
}
],
"metadata": {
Expand Down
41 changes: 41 additions & 0 deletions _sources/book/pythontricks/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,47 @@
"increment_numbers() # Output: Numbers: [0, 1, 2]\n",
"increment_numbers() # Output: Numbers: [0, 1, 2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optimize Your Python Objects with `__slots__`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Optimize your Python classes with this trick.\n",
"\n",
"When creating objects, a dynamic dictionary is created too to store instance attributes.\n",
"\n",
"But, what if you know all of your attributes beforehand?\n",
"\n",
"With `__𝐬𝐥𝐨𝐭𝐬__`, you can specify which attributes your object instances will have.\n",
"\n",
"This saves space in memory and prevents users from creating new attributes at runtime.\n",
"\n",
"See below how setting a new instance attribute will throw an error."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Point:\n",
" __slots__ = ['x', 'y']\n",
"\n",
" def __init__(self, x, y):\n",
" self.x = x\n",
" self.y = y\n",
"\n",
"point = Point(2,4)\n",
"point.z = 5 # Throws an error"
]
}
],
"metadata": {
Expand Down
43 changes: 43 additions & 0 deletions _sources/book/testing/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,49 @@
" UnixFS.rm('file')\n",
" os.remove.assert_called_once_with('file')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Freeze Datetime Module For Testing with `freezegun`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you want to test functions with datetime,\n",
"\n",
"Consider using `freezegun` for Python.\n",
"\n",
"`freezegun` mocks the datetime module which makes testing deterministic.\n",
"\n",
"See below how we can specify a date and freeze the return value of datetime.datetime.now()."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install freezegun"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from freezegun import freeze_time\n",
"import datetime\n",
"\n",
"@freeze_time(\"2015-02-20\")\n",
"def test():\n",
" assert datetime.datetime.now() == datetime.datetime(2015, 2, 20)"
]
}
],
"metadata": {
Expand Down
37 changes: 37 additions & 0 deletions book/jupyternotebook/Chapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ <h2> Contents </h2>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#apply-code-quality-tools-to-jupyter-notebooks">3.1.6. Apply Code Quality Tools to Jupyter Notebooks</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#create-and-reuse-juptyer-notebook-templates">3.1.7. Create and Reuse Juptyer Notebook Templates</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#remove-output-cells-automatically-with-nbstripout">3.1.8. Remove Output Cells Automatically with <code class="docutils literal notranslate"><span class="pre">nbstripout</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#bring-llms-into-your-notebook-with-jupyter-ai">3.1.9. Bring LLMs Into Your Notebook with <code class="docutils literal notranslate"><span class="pre">jupyter-ai</span></code></a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -741,6 +742,41 @@ <h2><span class="section-number">3.1.8. </span>Remove Output Cells Automatically
</div>
</div>
</section>
<section id="bring-llms-into-your-notebook-with-jupyter-ai">
<h2><span class="section-number">3.1.9. </span>Bring LLMs Into Your Notebook with <code class="docutils literal notranslate"><span class="pre">jupyter-ai</span></code><a class="headerlink" href="#bring-llms-into-your-notebook-with-jupyter-ai" title="Permalink to this heading">#</a></h2>
<p>Bring GenAI into your Jupyter Notebooks with <code class="docutils literal notranslate"><span class="pre">jupyter-ai</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">jupyter-ai</span></code> lets you use LLMs from vendors like OpenAI, Huggingface and Anthropic within your Notebook cells.</p>
<p>You can just ask for a code snippet and the result will be rendered into your Notebook.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">env</span> <span class="n">PROVIDER_API_KEY</span><span class="o">=</span><span class="n">YOUR_API_KEY_HERE</span>
</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>!pip install jupyter_ai
</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="o">%</span><span class="n">load_ext</span> <span class="n">jupyter_ai</span>
</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="o">%%</span><span class="n">ai</span> <span class="n">chatgpt</span>
<span class="n">Provide</span> <span class="n">a</span> <span class="n">hello</span> <span class="n">world</span> <span class="n">function</span> <span class="ow">in</span> <span class="n">Python</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -818,6 +854,7 @@ <h2><span class="section-number">3.1.8. </span>Remove Output Cells Automatically
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#apply-code-quality-tools-to-jupyter-notebooks">3.1.6. Apply Code Quality Tools to Jupyter Notebooks</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#create-and-reuse-juptyer-notebook-templates">3.1.7. Create and Reuse Juptyer Notebook Templates</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#remove-output-cells-automatically-with-nbstripout">3.1.8. Remove Output Cells Automatically with <code class="docutils literal notranslate"><span class="pre">nbstripout</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#bring-llms-into-your-notebook-with-jupyter-ai">3.1.9. Bring LLMs Into Your Notebook with <code class="docutils literal notranslate"><span class="pre">jupyter-ai</span></code></a></li>
</ul>
</nav></div>

Expand Down
35 changes: 35 additions & 0 deletions book/machinelearning/modeltraining.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ <h2> Contents </h2>
<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>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#online-ml-with-river">5.5.27. Online ML with <code class="docutils literal notranslate"><span class="pre">river</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#sota-computer-vision-models-with-timm">5.5.28. SOTA Computer Vision Models with <code class="docutils literal notranslate"><span class="pre">timm</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#generate-guaranteed-prediction-intervals-and-sets-with-mapie">5.5.29. Generate Guaranteed Prediction Intervals and Sets with <code class="docutils literal notranslate"><span class="pre">MAPIE</span></code></a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -1631,6 +1632,39 @@ <h2><span class="section-number">5.5.28. </span>SOTA Computer Vision Models with
</div>
</div>
</section>
<section id="generate-guaranteed-prediction-intervals-and-sets-with-mapie">
<h2><span class="section-number">5.5.29. </span>Generate Guaranteed Prediction Intervals and Sets with <code class="docutils literal notranslate"><span class="pre">MAPIE</span></code><a class="headerlink" href="#generate-guaranteed-prediction-intervals-and-sets-with-mapie" title="Permalink to this heading">#</a></h2>
<p>For quantifying uncertainties of your models, use MAPIE.</p>
<p><code class="docutils literal notranslate"><span class="pre">MAPIE</span></code> (Model Agnostic Prediction Interval Estimator) takes your sklearn-/tensorflow-/pytorch-compatible model and generate prediction intervals or sets with guaranteed coverage.</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>mapie
</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">mapie.regression</span> <span class="kn">import</span> <span class="n">MapieRegressor</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">sklearn.linear_model</span> <span class="kn">import</span> <span class="n">LinearRegression</span>
<span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">make_regression</span>
<span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">train_test_split</span>

<span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">make_regression</span><span class="p">(</span><span class="n">n_samples</span><span class="o">=</span><span class="mi">500</span><span class="p">,</span> <span class="n">n_features</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">noise</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">59</span><span class="p">)</span>
<span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</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">test_size</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">regressor</span> <span class="o">=</span> <span class="n">LinearRegression</span><span class="p">()</span>

<span class="n">mapie_regressor</span> <span class="o">=</span> <span class="n">MapieRegressor</span><span class="p">(</span><span class="n">regressor</span><span class="p">)</span>
<span class="n">mapie_regressor</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>

<span class="n">alpha</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.05</span><span class="p">,</span> <span class="mf">0.20</span><span class="p">]</span>
<span class="n">y_pred</span><span class="p">,</span> <span class="n">y_pis</span> <span class="o">=</span> <span class="n">mapie_regressor</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="n">alpha</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -1728,6 +1762,7 @@ <h2><span class="section-number">5.5.28. </span>SOTA Computer Vision Models with
<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>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#online-ml-with-river">5.5.27. Online ML with <code class="docutils literal notranslate"><span class="pre">river</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#sota-computer-vision-models-with-timm">5.5.28. SOTA Computer Vision Models with <code class="docutils literal notranslate"><span class="pre">timm</span></code></a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#generate-guaranteed-prediction-intervals-and-sets-with-mapie">5.5.29. Generate Guaranteed Prediction Intervals and Sets with <code class="docutils literal notranslate"><span class="pre">MAPIE</span></code></a></li>
</ul>
</nav></div>

Expand Down
Loading

0 comments on commit afb055e

Please sign in to comment.