{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 9.3. Fitting a function to data with nonlinear least squares" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Let's import the usual libraries." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import scipy.optimize as opt\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "np.random.seed(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We define a logistic function with four parameters." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$f_{a,b,c,d}(x) = \\frac{a}{1 + \\exp\\left(-c (x-d)\\right)} + b$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def f(x, a, b, c, d):\n", " return a/(1. + np.exp(-c * (x-d))) + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Let's define four random parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a, c = np.random.exponential(size=2)\n", "b, d = np.random.randn(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Now, we generate random data points, by using the sigmoid function and adding a bit of noise." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 100\n", "x = np.linspace(-10., 10., n)\n", "y_model = f(x, a, b, c, d)\n", "y = y_model + a * .2 * np.random.randn(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Here is a plot of the data points, with the particular sigmoid used for their generation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure(figsize=(6,4));\n", "plt.plot(x, y_model, '--k');\n", "plt.plot(x, y, 'o');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. We now assume that we only have access to the data points. These points could have been obtained during an experiment. By looking at the data, the points appear to approximately follow a sigmoid, so we may want to try to fit such a curve to the points. That's what **curve fitting** is about. SciPy's function `curve_fit` allows us to fit a curve defined by an arbitrary Python function to the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(a_, b_, c_, d_), _ = opt.curve_fit(f, x, y, (a, b, c, d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7. Now, let's take a look at the fitted simoid curve." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "y_fit = f(x, a_, b_, c_, d_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure(figsize=(6,4));\n", "plt.plot(x, y_model, '--k');\n", "plt.plot(x, y, 'o');\n", "plt.plot(x, y_fit, '-');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The fitted sigmoid appears to be quite close from the original sigmoid used for data generation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }