2014-05-04 22:57:02 +00:00
{
"metadata": {
"name": "",
2014-05-06 05:08:11 +00:00
"signature": "sha256:b8a2adab4cfa8ac1064656d4b3e3121a2acc1060e034c98ce29986312939c9ac"
2014-05-04 22:57:02 +00:00
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-05 00:16:02 +00:00
"[Sebastian Raschka](http://www.sebastianraschka.com) \n",
2014-05-06 05:08:11 +00:00
"last updated: 05/06/2014\n",
2014-05-04 22:57:02 +00:00
"\n",
2014-05-04 23:11:16 +00:00
"- [Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/cython_least_squares.ipynb) \n",
2014-05-04 22:57:02 +00:00
"- [Link to the GitHub repository](https://github.com/rasbt/python_reference)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-05 19:34:22 +00:00
"#### The code in this notebook was executed in Python 3.4.0"
2014-05-04 22:57:02 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"I am really looking forward to your comments and suggestions to improve and \n",
"extend this little collection! Just send me a quick note \n",
"via Twitter: [@rasbt](https://twitter.com/rasbt) \n",
"or Email: [bluewoodtree@gmail.com](mailto:bluewoodtree@gmail.com)\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Implementing the least squares fit method for linear regression and speeding it up via Cython"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"sections\"></a>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Sections"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [Introduction](#introduction)\n",
"- [Least squares fit implementations](#implementations)\n",
"- [Generating sample data and benchmarking](#sample_data)\n",
"- [Compiling the Python code via Cython in the IPython notebook](#cython_nb)\n",
2014-05-05 05:44:10 +00:00
"- [Performance growth rates for different sample sizes](#sample_sizes)\n",
2014-05-05 19:34:22 +00:00
"- [Bonus: How to use Cython without the IPython magic](#cython_bonus)\n",
"- [Appendix I: Cython vs. Numba](#numba)\n",
2014-05-05 23:31:23 +00:00
"- [Appendix II: Cython with and without type declarations](#type_declarations)\n",
2014-05-06 05:08:11 +00:00
"- [Appendix III: Cython performance after replacing list comprehensions by explicit for loops](#explicit_loops)\n",
"- [Final Comparison: Cython vs. NumPy vs. SciPy for least squares fitting](#showdown)"
2014-05-04 22:57:02 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"introduction\"></a>\n",
"<br>\n",
"<br>"
]
},
2014-05-05 19:34:22 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](https://raw.githubusercontent.com/rasbt/python_reference/master/Images/cython_vs_chart.png) \n",
"(Note that this chart just reflects my rather objective thoughts after experimenting with Cython, and it is not based on real numbers or benchmarks.)\n",
"<br>\n",
"<br>\n",
"<br>\n",
"<br>"
]
},
2014-05-04 22:57:02 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Linear regression via the least squares method is the simplest approach to performing a regression analysis of a dependent and a explanatory variable. The objective is to find the best-fitting straight line through a set of points that minimizes the sum of the squared offsets from the line. \n",
"The offsets come in 2 different flavors: perpendicular and vertical - with respect to the line. \n",
"![](https://raw.githubusercontent.com/rasbt/python_reference/master/Images/least_squares_vertical.png) \n",
"![](https://raw.githubusercontent.com/rasbt/python_reference/master/Images/least_squares_perpendicular.png) \n",
"\n",
2014-05-05 02:17:27 +00:00
"As Michael Burger summarizes it nicely in his article \"[Problems of Linear Least Square Regression - And Approaches to Handle Them](http://www.arsa-conf.com/archive/?vid=1&aid=2&kid=60101-220)\": \"the perpendicular offset method delivers a more precise result but is are more complicated to handle. Therefore normally the vertical offsets are used.\" \n",
"Here, we will also use the method of computing the vertical offsets.\n",
"\n"
2014-05-04 22:57:02 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In more mathematical terms, our goal is to compute the best fit to *n* points $(x_i, y_i)$ with $i=1,2,...n,$ via linear equation of the form \n",
"$f(x) = a\\cdot x + b$. \n",
"Here, we assume that the y-component is functionally dependent on the x-component. \n",
"In a cartesian coordinate system, $b$ is the intercept of the straight line with the y-axis, and $a$ is the slope of this line."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to obtain the parameters for the linear regression line for a set of multiple points, we can re-write the problem as matrix equation \n",
"$\\pmb X \\; \\pmb a = \\pmb y$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\Rightarrow\\Bigg[ \\begin{array}{cc}\n",
"x_1 & 1 \\\\\n",
"... & 1 \\\\\n",
"x_n & 1 \\end{array} \\Bigg]$\n",
"$\\bigg[ \\begin{array}{c}\n",
"a \\\\\n",
"b \\end{array} \\bigg]$\n",
"$=\\Bigg[ \\begin{array}{c}\n",
"y_1 \\\\\n",
"... \\\\\n",
"y_n \\end{array} \\Bigg]$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With a little bit of calculus, we can rearrange the term in order to obtain the parameter vector $\\pmb a = [a\\;b]^T$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\Rightarrow \\pmb a = (\\pmb X^T \\; \\pmb X)^{-1} \\pmb X^T \\; \\pmb y$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"The more classic approach to obtain the slope parameter $a$ and y-axis intercept $b$ would be:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$a = \\frac{S_{x,y}}{\\sigma_{x}^{2}}\\quad$ (slope)\n",
"\n",
"\n",
"$b = \\bar{y} - a\\bar{x}\\quad$ (y-axis intercept)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where \n",
"\n",
"\n",
"$S_{xy} = \\sum_{i=1}^{n} (x_i - \\bar{x})(y_i - \\bar{y})\\quad$ (covariance)\n",
"\n",
"\n",
"$\\sigma{_x}^{2} = \\sum_{i=1}^{n} (x_i - \\bar{x})^2\\quad$ (variance)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"implementations\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Least squares fit implementations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. The matrix approach"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let us implement the equation:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\pmb a = (\\pmb X^T \\; \\pmb X)^{-1} \\pmb X^T \\; \\pmb y$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which I will refer to as the \"matrix approach\"."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"\n",
"def lin_lstsqr_mat(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" X = np.vstack([x, np.ones(len(x))]).T\n",
" return (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(y)"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 36
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. The classic approach"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we will calculate the parameters separately, using only standard library functions in Python, which I will call the \"classic approach\"."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$a = \\frac{S_{x,y}}{\\sigma_{x}^{2}}\\quad$ (slope)\n",
"\n",
"\n",
"$b = \\bar{y} - a\\bar{x}\\quad$ (y-axis intercept)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def classic_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 37
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Using the lstsq numpy function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For our convenience, `numpy` has a function that can also compute the leat squares solution of a linear matrix equation. For more information, please refer to the [documentation](http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def numpy_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" X = np.vstack([x, np.ones(len(x))]).T\n",
" return np.linalg.lstsq(X,y)[0]"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 38
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. Using the linregress scipy function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The last approach is using `scipy.stats.linregress()`, which returns a tuple of 5 different attributes, where the 1st value in the tuple is the slope, and the second value is the y-axis intercept, respectively. The documentation for this function can be found [here](http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.stats.linregress.html)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import scipy.stats\n",
"\n",
"def scipy_lstsqr(x,y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" return scipy.stats.linregress(x, y)[0:2]"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 39
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='sample_data'></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generating sample data and benchmarking"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-05 00:16:02 +00:00
"In order to test our different least squares fit implementation, we will generate some sample data: \n",
2014-05-04 22:57:02 +00:00
"- 500 sample points for the x-component within the range [0,500) \n",
"- 500 sample points for the y-component within the range [100,600) \n",
"\n",
"where each sample point is multiplied by a random value within\n",
"the range [0.8, 12)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"random.seed(12345)\n",
"\n",
"x = [x_i*random.randrange(8,12)/10 for x_i in range(500)]\n",
"y = [y_i*random.randrange(8,12)/10 for y_i in range(100,600)]"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 40
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
2014-05-04 23:21:25 +00:00
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-05-04 22:57:02 +00:00
"#### Visualization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To check how our dataset is distributed, and how the straight line, which we obtain via the least square fit method, we will plot it in a scatter plot. \n",
"Note that we are using our \"matrix approach\" here for simplicity, but after plotting the data, we will check whether all of the four different implementations yield the same parameters."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline\n",
"from matplotlib import pyplot as plt\n",
"\n",
"slope, intercept = lin_lstsqr_mat(x, y)\n",
"\n",
"line_x = [round(min(x)) - 1, round(max(x)) + 1]\n",
"line_y = [slope*x_i + intercept for x_i in line_x]\n",
"\n",
"plt.figure(figsize=(8,8))\n",
"plt.scatter(x,y)\n",
"plt.plot(line_x, line_y, color='red', lw='2')\n",
"\n",
"plt.ylabel('y')\n",
"plt.xlabel('x')\n",
"plt.title('Linear regression via least squares fit')\n",
"\n",
"ftext = 'y = ax + b = {:.3f} + {:.3f}x'\\\n",
" .format(slope, intercept)\n",
"plt.figtext(.15,.8, ftext, fontsize=11, ha='left')\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAfoAAAH4CAYAAACi3S9CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdUVMfbwPHv0lm6giAgooBRbNi7GCv2EnvXWOPPnhg1\nGksSxRhjoiYaE0SNsb4xEY0Fe+zGmijGLkZARRERFljYnfeP1Y0ELJRlAedzDkf2lpln7q48e++d\nO6MQQggkSZIkSSqSTIwdgCRJkiRJhiMTvSRJkiQVYTLRS5IkSVIRJhO9JEmSJBVhMtFLkiRJUhEm\nE70kSZIkFWEy0UtGd+jQIcqXL2/sMAqtSpUq8fvvv+drnSNHjuTTTz/N0b7e3t7s3bs3jyN6s/3y\nyy+UKlUKe3t7zp07Z5TPhFRwKeRz9FJ+8fb2JiQkhGbNmhk7FMmIypQpQ0hICE2bNjVI+QcOHKBf\nv378888/Bim/IPLx8eGrr76iffv2mdbNnDmT69ev8+OPPxohMqkgkGf0Ur5RKBQoFApjh6Gn0Wjy\nZJvXJYRAfq+Wnsmrz5YQgtu3b+Pv758n5UlFj0z0ktEdOHCAUqVK6V97e3uzYMECqlatiqOjIz17\n9iQ1NVW/ftu2bQQEBODk5ESDBg3466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1ZuXIlly5dQqVSZWpLQEAAmzdvJjk5mWvXrhESEqJ/v588eYKZmRnOzs6o\n1Wpmz579whjg1Z+tTZs2cefOHQAcHR1RKBRZvuev4ubmxq1bt+Tl+zeYTPRSvmrTpg1KpVL/M2vW\nrFc+dvf8+ho1avD999/zv//9
"text": [
2014-05-05 19:34:22 +00:00
"<matplotlib.figure.Figure at 0x10f9e8a50>"
2014-05-04 22:57:02 +00:00
]
}
],
2014-05-05 19:34:22 +00:00
"prompt_number": 42
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
2014-05-04 23:21:25 +00:00
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-05-04 22:57:02 +00:00
"#### Comparing the results from the different implementations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As mentioned above, let us confirm that the different implementation computed the same parameters (i.e., slope and y-axis intercept) as solution for the linear equation."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import prettytable\n",
"\n",
"params = [appr(x,y) for appr in [lin_lstsqr_mat, classic_lstsqr, numpy_lstsqr, scipy_lstsqr]]\n",
"\n",
"print(params)\n",
"\n",
"fit_table = prettytable.PrettyTable([\"\", \"slope\", \"y-intercept\"])\n",
"fit_table.add_row([\"matrix approach\", params[0][0], params[0][1]])\n",
"fit_table.add_row([\"classic approach\", params[1][0], params[1][1]])\n",
"fit_table.add_row([\"numpy function\", params[2][0], params[2][1]])\n",
"fit_table.add_row([\"scipy function\", params[3][0], params[3][1]])\n",
"\n",
"print(fit_table)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[array([ 0.95181895, 107.01399744]), (0.95181895319126741, 107.01399744459181), array([ 0.95181895, 107.01399744]), (0.95181895319126764, 107.01399744459175)]\n",
"+------------------+----------------+---------------+\n",
"| | slope | y-intercept |\n",
"+------------------+----------------+---------------+\n",
"| matrix approach | 0.951818953191 | 107.013997445 |\n",
"| classic approach | 0.951818953191 | 107.013997445 |\n",
"| numpy function | 0.951818953191 | 107.013997445 |\n",
"| scipy function | 0.951818953191 | 107.013997445 |\n",
"+------------------+----------------+---------------+\n"
]
}
],
2014-05-05 19:34:22 +00:00
"prompt_number": 43
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
2014-05-04 23:21:25 +00:00
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-05-04 22:57:02 +00:00
"#### Initial performance comparison"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For a first impression how the performances of the different least squares implementations compare against each other, let us do a quick benchmark using the `timeit` module via IPython's `%timeit` magic."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"for lab,appr in zip([\"matrix approach\",\"classic approach\",\n",
" \"numpy function\",\"scipy function\"],\n",
" [lin_lstsqr_mat, classic_lstsqr, \n",
" numpy_lstsqr, scipy_lstsqr]):\n",
" print(\"\\n{}: \".format(lab), end=\"\")\n",
" %timeit appr(x, y)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-05 19:34:22 +00:00
"matrix approach: 10000 loops, best of 3: 163 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
2014-05-05 19:34:22 +00:00
"classic approach: 1000 loops, best of 3: 1.55 ms per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
2014-05-05 19:34:22 +00:00
"numpy function: 1000 loops, best of 3: 221 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
2014-05-05 19:34:22 +00:00
"scipy function: 1000 loops, best of 3: 362 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-05 19:34:22 +00:00
"prompt_number": 44
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"The timing above indicates, that the \"classic\" approach (Python's standard library functions only) is significantly worse in performance than the other implemenations - roughly by a magnitude of 10."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"<a name=\"cython_nb\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compiling the Python code via Cython in the IPython notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Maybe we can speed things up a little bit via [Cython's C-extensions for Python](http://cython.org). Cython is basically a hybrid between C and Python and can be pictured as compiled Python code with type declarations. \n",
"Since we are working in an IPython notebook here, we can make use of the IPython magic: It will automatically convert it to C code, compile it, and load the function. \n",
2014-05-05 19:34:22 +00:00
"Just to be thorough, let us also compile the other functions, which already use numpy objects.\n",
"\n",
"**Note** \n",
"Of course Cython has much more horsepower under its hood - more than I am showing in this article (for example, I am not using Cython's type definitions via `cdef` here). Here, I want to focus on how to speed up existing Python code by making only minimal changes to it. \n",
"[In a later section - Appendix II](#type_declarations) We will see how static type declarations can further improve the performance via Cython."
2014-05-04 22:57:02 +00:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
2014-05-05 19:34:22 +00:00
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The cythonmagic extension is already loaded. To reload it, use:\n",
" %reload_ext cythonmagic\n"
]
}
],
"prompt_number": 45
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"import numpy as np\n",
"import scipy.stats\n",
"cimport numpy as np\n",
"\n",
"def cy_lin_lstsqr_mat(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" X = np.vstack([x, np.ones(len(x))]).T\n",
" return (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(y)\n",
"\n",
"def cy_classic_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)\n",
"\n",
"def cy_numpy_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" X = np.vstack([x, np.ones(len(x))]).T\n",
" return np.linalg.lstsq(X,y)[0]\n",
"\n",
"def cy_scipy_lstsqr(x,y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" return scipy.stats.linregress(x, y)[0:2]"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 46
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Comparing the compiled Cython code to the original Python code"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"for lab,appr in zip([\"matrix approach\",\"classic approach\",\n",
" \"numpy function\",\"scipy function\"],\n",
" [(lin_lstsqr_mat, cy_lin_lstsqr_mat), \n",
" (classic_lstsqr, cy_classic_lstsqr),\n",
" (numpy_lstsqr, cy_numpy_lstsqr),\n",
" (scipy_lstsqr, cy_scipy_lstsqr)]):\n",
" print(\"\\n\\n{}: \".format(lab))\n",
" %timeit appr[0](x, y)\n",
" %timeit appr[1](x, y)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"matrix approach: \n",
2014-05-05 19:34:22 +00:00
"10000 loops, best of 3: 165 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-05 19:34:22 +00:00
"10000 loops, best of 3: 166 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"\n",
"classic approach: \n",
2014-05-05 19:34:22 +00:00
"1000 loops, best of 3: 1.59 ms per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-05 19:34:22 +00:00
"10000 loops, best of 3: 127 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"\n",
"numpy function: \n",
2014-05-05 19:34:22 +00:00
"1000 loops, best of 3: 220 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-05 19:34:22 +00:00
"1000 loops, best of 3: 221 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"\n",
"scipy function: \n",
2014-05-05 19:34:22 +00:00
"1000 loops, best of 3: 361 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-05 19:34:22 +00:00
"1000 loops, best of 3: 367 \u00b5s per loop"
2014-05-04 22:57:02 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-05 19:34:22 +00:00
"prompt_number": 47
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"As we've seen before, our \"classic\" implementation of the least square method is pretty slow compared to using numpy's functions. This is not surprising, since numpy is highly optmized and uses compiled C/C++ and Fortran code already. This explains why there is no significant difference if we used Cython to compile the numpy-objects-containing functions. \n",
"However, we were able to speed up the \"classic approach\" quite significantly, roughly by 1500%.\n",
"\n",
"The following 2 code blocks are just to visualize our results in a bar plot."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"funcs = ['classic_lstsqr', 'cy_classic_lstsqr', \n",
" 'lin_lstsqr_mat', 'numpy_lstsqr', 'scipy_lstsqr']\n",
"labels = ['classic approach','classic approach (cython)', \n",
" 'matrix approach', 'numpy function', 'scipy function']\n",
"\n",
"times = [timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000)\n",
" for f in funcs]\n",
"\n",
"times_rel = [times[0]/times[i+1] for i in range(len(times[1:]))]"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 50
2014-05-04 22:57:02 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-05-05 19:34:22 +00:00
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
2014-05-04 22:57:02 +00:00
"\n",
"x_pos = np.arange(len(funcs))\n",
"plt.bar(x_pos, times, align='center', alpha=0.5)\n",
"plt.xticks(x_pos, labels, rotation=45)\n",
"plt.ylabel('time in ms')\n",
"plt.title('Performance of different least square fit implementations')\n",
2014-05-05 19:34:22 +00:00
"plt.grid()\n",
2014-05-04 22:57:02 +00:00
"plt.show()\n",
"\n",
"x_pos = np.arange(len(funcs[1:]))\n",
"plt.bar(x_pos, times_rel, align='center', alpha=0.5, color=\"green\")\n",
"plt.xticks(x_pos, labels[1:], rotation=45)\n",
"plt.ylabel('relative performance gain')\n",
"plt.title('Performance gain compared to the classic least square implementation')\n",
2014-05-05 19:34:22 +00:00
"plt.grid()\n",
2014-05-04 22:57:02 +00:00
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
2014-05-05 19:34:22 +00:00
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qKioiIqKcnBySSCTCB7y0AQMG0Jo1a4T5m5qaqmz0Bg0aCAceU1NTunDhQpl5VJZ3SUePHqVG\njRqpvBYSEiJ8G5k3bx6NGDGi3FiJSOWgSET0/fffq3zYS374Sn+DKX3Q2rZtm3BAU5owYQLNnz+f\niIoLwujRo4VxiYmJ1KRJE5XpFy9eTGPGjBFif/PNN4VxygNHebGVp2RBqGxZpa1atYreeustIiI6\nfPgwNW/enBITE8u0+Zbe30o7cuRIue+Vy+VkZGQkFG4iotmzZ7/wDKH0QbqkXbt2UZs2bYRhZ2dn\nlbPR6uxTymUpz0ZK51mVPoTS05uampb5DCUnJwvTt2vXjvbs2UNExdvd19dXGFdUVESNGzemY8eO\nCbkpt7u7u7vKPnDnzh0yMjIihUIhrMM7d+4I421sbFTOWgcNGiR8Xvv06aOyfhUKBZmZmdHNmzeJ\nqLggHD9+XBg/ZMgQWrp0KRFVrQ9h2rRp9MEHHxBR+du35Dx+/PFH6tixo8r7fX19KSoqioiKt8+i\nRYuEcd9++y316dOHiIpbLTp37lzusaU8hppusipNIpFgz549eOONN1ReT09Px44dO7Bv3z7hNblc\nrjJdZVeR3LlzB05OTsJwkyZNcOfOHWG4fv36MDY2VnlPgwYNhL9NTU1ha2sLiUQiDANAbm4uLCws\nEBcXh/nz5+Pq1asoKipCXl4eWrduLbzfxsZGpf3ezMwMubm5yM7OxrNnz9CsWbMyMVcl75L5lV4H\nTk5OyMjIeOF6qej9TZo0qdL7
2014-05-04 22:57:02 +00:00
"text": [
2014-05-05 19:34:22 +00:00
"<matplotlib.figure.Figure at 0x10eeafd50>"
2014-05-04 22:57:02 +00:00
]
},
{
"metadata": {},
"output_type": "display_data",
2014-05-05 19:34:22 +00:00
"png": "iVBORw0KGgoAAAANSUhEUgAAAbwAAAFhCAYAAAALEB8uAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdYFGfXBvB7qNJ7EaSIxgiKghWxYVBj16BGiS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kpPTydBEMSdorBu3brR3LlzxeVbWFjo7Aiurq7izmphYUFnz54t\nsowXbfez9u7dS56enjqPNWvW7Lk79qlTp8RPOURFC1716tXFv2VlZZEgCJSamlpkOQcPHiQXFxed\nbdN66623aOHCheJ0YmIimZqakkajEXeiZz9BOTk50R9//CFOd+/eXTyALVmypEjRbdSoES1btqzY\n7Sucf+HX19/fX6eA3b59m0xNTUmtVtN///tfsfhpt9/MzKzEgvfse23fvn3k7u6u85zIyEiKiYkp\ndv6wsDDxLEBLEASd4vHuu+/StGnTiIioXbt2OgVSo9GQpaUlXb9+vciyJ0+eTBERES/V7mc9+/7I\nzMwke3t7Wrt2LWVnZ+s8r3///jR48GCds8Fnt+HKlSslvscLe7bgHT58mLy9vYtsz4ABA4qdd/bs\n2fTOO+8Q0dODV40aNejw4cNF3pslbTcR0e7du4udV61Wk6mpqc4Hl7Fjx5Z4hlfchxmtdevWUXBw\nsDjt6+ur0/PzKvu/dv5nC96rHL/CwsLEQkP09IzIzMyMCgoKdLYrJSWFzM3Ndc7OV65cSa1atSKi\np/vpG2+8If7t7NmzJAgC3b17
2014-05-04 22:57:02 +00:00
"text": [
2014-05-05 19:34:22 +00:00
"<matplotlib.figure.Figure at 0x10eeac310>"
2014-05-04 22:57:02 +00:00
]
}
],
2014-05-05 19:34:22 +00:00
"prompt_number": 51
2014-05-04 22:57:02 +00:00
},
2014-05-05 05:44:10 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"sample_sizes\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Performance growth rates for different sample sizes"
]
},
2014-05-05 19:34:22 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-05-05 05:44:10 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the plot above, we've seen how the different implemantations perform for a fixed sample size n=500. Now, let us take a look at the effect of the sample size on the relative performances for each approach. We will consider the sample sizes 10, 100, 1000, 10000, 100000, and 1000000."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
2014-05-05 19:34:22 +00:00
"import random\n",
"random.seed(12345)\n",
2014-05-05 05:44:10 +00:00
"\n",
"funcs = ['cy_classic_lstsqr', \n",
" 'lin_lstsqr_mat', 'numpy_lstsqr', 'scipy_lstsqr']\n",
2014-05-06 05:08:11 +00:00
"\n",
2014-05-05 05:44:10 +00:00
"orders_n = [10**n for n in range(1, 7)]\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for n in orders_n:\n",
" x = [x_i*random.randrange(8,12)/10 for x_i in range(n)]\n",
" y = [y_i*random.randrange(10,14)/10 for y_i in range(n)]\n",
" for f in funcs:\n",
" times_n[f].append(timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000))"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 19:34:22 +00:00
"prompt_number": 22
2014-05-05 05:44:10 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
"\n",
"plt.figure(figsize=(10,8))\n",
"\n",
"for f in times_n.keys():\n",
" plt.plot(orders_n, times_n[f], alpha=0.5, label=f, marker='o', lw=2)\n",
"\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time in ms')\n",
"plt.xlim([0,max(orders_n) + max(orders_n) * 0.1])\n",
"plt.legend(loc=2)\n",
2014-05-05 19:34:22 +00:00
"plt.grid()\n",
2014-05-05 05:44:10 +00:00
"\n",
"plt.title('Performance of least square fit implementations for different sample sizes')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
2014-05-05 19:34:22 +00:00
"png": "iVBORw0KGgoAAAANSUhEUgAAAmEAAAH4CAYAAAACdDpdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcVOX+B/DPDLIz7Jvs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Ozs507tw5evLkCQ0aNIh8fX1p69atquNy4MCBqvl9fX2pS5cudPv2bSosLKTQ\n0FBVcl41YZLL5fTMM8/Q8uXLSSaT0Y0bN6ht27aq46A6d3d31fnn4cOHaufyqoldpUePHlHHjh1p\n48aNRFT/56O+c6Sx4SRMYHx9fcnGxobs7e3J19eXZs+eTWVlZbR69eoaX2RDhgxR/TIPCwujZcuW\nqb0fExOj9kvZ399fdcIhIjp69Cj5+fkRkfKDZWZmpmoZI1J+GQ4ePFg1/e2335KNjY3qF8ujR49I\nJBJRUVFRrdsyatQo+r//+z9V+ZaWlmonLVdXVzpz5gzJ5XKytLRU+9VaqaHtruqnn34id3d3tdcm\nTpyo+pVcvaWruqrvN2a91bd16dKlNHLkSLp+/XqN+RpKwupa9scffyQPDw+116om0Q0lYdXNnTuX\n5s+fT0R/fdndvHlT9f7QoUMpLi5ONS2Xy8nKyoqys7NrLa++JKyh/V7b/I057sLCwlQndyJl64yZ\nmRkpFAq1L/I7d+6Qubm56tc4EdH27dtVX2ibN2+mdu3aqd67ePEiiUQiys/PV73m5OREFy5cIIVC\nQdbW1mrb/PPPP6uSw/qOd6Ka
2014-05-05 05:44:10 +00:00
"text": [
2014-05-05 19:34:22 +00:00
"<matplotlib.figure.Figure at 0x10f9e1050>"
2014-05-05 05:44:10 +00:00
]
}
],
2014-05-05 19:34:22 +00:00
"prompt_number": 34
2014-05-05 05:44:10 +00:00
},
2014-05-06 05:08:11 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"In this performance comparison for different sample sizes, we see that our Cython approach is actually not so fast anymore. However, this is just the simplest approach to using Cython. There are a lot of tweaks that can be made. In a [later section](#showdown) we will come back to this comparison and see how the Cython version of our simple least squares implementation holds up against the other approaches\n",
"<br>"
]
},
2014-05-04 22:57:02 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"cython_bonus\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bonus: How to use Cython without the IPython magic"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"IPython's notebook is really great for explanatory analysis and documentation, but what if we want to compile our Python code via Cython without letting IPython's magic doing all the work? \n",
"These are the steps you would need."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. Creating a .pyx file containing the the desired code or function."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%file ccy_classic_lstsqr.pyx\n",
"\n",
"def ccy_classic_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Writing ccy_classic_lstsqr.pyx\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Creating a simple setup file"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%file setup.py\n",
"\n",
"from distutils.core import setup\n",
"from distutils.extension import Extension\n",
"from Cython.Distutils import build_ext\n",
"\n",
"setup(\n",
" cmdclass = {'build_ext': build_ext},\n",
" ext_modules = [Extension(\"ccy_classic_lstsqr\", [\"ccy_classic_lstsqr.pyx\"])]\n",
")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Writing setup.py\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"####3. Building and Compiling"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"!python3 setup.py build_ext --inplace"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"running build_ext\r\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"cythoning ccy_classic_lstsqr.pyx to ccy_classic_lstsqr.c\r\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"building 'ccy_classic_lstsqr' extension\r\n",
"creating build\r\n",
"creating build/temp.macosx-10.6-intel-3.4\r\n",
"/usr/bin/clang -fno-strict-aliasing -Werror=declaration-after-statement -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -I/Library/Frameworks/Python.framework/Versions/3.4/include/python3.4m -c ccy_classic_lstsqr.c -o build/temp.macosx-10.6-intel-3.4/ccy_classic_lstsqr.o\r\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\u001b[1mccy_classic_lstsqr.c:2040:28: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyObject_AsString'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2037:32: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function\r\n",
" '__Pyx_PyUnicode_FromString' [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2104:26: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyObject_IsTrue'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2159:33: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyIndex_AsSsize_t'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2188:33: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyInt_FromSize_t'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:1584:32: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyInt_From_long'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:1631:27: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mfunction '__Pyx_PyInt_As_long' is not\r\n",
" needed and will not be emitted [-Wunneeded-internal-declaration]\u001b[0m\r\n",
"static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:1731:26: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mfunction '__Pyx_PyInt_As_int' is not\r\n",
" needed and will not be emitted [-Wunneeded-internal-declaration]\u001b[0m\r\n",
"static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"8 warnings generated.\r\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\u001b[1mccy_classic_lstsqr.c:2040:28: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyObject_AsString'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2037:32: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function\r\n",
" '__Pyx_PyUnicode_FromString' [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2104:26: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyObject_IsTrue'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2159:33: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyIndex_AsSsize_t'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:2188:33: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyInt_FromSize_t'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:1584:32: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1munused function '__Pyx_PyInt_From_long'\r\n",
" [-Wunused-function]\u001b[0m\r\n",
"static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:1631:27: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mfunction '__Pyx_PyInt_As_long' is not\r\n",
" needed and will not be emitted [-Wunneeded-internal-declaration]\u001b[0m\r\n",
"static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m\u001b[1mccy_classic_lstsqr.c:1731:26: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mfunction '__Pyx_PyInt_As_int' is not\r\n",
" needed and will not be emitted [-Wunneeded-internal-declaration]\u001b[0m\r\n",
"static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {\r\n",
"\u001b[0;1;32m ^\r\n",
"\u001b[0m"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"8"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" warnings generated.\r\n",
"/usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/temp.macosx-10.6-intel-3.4/ccy_classic_lstsqr.o -o /Users/sebastian/Github/python_reference/benchmarks/ccy_classic_lstsqr.so\r\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Importing and running the code"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import ccy_classic_lstsqr\n",
"\n",
"%timeit classic_lstsqr(x, y)\n",
"%timeit cy_classic_lstsqr(x, y)\n",
"%timeit ccy_classic_lstsqr.ccy_classic_lstsqr(x, y)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 loops, best of 3: 2.9 ms per loop\n",
"1000 loops, best of 3: 212 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 207 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 20
2014-05-05 19:34:22 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"numba\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Appendix I: Cython vs. Numba"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like we did with Cython before, we will use the minimalist approach to Numba and see how they compare against each other. \n",
"\n",
2014-05-05 23:31:23 +00:00
"Numba is using the [LLVM compiler infrastructure](http://llvm.org) for compiling Python code to machine code. Its strength is to work with NumPy arrays to speed-up code. If you want to read more about Numba, see the original [website and documentation](http://numba.pydata.org/numba-doc/0.13/index.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is our \"classic\" approach in Python, where I removed the list comprehensions, since they caused errors in the Numba compilation."
2014-05-05 19:34:22 +00:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
2014-05-05 23:31:23 +00:00
" var_x = 0\n",
" for x_i in x:\n",
" var_x += (x_i - x_avg)**2\n",
" cov_xy = 0\n",
" for x_i, y_i in zip(x,y):\n",
" cov_xy += (x_i - x_avg)*(y_i - y_avg)\n",
2014-05-05 19:34:22 +00:00
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 23:31:23 +00:00
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Cython-compiled version of it:"
]
2014-05-05 19:34:22 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
2014-05-05 23:31:23 +00:00
"outputs": []
2014-05-05 19:34:22 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"\n",
"def cy_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
2014-05-05 23:31:23 +00:00
" var_x = 0\n",
" for x_i in x:\n",
" var_x += (x_i - x_avg)**2\n",
" cov_xy = 0\n",
" for x_i, y_i in zip(x,y):\n",
" cov_xy += (x_i - x_avg)*(y_i - y_avg)\n",
2014-05-05 19:34:22 +00:00
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 23:31:23 +00:00
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now the Numba-compiled version:"
]
2014-05-05 19:34:22 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from numba import jit\n",
"\n",
"@jit\n",
"def nmb_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
2014-05-05 23:31:23 +00:00
" var_x = 0\n",
" for x_i in x:\n",
" var_x += (x_i - x_avg)**2\n",
" cov_xy = 0\n",
" for x_i, y_i in zip(x,y):\n",
" cov_xy += (x_i - x_avg)*(y_i - y_avg)\n",
" \n",
2014-05-05 19:34:22 +00:00
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
2014-05-05 23:31:23 +00:00
"outputs": [],
"prompt_number": 27
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"Now, let us see how the different approaches compare against each other for different sample sizes."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"funcs = ['lstsqr', 'cy_lstsqr', 'nmb_lstsqr'] \n",
"orders_n = [10**n for n in range(1, 7)]\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for n in orders_n:\n",
" x = [x_i*random.randrange(8,12)/10 for x_i in range(n)]\n",
" y = [y_i*random.randrange(10,14)/10 for y_i in range(n)]\n",
" for f in funcs:\n",
" times_n[f].append(timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
"\n",
"plt.figure(figsize=(10,8))\n",
"\n",
"for f in times_n.keys():\n",
" plt.plot(orders_n, times_n[f], alpha=0.5, label=f, marker='o', lw=2)\n",
"\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time in ms')\n",
"plt.xlim([0,max(orders_n) + max(orders_n) * 0.1])\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"\n",
"plt.title('Performance of a simple least square fit implementation')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
2014-05-05 19:34:22 +00:00
"outputs": [
{
2014-05-05 23:31:23 +00:00
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAmEAAAH4CAYAAAACdDpdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xl4k2XW+PFv0n1vKV2A0gUKhULZBRSQQhFwYRlBliKL\nivO6jCvzc3RUFp0RxhnfcfB1m2GpClhABWRHliqiUtmkUKC00AJtKS2lQOmSNrl/fzxDpFCgS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klNLedH18fNT27dvNRfOVxdOV\nb7rPP/+8GjFihDp37py6ePGiGjZsmHrllVdqjffKIqy0tFSFhYWppKQkZTQa1d69e1XTpk1Venq6\nUkqplJQUdeDAAaWUUvv371chISFq5cqVt3S/b/Ux6927t5o2bZoyGAzqu+++Uz4+PuZc1VaEXflh\n5WZ50+l0avDgwercuXOqoqJC7dmzRwUHB6vU1FRlMpnUJ598oiIjI80fSK529QejK+9nUlJSjfzU\nxhqeD5ePvzfffNN87AYGBqrExERVWlqqDh48qDw8PFR2drZSSql3331X3XnnnSo3N1cZDAb1P//z\nP2r8+PFKqV+LsN///veqoqJC/fLLL8rNzU0dPnxYKVX7cXaj14aUlJRr8jtz5kxzEXbkyBHl5eWl\nNm/erKqrq9Xbb7+toqOjVVVVlTk/vXr1Uvn5+aq4uFi1b99effTRRzfMibA9UoSJRhEREaG8vb2V\nv7+/ioiIUE8//bQqLy9Xc+bMueaFbciQIeZPzfHx8WrGjBk1zp8yZYp67bXXzNutW7c2FzNKKbVx\n40YVGRmplNJepF1dXWu8Ec2YMUMNHjzYvP31118rb29vZTKZlFJKXbhwQel0OnX+/Pla78vIkSPV\nv/71L/P1e3h4KKPRaD4/ODjY/Obp4eFR62jEze73lb777jsVGhpa47Tx48ebR3Ku/nR9M//85z/V\n7373u1rPW7BgwXVHUK5+023Tpo35vP379yudTmceNVNKqcDAQPXLL78opbQ33ctvdkppBZOTk5M6\ndeqUUurXN12TyaS8vLzMb8BKKfXDDz/UKIqvdGURlpycrPr161fj/N///vdq1qxZte773HPPqRde\neKFO9/vyiE9trrdvTk6OcnZ2VmVlZebTEhMT61yEXe3qvOl0OrVt2zbz9hNPPFHjw4lSSsXExKhv\nv/221uu7URF29Qhjbazh+XD5+Lv62E1NTTVfvnv37mrVqlVKKaXatWtX4z7n5eUpFxcXZTQazUVY\nbm6u+fyePXuqpUuXKqXqdpxd/dpwdX6vvI433nhDjR071nyeyWRSLVq0MOcrMjJSLV682Hz+Sy+9\npJ544okb3r6wPbIwXzQKnU7HqlWrGDhwYI3Tc3JyWL58OatXrzafVl1dXeNyLVu2vOF15+XlERER\nYd4ODw8nLy/PvB0UFISrq2uNfYKDg83/e3h40LRpU/PaHw8PDwBKS0vx9fVl/fr1zJo1i6NHj2Iy\nmSgrK6NTp07m/QMDA2usx/L09KS0tJSioiIqKipo3br1NTHX5X5fef+ufgwiIiLIzc294eNyWUZG\nBi+++CK7d++mrKyM6upqevToUetlLy8kHjduHCUlJTz88MP89a9/xdn52peKkJAQ8/+XH7OgoKAa\np5WWlgJa/sPCwszneXl50aRJE/Ly8mjRooX59MLCQsrKyujevbv5NPXftYA3k5OTw86dOwkICDCf\nVl1dzaRJkwDYuXMnL7/8MgcPHsRgMFBZWcmYMWPqdL9vtC7sevvm5eUREBBgfmxAy9uVC7VvpC55\nu/J5kZOTw6effsp7771nPq2q
"text": [
"<matplotlib.figure.Figure at 0x1031a8710>"
2014-05-05 19:34:22 +00:00
]
}
],
2014-05-05 23:31:23 +00:00
"prompt_number": 32
2014-05-05 19:34:22 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-05 23:31:23 +00:00
"Without making any modifications to the original code in order to account for the strengths of Numba (Numpy) and Cython (static type declarations), we see that Cython performs significantly better than Numba."
2014-05-05 19:34:22 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"type_declarations\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Appendix II: Cython with and without type declarations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the sections above, we have been using the simplest approach to Cython without using static type declarations and thereby neglecting one of its major strengths. \n",
"Let us now see how we can further improve the Cython implementation of our \"classic least squares approach\" by adding those type declarations."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-05 23:31:23 +00:00
"Here is our \"classic\" approach in Python:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 44
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Cython-compiled version of it:"
2014-05-05 19:34:22 +00:00
]
},
2014-05-05 23:31:23 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
"outputs": []
},
2014-05-05 19:34:22 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"def cy_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-05 23:31:23 +00:00
"prompt_number": 45
2014-05-05 19:34:22 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now, the same code with static type declarations:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"def static_type_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" cdef double x_avg, y_avg, var_x, cov_xy, slope, y_interc, x_i, y_i\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 55
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"Now, let us see how the two functions (with and without static type declarations) compare against each other for different sample sizes."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"funcs = ['cy_lstsqr', 'static_type_lstsqr'] \n",
"labels = ['simple Cython', 'Cython w. type declarations']\n",
"orders_n = [10**n for n in range(1, 7)]\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for n in orders_n:\n",
" x = [x_i*random.randrange(8,12)/10 for x_i in range(n)]\n",
" y = [y_i*random.randrange(10,14)/10 for y_i in range(n)]\n",
" for f in funcs:\n",
" times_n[f].append(timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 58
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
"\n",
"plt.figure(figsize=(10,8))\n",
"\n",
"for f in times_n.keys():\n",
" plt.plot(orders_n, times_n[f], alpha=0.5, label=f, marker='o', lw=2)\n",
"\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time in ms')\n",
"plt.xlim([0,max(orders_n) + max(orders_n) * 0.1])\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"\n",
"plt.title('Performance of a simple least square fit implementation')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAmEAAAH4CAYAAAACdDpdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAL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XY\nv3+/fvu1114r88+JEEI0b95cHDhwoNzjVVaEPTrCWB45vB4evv8efe8eP35cf//27duLrVu3CiGE\naNGiRZnHnJmZKaytrUVJSYm+CMvIyNDf3qlTJ7Fu3TohRPXeZ4/+bXg0v6WP8dFHH4nRo0frb9Pp\ndKJhw4b6fAUEBIjVq1frb586dap47bXXKv39ZHrYmE91QqVSYevWrejVq1eZ69PS0rBhwwbExcXp\nr9NqtWXu5+fnV+mxMzMz0ahRI/22v78/MjMz9dseHh6wsbEps4+np6f+sp2dHdzd3fW9P3Z2dgCA\nvLw8ODk5YdeuXZg9ezYuXrwInU6H/Px8tGnTRr+/m5tbmX4se3t75OXlIScnB4WFhQgMDHws5uo8\n7tKP79HnoFGjRsjIyKj0eXkoOTkZU6ZMwW+//Yb8/HxotVp06NCh3Ps+bCR+4YUXcOfOHYwdOxb/\n+te/YGX1+J8KLy8v/eWHz5mHh0eZ6/Ly8gBI+ff19dXf5uDgAFdXV2RmZqJhw4b662/evIn8/Hy0\nb99ef534v17AqqSlpeHYsWNwcXHRX6fVajFu3DgAwLFjxzBt2jScO3cOGo0GRUVFiIyMrNbjrqwv\nrKJ9MzMz4eLion9uAClvpRu1K1OdvJV+XaSlpWHFihX45ptv9NcVFxcjKyurWr+vpoz1enBzc3vs\nvftoLA9/b1paGoYNG1bm/Wpl
"text": [
"<matplotlib.figure.Figure at 0x10eea0790>"
]
}
],
"prompt_number": 59
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"The improvement is pretty significant when static type declarations are used. One more experiment to see by how much we could improve our \"classic least squares\" code via Cython compared to the initial Python implementation.\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"random.seed(12345)\n",
"\n",
"x = [x_i*random.randrange(8,12)/10 for x_i in range(500)]\n",
"y = [y_i*random.randrange(8,12)/10 for y_i in range(100,600)]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 60
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"funcs = ['classic_lstsqr', 'cy_lstsqr', 'static_type_lstsqr', \n",
" 'lin_lstsqr_mat', 'numpy_lstsqr', 'scipy_lstsqr']\n",
"labels = ['classic approach','classic approach (cython)', \n",
" 'classic approach (cython + type decl.)',\n",
" 'matrix approach', 'numpy function', 'scipy function']\n",
"\n",
"times = [timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000)\n",
" for f in funcs]\n",
"\n",
"times_rel = [times[0]/times[i+1] for i in range(len(times[1:]))]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
"\n",
"x_pos = np.arange(len(funcs[1:]))\n",
"plt.bar(x_pos, times_rel, align='center', alpha=0.5, color=\"green\")\n",
"plt.xticks(x_pos, labels[1:], rotation=90)\n",
"plt.ylabel('relative performance gain')\n",
"plt.title('Performance gain compared to the classic least square implementation')\n",
"plt.grid()\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAbwAAAG/CAYAAAAjJGJxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xl8Dff3P/DXJGLLLhFJSHIRSgiJKLEmqb3WWoLakqBo\naX3ThSoV9aH0Ux+EVmk1UdUWpYSiisYeEhVbKrFdSyQRImSP3Lx/f+SXaa5ESNw7c+d9z/PxyOOR\nu82cc+/cOXfmzLxHYIwxEEIIIZwzkTsAQgghRApU8AghhBgFKniEEEKMAhU8QgghRoEKHiGEEKNA\nBY8QQohRkLTgpaWloXv37rCyssKHH34o5awV6fXXX8fGjRvlDsNgqVQqHDx4ULL5mZiY4Pr163qd\nR1BQEObNm6e36VtaWkKtVlfpNdHR0XBxcdFPQJxr3bo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EW8PGzZMXIFF\nRESUK7odOnRgGzdurDC/p9//pz/fli1bahWwu3fvMjMzM1ZUVMQWLFggFr/S/GvWrFlpwSu7rB05\ncoQ5OjpqPWf06NEsLCyswtf7+/uLWwGlBEHQKh6BgYFs6dKljDHG+vbtq1UgNRoNq1u3Lrt161a5\naS9evJgNHTr0heIuq+zykZ2dzWxsbNi2bdtYbm6u1vPGjx/P3nrrLa2twbI5XLt2rdJl/GllC15M\nTAxzdXUtl09wcHCFr12+fDl74403GGMlK6/mzZuzmJiYcstmZXkzxtihQ4cqfG1RUREzMzPT+uEy\nZ86cSrfwKvoxU+q3335j3t7e
"text": [
"<matplotlib.figure.Figure at 0x106418090>"
]
}
],
"prompt_number": 63
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a pretty significant performance gain. The \"Cython + type declarations\" approach sped up our initial Python code 25 times."
]
2014-05-05 23:31:23 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"explicit_loops\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Appendix III: Cython performance after replacing list comprehensions by explicit for loops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List, set and dictionary comprehensions in Python do not only look prettier and are easier to read (at least most of the time) than nested loop structures, but they also come with some small performance benefits. \n",
"Does this also apply in Cython? Let's check it out."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"This is the code for our \"classic\" least squares approach that we have been using in the previous sections:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lstsqr_comprehensions(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 46
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"And here is a version where I replaced the list comprehensions by for-loops:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lstsqr_loops(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = 0\n",
" for x_i in x:\n",
" var_x += (x_i - x_avg)**2\n",
" cov_xy = 0\n",
" for x_i, y_i in zip(x,y):\n",
" cov_xy += (x_i - x_avg)*(y_i - y_avg)\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 48
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"Finally, the Cython versions of the two functions (with and without using list comprehensions) that we have defined above:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"\n",
"def cy_lstsqr_comprehensions(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" cdef double x_avg, y_avg, var_x, cov_xy, slope, y_interc, x_i, y_i\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = sum([(x_i - x_avg)**2 for x_i in x])\n",
" cov_xy = sum([(x_i - x_avg)*(y_i - y_avg) for x_i,y_i in zip(x,y)])\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 49
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"\n",
"def cy_lstsqr_loops(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" cdef double x_avg, y_avg, var_x, cov_xy, slope, y_interc, x_i, y_i\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = 0\n",
" for x_i in x:\n",
" var_x += (x_i - x_avg)**2\n",
" cov_xy = 0\n",
" for x_i, y_i in zip(x,y):\n",
" cov_xy += (x_i - x_avg)*(y_i - y_avg)\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 50
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"We will generate some sample data for different sample sizes and take a look at the results for the regular Python functions, and the Cython code separately."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"funcs = ['lstsqr_comprehensions', 'lstsqr_loops',\n",
" 'cy_lstsqr_comprehensions', 'cy_lstsqr_loops'] \n",
"\n",
"orders_n = [10**n for n in range(1, 6)]\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for n in orders_n:\n",
" x = [x_i*random.randrange(8,12)/10 for x_i in range(n)]\n",
" y = [y_i*random.randrange(10,14)/10 for y_i in range(n)]\n",
" for f in funcs:\n",
" times_n[f].append(timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 52
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plt.figure(figsize=(8,6))\n",
"plt.plot(orders_n, times_n['lstsqr_comprehensions'], alpha=0.5, \n",
" label='list comprehensions', marker='o', lw=2)\n",
"plt.plot(orders_n, times_n['lstsqr_loops'], alpha=0.5, \n",
" label='for-loops', marker='o', lw=2)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time in ms')\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xlim([0,max(orders_n) + max(orders_n) * 0.1])\n",
"plt.title('Performance comparison list comprehensions and for-loops')\n",
"plt.show()\n",
"\n",
"plt.figure(figsize=(8,6))\n",
"plt.plot(orders_n, times_n['cy_lstsqr_comprehensions'], alpha=0.5, \n",
" label='list comprehensions (Cython', marker='o', lw=2)\n",
"plt.plot(orders_n, times_n['cy_lstsqr_loops'], alpha=0.5, \n",
" label='for-loops (Cython)', marker='o', lw=2)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time in ms')\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xlim([0,max(orders_n) + max(orders_n) * 0.1])\n",
"plt.title('Performance comparison list comprehensions and for-loops in Cython')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAfEAAAGJCAYAAACaQwrRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd4VFX++PH3pBHSG0kIaZDQEkoQBKSGLh1pCwiC4FdZ\nle+6+lt1UcEO+oW1rS7qIqjoioB0WJASpIgBaUpIIJCENEJCgCSkTDJzfn/cZTaBVFIn+byeJ8+T\nOzPnzLmfuclnzj3nnqtTSimEEEIIYXYs6rsBQgghhLg3ksSFEEIIMyVJXAghhDBTksSFEEIIMyVJ\nXAghhDBTksSFEEIIMyVJ3AylpaUxYMAAnJyc+Mtf/lLfzRFlOHjwIB06dKjT9wwPD2flypUAfPPN\nN4wYMaJO378hKR6LujJq1Ci+/vrrOn3PmhQYGMjevXtLfS4vL4+xY8fi4uLCH/7wh2q/16uvvsqs\nWbOqXU9TZ1XfDWgqAgMDuXr1KpaWltjb2zNy5Ej+/ve/Y29vX+W6PvvsMzw9PcnKyqqFloqa0r9/\nf6Kjo+v0PXU6HTqdDoCHH36Yhx9+uMIyc+bMwc/PjzfeeKO2m1enise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S9RX/G+hMseQt7d3qe9VnbKzZs1ixIgRTJs2\njVatWvHCCy+UOuZbmWOz+HsU/1xXrlzJ+fPn6dixIz179mT79u131Q+Qm5vLE088QWBgIM7Ozgwc\nOJCbN2+WGG8uaz9SUlJKxNvO
"text": [
"<matplotlib.figure.Figure at 0x10e8f8610>"
]
},
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAeoAAAGJCAYAAABFDXDOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xl8TXf++PFXQoJE9kgiO0ntgtZSFLG2lNYoKtagqr/B\nTKtT7bSKKqodVBedfnVUSmyli30pGqNUaRGqxJpYEksESSSyfn5/nMltQja5N7n3JO/n4+Eh5957\nznmf9z03n5zz/tzPx0oppRBCCCGERbI2dwBCCCGEKJo01EIIIYQFk4ZaCCGEsGDSUAshhBAWTBpq\nIYQQwoJJQy2EEEJYMGmoi3Ht2jU6d+6Mo6Mjr732mrnDEUXYu3cvjRo1qtB9hoaGsmTJEgBWrFjB\nk08+WaH7tyT5c1FR+vTpw/Llyyt0n6YUGBjIrl27Cn0uPT2dfv364ezszPPPP2/0vmbMmMGIESOM\n3o6pWNJ7Fx4ezttvv23uMEpU6RrqwMBA7OzscHBwwMvLi9GjR3P37t0ybWvx4sV4eHiQnJzMv/71\nLxNHKkylU6dOnDp1qkL3aWVlhZWVFQDDhg1j+/btJa6jl18KDyt/LirKli1bLKrxeVjF5WzdunVc\nv36dpKQk1qxZY5J9WRJj37uVK1fSunVrHBwc8Pb2pk+fPuzbt6/E9SIiIujUqVOBx8xx7pZFpWuo\nrays2LRpEykpKRw+fJhff/2VWbNmPdQ2lFLk5uYSFxdH48aNyxRHdnZ2mdYTD0fyXL4kvxUvLi6O\nBg0aYG398L+eC3u/KtOYVgsWLOCVV15h6tSpXL9+nUuXLjFhwgQ2bNhg7tDKVaVrqPPz9vbmqaee\n4vfffwfgwIEDdOjQARcXF1q2bMmePXsMrw0NDWXq1Kk88cQT2NvbM2rUKJYtW8YHH3yAg4MDu3fv\nJjMzk5dffhkfHx98fHx45ZVXyMzMBCAqKgpfX18++OAD6taty5gxY3jnnXcYNGgQI0aMwNHRkZCQ\nEM6cOcN7772Hp6cnAQEB/PDDD4YYli5dSpMmTXB0dCQoKIjFixcbnsvb/oIFC/D09MTb25uIiAjD\n8+np6bz66qsEBgbi7OxMp06duHfvXonHfb9Lly4xYMAAPDw8cHd3Z9KkSQDk5uYya9YsAgMD8fT0\nZNSoUSQnJwMQGxuLtbU1ERER+Pv74+bmxueff86hQ4cICQnBxcXFsB3Q/rLt2LEjkyZNwtnZmcaN\nG7N79+6HykNenseOHUtUVBR+fn6G17z//vv4+vri6OhIo0aNDNvOyMgo8f0rKr/Fyf+XulKKV155\nBU9PT5ycnAgJCeHEiRMsXryYlStXGs6nZ599ttBtnThxgp49e+Lm5oaXlxfvvfdeqWP/17/+hYeH\nB97e3nz//fds2bKFBg0a4Obmxty5cw37mDFjBgMHDmTIkCE4Ojry2GOPcezYMcPzgYGBfPDBB4SE\nhODg4EBubm6J51BsbCxPPPEEjo6OPPnkk9y8edPwXEmfu2nTphW67r179xg+fDju7u64uLjQtm1b\nbty4YVgv73a7UqrEc3PZsmUEBARQp04d5syZY9j/wYMHad26NU5OTnh5efHqq68W+r7cvn2bvn37\n4uHhgaurK/369ePKlSulOg6A5cuXExAQgLu7e4H932/69Om8++67rFmzBgcHB5YuXVqq4/vyyy8J\nCAigR48eRW47z4YNG2jatCkuLi507dq1wN2okydPEhoaiouLC82aNWPjxo2G58LDw3nppZfo1asX\njo6OhIaGcvHiRcPzhZ33hcn/3kVERPDEE0/w2muv4erqSv369dm2bVuh6925c4fp06fz2Wef0b9/\nf2rVqkW1atV4+umnef/997l69Sr29vYkJSUZ1jl8+DAeHh78/vvvvPTSS/z88884ODjg6upqeE1S\nUhJ9+/bF0dGRxx9/nPPnzxue279/P23atMHZ2Zm2bdvy888/FziO4t5zk1KVTGBgoNq5c6dSSqmL\nFy+qpk2bqmnTpqnLly8rNzc3tXXrVqWUUj/88INyc3NTiYmJSimlunTpogICAtQff/yhcnJyVFZW\nlgoPD1dvv/22Ydtvv/22at++vbpx44a6ceOG6tChg+H5H3/8UVWvXl298cYbKjMzU6Wnp6vp06er\nmjVrqh07dqjs7Gw1cuRIFRAQoObMmaOys7PVF198oerVq2fY/ubNm9X58+eVUkrt2bNH2dnZqcOH\nDxfY/vTp01V2drbasmWLsrOzU7dv31ZKKfXXv/5Vde3aVcXHx6ucnBz1888/q4yMjCKP+8aNGw/k\nLjs7W4WEhKjJkyertLQ0de/ePbVv3z6llFJLlixRwcHB6sKFCyo1NVUNGDBAjRgxQiml1IULF5SV\nlZX6f//v/6mMjAy1Y8cOZWtrq/r3769u3Lihrly5ojw8PNSePXuUUkotXbpUVa9eXS1cuFBlZ2er\nNWvWKCcnJ5WUlFTqPOTP848//qh8fX2VUkqdOnVK+fn5qYSEBKWUUnFxcercuXOlfv+Kyu/9QkND\n1ZIlSwzH88QTTyillNq2bZt67LHH1J07dwzx5MVy//l0v+TkZOXl5aUWLFigMjIyVEpKivrll19K\nHfu7775rOK/c3NzU0KFDVWpqqjpx4oSqVauWio2NVUopNX36dGVjY6O++eYblZ2drebNm6fq1aun\nsrOzlVJKBQQEqFatWqnLly+re/fuleqzExQUpM6cOaPS09NVaGioeuONN5RSqlTrBgcHF7ru559/\nrvr166fS09NVbm6uOnz4sEpOTn4g/6U5N1988UV17949FR0drWrUqKFOnTqllFLq8ccfV5GRkUop\npe7evasOHDhQ6Htz8+ZN9e2336r09HSVkpKiBg0apPr37294vrjjOHHihKpdu7bau3evysjIUJMn\nT1bVq1dXu3btKnRfM2bMMMRf2uMbNWqU4TN7v+nTp6vhw4crpZSKiYlR9vb2aufOnSo7O1t98MEH\nKjg4WGVlZanMzEwVFBSk3nvvPZWVlaV2796tHBwcVExMjFJKqVGjRikHBwfDcfz9738v1Xl/v/s/\nOzY2Nuo///mPys3NVf/+97+Vt7d3oett3bpVVa9eXeXk5BT6vFJK9enTR/373/82LL/88svqb3/7\nm1JKqYiICEO8eUaNGqXc3NzUoUOHVHZ2tho2bJgaMmSIUkp7z52dnVVkZKTKyclRq1atUi4uLobf\nU8W956ZW6RrqgIAAVbt2beXs7KwCAgLUhAkTVHp6upo7d26Bk18ppZ588kn11VdfKaW0k2f69OkF\nng8PD1dTp041LAcFBRl+4Sil1Pbt21VgYKBSSvtlaWtrqzIyMgzPT58+XfXq1cuwvGHDBlW7dm2V\nm5urlNJ+MVtZWRlO7vv1799fffTRR4bt16pVq8BJ6uHhoX755ReVk5OjatWqpY4dO/bANko67vz2\n79+v6tSpU+gHoVu3bgU+ADExMcrGxkbl5OQYflnEx8cbnndzc1Nff/21Yfm5555TCxcuVEppH877\nP4xt27ZVy5cvL1Ue7s9z/ob6zJkzysPDQ+3cuVNlZmYW2E5J719R+S1MUQ31rl27VIMGDdSBAwce\nyOP959P9Vq5cqR599NFCnytN7PefVwcPHjS8/rHHHlPr169XSmnnZfv27Q3P5ebmqrp166qffvpJ\nKaX9sbt06VLD86X57MyePdvw3Geffaaeeuopo9f98ssvVYcOHQo9r/PnvzTn5pUrVwzPt23bVq1Z\ns0YppVTnzp3V9OnTC/3DtThHjhxRLi4uBeIp6jjeeecdFRYWZnju7t27ytbWtsiGOn/DWtrju3Dh\nQpGx5t/ezJkz1fPPP294Ljc3V/n4+KioqCj13//+V3l5eRVYNywsTM2YMUMppTVq+Y8jNTVVVatW\nTV2+fFnt3r27yPP+fvd/doKDgwvkxsrKSl27du2B9SIjIx+I736rV69WHTt2VEppFx5eXl7q0KFD\nhn3d31CHh4ercePGGZa3bNmi
"text": [
"<matplotlib.figure.Figure at 0x10565c6d0>"
]
}
],
"prompt_number": 71
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"As we can see in the first plot, the list comprehensions lead to a slightly increased performance in regular Python code. \n",
"But the second plot is quite interesting: List comprehensions in Cython are significantly slower than the regular for-loop structures.\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"Let us do a quick comparison by how much we were able to improve the performance of the simple least square implementation using Cython so far:\n",
"<br>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"random.seed(12345)\n",
"\n",
"x = [x_i*random.randrange(8,12)/10 for x_i in range(500)]\n",
"y = [y_i*random.randrange(8,12)/10 for y_i in range(100,600)]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"funcs = ['lstsqr_comprehensions', 'cy_lstsqr_comprehensions', \n",
" 'cy_lstsqr_loops'] \n",
"labels = ['list comprehensions', 'list comprehensions (Cython)', \n",
" 'for-loops (Cython)']\n",
"\n",
"times = [timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f).timeit(1000)\n",
" for f in funcs]\n",
"\n",
"times_rel = [times[0]/times[i+1] for i in range(len(times[1:]))]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 73
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
"\n",
"plt.figure(figsize=(8,6))\n",
"x_pos = np.arange(len(funcs[1:]))\n",
"plt.bar(x_pos, times_rel, align='center', alpha=0.5, color=\"green\")\n",
"plt.xticks(x_pos, labels[1:], rotation=90)\n",
"plt.ylabel('relative performance gain')\n",
"plt.title('Performance gain compared to the classic least square implementation')\n",
"ftext = 'For-loops in Cython are {:.2f}x faster then list comprehensions'\\\n",
" .format(times[1]/times[2],1)\n",
"plt.figtext(.15,.8, ftext, fontsize=11, ha='left')\n",
"plt.xlim([-1,len(funcs[1:])])\n",
"plt.ylim([0,max(times_rel)*1.2])\n",
"plt.grid()\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAesAAAIB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770qr\nVq2UzxZ2TD6OpzpZ+/v7i7Ozs7i5uYm/v7+888478uDBA5kxY4Zyks/WoUMH5RtaSEiITJkyxWR8\nzhORiEi1atVMeoe//fab6PV6EclqFI6OjiYnjylTpkj79u2V4aioKHF2dpbMzEwREbl3757odDrl\ngM6tW7du8tVXXynzL126tMlB7O3trZxoSpcuLcePH88zD3PrndPu3bulYsWKJn9r0aJFgSelo0eP\nKt8uRfIm6+rVqyvjUlJSRKfTSWJiYp75/Pnnn+Ll5WWybtlat24t3377rTIcGxsrDg4OYjQalRNA\nzm+uHh4esnr1amW4R48eysl38eLFeb4wNGnSRJYtW5bv+uXe/rn3b+3atU2S79WrV8XBwUEMBoN8\n8sknSuLOXn9HR8dCk3XOtrZnzx7x8fEx+Uzfvn0lPDw83+lDQkKU3lc2nU5nkvh69eolM2fOFBGR\njh07miR3o9EoZcqUkYsXL+aZ97Rp06R79+5FijunnO0jOTlZ3NzcZO3atZKammryuYEDB8rQoUNN\neuE51+Hs2bOFtvHccibrAwcOiJ+fX571GTx4cL7TzpkzR1555RURyTrx1qxZUw4cOJCnbRa23iIi\nO3bsyHdag8EgDg4OJl+6Jk6cWGjPOr8vYtnWrVsnQUFByrBerze54vYox3/29DmT9aOcv0JCQpQk\nKZLVE3V0dJTMzEyT9UpISJCS
"text": [
"<matplotlib.figure.Figure at 0x10e919d10>"
]
}
],
"prompt_number": 88
2014-05-06 05:08:11 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"showdown\"></a>\n",
"<br>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Final Comparison: Cython vs. NumPy vs. SciPy for least squares fitting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To wrap it up, let us compare the Cython code of our simple least squares fit implementation to the Numpy and Scipy functions - after we made some improvements by adding static type declarations and explicit for loops."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%cython\n",
"\n",
"def cy_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" cdef double x_avg, y_avg, temp, var_x, cov_xy, slope, y_interc, x_i, y_i\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = 0\n",
" for x_i, y_i in zip(x,y):\n",
" temp = (x_i - x_avg)\n",
" var_x += temp**2\n",
" cov_xy += temp*(y_i - y_avg)\n",
" slope = cov_xy / var_x\n",
" y_interc = y_avg - slope*x_avg\n",
" return (slope, y_interc)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import scipy.stats"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def lin_lstsqr_mat(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" X = np.vstack([x, np.ones(len(x))]).T\n",
" return (np.linalg.inv(X.T.dot(X)).dot(X.T)).dot(y)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def numpy_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" X = np.vstack([x, np.ones(len(x))]).T\n",
" return np.linalg.lstsq(X,y)[0]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def scipy_lstsqr(x,y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
" return scipy.stats.linregress(x, y)[0:2]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"funcs = ['cy_lstsqr', 'lin_lstsqr_mat',\n",
" 'numpy_lstsqr', 'scipy_lstsqr'] \n",
"\n",
"orders_n = [10**n for n in range(1, 6)]\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for n in orders_n:\n",
" x = [x_i*random.randrange(8,12)/10 for x_i in range(n)]\n",
" y = [y_i*random.randrange(10,14)/10 for y_i in range(n)]\n",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(x,y)' %f, \n",
" 'from __main__ import %s, x, y' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#%pylab inline\n",
"#import matplotlib.pyplot as plt\n",
"\n",
"labels = [('cy_lstsqr', 'Cython implementation'), \n",
" ('lin_lstsqr_mat', 'numpy matrix equation'),\n",
" ('numpy_lstsqr', 'numpy.linalg.lstsq()'), \n",
" ('scipy_lstsqr', 'scipy.stats.linregress()')] \n",
"\n",
"matplotlib.rcParams.update({'font.size': 12})\n",
"\n",
"fig = plt.figure(figsize=(10,8))\n",
"for lb in labels:\n",
" plt.plot(orders_n, times_n[lb[0]], alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('performance gain relative to the slowest approach')\n",
"plt.xlim([1,max(orders_n) + max(orders_n) * 10])\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xscale('log')\n",
"plt.yscale('log')\n",
"plt.title('Performance of least square fit implementations for different sample sizes')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnIAAAIECAYAAACdVcNJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXdYVMfXx7/30vsuZQFR6YKgYtdYEBQ1RBNjiYIVLDG2\nvBrzsytg10SNUYyRqD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6Vvasl6Ym28nExES5eShSv0o/t+np6dSgQQOaOXMmZWRk0PPnzyk9PZ06\ndOhAQ4cOFep7YWFhpXWn2JGpX78+bd26lZKTk+nWrVsKv1sqapufP39OQ4YMoY4dOwr3tzxbT5o0\niVxcXOj06dOUmppKJ0+epF9++UW4Xll7WN06UFz+evXq0datW+n69es0Z84cUlFRoXPnzolkl3we\nq9omERF9+umn1L17d7pw4QKlpKRQVFQUbdu2Tbju5uZGY8aMIaKiPwvFNsvIyKCrV6+ShYUFjRw5\nkoiK/oCZmprS+PHj6fLly3T9+nWaNGkSGRkZ0f379xWyaW3CHLkSlHYArly5QjY2NvTRRx8RUdHL\npPQ/85iYGOI4jp48eUJERZXNwcGhjOzi3o9isrOzSUNDg3766SdRvL59+1LXrl2F3xzHCf8aiinu\nAblw4YIQ9t133xHHcXT27FkhbNWqVWRsbFxhmfv06SM8zMX6N2/eXBRn3Lhxgg2IiBo0aECTJk2S\nKy87O5u0tbUpMjJSFB4SEkISiaRcPebMmUMNGjSg/Px8IezChQvEcZzQY1HcCJw4caLCMpW+j4rc\nt9IUFBSQVCoVGpPdu3eTgYFB
"text": [
"<matplotlib.figure.Figure at 0x111098fd0>"
]
}
],
"prompt_number": 70
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"So, using a few tweaks, such as static type declarations and explicit for-loops instead of list comprehensions, we managed to increase the performance of our least squares fit implementation quite significantly - it outperforms the alternative functions in Numpy and Scipy now."
]
2014-05-04 22:57:02 +00:00
}
],
"metadata": {}
}
]
2014-05-05 19:34:22 +00:00
}