{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "95412481",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "2f7f7f5d",
            "cell_type": "markdown",
            "source": "Generate some noisy data:\n",
            "metadata": {}
        },
        {
            "id": "9d1a1a3f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nimport matplotlib.pyplot as plt\nrng = np.random.default_rng()\nx = np.linspace(-3, 3, 50)\ny = np.exp(-x**2) + 0.1 * rng.standard_normal(50)",
            "outputs": []
        },
        {
            "id": "6827283d",
            "cell_type": "markdown",
            "source": "Now fit a smoothing cubic spline with a pre-defined internal knots.\nHere we make the knot vector (k+1)-regular by adding boundary knots:\n",
            "metadata": {}
        },
        {
            "id": "fcb0d501",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import make_lsq_spline, BSpline\nt = [-1, 0, 1]\nk = 3\nt = np.r_[(x[0],)*(k+1),\n          t,\n          (x[-1],)*(k+1)]\nspl = make_lsq_spline(x, y, t, k)",
            "outputs": []
        },
        {
            "id": "fb0bfa9e",
            "cell_type": "markdown",
            "source": "For comparison, we also construct an interpolating spline for the same\nset of data:\n",
            "metadata": {}
        },
        {
            "id": "c4060738",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import make_interp_spline\nspl_i = make_interp_spline(x, y)",
            "outputs": []
        },
        {
            "id": "f2de182b",
            "cell_type": "markdown",
            "source": "Plot both:\n",
            "metadata": {}
        },
        {
            "id": "38d6428e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xs = np.linspace(-3, 3, 100)\nplt.plot(x, y, 'ro', ms=5)\nplt.plot(xs, spl(xs), 'g-', lw=3, label='LSQ spline')\nplt.plot(xs, spl_i(xs), 'b-', lw=3, alpha=0.7, label='interp spline')\nplt.legend(loc='best')\nplt.show()",
            "outputs": []
        },
        {
            "id": "77bdda2b",
            "cell_type": "markdown",
            "source": "**NaN handling**: If the input arrays contain ``nan`` values, the result is\nnot useful since the underlying spline fitting routines cannot deal with\n``nan``. A workaround is to use zero weights for not-a-number data points:\n",
            "metadata": {}
        },
        {
            "id": "8b24d1b0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y[8] = np.nan\nw = np.isnan(y)\ny[w] = 0.\ntck = make_lsq_spline(x, y, t, w=~w)",
            "outputs": []
        },
        {
            "id": "89d55503",
            "cell_type": "markdown",
            "source": "Notice the need to replace a ``nan`` by a numerical value (precise value\ndoes not matter as long as the corresponding weight is zero.)\n",
            "metadata": {}
        }
    ]
}