2014-03-25 19:36:28 +00:00
{
"metadata": {
2014-04-13 23:22:30 +00:00
"name": "",
2014-05-08 18:57:48 +00:00
"signature": "sha256:72a4e6f875d6b68497036a926caa242f8bcd993b827ce9b6dd576c414b944cd5"
2014-03-25 19:36:28 +00:00
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
2014-04-13 23:44:19 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-07 22:38:42 +00:00
"[Sebastian Raschka](http://sebastianraschka.com)\n",
"last updated: 05/07/2014 \n",
2014-04-14 18:28:42 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"- [Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb) \n",
"- [Link to the GitHub repository](https://github.com/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb) \n"
2014-04-14 18:28:42 +00:00
]
},
2014-04-22 17:30:30 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"I am really looking forward to your comments and suggestions to improve and extend this collection! Just send me a quick note \n",
2014-05-08 01:57:53 +00:00
"via Twitter: [@rasbt](https://twitter.com/rasbt) \n",
2014-04-22 17:30:30 +00:00
"or Email: [bluewoodtree@gmail.com](mailto:bluewoodtree@gmail.com)\n",
"<hr>"
]
},
2014-04-14 18:28:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-13 23:44:19 +00:00
"# Python benchmarks via `timeit`"
]
},
2014-05-07 22:38:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Code was executed in Python 3.4.0"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"sections\"></a>\n",
"<br>\n",
"<br>"
]
},
2014-04-13 23:22:30 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 15:48:34 +00:00
"# Sections\n",
2014-04-14 18:28:42 +00:00
"- [String operations](#string_operations)\n",
" - [String formatting: .format() vs. binary operator %s](#str_format_bin)\n",
" - [String reversing: [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
" - [String concatenation: `+=` vs. `''.join()`](#string_concat)\n",
" - [Assembling strings](#string_assembly) \n",
2014-04-22 17:30:30 +00:00
" - [Testing if a string is an integer](#is_integer)\n",
" - [Testing if a string is a number](#is_number)\n",
2014-04-14 18:28:42 +00:00
"- [List operations](#list_operations)\n",
" - [List reversing: [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
" - [Creating lists using conditional statements](#create_cond_list)\n",
"- [Dictionary operations](#dict_ops) \n",
2014-04-24 21:25:45 +00:00
" - [Adding elements to a dictionary](#adding_dict_elements)\n",
2014-04-26 05:15:18 +00:00
"- [Comprehensions vs. for-loops](#comprehensions)\n",
2014-04-26 18:40:28 +00:00
"- [Copying files by searching directory trees](#find_copy)\n",
2014-05-01 20:07:40 +00:00
"- [Returning column vectors slicing through a numpy array](#row_vectors)\n",
2014-05-07 07:04:41 +00:00
"- [Speed of numpy functions vs Python built-ins and std. lib.](#numpy)\n",
2014-05-07 22:38:42 +00:00
" - [`sum()` vs. `numpy.sum()`](#np_sum)\n",
" - [`range()` vs. `numpy.arange()`](#np_arange)\n",
" - [`statistics.mean()` vs. `numpy.mean()`](#np_mean)\n",
2014-05-08 18:57:48 +00:00
"- [Cython vs. regular (C)Python](#cython)\n",
"- [Numba vs regular (C)Python & Numpy](#numba)"
2014-04-24 21:25:45 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_operations'></a>"
2014-04-14 15:48:34 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 18:28:42 +00:00
"# String operations"
2014-04-14 15:48:34 +00:00
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-14 15:48:34 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='str_format_bin'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 18:28:42 +00:00
"\n",
2014-04-24 21:25:45 +00:00
"## String formatting: `.format()` vs. binary operator `%s`\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-07 07:04:41 +00:00
"We expect the string `.format()` method to perform slower than %, because it is doing the formatting for each object itself, where formatting via the binary % is hard-coded for known types. But let's see how big the difference really is..."
2014-04-13 23:22:30 +00:00
]
},
2014-03-25 19:36:28 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
2014-05-07 07:04:41 +00:00
"n = 10000\n",
2014-03-25 19:36:28 +00:00
"\n",
2014-05-07 07:04:41 +00:00
"def test_format(n):\n",
" return ['{}'.format(i) for i in range(n)]\n",
2014-03-25 19:36:28 +00:00
"\n",
2014-05-07 07:04:41 +00:00
"def test_binaryop(n):\n",
" return ['%s' %i for i in range(n)]\n",
2014-03-25 19:36:28 +00:00
"\n",
2014-05-07 07:04:41 +00:00
"%timeit test_format(n)\n",
"%timeit test_binaryop(n)"
2014-03-25 19:36:28 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"100 loops, best of 3: 4.01 ms per loop\n",
"1000 loops, best of 3: 1.82 ms per loop"
2014-03-25 19:36:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 131
2014-03-25 19:36:28 +00:00
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['test_format', 'test_binaryop']\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",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(n)' %f, \n",
" 'from __main__ import %s, n' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 132
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('test_format', '.format() method'), \n",
" ('test_binaryop', 'binary operator %')] \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('time per computation in milliseconds [ms]')\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 different string formatting methods')\n",
2014-05-07 22:38:42 +00:00
"max_perf = max( f/b for f,b in zip(times_n['test_format'],\n",
" times_n['test_binaryop']) )\n",
"min_perf = min( f/b for f,b in zip(times_n['test_format'],\n",
" times_n['test_binaryop']) )\n",
" \n",
"ftext = 'The binary op. % is {:.2f}x to {:.2f}x faster than .format()'\\\n",
" .format(min_perf, max_perf) \n",
2014-05-07 07:04:41 +00:00
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
2014-05-07 22:38:42 +00:00
"\n",
"\n",
2014-05-07 07:04:41 +00:00
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
2014-05-07 22:38:42 +00:00
"png": "iVBORw0KGgoAAAANSUhEUgAAAnIAAAIECAYAAACdVcNJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1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WD0PDw8aMGAAd8zj8WjatGlidcqiFrGxsVzZypUricfj0Y0bN7iy\nNWvWkK6ubrVjHjFiBPn6+nLH/fv3JwcHB7E6M2bM4GxARNShQweaOXOmRHmvX78mVVXVSv9DDg4O\nJi0trSr1WLhwodiPChFRbGws8Xg8ioiIIKL3P1AXL16sUk5BQQEpKyvTn3/+KVbu5ORUyZErfz2q\nkl3xxzcgIIB69uwpVqekpEQsulB2faKiorg6Dx8+JD6fTwkJCWJtAwMDxezN4/Ho66+/FqtjaWlJ\n33//PXdsbm5OgYGBVdqgjFatWlFQUFCV5+Xl5bkoYxmSdC8rr+jI8Xg8unnzJld25coV4vF4lJiY\nSESlES0TExMqKSnh6pw+fbpaR45I8rXw9PSkHj16iNU7evQo8fl8evz4MRGVfn+1tbXFonREpTad\nPXs2d5yZmUk8Ho9mzZrFlb18+ZJ4PB6dOHGiSr0OHTpESkpK3PHOnTuJx+NVqufm5sZFqirqUdGR\nq+5aJyYmEo/HowsXLnDni4qKqEOHDrVy5NatW8eVlTnHq1ev5spu3rxJPB6P4uLiiKh2c7uqMdfm\nvmFoaEjz5s0TqzN79mwyNTUlotJoII/Ho/Pnz3PnhUIhtW/fnhtvTk4O8Xg8CgsLq3L8jOYB2+zA\nkBphYWHQ0NCAqqoqbG1tYW5u
2014-05-07 07:04:41 +00:00
"text": [
2014-05-07 22:38:42 +00:00
"<matplotlib.figure.Figure at 0x107efc590>"
2014-05-07 07:04:41 +00:00
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 135
2014-05-07 07:04:41 +00:00
},
2014-04-13 23:22:30 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 15:48:34 +00:00
"<a name='str_reverse'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-14 18:28:42 +00:00
"## String reversing: `[::-1]` vs. `''.join(reversed())`"
2014-04-13 23:22:30 +00:00
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-03-25 19:36:28 +00:00
{
"cell_type": "code",
"collapsed": false,
2014-04-13 23:22:30 +00:00
"input": [
2014-04-13 23:44:19 +00:00
"import timeit\n",
"\n",
2014-04-13 23:22:30 +00:00
"def reverse_join(my_str):\n",
" return ''.join(reversed(my_str))\n",
" \n",
"def reverse_slizing(my_str):\n",
" return my_str[::-1]\n",
"\n",
2014-05-07 07:04:41 +00:00
"test_str = 'abcdefg'\n",
2014-04-13 23:22:30 +00:00
"\n",
"# Test to show that both work\n",
2014-05-07 07:04:41 +00:00
"a = reverse_join(test_str)\n",
"b = reverse_slizing(test_str)\n",
"assert(a == b and a == 'gfedcba')\n",
2014-04-13 23:22:30 +00:00
"\n",
2014-05-07 07:04:41 +00:00
"%timeit reverse_join(test_str)\n",
"%timeit reverse_slizing(test_str)"
2014-04-13 23:22:30 +00:00
],
2014-03-25 19:36:28 +00:00
"language": "python",
"metadata": {},
2014-04-13 23:22:30 +00:00
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 1.33 \u00b5s per loop\n",
"1000000 loops, best of 3: 268 ns per loop"
2014-04-13 23:22:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 10
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['reverse_join', 'reverse_slizing']\n",
"\n",
"orders_n = [10**n for n in range(1, 6)]\n",
"test_strings = (test_str*n for n in orders_n)\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for st,n in zip(test_strings, orders_n):\n",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(st)' %f, \n",
" 'from __main__ import %s, st' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 11
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('reverse_join', '\"\".join(reversed(my_str))'), \n",
" ('reverse_slizing', 'my_str[::-1]')] \n",
"\n",
"matplotlib.rcParams.update({'font.size': 12})\n",
"\n",
"fig = plt.figure(figsize=(10,8))\n",
"for lb in labels:\n",
2014-05-07 22:38:42 +00:00
" plt.plot([n*len(test_str) for n in orders_n], \n",
" times_n[lb[0]], alpha=0.5, label=lb[1], marker='o', lw=3)\n",
2014-05-07 07:04:41 +00:00
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\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 different string reversing methods')\n",
2014-05-07 22:38:42 +00:00
"max_perf = max( j/s for j,s in zip(times_n['reverse_join'],\n",
" times_n['reverse_slizing']) )\n",
"min_perf = min( j/s for j,s in zip(times_n['reverse_join'],\n",
" times_n['reverse_slizing']) )\n",
" \n",
2014-05-07 07:04:41 +00:00
"ftext = 'my_str[::-1] is {:.2f}x to {:.2f}x faster than \"\".join(reversed(my_str))'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
2014-05-07 22:38:42 +00:00
"png": "iVBORw0KGgoAAAANSUhEUgAAAnIAAAIECAYAAACdVcNJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdUFNfbB/DvLEWaFEGwIiBSRYqALQoCg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sXCrv\ngIAAZmhoyE9nZWWxRo0asd9++01kuUGDBjEnJyd+muM4Nn78eJFltm7dyjiOY7GxsXzaihUrGMdx\n7MaNG3za6tWrmZaWVrn7PGDAAJEfDQcHB2ZlZSWyjJ+fHx8Dxhhr3bo1+9///ic2v6ysLKakpMRO\nnTolkr5t2zamrq5eZjnmz5/PWrduzfLy8vi02NhYxnEcO3/+PGOs9I+lOJmZmUxBQYFt3rxZJN3W\n1rZURa7491FW3iUrHQEBAaxz584iyxQWFrK2bduyX3/9lTH23/dz8eJFfpnk5GQmEAhYYmKiyLpB\nQUEi8eY4jk2dOlVkGVNTU/b999/z04aGhiwoKKjMGAipqamxkJCQMufLysqybdu2iaSJK7swvWRF\njuM4FhMTw6cJK03Cioq/vz/T19dnhYWF/DInT56UqCInEAjY48eP+bSIiAimoKDA3r9/L7LsmDFj\n2MCBAxljjL17944pKiqyvXv38vMnT57Munbtyk9X9Ry+ceMG4ziOPXz4UGyZy6rIrV69ml+moKCA\nNW7cmG3atIkxxtjp06cZx3HswYMH/DJv375lSkpKElXkDh8+zKft3buXcRzHDhw4wKcdPHiQcRzH\nV8a8vb3ZiBEjRPL69OkTU1JS
2014-05-07 07:04:41 +00:00
"text": [
2014-05-07 22:38:42 +00:00
"<matplotlib.figure.Figure at 0x105a6fe90>"
2014-05-07 07:04:41 +00:00
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 13
2014-04-13 23:44:19 +00:00
},
2014-04-14 18:28:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_concat'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-14 18:28:42 +00:00
"## String concatenation: `+=` vs. `''.join()`"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-14 18:28:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings in Python are immutable objects. So, each time we append a character to a string, it has to be created \u201cfrom scratch\u201d in memory. Thus, the answer to the question \u201cWhat is the most efficient way to concatenate strings?\u201d is a quite obvious, but the relative numbers of performance gains are nonetheless interesting."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def string_add(in_chars):\n",
" new_str = ''\n",
" for char in in_chars:\n",
" new_str += char\n",
" return new_str\n",
"\n",
"def string_join(in_chars):\n",
" return ''.join(in_chars)\n",
"\n",
"test_chars = ['a', 'b', 'c', 'd', 'e', 'f']\n",
"\n",
"%timeit string_add(test_chars)\n",
2014-05-07 07:04:41 +00:00
"%timeit string_join(test_chars)"
2014-04-14 18:28:42 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 764 ns per loop\n",
"1000000 loops, best of 3: 321 ns per loop"
2014-04-14 18:28:42 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 15
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['string_add', 'string_join']\n",
"\n",
2014-05-07 22:38:42 +00:00
"orders_n = [10**n for n in range(1, 6)]\n",
"test_chars_n = (test_chars*n for n in orders_n)\n",
2014-05-07 07:04:41 +00:00
"times_n = {f:[] for f in funcs}\n",
"\n",
2014-05-07 22:38:42 +00:00
"for st,n in zip(test_chars_n, orders_n):\n",
2014-05-07 07:04:41 +00:00
" for f in funcs:\n",
2014-05-07 22:38:42 +00:00
" times_n[f].append(min(timeit.Timer('%s(st)' %f, \n",
" 'from __main__ import %s, st' %f)\n",
" .repeat(repeat=3, number=1000)))"
2014-05-07 07:04:41 +00:00
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 16
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
2014-05-07 22:38:42 +00:00
"outputs": [],
"prompt_number": 4
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('string_add', 'new_str += char'), \n",
" ('string_join', '\"\".join(chars)')] \n",
"\n",
"matplotlib.rcParams.update({'font.size': 12})\n",
"\n",
"fig = plt.figure(figsize=(10,8))\n",
"for lb in labels:\n",
2014-05-07 22:38:42 +00:00
" plt.plot([len(test_chars)*n for n in orders_n], \n",
" times_n[lb[0]], alpha=0.5, label=lb[1], marker='o', lw=3)\n",
2014-05-07 07:04:41 +00:00
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\n",
2014-05-07 22:38:42 +00:00
"#plt.xlim([1,max(orders_n) + max(orders_n) * 10])\n",
2014-05-07 07:04:41 +00:00
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xscale('log')\n",
"plt.yscale('log')\n",
2014-05-07 22:38:42 +00:00
"plt.title('Performance of different string concatenation methods')\n",
"max_perf = max( a/j for a,j in zip(times_n['string_add'],\n",
" times_n['string_join']) )\n",
"min_perf = min( a/j for a,j in zip(times_n['string_add'],\n",
" times_n['string_join']) )\n",
"\n",
"ftext = '\"\".join(chars) is {:.2f}x to {:.2f}x faster than new_str += char'\\\n",
" .format(min_perf, max_perf)\n",
2014-05-07 07:04:41 +00:00
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
2014-05-07 22:38:42 +00:00
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXdYVEf3x7+7dAQEQVBEaSJdmgJBhaXYY/eNGhtiiRhf\nE2O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z9IS0uLDh8+THfv3qVr167Rzz//zBy7ZgRz7Bj1Ur3HLiUlhczMzOijjz4iIuGXRfUellOnThGP\nx6OCggIiEjpGlpaWNWRX7SEiInr16hWpqKjQxo0bxfINGzaMfHx8uGMej0dTpkwRyyPqYUpKSuLS\nfvzxR+LxeJSYmMilrV69mvT09Oqs85AhQ2jq1KncsZeXFzk6OorlCQwM5NqAiKhjx4703//+V6K8\nV69ekbq6Op04cUIsfceOHaStrV2rHosXL6aOHTtSaWkpl5aUlEQ8Ho8SEhKIiLiX+tmzZ2uV8/Ll\nS1JVVaWtW7eKpXfr1q2GY1fVHrXJru6EBAUFkbu7u1ieiooKsV4jkX3OnDnD5bl9+zbx+XzOORER\nEhIi1t48Ho+++OILsTzW1tb0zTffcMedO3emkJCQWttAROvWrWv0ZFRFUVGR64UUIUl3UXp1x47H\n49HVq1e5tAsXLhCPx+O+YBcuXEimpqZUUVHB5Tl+/Hi9jl3Pnj1p0KBBtZ43MjIScxKIiGbPnk1m\nZmbcsbGxMQ0ZMkQsT//+/WnMmDFEVHmf/PTTT7VepzpPnjwhHo9H586dIyKiu3fvSuw59vLyErMX\nEVFOTo7YMztx4kQyMDAQ+3Gy
2014-05-07 07:04:41 +00:00
"text": [
2014-05-07 22:38:42 +00:00
"<matplotlib.figure.Figure at 0x106fa92d0>"
2014-05-07 07:04:41 +00:00
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 18
2014-04-14 18:28:42 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-24 21:25:45 +00:00
"\n",
2014-04-14 18:28:42 +00:00
"<a name='string_assembly'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 18:28:42 +00:00
"\n",
2014-04-24 21:25:45 +00:00
"## Assembling strings\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 18:28:42 +00:00
"Next, I wanted to compare different methods string \u201cassembly.\u201d This is different from simple string concatenation, which we have seen in the previous section, since we insert values into a string, e.g., from a variable."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
2014-05-07 22:38:42 +00:00
"n = 1000\n",
"\n",
"def plus_operator(n):\n",
" my_str = 'a'\n",
" for i in range(n):\n",
" my_str = my_str + str(1) + str(2)\n",
" return my_str \n",
2014-04-14 18:28:42 +00:00
" \n",
2014-05-07 22:38:42 +00:00
"def format_method(n):\n",
" my_str = 'a'\n",
" for i in range(n):\n",
" my_str = '{}{}{}'.format(my_str,1,2)\n",
2014-04-14 18:28:42 +00:00
" \n",
2014-05-07 22:38:42 +00:00
"def binary_operator(n):\n",
" my_str = 'a'\n",
" for i in range(n):\n",
" my_str = '%s%s%s' %(my_str,1,2)\n",
" return my_str\n",
2014-04-14 18:28:42 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"%timeit plus_operator(n)\n",
"%timeit format_method(n)\n",
"%timeit binary_operator(n)"
2014-04-14 18:28:42 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 869 \u00b5s per loop\n",
"1000 loops, best of 3: 686 \u00b5s per loop"
2014-04-14 18:28:42 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 445 \u00b5s per loop"
2014-04-14 18:28:42 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['plus_operator', 'format_method', 'binary_operator']\n",
"\n",
"orders_n = [10**n for n in range(1, 5)]\n",
"times_n = {f:[] for f in funcs}\n",
"\n",
"for n in orders_n:\n",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(n)' %f, \n",
" 'from __main__ import %s, n' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('plus_operator', 'my_str + str(1) + str(2)'), \n",
" ('format_method', '\"{}{}{}\".format(my_str,1,2)'),\n",
" ('binary_operator', '\"%s%s%s\" %(my_str,1,2)'),\n",
" ] \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]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\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 different string assembly methods')\n",
"\n",
"max_perf = max( p/b for p,b in zip(times_n['plus_operator'],\n",
" times_n['binary_operator']) )\n",
"min_perf = min( p/b for p,b in zip(times_n['plus_operator'],\n",
" times_n['binary_operator']) )\n",
"\n",
"ftext = '\"%s%s%s\" %(my_str,1,2) is {:.2f}x to'\\\n",
" '{:.2f}x faster than my_str + str(1) + str(2)'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcT9n/B/DX/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FIiJXV1dydnbmv3McR5Mm\nTRLk2bFjB3EcR7GxsXzab7/9RhzH0fXr1/m0tWvXko6OTrVtHjp0KE2ePJn/7ujoSO3atRPkmT59\nOr8NiIiaN29Os2fPlhjv1atXpKysTCdOnBCk79y5kzQ0NKqsx88//0zNmzen4uJiPi02NpY4jqOI\niAgiIv6Xy/nz56uMk5eXR4qKirR161ZBup2dXaWO3dv7o6rYFTshXl5e1LlzZ0GesrIyatmyJa1b\nt46I/rd/IiMj+Tz37t0jkUhECQkJgrI+Pj6C7c1xHM2ZM0eQp02bNvTjjz/y383MzCp1BCRp0qQJ\nBQYGVrlcVlaWdu7cKUiTVPfy9IodO47jKDo6mk+7dOkScRzH/6JftGgRmZiYUFlZGZ/n+PHjNXbs\nJHn7OH369ClxHEfh4eES865Zs4Zat24tOJbe5ujoKNieRERpaWmCn6lx48aRrq6uIMagQYOoadOm\ngj8ohg4dSm5uboLYFdscEBBAioqKlJ+fz8d++zyzZs0aMjIy4svcuXOHOI6jmJiYKreHl5cXycrK\nUk5ODp82c+ZMkpGR4TtRRERz5swhOzs7/rs057BevXrR+PHjBXnKfz7Wrl3Lp5WWlpKamhoFBAQQ\nEdHp06eJ4zi6c+cOn6ewsJCa
"text": [
"<matplotlib.figure.Figure at 0x105595650>"
]
}
],
"prompt_number": 27
2014-04-14 18:28:42 +00:00
},
2014-04-13 23:44:19 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-24 21:25:45 +00:00
"\n",
2014-04-22 17:30:30 +00:00
"<a name='is_integer'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-22 17:30:30 +00:00
"## Testing if a string is an integer"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-22 17:30:30 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def string_is_int(a_str):\n",
" try:\n",
" int(a_str)\n",
" return True\n",
" except ValueError:\n",
" return False\n",
"\n",
"an_int = '123'\n",
"no_int = '123abc'\n",
"\n",
"%timeit string_is_int(an_int)\n",
"%timeit string_is_int(no_int)\n",
"%timeit an_int.isdigit()\n",
2014-05-07 22:38:42 +00:00
"%timeit no_int.isdigit()"
2014-04-22 17:30:30 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 420 ns per loop\n",
"100000 loops, best of 3: 2.83 \u00b5s per loop"
2014-04-22 17:30:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"10000000 loops, best of 3: 116 ns per loop"
2014-04-22 17:30:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"10000000 loops, best of 3: 119 ns per loop"
2014-04-22 17:30:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['string_is_int', 'isdigit']\n",
"t1 = '123'\n",
"t2 = '123abc'\n",
"isdigit_method = []\n",
"string_is_int_method = []\n",
"\n",
"for t in [t1,t2]:\n",
" string_is_int_method.append(min(timeit.Timer('string_is_int(t)', \n",
" 'from __main__ import string_is_int, t')\n",
" .repeat(repeat=3, number=1000000)))\n",
" isdigit_method.append(min(timeit.Timer('t.isdigit()', \n",
" 'from __main__ import t')\n",
" .repeat(repeat=3, number=1000000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 52
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"N = len(isdigit_method)\n",
"ind = np.arange(N) # the x locations for the groups\n",
"width = 0.25 # the width of the bars\n",
"\n",
" \n",
"fig, ax = plt.subplots()\n",
"plt.bar(ind, \n",
" [i for i in string_is_int_method], \n",
" width,\n",
" alpha=0.5,\n",
" color='g',\n",
" label='string_is_int(a_str)')\n",
"\n",
"plt.bar(ind + width, \n",
" [i for i in isdigit_method], \n",
" width,\n",
" alpha=0.5,\n",
" color='b',\n",
" label='a_str.isdigit()')\n",
" \n",
"ax.set_ylabel('time in microseconds')\n",
"ax.set_title('Time to check if a string is an integer')\n",
"ax.set_xticks(ind + width)\n",
"ax.set_xticklabels(['\"%s\"' %t for t in [t1, t2]])\n",
"plt.xlabel('test strings')\n",
"plt.xlim(-0.1,1.6)\n",
"#plt.ylim(0,15)\n",
"plt.legend(loc='upper left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEgCAYAAAC0MAQrAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcTfn/B/DXuZGWe1u1kZQsbYr5YtAgxr5np0WUGQaD\nGUu2lokh6wwGY8bIZJlhmGGGZEgSIiahRdOuoWxpoaT6/P6g83N1qxPde5P38/HwcM+5n/P5vO/t\n3Pu+53PO+Xw4xhgDIYSQ955I2QEQQgipHyghEEIIAUAJgRBCyEuUEAghhACghEAIIeQlSgiEEEIA\nUEKQu/DwcIhEIty5c0fZocidSCTCvn375NpGeno6RCIRLly4UKvtXo8tPz8fzs7O0NHRgUgkQmZm\nZl2HWic8PDzQr1+/BtdWbTk5OeGTTz5RdhgNXiNlB/AuE4mqz6fm5uZISkpCdnY2DAwMFBSVtNat\nW8PNzQ2+vr5Kab++yM7Ohra2Nr+8bds2REVF4fz58zAwMEDTpk0VEkdkZCR69uyJ9PR0mJmZ1Vh+\n8+bNKC8vV0Bkim2rtv744w80alS7rysvLy+kpKTgzJkzcoqq4aG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MaM\nGVi9ejXWrVtXZUKIiIjAoEGD4OHhwa9LSkqqVKeirnyTte937twZjx8/RlFRUZVXmNna2mLnzp3I\nz8+HlpYWAODWrVt4/PgxX6ZTp0411vMuohvT3hFLlizhuwKio6ORlpaGM2fOYO7cuUhLS6tyOwsL\nC0RGRuL27dt48OABGGOYNWsW
"text": [
"<matplotlib.figure.Figure at 0x107b9de10>"
]
}
],
"prompt_number": 90
2014-04-22 17:30:30 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='is_number'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-22 17:30:30 +00:00
"## Testing if a string is a number"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-22 17:30:30 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def string_is_number(a_str):\n",
" try:\n",
" float(a_str)\n",
" return True\n",
" except ValueError:\n",
" return False\n",
" \n",
"a_float = '1.234'\n",
"no_float = '123abc'\n",
"\n",
"a_float.replace('.','',1).isdigit()\n",
"no_float.replace('.','',1).isdigit()\n",
"\n",
"%timeit string_is_number(an_int)\n",
"%timeit string_is_number(no_int)\n",
"%timeit a_float.replace('.','',1).isdigit()\n",
2014-05-07 22:38:42 +00:00
"%timeit no_float.replace('.','',1).isdigit()"
2014-04-22 17:30:30 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 418 ns per loop\n",
"1000000 loops, best of 3: 1.28 \u00b5s per loop"
2014-04-22 17:30:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 500 ns per loop"
2014-04-22 17:30:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 427 ns per loop"
2014-04-22 17:30:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 91
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = [\"string_is_number\", \"replace('.','',1).isdigit()\"]\n",
"t1 = '1.234'\n",
"t2 = '123abc'\n",
"isdigit_method = []\n",
"string_is_number_method = []\n",
"\n",
"for t in [t1,t2]:\n",
" string_is_number_method.append(min(timeit.Timer('string_is_number(t)', \n",
" 'from __main__ import string_is_number, t')\n",
" .repeat(repeat=3, number=1000000)))\n",
" isdigit_method.append(min(timeit.Timer(\"t.replace('.','',1).isdigit()\", \n",
" 'from __main__ import t')\n",
" .repeat(repeat=3, number=1000000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 96
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"N = len(isdigit_method)\n",
"ind = np.arange(N) # the x locations for the groups\n",
"width = 0.25 # the width of the bars\n",
"\n",
" \n",
"fig, ax = plt.subplots()\n",
"\n",
"plt.bar(ind , \n",
" [i for i in isdigit_method], \n",
" width,\n",
" alpha=0.5,\n",
" color='b',\n",
" label=\"a_str.replace('.','',1).isdigit()\")\n",
"\n",
"plt.bar(ind + width, \n",
" [i for i in string_is_number_method], \n",
" width,\n",
" alpha=0.5,\n",
" color='g',\n",
" label='string_is_number(a_str)')\n",
"\n",
" \n",
"ax.set_ylabel('time in microseconds')\n",
"ax.set_title('Time to check if a string is a number')\n",
"ax.set_xticks(ind + width)\n",
"ax.set_xticklabels(['\"%s\"' %t for t in [t2, t1]])\n",
"plt.xlabel('test strings')\n",
"plt.xlim(-0.1,1.6)\n",
"#plt.ylim(0,15)\n",
"plt.legend(loc='upper left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEgCAYAAAC0MAQrAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XtcTPn/B/DXjNBtutGNykTShXInucS6rGuby7p0QeV+\ny3cti6S2teKLRdi2xbLV+q7dFVnrsiuxqagkK5HuomRJNaFU5/cHnV/TNHVKMyXv5+PRQ/OZzzmf\n9xnTvOd8Pp9zPjyGYRgQQgj54PGbOwBCCCEtAyUEQgghACghEEIIeYsSAiGEEACUEAghhLxFCYEQ\nQggASghyFRERAT6fj0ePHjV3KDLH5/Px008/ybSNzMxM8Pl8REVFNWi7mrEVFRXBwcEBGhoa4PP5\nyM7ObupQm8S8efMwZsyYVtdWSyIUCrFly5bmDqPZKDR3AK0Fn193bhUKhUhJSUFeXh60tbXlFJU4\nExMTODs7Y/Pmzc3SfkuRl5cHdXV19vG3336LmJgYXL16Fdra2ujYsaNc4oiMjMTw4cORmZkJIyOj\neuv7+/ujsrJSDpHJt62WhMfjgcfjNXcYzYYSQhPJy8tjf7969SqmTZuGhIQE6OvrAwDatGkDBQUF\n6OjoNFeIH/Qbvbqa/wf379+HpaUlLC0tmyWe+q4Nff36Ndq2bQuBQCCniCDXtj4EZWVlaNeuXXOH\nUS/qMmoiOjo67I+mpiYAQFtbmy3r0KGDRJdR1eOzZ8/CxsYGysrKGDBgAJKTk3Hr1i3Y2tpCRUUF\ngwYNQnJyslh78fHxGDt2LAQCAXR0dDBt2rQ6uzrs7OyQlpYGHx8f8Pl8sa6RmJgYDB8+HMrKytDS\n0oKjoyOePHlS5/GWl5fDx8cH3bp1g6KiIgwMDLBy5UqxOoWFhXB2doaamhoMDQ3h5+cn9vzr16/h\n7e2Nrl27QklJCT179kRgYKBYHZFIBA8PDxgZGUFRURHGxsbYunWr1Lj8/PzQoUMHREZGSq1TvctI\nKBT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rfcMVj8cT+7sCAD09PXZMr3pZ9X3zeDzY2NiI1RkyZAi2bt0KkUjE6X1bZeDA\ngQ2KuSWgMYQWoPofdNXAb21lVbM+GIaBi4sLEhMTxX5SUlLg5ubWoLZ5PF69g5qNVdsgWtUxVP0b\nHR0tdgxJSUm4detWg9qZNGkSMjIycPbs2XcPWopffvkFy5cvx+zZs3H27FncvHkTXl5edY4hVI0P\nhYeHY8CAAfjtt99gamqKM2fO1Nsel2QAiL9PgNr/P5tiMoGnpydSUlLw6aef4vbt2xg8eDA2bdok\ntf7OnTvh5+cHDw8P/PXXX0hMTIS7uztKS0vrbavm+4bH44nNeOLz+RLH+Pr1a4n91Pba1CwD6h/U\nr64h71sVFRXO+20p6AzhPdS/f38kJiaia9euDdquXbt2qKioECuztLTEDz/8wM5kAYDExEQUFhai\nZ8+ete6n6hv7+fPnMW3aNM7tV/9gqvqGlZWVhYkTJ9Zav3///igoKEB8fLzEN7LqHB0dMXz4cMyd\nOxfl5eVwcXHhHFNtsdXmypUr6NOnDzw8PNiyjIwMTh+2AwYMwIABA7B+/XqMHz8eP/zwAyZOnMh+\n8NX8P2nKuC0sLBAVFYXFixezZTExMY3at7GxMZYsWYIlS5bAz88PO3bsgK+vb63bXrlyBePHj8e8\nefPYspSUlCZJTjo6Onj48KFYWUJCQqOmC9eMh2EYREdHi5VFRUXBwMAAqqqqnN637zM6Q3gPbdiw\nge0KiI2NRUZGBi5dugQPDw9kZGRI3c7Y2BiRkZF48OAB/v33XzAMg+XLl6OoqAjz5s1DUlISIiMj\n4ezsjOHDh8PW1rbW/ZiYmMDR
"text": [
"<matplotlib.figure.Figure at 0x10782cfd0>"
]
}
],
"prompt_number": 98
2014-04-22 17:30:30 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-24 21:25:45 +00:00
"\n",
2014-04-14 15:48:34 +00:00
"<a name='list_operations'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-14 18:28:42 +00:00
"# List operations"
2014-04-14 15:48:34 +00:00
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-14 15:48:34 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_reverse'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-14 18:28:42 +00:00
"## List reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
2014-04-13 23:44:19 +00:00
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-13 23:44:19 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
2014-05-07 22:38:42 +00:00
"import copy\n",
2014-04-13 23:44:19 +00:00
"\n",
"def reverse_func(my_list):\n",
2014-05-07 22:38:42 +00:00
" return copy.deepcopy(my_list).reverse()\n",
2014-04-13 23:44:19 +00:00
" \n",
"def reversed_func(my_list):\n",
" return list(reversed(my_list))\n",
"\n",
"def reverse_slizing(my_list):\n",
" return my_list[::-1]\n",
"\n",
2014-05-07 22:38:42 +00:00
"n = 10\n",
"test_list = list([i for i in range(n)])\n",
2014-04-13 23:44:19 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"%timeit reverse_func(test_list)\n",
"%timeit reversed_func(test_list)\n",
"%timeit reverse_slizing(test_list)"
2014-04-13 23:44:19 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"100000 loops, best of 3: 13.8 \u00b5s per loop\n",
"1000000 loops, best of 3: 1.44 \u00b5s per loop"
2014-04-13 23:44:19 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000000 loops, best of 3: 330 ns per loop"
2014-04-13 23:44:19 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 104
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['reverse_func', 'reversed_func',\n",
" 'reverse_slizing']\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",
" test_list = list([i for i in range(n)])\n",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(test_list)' %f, \n",
" 'from __main__ import %s, test_list' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 117
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('reverse_func', 'copy.deepcopy(my_list).reverse()'), \n",
" ('reversed_func', 'list(reversed(my_list))'),\n",
" ('reverse_slizing', 'my_list[::-1]'),\n",
" ] \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]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\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 different list reversing approaches')\n",
"\n",
"max_perf = max( f/s for f,s in zip(times_n['reverse_func'],\n",
" times_n['reverse_slizing']) )\n",
"min_perf = min( f/s for f,s in zip(times_n['reverse_func'],\n",
" times_n['reverse_slizing']) )\n",
"\n",
"ftext = 'my_list[::-1] is {:.2f}x to '\\\n",
" '{:.2f}x faster than copy.deepcopy(my_list).reverse()'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXlczdn/x1/3tpfbqk3RqlKRpcWgulRI1jBfe9nGxMxg\nmPkZNOqLmBnL12C2GNkNZuwUbciaKCSVUmJkjySt798fd/pMn26lcmlxno/HffB5n3Pe530+n889\nve8573OOgIgIDAaDwWAwGIxmj7CxDWAwGAwGg8FgyAbm2DEYDAaDwWC0EJhjx2AwGAwGg9FCYI4d\ng8FgMBgMRguBOXYMBoPBYDAYLQTm2DEYDAaDwWC0EJhjx2j2lJaWYtKkSWjdujWEQiFOnTrV2CY1\nS/bs2QMLCwvIy8tj0qRJdS4XFBSE9u3b13hdk+7Y2FjY29tDUVERffr0kU0j3hNV2xgWFgYFBYVG\ntKjpkJWVBaFQiLNnzza2KS0Gf39/eHl5NbYZjGYCc+wY7wV/f38IhUIIhUIoKCjA1NQUAQEBePr0\n6Vvr/vPPP7Fz504cPnwYubm5+Oijj2Rg8YdFWVkZJk2ahFGjRiEnJwdr1qxpsK6vvvoKFy5ceKPu\ngIAAODo64vbt2/jrr7/eug2ywNLSEsHBwXXKKxAIuP+PGjUKf//9d53r8fT0xMSJE+ttX3OgXbt2\nyM3NhbOzc2Ob0mIQCAS8943BqA35xjaA8eHg5uaG3bt3o7S0FJcuXcLUqVORk5ODw4cPN0hfcXEx\nFBUVkZ6eDiMjI3Tv3v2t7KvQ9yHy999/o6CgAN7e3jA0NHwrXWpqalBTU6tVNxHh1q1bWLBgAYyM\njBpcFxGhrKwM8vKy6crq88ez8t7uysrKUFZWlokNtVFeXg4AEAob5zd5Xb4jQq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s2XT16lVKTU2lsLAwSk1NJSJJ/6mhoUFjxoyh5ORkOnfuHJmZmfGeVVRUFKmqqtK6\ndevo1q1bFB8fT7179yZ3d3cuT8eOHWns2LGUkpJCt2/fpmPHjtHhw4drvL+M5glz7BjvBT8/P/L0\n9OSuk5OTydzcnD766CMiIjIxMaFff/2VV+bkyZMkEAgoLy+PiCR/ZKytraV0V/0jW1BQQEpKSvTz\nzz/z8g0bNoz69OnDXQsEApoyZQovz6ZNm0ggEFBSUhIn++GHH0ggENDly5c52erVq6l169a1tnnI\nkCG8P0ru7u7UuXNnXp6AgADuHhARtW3blj7//PNq9RUUFJCqqipFRETw5Js3byZNTc0a7Vi4cCG1\nbduWSkpKOFlSUhIJBAI6deoUEUn/Ma6Oly9fkrKyMuegVODo6Cjl2FV+HjXpFggEtH37dl657t27\n8/KUl5eThYUF/e9//yOif59PXFwclyczM5OEQiH3R7CC4OBg3v0WCAQ0c+ZMXp4OHTrQN998w11b\nWlpScHBwjfegpjZWdeyGDBlC/v7+NZb39PSkiRMn1qkeoVBIOTk5nCwmJoaUlZXp+fPnvLwTJ06k\noUOHEhHRs2fPSEVFhfbs2cOlz5gxg3r06MFdN/Q7d/nyZRIIBJSVlVWtzTU5dqtXr+bylJWVkUgk\not9++42IiI4fP04CgYAyMjK4PE+fPiVVVdVaHbvqWLVqldT7WFV3WloaCQQCio6Orleeqs8iMjKS\nBAIBpaSkcLKioiIyNDSk//73v3W2sVevXjRo0KAa8/v5+ZG+vj7vB8B3331HhoaG3LW7uzvvXSYi\nys7O5vVnGhoaFBYWVmM9jJYBm4plvDdiY2MhEomgqqqKjh07wtLSEtu3b8ejR49w584dzJ49GyKR\niPsMGDAAAoEAt27d4nR069bt
"text": [
"<matplotlib.figure.Figure at 0x1069c7890>"
]
}
],
"prompt_number": 118
2014-04-14 15:48:34 +00:00
},
2014-04-14 18:28:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='create_cond_list'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Creating lists using conditional statements\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 18:28:42 +00:00
"\n",
"In this test, I attempted to figure out the fastest way to create a new list of elements that meet a certain criterion. For the sake of simplicity, the criterion was to check if an element is even or odd, and only if the element was even, it should be included in the list. For example, the resulting list for numbers in the range from 1 to 10 would be \n",
"[2, 4, 6, 8, 10].\n",
"\n",
"Here, I tested three different approaches: \n",
"1) a simple for loop with an if-statement check (`cond_loop()`) \n",
"2) a list comprehension (`list_compr()`) \n",
"3) the built-in filter() function (`filter_func()`) \n",
"\n",
"Note that the filter() function now returns a generator in Python 3, so I had to wrap it in an additional list() function call."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-05-07 22:38:42 +00:00
"import timeit\n",
"\n",
"def cond_loop(n):\n",
" even_nums = []\n",
" for i in range(n):\n",
" if i % 2 == 0:\n",
" even_nums.append(i)\n",
" return even_nums\n",
"\n",
"def list_compr(n):\n",
" even_nums = [i for i in range(n) if i % 2 == 0]\n",
" return even_nums\n",
" \n",
"def filter_func(n):\n",
" even_nums = list(filter((lambda x: x % 2 != 0), range(n)))\n",
" return even_nums\n",
"\n",
"%timeit cond_loop(n)\n",
"%timeit list_compr(n)\n",
"%timeit filter_func(n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 18.1 ms per loop\n",
"100 loops, best of 3: 15.3 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10 loops, best of 3: 22.8 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 119
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['cond_loop', 'list_compr',\n",
" 'filter_func']\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",
" test_list = list([i for i in range(n)])\n",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(n)' %f, \n",
" 'from __main__ import %s, n' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 120
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
2014-04-14 18:28:42 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"labels = [('cond_loop', 'explicit for-loop'), \n",
" ('list_compr', 'list comprehension'),\n",
" ('filter_func', 'lambda function'),\n",
" ] \n",
2014-04-14 18:28:42 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"matplotlib.rcParams.update({'font.size': 12})\n",
2014-04-14 18:28:42 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"fig = plt.figure(figsize=(10,8))\n",
"for lb in labels:\n",
" plt.plot(orders_n, times_n[lb[0]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\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 different conditional list creation methods')\n",
2014-04-14 18:28:42 +00:00
"\n",
2014-05-07 22:38:42 +00:00
"max_perf = max( f/c for f,c in zip(times_n['filter_func'],\n",
" times_n['cond_loop']) )\n",
"min_perf = min( f/c for f,c in zip(times_n['filter_func'],\n",
" times_n['cond_loop']) )\n",
"\n",
"ftext = 'the list comprehension is {:.2f}x to '\\\n",
" '{:.2f}x faster than the lambda function'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"\n",
"plt.show()"
2014-04-14 18:28:42 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
2014-05-07 22:38:42 +00:00
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXlcjdkfxz/3at+UciPtK0qLVGjkkr2msQ0hRAzFDMY6\nBhUyWUf2ZZB9GT+GwdhSEdlCQ5GKspOdRKnv74+mR0/d6t5cSs779bqv133Oc873fM95vs9zv/d7\nlkdARAQGg8FgMBgMxhePsKoVYDAYDAaDwWDIB+bYMRgMBoPBYNQQmGPHYDAYDAaDUUNgjh2DwWAw\nGAxGDYE5dgwGg8FgMBg1BObYMRgMBoPBYNQQmGPHkDvv37/H4MGDoaenB6FQiOPHj1e1Sl8kf/75\nJywsLKCgoIDBgwdLXS4kJARWVlZlHpclOyYmBnZ2dlBSUkLbtm3l04ivAH9/f7Rv377M47IwMzPD\nrFmzPqVqEsnIyIBQKMSpU6fKzBMZGQlFRUXuOCYmBkKhEPfu3fscKlYp0l6/LxGxWIyhQ4d+Etk1\nud++NJhj95Xi7+8PoVAIoVAIRUVFmJqaIjAwEE+fPv1o2f/73/+wdetW7Nu3Dw8ePECLFi3koPHX\nRX5+PgYPHgxfX1/cvn0bERERlZY1fvx4nDlzpkLZgYGBaNasGW7evIldu3Z9dBvkgaWlJUJDQ6ta\njQoRCATc98WLF2Pnzp3c8ZAhQ9CmTZtSZc6fP48xY8Z8Fv0+Fnd3dzx48AD169eXKn9Zba5ObNq0\nCUJh6Z/AktfvS6SstgkEAp6typtPKZshPQpVrQCj6vDw8MCOHTvw/v17nD9/HkOHDsXt27exb9++\nSsnLzc2FkpISUlNT0aBBAzRv3vyj9CuS9zVy7949ZGdno3PnzlL/mJaFuro61NXVy5VNREhLS8Ov\nv/6KBg0aVLouIkJ+fj4UF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amRmKxmM6ePUupqan06tUrEovF1KZNGzp58iTdvHmTVq1aRUpK\nSrxrNXPmTIqLi6PMzEyKioqihg0b0sCBA4mIKDc3l7u+RTq+fPmy1PUjIlqyZAmpqqrS6tWrKS0t\njVasWEEqKio8WzYxMSEdHR2aPXs2paWl0Y4dO0hRUZGXpyRF18HCwoL27NlD169fp27dulGDBg1I\nLBbTX3/9RdevX6eePXuSkZER5eXlERFVeF+W1zZpnnnStLei+yAvL490dHRo7NixlJaWRmlpafTX\nX3/RiRMnyuwPhvxgjt1XysCBA6ldu3bccVJSEpmbm1OLFi2IqPBBtXLlSl6Z2NhYEggE9Pz5cyIq\nfEjY2NiUkh0cHEyWlpbccXZ2NikrK9Py5ct5+bp160Zt27bljgUCAQ0ZMoSXZ926dSQQCCgxMZFL\nmzt3LgkEArpw4QKX9vvvv5Oenl65bf7uu+9o6NCh3HHr1q3J0dGRlycwMJDrAyIiIyMj+vHHHyXK\ny87OJjU1NTp06BAvff369aStrV2mHlOmTOE9qImIEhMTSSAQ0PHjx4lIuh/f169fk4qKCv3xxx+8\n9GbNmpVy7Ipfj7JkCwQC2rx5M69c8+bNeXkKCgrIwsKCFi5cSEQfrk9cXByX58aNGyQUCiklJYVX\nNjQ0lNffAoGARo0axcvTqFEj+uWXX7hjS0tLCg0NLbMPiqjoOklrf+Xpk5qaSgKBgI4ePcqdz83N\npQYNGpRy7IrfWwEBASQWi0vpVdyxK5L9zz//8PI0bdqUBg8eTEQfrtvvv//Onc/PzydNTU1atWqV\nxLYTEV24cIEEAgHdu3ePJ+djHLtRo0bx+q4kZbW5JH5+fqXuweIEBweTUCik27dv83RRUVGhFy9e\n8PIOGjSIunbtWqasXbt2kbKy
2014-04-14 18:28:42 +00:00
"text": [
2014-05-07 22:38:42 +00:00
"<matplotlib.figure.Figure at 0x106fe4290>"
2014-04-14 18:28:42 +00:00
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 122
2014-04-14 18:28:42 +00:00
},
2014-04-14 15:48:34 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='dict_ops'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
2014-04-14 18:28:42 +00:00
"# Dictionary operations "
2014-04-14 15:48:34 +00:00
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-14 15:48:34 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='adding_dict_elements'></a>\n",
2014-04-24 21:25:45 +00:00
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Adding elements to a Dictionary\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-14 15:48:34 +00:00
"\n",
"All three functions below count how often different elements (values) occur in a list. \n",
"E.g., for the list ['a', 'b', 'a', 'c'], the dictionary would look like this: \n",
"`my_dict = {'a': 2, 'b': 1, 'c': 1}`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"import timeit\n",
2014-05-02 01:25:08 +00:00
"from collections import defaultdict\n",
2014-04-14 15:48:34 +00:00
"\n",
"\n",
2014-05-02 01:25:08 +00:00
"def add_element_check1(elements):\n",
2014-05-07 22:38:42 +00:00
" \"\"\"if ele not in dict (v1)\"\"\"\n",
2014-05-02 01:25:08 +00:00
" d = dict()\n",
2014-04-14 15:48:34 +00:00
" for e in elements:\n",
2014-05-02 01:25:08 +00:00
" if e not in d:\n",
" d[e] = 1\n",
2014-04-14 15:48:34 +00:00
" else:\n",
2014-05-02 01:25:08 +00:00
" d[e] += 1\n",
" return d\n",
2014-04-14 15:48:34 +00:00
" \n",
2014-05-02 01:25:08 +00:00
"def add_element_check2(elements):\n",
2014-05-07 22:38:42 +00:00
" \"\"\"if ele not in dict (v2)\"\"\"\n",
2014-05-02 01:25:08 +00:00
" d = dict()\n",
2014-04-14 15:48:34 +00:00
" for e in elements:\n",
2014-05-02 01:25:08 +00:00
" if e not in d:\n",
" d[e] = 0\n",
" d[e] += 1 \n",
" return d\n",
" \n",
"def add_element_except(elements):\n",
2014-05-07 22:38:42 +00:00
" \"\"\"try-except\"\"\"\n",
2014-05-02 01:25:08 +00:00
" d = dict()\n",
2014-04-14 15:48:34 +00:00
" for e in elements:\n",
" try:\n",
2014-05-02 01:25:08 +00:00
" d[e] += 1\n",
2014-04-14 15:48:34 +00:00
" except KeyError:\n",
2014-05-02 01:25:08 +00:00
" d[e] = 1\n",
" return d\n",
2014-04-14 15:48:34 +00:00
" \n",
2014-05-02 01:25:08 +00:00
"def add_element_defaultdict(elements):\n",
2014-05-07 22:38:42 +00:00
" \"\"\"defaultdict\"\"\"\n",
2014-05-02 01:25:08 +00:00
" d = defaultdict(int)\n",
" for e in elements:\n",
" d[e] += 1\n",
" return d\n",
2014-04-14 15:48:34 +00:00
"\n",
2014-05-02 01:34:45 +00:00
"def add_element_get(elements):\n",
2014-05-07 22:38:42 +00:00
" \"\"\".get() method\"\"\"\n",
2014-05-02 01:34:45 +00:00
" d = dict()\n",
" for e in elements:\n",
" d[e] = d.get(e, 1) + 1\n",
" return d\n",
"\n",
"\n",
2014-04-14 15:48:34 +00:00
"random.seed(123)\n",
"\n",
"print('Results for 100 integers in range 1-10') \n",
2014-05-02 01:25:08 +00:00
"rand_ints = [random.randrange(1, 10) for i in range(100)]\n",
"%timeit add_element_check1(rand_ints)\n",
"%timeit add_element_check2(rand_ints)\n",
"%timeit add_element_except(rand_ints)\n",
"%timeit add_element_defaultdict(rand_ints)\n",
2014-05-02 01:34:45 +00:00
"%timeit add_element_get(rand_ints)\n",
2014-05-02 01:25:08 +00:00
"\n",
"print('\\nResults for 1000 integers in range 1-5') \n",
"rand_ints = [random.randrange(1, 5) for i in range(1000)]\n",
"%timeit add_element_check1(rand_ints)\n",
"%timeit add_element_check2(rand_ints)\n",
"%timeit add_element_except(rand_ints)\n",
"%timeit add_element_defaultdict(rand_ints)\n",
2014-05-02 01:34:45 +00:00
"%timeit add_element_get(rand_ints)\n",
2014-04-14 15:48:34 +00:00
"\n",
"print('\\nResults for 1000 integers in range 1-1000') \n",
2014-05-02 01:25:08 +00:00
"rand_ints = [random.randrange(1, 1000) for i in range(1000)]\n",
"%timeit add_element_check1(rand_ints)\n",
"%timeit add_element_check2(rand_ints)\n",
"%timeit add_element_except(rand_ints)\n",
2014-05-02 01:34:45 +00:00
"%timeit add_element_defaultdict(rand_ints)\n",
"%timeit add_element_get(rand_ints)"
2014-04-14 15:48:34 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Results for 100 integers in range 1-10\n",
2014-05-07 22:38:42 +00:00
"100000 loops, best of 3: 16.8 \u00b5s per loop"
2014-05-02 01:25:08 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"100000 loops, best of 3: 18.2 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"100000 loops, best of 3: 17.1 \u00b5s per loop"
2014-05-02 01:34:45 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"100000 loops, best of 3: 14.7 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 20.8 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
2014-05-02 01:25:08 +00:00
"Results for 1000 integers in range 1-5\n",
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 161 \u00b5s per loop"
2014-05-02 01:34:45 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 166 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 128 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 114 \u00b5s per loop"
2014-05-02 01:25:08 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 197 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Results for 1000 integers in range 1-1000\n",
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 166 \u00b5s per loop"
2014-05-02 01:34:45 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 240 \u00b5s per loop"
2014-05-02 01:25:08 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 354 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 281 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 234 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 123
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['add_element_check1', 'add_element_check2',\n",
" 'add_element_except', 'add_element_defaultdict',\n",
" 'add_element_get']\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",
" elements = [random.randrange(1, 100) for i in range(n)]\n",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(elements)' %f, \n",
" 'from __main__ import %s, elements' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 136
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('add_element_check1', 'if ele not in dict (v1)'), \n",
" ('add_element_check2', 'if ele not in dict (v2)'),\n",
" ('add_element_except', 'try-except'),\n",
" ('add_element_defaultdict', 'defaultdict'),\n",
" ('add_element_get', '.get() method')\n",
" ] \n",
"\n",
"matplotlib.rcParams.update({'font.size': 12})\n",
"\n",
"fig = plt.figure(figsize=(10,10))\n",
"for lb in labels:\n",
" plt.plot(orders_n, times_n[lb[0]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\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 different methods to count elements in a dictionary')\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAJ0CAYAAACfnG7yAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcVNX7B/DPvew7KA4oiKyigqK4LwEqWG6V5jdRXNC0\n1DQ1M80NzLCyNJU0v2qCS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+Xybdu2jdzd3UlXV5caN25M/fv3p/v378uWr169mpydnUlX\nV5ecnJwoKCiIiouLq9z/Z86coTfeeIP09PTIzMyMRo4cSTk5OUREFBISIrcPBUGgqKgohXEKCgpo\n0qRJZGJiQmZmZjR58mSaO3cuOTo6yvIEBATIXpePLYoiRUZGVljf5s2biYjo2rVrNHToUDI1NSUz\nMzPq168f/fPPP7LYz78/7du3J21tbTp8+DA9e/aMAgICyM7OjnR1dcnFxYX++9//Vni/165dS6NG\njSIjIyOytramr7/+Wu79KV+vjIwMhfuhrD2vXr2arKysyNDQkD766CMqLi6m4OBgsrGxITMzM/rw\nww/p2bNncmWrev/K10EURcrIyKATJ06QIAj0xx9/0BtvvEH6+vrUpk0bOnTokFzshIQEGjBgABka\nGpKhoSENHjyYkpOT5fJs376dHBwcSFdXl3r06EH79u0jQRDo1KlTRFR67MycOZOsra1JR0eHmjZt\nSr6+vgr3AxFRixYtKtS5zMGDB8nd3Z10dHRIIpHQlClT5I5pRR49ekTTp0+n5s2bk46ODtna2tLS\npUuV3sayNvK8zMxMuXatzP4s3xbKzi2KKNP+bG1tKSgoSKUygiBQcHAwvf/++2RgYEAtWrSg3bt3\n071798jX15eMjIzI3t6efv31V7lyWVlZNHbsWGrSpAkZGRlRz5496eTJk7LlL7L9mZmZNHToUDI3\nNyddXV2yt7en7777rtJ9k5aWJte+yl7v2LGDBg4cSPr6+mRvb0+hoaGVxiAiun//Pvn5+ZGNjQ3p\n6emRs7MzLV++vMoyRETp6en05ptvkp6eHjVv3pxWr15NXl5eNGHCBFmesnPz83788Udq3bq1rO2+\n9957sryKzhPlt5NI+bZ66tQp6tChA+nr61PHjh0pJiZGri4TJkwgBwcH0tPTI3t7e5o3bx4VFhbK\nlpedc7dv307Ozs6kqalJP/30E2loaFBmZqZcrM2bN5OJiQk9efKk2n1XH3HHrhaNHTuWfHx85NKW\nL19OgiDQ48ePKSQkhMzMzCg8PJzS0tLo5MmT1K5dOxo9erQsv6enJxkZGdHkyZPp6t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abb+jixYuUnp5O4eHh\nZGhoSCEhIZWWKSkpoQ4dOlCXLl3o3LlzdOnSJfLx8SFjY2OaOHGiLJ+Xl5fc60WLFpGhoSGtWbOG\nrl27RpcuXZJ9CazsGC2/ncq2VVEUydPTk6KjoykhIYH69+9PdnZ2si97JSUlNH/+fDp37hxlZGTQ\n/v37qWnTphQQECCrb0BAAOnr65OXlxedO3eOrl27Ro8ePaJWrVrR4sWL5fZJr169aMqUKZXus/qO\nO3a1qPyIXXx8PNnb21P37t2JqPRbf/lvq1FRUSQIAj148ICISjtGzs7OFWI/P0JERJSfn086Ojpy\nH6hEREOGDKE+ffrIXguCUOFbWtkI0+XLl2Vp3333HQmCQLGxsbK0H374gczNzavc5nfeeUfuZOHp\n6Unt27eXyzN58mTZPiAiat68OU2bNk1hvPz8fNLX16cjR47IpW/evJlMTU0rrceCBQuoefPmVFRU\nJEu7fPkyCYIg+xav6NtmeY8fPyZdXV3auHGjXHqnTp0qdOyefz8qiy0IAv3yyy9y5bp16yaXp6Sk\nhBwcHGjlypVE9O/7Ex0dLcuT
"text": [
"<matplotlib.figure.Figure at 0x107ebb4d0>"
]
}
],
"prompt_number": 139
2014-05-02 01:34:45 +00:00
},
2014-05-02 01:25:08 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conclusion"
]
2014-04-14 15:48:34 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-05-02 01:25:08 +00:00
"We see from the results that the `try-except` variant is faster than then the `if element in my_dict` alternative if we have a low number of unique elements (here: 1000 integers in the range 1-5), which makes sense: the `except`-block is skipped if an element is already added as a key to the dictionary. However, in this case the `collections.defaultdict` has even a better performance. \n",
"However, if we are having a relative large number of unique entries(here: 1000 integers in range 1-1000), the `if element in my_dict` approach outperforms the alternative approaches."
2014-04-14 15:48:34 +00:00
]
2014-04-22 17:30:30 +00:00
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"comprehensions\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Comprehesions vs. for-loops"
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-25 17:05:32 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comprehensions are not only shorter and prettier than ye goode olde for-loop, \n",
"but they are also up to ~1.2x faster."
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
2014-05-07 22:38:42 +00:00
"n = 1000"
2014-04-24 21:25:45 +00:00
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 140
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set comprehensions"
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"def set_loop(n):\n",
" a_set = set()\n",
" for i in range(n):\n",
" if i % 3 == 0:\n",
" a_set.add(i)\n",
2014-05-07 22:38:42 +00:00
" return a_set\n",
"\n",
2014-04-24 21:25:45 +00:00
"def set_compr(n):\n",
" return {i for i in range(n) if i % 3 == 0}"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 141
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit set_loop(n)\n",
"%timeit set_compr(n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"1000 loops, best of 3: 165 \u00b5s per loop\n",
"10000 loops, best of 3: 145 \u00b5s per loop"
2014-04-24 21:25:45 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 142
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## List comprehensions"
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"def list_loop(n):\n",
" a_list = list()\n",
" for i in range(n):\n",
" if i % 3 == 0:\n",
" a_list.append(i)\n",
" return a_list"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 143
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def list_compr(n):\n",
" return [i for i in range(n) if i % 3 == 0]"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 144
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit list_loop(n)\n",
"%timeit list_compr(n)"
],
"language": "python",
"metadata": {},
2014-04-25 17:05:32 +00:00
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 164 \u00b5s per loop\n",
"10000 loops, best of 3: 143 \u00b5s per loop"
2014-04-25 17:05:32 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 145
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['list_loop', 'list_compr']\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",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(n)' %f, \n",
" 'from __main__ import %s, n' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 152
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('list_loop', 'explicit for-loop'), \n",
" ('list_compr', 'list comprehension')]\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]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\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 explicit for-loops vs. list comprehensions')\n",
"\n",
"max_perf = max( l/c for l,c in zip(times_n['list_loop'],\n",
" times_n['list_compr']) )\n",
"min_perf = min( l/c for l,c in zip(times_n['list_loop'],\n",
" times_n['list_compr']) )\n",
"\n",
"ftext = 'the list comprehension is {:.2f}x to '\\\n",
" '{:.2f}x faster than the explicit for-loop'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlcTfn/B/DXvdo3JbK3SEXbvUmFSEm2yDKGkGQNMwZf\n688womHGd8bSkGWIkN1gTNZUN/sWolWFGWuyhBatn98ffe/RrVvdm0tp3s/Ho8ejc87nvD+fz7mf\nc/r0OZ9zLo8xxkAIIYQQQr54/NouACGEEEIIUQzq2BFCCCGE1BPUsSOEEEIIqSeoY0cIIYQQUk9Q\nx44QQgghpJ6gjh0hhBBCSD1BHTvyWRQVFWHcuHFo3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bNgwJhQK2ZAhQyTSjxkzhmlra0t0rk6fPs14PB5LT09njFXf\nsfPx8alwLMsqn37nzp2Mx+NVmr6s8h2NM2fOMB6Px5KSkrh1+fn5rHnz5mzp0qWMsQ9/oMLCwqqN\nX7Zjl5qayng8Hjtx4oREmg4dOrBx48YxxhjbsmUL09DQYC9fvuS2Z2RkMHV1dbZjxw6uvjwej0VF\nRXFpXr9+zbS0tNjWrVsZY6Xt0c/PT6ZjIN5fXV2dHThwgFv3zTffsC5dunDLDRs2ZKGhoTLHlKb8\n8S5/raguDyUlJbZ9+/Yq88jOzmZqamps5cqVUrfL8jmIz5GgoCBuu/gfolWrVnHrbt68yXg8HktI\nSGCMfWgb4s+BMcaKioqYkZER17GS1n5kPd+MjIzYwIEDJdL07duXjRgxQqFxXr16xXg8HhOJRBUP\n4P+U7djJc94cPnyYS5ORkcF4
"text": [
"<matplotlib.figure.Figure at 0x1055b1910>"
]
}
],
"prompt_number": 153
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dictionary comprehensions"
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-24 21:25:45 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"def dict_loop(n):\n",
" a_dict = dict()\n",
" for i in range(n):\n",
" if i % 3 == 0:\n",
" a_dict[i] = i\n",
" return a_dict"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 146
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def dict_compr(n):\n",
" return {i:i for i in range(n) if i % 3 == 0}"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-07 22:38:42 +00:00
"prompt_number": 147
2014-04-24 21:25:45 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit dict_loop(n)\n",
"%timeit dict_compr(n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-07 22:38:42 +00:00
"10000 loops, best of 3: 159 \u00b5s per loop\n",
"10000 loops, best of 3: 151 \u00b5s per loop"
2014-04-24 21:25:45 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 22:38:42 +00:00
"prompt_number": 148
2014-04-24 21:25:45 +00:00
},
2014-04-26 05:15:18 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"find_copy\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Copying files by searching directory trees"
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-26 05:15:18 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Executing `Unix`/`Linux` shell commands:"
]
},
2014-04-22 17:30:30 +00:00
{
"cell_type": "code",
"collapsed": false,
2014-04-26 05:15:18 +00:00
"input": [
"import subprocess\n",
"\n",
"def subprocess_findcopy(path, search_str, dest): \n",
2014-04-26 08:11:20 +00:00
" query = 'find %s -name \"%s\" -exec cp {} %s \\;' %(path, search_str, dest)\n",
2014-04-26 05:15:18 +00:00
" subprocess.call(query, shell=True)\n",
" return "
],
2014-04-22 17:30:30 +00:00
"language": "python",
"metadata": {},
2014-04-26 05:15:18 +00:00
"outputs": [],
2014-04-26 08:11:20 +00:00
"prompt_number": 30
2014-04-26 05:15:18 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using Python's `os.walk()` to search the directory tree recursively and matching patterns via `fnmatch.filter()`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import shutil\n",
"import os\n",
"import fnmatch\n",
"\n",
"def walk_findcopy(path, search_str, dest):\n",
" for path, subdirs, files in os.walk(path):\n",
" for name in fnmatch.filter(files, search_str):\n",
2014-04-26 08:11:20 +00:00
" try:\n",
" shutil.copy(os.path.join(path,name), dest)\n",
" except NameError:\n",
" pass\n",
" return"
2014-04-26 05:15:18 +00:00
],
"language": "python",
"metadata": {},
"outputs": [],
2014-04-26 08:11:20 +00:00
"prompt_number": 33
2014-04-26 05:15:18 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
2014-04-26 08:11:20 +00:00
"\n",
"def findcopy_timeit(inpath, outpath, search_str):\n",
" \n",
" shutil.rmtree(outpath)\n",
" os.mkdir(outpath)\n",
" print(50*'#')\n",
" print('subprocsess call')\n",
" %timeit subprocess_findcopy(inpath, search_str, outpath)\n",
" print(\"copied %s files\" %len(os.listdir(outpath)))\n",
" shutil.rmtree(outpath)\n",
" os.mkdir(outpath)\n",
" print('\\nos.walk approach')\n",
" %timeit walk_findcopy(inpath, search_str, outpath)\n",
" print(\"copied %s files\" %len(os.listdir(outpath)))\n",
" print(50*'#')\n",
"\n",
2014-04-26 05:15:18 +00:00
"print('small tree')\n",
"inpath = '/Users/sebastian/Desktop/testdir_in'\n",
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
2014-04-26 08:11:20 +00:00
"search_str = '*.png'\n",
"findcopy_timeit(inpath, outpath, search_str)\n",
2014-04-26 05:15:18 +00:00
"\n",
2014-04-26 08:11:20 +00:00
"print('larger tree')\n",
2014-04-26 05:15:18 +00:00
"inpath = '/Users/sebastian/Dropbox'\n",
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
2014-04-26 08:11:20 +00:00
"search_str = '*.csv'\n",
"findcopy_timeit(inpath, outpath, search_str)\n"
2014-04-26 05:15:18 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"small tree\n",
2014-04-26 08:11:20 +00:00
"##################################################"
2014-04-26 05:15:18 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-04-26 08:11:20 +00:00
"subprocsess call\n",
"1 loops, best of 3: 268 ms per loop"
2014-04-26 05:15:18 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-04-26 08:11:20 +00:00
"copied 13 files\n",
"\n",
"os.walk approach\n",
"100 loops, best of 3: 12.2 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-04-26 05:15:18 +00:00
"\n",
2014-04-26 08:11:20 +00:00
"copied 13 files\n",
"##################################################\n",
2014-04-26 05:15:18 +00:00
"larger tree\n",
2014-04-26 08:11:20 +00:00
"##################################################\n",
"subprocsess call\n",
"1 loops, best of 3: 623 ms per loop"
2014-04-26 05:15:18 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-04-26 08:11:20 +00:00
"copied 105 files\n",
"\n",
"os.walk approach\n",
"1 loops, best of 3: 417 ms per loop"
2014-04-26 05:15:18 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-04-26 08:11:20 +00:00
"\n",
"copied 105 files\n",
"##################################################\n"
2014-04-26 05:15:18 +00:00
]
}
],
2014-04-26 08:11:20 +00:00
"prompt_number": 35
},
{
2014-04-26 18:40:28 +00:00
"cell_type": "markdown",
2014-04-26 08:11:20 +00:00
"metadata": {},
2014-04-26 18:40:28 +00:00
"source": [
"I have to say that I am really positively surprised. The shell's `find` scales even better than expected!"
]
2014-04-26 05:15:18 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2014-04-26 18:40:28 +00:00
"<br>\n",
"<br>\n",
"<a name='row_vectors'></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Returning column vectors slicing through a numpy array"
2014-04-26 05:15:18 +00:00
]
2014-04-26 05:15:39 +00:00
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-04-26 18:40:28 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given a numpy matrix, I want to iterate through it and return each column as a 1-column vector. \n",
"E.g., if I want to return the 1st column from matrix A below\n",
"\n",
"<pre>\n",
"A = np.array([ [1,2,3], [4,5,6], [7,8,9] ])\n",
">>> A\n",
"array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])</pre>\n",
"\n",
"I want my result to be:\n",
"<pre>\n",
"array([[1],\n",
" [4],\n",
" [7]])</pre>\n",
"\n",
"with `.shape` = `(3,1)`\n",
"\n",
"\n",
"However, the default behavior of numpy is to return the column as a row vector:\n",
"\n",
"<pre>\n",
">>> A[:,0]\n",
"array([1, 4, 7])\n",
">>> A[:,0].shape\n",
"(3,)\n",
"</pre>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"\n",
2014-04-26 18:48:28 +00:00
"# 1st column, e.g., A[:,0,np.newaxis]\n",
"\n",
2014-04-26 18:40:28 +00:00
"def colvec_method1(A):\n",
" for col in A.T:\n",
" colvec = row[:,np.newaxis]\n",
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 83
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-04-26 18:47:03 +00:00
"# 1st column, e.g., A[:,0:1]\n",
"\n",
2014-04-26 18:40:28 +00:00
"def colvec_method2(A):\n",
" for idx in range(A.shape[1]):\n",
" colvec = A[:,idx:idx+1]\n",
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 82
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-04-26 18:47:03 +00:00
"# 1st column, e.g., A[:,0].reshape(-1,1)\n",
"\n",
2014-04-26 18:40:28 +00:00
"def colvec_method3(A):\n",
" for idx in range(A.shape[1]):\n",
2014-04-26 18:47:03 +00:00
" colvec = A[:,idx].reshape(-1,1)\n",
2014-04-26 18:40:28 +00:00
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 81
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-04-26 18:47:03 +00:00
"# 1st column, e.g., np.vstack(A[:,0]\n",
"\n",
2014-04-26 18:40:28 +00:00
"def colvec_method4(A):\n",
" for idx in range(A.shape[1]):\n",
" colvec = np.vstack(A[:,idx])\n",
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 79
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-04-26 18:47:03 +00:00
"# 1st column, e.g., np.row_stack(A[:,0])\n",
"\n",
2014-04-26 18:40:28 +00:00
"def colvec_method5(A):\n",
" for idx in range(A.shape[1]):\n",
" colvec = np.row_stack(A[:,idx])\n",
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 77
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-04-26 18:47:03 +00:00
"# 1st column, e.g., np.column_stack((A[:,0],))\n",
"\n",
2014-04-26 18:40:28 +00:00
"def colvec_method6(A):\n",
" for idx in range(A.shape[1]):\n",
" colvec = np.column_stack((A[:,idx],))\n",
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 74
},
2014-04-26 18:47:03 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"# 1st column, e.g., A[:,[0]]\n",
"\n",
"def colvec_method7(A):\n",
" for idx in range(A.shape[1]):\n",
" colvec = A[:,[idx]]\n",
" yield colvec"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 89
},
2014-04-26 18:40:28 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"def test_method(method, A):\n",
" for i in method(A): \n",
" assert i.shape == (A.shape[0],1), \"{}, {}\".format(i.shape, A.shape[0],1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 69
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"A = np.random.random((300, 3))\n",
"\n",
"for method in [\n",
" colvec_method1, colvec_method2, \n",
" colvec_method3, colvec_method4, \n",
2014-04-26 18:47:03 +00:00
" colvec_method5, colvec_method6,\n",
" colvec_method7]:\n",
2014-04-26 18:40:28 +00:00
" print('\\nTest:', method.__name__)\n",
" %timeit test_method(colvec_method2, A)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Test: colvec_method1\n",
2014-04-26 18:47:03 +00:00
"100000 loops, best of 3: 16.6 \u00b5s per loop"
2014-04-26 18:40:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Test: colvec_method2\n",
2014-04-26 18:47:03 +00:00
"10000 loops, best of 3: 16.1 \u00b5s per loop"
2014-04-26 18:40:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Test: colvec_method3\n",
2014-04-26 18:47:03 +00:00
"100000 loops, best of 3: 16.2 \u00b5s per loop"
2014-04-26 18:40:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Test: colvec_method4\n",
2014-04-26 18:47:03 +00:00
"100000 loops, best of 3: 16.4 \u00b5s per loop"
2014-04-26 18:40:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Test: colvec_method5\n",
2014-04-26 18:47:03 +00:00
"100000 loops, best of 3: 16.2 \u00b5s per loop"
2014-04-26 18:40:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Test: colvec_method6\n",
2014-04-26 18:47:03 +00:00
"100000 loops, best of 3: 16.8 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Test: colvec_method7\n",
"100000 loops, best of 3: 16.3 \u00b5s per loop"
2014-04-26 18:40:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-04-26 18:47:03 +00:00
"prompt_number": 91
2014-04-26 18:40:28 +00:00
},
2014-05-01 20:07:40 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='numpy'></a>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Speed of numpy functions vs Python built-ins and std. lib."
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-05-07 22:38:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"np_sum\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `sum()` vs. `numpy.sum()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-05-01 20:07:40 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-05-08 01:57:53 +00:00
"from numpy import sum as np_sum\n",
2014-05-01 20:07:40 +00:00
"import timeit\n",
"\n",
"samples = list(range(1000000))\n",
"\n",
"%timeit(sum(samples))\n",
2014-05-08 01:57:53 +00:00
"%timeit(np_sum(samples))"
2014-05-01 20:07:40 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-08 01:57:53 +00:00
"10 loops, best of 3: 18.2 ms per loop\n",
"10 loops, best of 3: 138 ms per loop"
2014-05-01 20:07:40 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-08 01:57:53 +00:00
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['sum', 'np_sum']\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",
" samples = list(range(n))\n",
" times_n['sum'].append(min(timeit.Timer('sum(samples)', \n",
" 'from __main__ import samples')\n",
" .repeat(repeat=3, number=1000)))\n",
" times_n['np_sum'].append(min(timeit.Timer('np_sum(samples)', \n",
" 'from __main__ import np_sum, samples')\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('sum', 'in-built sum() function'), \n",
" ('np_sum', 'numpy.sum() function')]\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]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xscale('log')\n",
"plt.yscale('log')\n",
"plt.title('Performance of explicit for-loops vs. list comprehensions')\n",
"\n",
"max_perf = max( n/i for i,n in zip(times_n['sum'],\n",
" times_n['np_sum']) )\n",
"min_perf = min( n/i for i,n in zip(times_n['sum'],\n",
" times_n['np_sum']) )\n",
"\n",
"ftext = 'the in-built sum() is {:.2f}x to '\\\n",
" '{:.2f}x faster than the numpy.sum()'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdYFMf/B/D3HUXKnYLoAYJyCorSi2CnCGKLJhqjWKLY\n4tcWG6JfY4EYTewFMSY2NMbY8k00xoJKURQlIpKAiiCiWCgWRBCp8/vDHxsWDrjDExA/r+fhedjZ\n2dnP7M4ew+zsnoAxxkAIIYQQQt57wvoOgBBCCCGEKAd17AghhBBCGgnq2BFCCCGENBLUsSOEEEII\naSSoY0cIIYQQ0khQx44QQgghpJGgjh2pE8XFxZgwYQJatGgBoVCI8+fP13dI76XDhw/D1N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M7Ojg0dOpSXf9y4cUwsFvM6VyEhIUwgELA7d+4w\nxmru2I0ZM6bSsSyvYv6ffvqJCQSCKvOXV7GjcfbsWSYQCNjNmze5tIKCAmZoaMi+/vprxti/f6D2\n7dtXY/nlO3ZJSUlMIBCwkydP8vI4ODiwCRMmMMYY27FjB9PS0mJPnz7l1mdkZDBNTU22d+9err4C\ngYCFhoZyeZ4/f85EIhHbtWsXY+xNe/Tx8ZHrGJRtr6mpyQ4fPsylTZ8+nXXv3p1bbtasGQsODpa7\nTFkqHu+KnxU17UNVVZXt2bOn2n3k5uYyDQ0Ntm7dOpnr5TkPZdfIpk2buPVl/xCtX7+eS4uNjWUC\ngYAlJCQwxv5tG2XngTHGiouLmYmJCdexktV+5L3eTExM2Mcff8zL079/fzZy5EillvPs2TMmEAhY\neHh45QP4/8p37BS5bn777Tcu
"text": [
"<matplotlib.figure.Figure at 0x106783160>"
]
}
],
"prompt_number": 27
2014-05-07 22:38:42 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"np_arange\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `range()` vs. `numpy.arange()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
2014-05-01 20:07:40 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-05-08 01:57:53 +00:00
"from numpy import arange as np_arange\n",
"\n",
"n = 1000000\n",
"\n",
"def loop_range(n):\n",
" for i in range(n):\n",
" pass\n",
" return\n",
2014-05-01 20:07:40 +00:00
"\n",
2014-05-08 01:57:53 +00:00
"def loop_arange(n):\n",
" for i in np_arange(n):\n",
" pass\n",
" return\n",
"\n",
"%timeit(loop_range(n))\n",
"%timeit(loop_arange(n))"
2014-05-01 20:07:40 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-05-08 01:57:53 +00:00
"10 loops, best of 3: 50.9 ms per loop\n",
"10 loops, best of 3: 183 ms per loop"
2014-05-01 20:07:40 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-08 01:57:53 +00:00
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['loop_range', 'loop_arange']\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",
" for f in funcs:\n",
" times_n[f].append(min(timeit.Timer('%s(n)' %f, \n",
" 'from __main__ import %s, n' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('loop_range', 'in-built range()'), \n",
" ('loop_arange', 'numpy.arange()')]\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]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xscale('log')\n",
"plt.yscale('log')\n",
"plt.title('Performance of explicit for-loops vs. list comprehensions')\n",
"\n",
"max_perf = max( a/r for r,a in zip(times_n['loop_range'],\n",
" times_n['loop_arange']) )\n",
"min_perf = min( a/r for r,a in zip(times_n['loop_range'],\n",
" times_n['loop_arange']) )\n",
"\n",
"ftext = 'the in-built range() is {:.2f}x to '\\\n",
" '{:.2f}x faster than numpy.arange()'\\\n",
" .format(min_perf, max_perf)\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECA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GjRrFfH19+eXY2FjWqlUr1q5dO8ZY6ZfK\n77//LrJNVFQU4ziOZWRkMMZKO0bW1taV0q7YCcjJyWFKSkoinQnGGBs0aBDr2rUrv8xxHPviiy9E\n4mzZsoVxHMdu3brFhy1evJhxHMeuX7/Ohy1dupTp6upWW+cBAwaI/GB16dKFOTs7i8SZMGECvw8Y\nY6xly5bsm2++EZteTk4OU1VVZSdOnBAJ37p1K9PS0qqyHDNnzhT5oWGMsVu3bjGO49i5c+cYY+9/\ntP75558q05E0/9zcXGZvb8+GDh3KnJ2dmZ+fn0j8UaNGMQ0NDZHO1cmTJxnHcSwxMZExVnPHbuTI\nkZX2ZXkV42/fvp1xHFdl/PIqdjROnz7NOI5j9+7d48MKCgqYvr4+mzdvHmPs/Q9UREREjemX79g9\nePCAcRzHjh07JhKnTZs2bMyYMYwxxjZu3MhUVVXZq1ev+PWpqalMRUWFbdu2ja8vx3Hs7NmzfJw3\nb94wdXV1tnnzZsZYaXsMCgqSaB+Uba+iosL27NnDh3311Vesffv2/HKTJk1YeHi4xGmKU3F/V/yu\nqCkPeXl5tnXr1mrzyM7OZsrKyiwsLEzsekk+h7JjZPny5fz6sj9ES5Ys4cNu3LjBOI5jsbGxjLH3\nbaPsc2CMsaKiImZiYsJ3rMS1H0mPNxMTEzZgwACROL169WIBAQEyTef169eM4zgmFAor78D/V75j\nJ81xs2/fPj5Oamoq4ziOnTx5
"text": [
"<matplotlib.figure.Figure at 0x1050da390>"
]
}
],
"prompt_number": 40
2014-05-07 22:38:42 +00:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"np_mean\"></a>\n",
"<br>\n",
"<br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `statistics.mean()` vs. `numpy.mean()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
2014-05-01 20:07:40 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-05-08 18:57:48 +00:00
"# The statistics module has been added to\n",
"# the standard library in Python 3.4\n",
2014-05-08 01:57:53 +00:00
"\n",
2014-05-08 18:57:48 +00:00
"import timeit\n",
"import statistics as stats\n",
"import numpy as np\n",
2014-05-08 01:57:53 +00:00
"\n",
"def calc_mean(samples):\n",
" return sum(samples)/len(samples)\n",
"\n",
2014-05-08 18:57:48 +00:00
"def np_mean(samples):\n",
" return np.mean(samples)\n",
"\n",
"def np_mean_ary(np_array):\n",
" return np.mean(np_array)\n",
"\n",
"def st_mean(samples):\n",
" return stats.mean(samples)\n",
"\n",
"n = 1000000\n",
"samples = list(range(n))\n",
"samples_array = np_arange(n)\n",
"\n",
"assert(st_mean(samples) == np_mean(samples)\n",
" == calc_mean(samples) == np_mean_ary(samples_array))\n",
2014-05-08 01:57:53 +00:00
"\n",
2014-05-08 18:57:48 +00:00
"%timeit(calc_mean(samples))\n",
2014-05-08 01:57:53 +00:00
"%timeit(np_mean(samples))\n",
2014-05-08 18:57:48 +00:00
"%timeit(np_mean_ary(samples_array))\n",
"%timeit(st_mean(samples))"
2014-05-08 01:57:53 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
2014-05-08 18:57:48 +00:00
"ename": "ImportError",
"evalue": "No module named 'statistics'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-15-6e75a2d67720>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtimeit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mstatistics\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mstats\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mImportError\u001b[0m: No module named 'statistics'"
2014-05-08 01:57:53 +00:00
]
}
],
2014-05-08 18:57:48 +00:00
"prompt_number": 15
2014-05-08 01:57:53 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2014-05-08 18:57:48 +00:00
"funcs = ['st_mean', 'np_mean', 'calc_mean', 'np_mean_ary']\n",
2014-05-08 01:57:53 +00:00
"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",
" samples = list(range(n))\n",
" for f in funcs:\n",
2014-05-08 18:57:48 +00:00
" if f == 'np_mean_ary':\n",
" samples = np.arange(n)\n",
2014-05-08 01:57:53 +00:00
" times_n[f].append(min(timeit.Timer('%s(samples)' %f, \n",
" 'from __main__ import %s, samples' %f)\n",
" .repeat(repeat=3, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 49
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('st_mean', 'statistics.mean()'), \n",
2014-05-08 18:57:48 +00:00
" ('np_mean', 'numpy.mean() on list'),\n",
" ('np_mean_ary', 'numpy.mean() on array'),\n",
" ('calc_mean', 'sum(samples)/len(samples)')\n",
" ]\n",
2014-05-08 01:57:53 +00:00
"\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]], \n",
" alpha=0.5, label=lb[1], marker='o', lw=3)\n",
"plt.xlabel('sample size n')\n",
"plt.ylabel('time per computation in milliseconds [ms]')\n",
"plt.legend(loc=2)\n",
"plt.grid()\n",
"plt.xscale('log')\n",
"plt.yscale('log')\n",
"plt.title('Performance of different approaches for calculating sample means')\n",
"\n",
"max_perf = max( s/c for s,c in zip(times_n['st_mean'],\n",
2014-05-08 18:57:48 +00:00
" times_n['np_mean_ary']) )\n",
2014-05-08 01:57:53 +00:00
"min_perf = min( s/c for s,c in zip(times_n['st_mean'],\n",
2014-05-08 18:57:48 +00:00
" times_n['np_mean_ary']) )\n",
2014-05-08 01:57:53 +00:00
"\n",
2014-05-08 18:57:48 +00:00
"ftext = 'using numpy.mean() on np.arrays is {:.2f}x to '\\\n",
" '{:.2f}x faster than statistics.mean() on lists'\\\n",
2014-05-08 01:57:53 +00:00
" .format(min_perf, max_perf)\n",
2014-05-08 18:57:48 +00:00
"plt.figtext(.14,.15, ftext, fontsize=11, ha='left')\n",
2014-05-01 20:07:40 +00:00
"\n",
2014-05-08 01:57:53 +00:00
"plt.show()"
2014-05-01 20:07:40 +00:00
],
"language": "python",
"metadata": {},
"outputs": [
{
2014-05-08 01:57:53 +00:00
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAIECAYAAACUvmMzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdYFFf7N/DvrPTeXCwgSHGRtqAgVkBBsCciiRqJYkls\nUaxIFARieYIdwTxGomBEUzQafYydZo1gjSIiUlRMVKKCgoV2v3/wMj8WFtglKErO57q8dM/M3HPP\nmTOzxzNlOSIiMAzDMAzDMO89QUsnwDAMwzAMwzQP1rFjGIZhGIZpJVjHjmEYhmEYppVgHTuGYRiG\nYZhWgnXsGIZhGIZhWgnWsWMYhmEYhmklWMeuFSovL8ekSZNgYGAAgUCAkydPtnRK76Xdu3fD3Nwc\nCgoKmDRpkszLhYWFwdLSst7P9cVOTk6Gra0tlJSUMGDAgObZCKZZCQQC7Nq1q0XW/eWXX8LQ0BAC\ngQDff/99i+TQEH9/fwwcOLBZY0o7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cXFpKKiQt99951EuZOTU52O\nXc39UV/s2l88oaGh1LNnT4l5KisrydzcnDZs2EBE/7d/Tp8+zc+Tk5NDAoGAMjMzJZYNDw+XqG+O\n4yggIEBinq5du9KXX37Jf7awsKDw8PB666AhYrGYVqxYwX82MTHhO8/VFi9eTMbGxnLPU/PYISLy\n8/OT+AIhItq/fz8JBAK6e/euTDlmZWURx3H0yy+/1Dt/Y3UmS93v3buXtLW16dmzZ/Wup7bY2FhS\nUFCQKDMyMqJFixZJlM2dO5fM
"text": [
"<matplotlib.figure.Figure at 0x10a23e908>"
2014-05-01 20:07:40 +00:00
]
}
],
2014-05-08 01:57:53 +00:00
"prompt_number": 51
2014-05-01 20:07:40 +00:00
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"cython\"></a>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cython vs regular (C)Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
2014-05-08 18:57:48 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we implement a linear regression via least squares fitting (with vertical offsets) by solving to fit *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",
"\n",
"Therefore we calculate the following parameters as follows:\n",
"\n",
"$a = \\frac{S_{x,y}}{\\sigma_{x}^{2}}\\quad$ (slope)\n",
"\n",
"$b = \\bar{y} - a\\bar{x}\\quad$ (y-axis intercept)\n",
"\n",
"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)\n",
"\n",
"I have described the approach in more detail in this [IPython notebook](http://sebastianraschka.com/IPython_htmls/cython_least_squares.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"**First, the implementation in Python (CPython)**:"
]
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"def py_lstsqr(x, y):\n",
" \"\"\" Computes the least-squares solution to a linear matrix equation. \"\"\"\n",
"\n",
" x_avg = sum(x)/len(x)\n",
" y_avg = sum(y)/len(y)\n",
" var_x = 0\n",
" cov_xy = 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": [],
2014-05-08 18:57:48 +00:00
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"**And now, adding type definitions and compiling the code via Cython**:"
]
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
2014-05-08 18:57:48 +00:00
"prompt_number": 2
2014-05-07 07:04:41 +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",
" 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",
" cov_xy = 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": [],
2014-05-08 18:57:48 +00:00
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"**A small visual proof of concept that our least squares fit works as intended:**"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from matplotlib import pyplot as plt\n",
"\n",
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"n = 500\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",
"\n",
"slope, intercept = cy_lstsqr(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": "iVBORw0KGgoAAAANSUhEUgAAAgIAAAH4CAYAAAA4pIUuAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8jdcfwPHPvZn3ZkkkEpIQYsaMhColVmLP2qMoSukw\na+8WtYqqllbt1qoaLapmVa0aNWtTIoIMIzcy7v3+/gj3Z8SIJG7Ieb9eeVXu8zznfM9zb3O+93nO\nc45GRARFURRFUbIlraUDUBRFURTFclQioCiKoijZmEoEFEVRFCUbU4mAoiiKomRjKhFQFEVRlGxM\nJQKKoiiKko2pREDJ8nbs2EHRokUtHcYrq0SJEvzxxx8vtc7333+fTz/99IWO9fPzY/PmzRkcU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1+fU6dOsWjRIpKSkkhKSmLfvn38+++/JCUlsXjxYm7evImVlRVOTk5YWVkBKQMkz5w5g4jg\n7OyMlZVVqp1H69at+fTTT7lx4wY3btxg9OjRtG/fPk3n4D4bGxuaN29Ov379iImJITQ09KE23W/X\nnTt30Ol0uLi4EB0dnabL6d27d2fw4MHmDvf69eusWbPGXK6dnR1ubm7ExcU9lPw97Vx5enoSFRXF\nrVu3Uq3zace2aNGCefPmceLECQwGw2NtKVOmDCtXriQ+Pp4zZ84wZ84c8/t9+/ZtrK2tcXd3JzEx\nkdGjRz8xBnj2Z2v58uVcvnwZgBw5cqDRaFJ9z5/Fy8uLCxcuqNsD2ZhKBJQspW7duuj1evPPqFGj\nnvlY4YPbg4KC+Pbbb/nggw9w
"text": [
"<matplotlib.figure.Figure at 0x10699bb10>"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"**Eventually, the benchmark:**:"
]
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"funcs = ['py_lstsqr', 'cy_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": [],
2014-05-08 18:57:48 +00:00
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
"labels = [('py_lstsqr', 'regular Python (CPython)'), \n",
" ('cy_lstsqr', 'Cython implementation')]\n",
"\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('time per computation in milliseconds [ms]')\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",
"max_perf = max( py/nu for py,nu in zip(times_n['py_lstsqr'],\n",
" times_n['cy_lstsqr']) )\n",
"min_perf = min( py/nu for py,nu in zip(times_n['py_lstsqr'],\n",
" times_n['cy_lstsqr']) )\n",
"\n",
"ftext = 'Using Cython is {:.2f}x to '\\\n",
" '{:.2f}x faster than regular (C)Python'\\\n",
" .format(min_perf, max_perf)\n",
"plt.title('Performance of least square fit implementations in Cython and (C)Python')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnIAAAIECAYAAACdVcNJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdYFFf3B/DvLB2k4yIg0gVBRWONhaIYY0mssRdsMZpo\nNOaNvkYFVGKLNWLeiL3GjgajWLE3xIqKIkJEBY1dRGnn98f+GFhYYEHYBTyf5/FJ9u7MnTNnyl7u\n3JkRiIjAGGOMMcYqHYm6A2CMMcYYY6XDDTnGGGOMsUqKG3KMMcYYY5UUN+QYY4wxxiopbsgxxhhj\njFVS3JBjjDHGGKukuCGnBpmZmRg6dCgsLCwgkUhw/PhxdYdUKW3btg1OTk7Q1NTE0KFDFU7j7++P\ndu3aqTgyln/bHDt2DBKJBA8fPixxXQ4ODvjll1/KIcqC7O3tERwcrJJlVVQSiQSbNm1Sdxjw8fHB\n119/re4wilVV95mEhARIJBKcPn26yOmICI0aNcK2bdtKVH9WVhbq1KmDffv2fUi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8fHx4nmksGONSPlj28DAgDp06EDR0dF05coVatSoEbVu3VqcJjo6mjQ0\nNGjy5Ml0+/Zt2rlzJ9nb2xebuz/++IOqV69eqpznmD9/Ptnb24ufFTXknj59SoIgUEhICKWlpZGp\nqSmtXbtW/D4rK4vs7Oxo7ty5RZ5fBg8eTMbGxtSvXz+KiYmhM2fOkIODg9z5Pzw8nDQ0NGj27Nl0\n584d2rJlC5mamsrtZ6XZF8sT
"text": [
"<matplotlib.figure.Figure at 0x10ca5b4d0>"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"numba\"></a>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numba vs regular (C)Python & Numpy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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 the code. If you want to read more about Numba, please see refer to the original [website and documentation](http://numba.pydata.org/numba-doc/0.13/index.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we implement a linear regression via least squares fitting (with vertical offsets) by solving to fit *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",
"\n",
"\n",
"\n",
"$\\Rightarrow \\pmb a = (\\pmb X^T \\; \\pmb X)^{-1} \\pmb X^T \\; \\pmb y$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I have described the approach in more detail in this [IPython notebook](http://sebastianraschka.com/IPython_htmls/cython_least_squares.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrix equation "
]
},
{
"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$ \n",
"\n",
"$\\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]$ \n",
"\n",
"With a little bit of calculus, we can rearrange the term in order to obtain the parameter vector \n",
"$\\pmb a = [a\\;b]^T$ \n",
"\n",
"We will implement this matrix equation in \n",
"- `py_mat_lstsqr()` and \n",
"- `numba_mat_lstqrs()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Classic approach"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the more \"classic\" approach that is often found in statistics textbooks, we calculate the following parameters as follows:\n",
"\n",
"$a = \\frac{S_{x,y}}{\\sigma_{x}^{2}}\\quad$ (slope)\n",
"\n",
"$b = \\bar{y} - a\\bar{x}\\quad$ (y-axis intercept)\n",
"\n",
"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": [
"We will implement this \"classic\" approach in\n",
"- `py_lstsqr()` and \n",
"- `numba_lstqrs()`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"\n",
"def py_mat_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.inv(X.T.dot(X)).dot(X.T)).dot(y)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from numba import jit\n",
"\n",
"@jit\n",
"def numba_mat_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.inv(X.T.dot(X)).dot(X.T)).dot(y)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def py_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 = 0\n",
" cov_xy = 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": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@jit\n",
"def numba_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 = 0\n",
" cov_xy = 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": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Verifying that both approaches yield the same results"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"random.seed(12345)\n",
"\n",
"n = 500\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",
"\n",
"np.testing.assert_array_almost_equal(py_lstsqr(x, y),\n",
" py_mat_lstsqr(x, y), decimal=6)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visual checking the least square fit"
]
2014-05-07 07:04:41 +00:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": []
},
2014-05-08 18:57:48 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"from matplotlib import pyplot as plt\n",
"\n",
"slope, intercept = py_mat_lstsqr(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=(7,6))\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": "iVBORw0KGgoAAAANSUhEUgAAAdQAAAGQCAYAAAAX7pEWAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xl4TNf/wPH3zGSbySabLbGEiCzErpYiKEVbpaVqD6qr\nVrXWtvZqS1v90mpRFKW0iKVaamnsRWyx7xHEFktC9mTm8/sjMT+RRCIZBOf1PHnq3nPuWe7czmfu\nveeeqxERQVEURVGUQtE+6gYoiqIoypNABVRFURRFsQAVUBVFURTFAlRAVRRFURQLUAFVURRFUSxA\nBVRFURRFsQAVUBWCg4N58803H3UznjobNmxAq9Vy4cKFR90Uzpw5g1arZdu2bYUqJyQkhBYtWlio\nVYolpKen07t3b9zd3dFqtWzcuFF9Tg+I1aNugPLghYSEEB0dzdq1a3NMX7ZsGVZW6lB42Bo2bMil\nS5fw8PB41E2hbNmyXLp0CVdX10KVo9Fo0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3dnzJgx1KlT\nBx8fH86dO3dfdVryOLnXfrz9AyO/P0aef/55xo8fz4EDB0hMTGT58uUABAQEsHXr1iz5714uXrw4\nRqMxS/vuDuybNm2iRo0afPjhh9SoUYOKFSsSGRmZZ9vyczzn1YeCsrGxwWg0FqoMRT0289S4desW\nERERWb509Ho9lStXzjYK+O7lnIwbN44+ffrg4uJC27Ztsba25siRI6xevZqpU6fi4uKCh4cH06dP\np0KFCly9epXBgwej1+st1qdm
"text": [
"<matplotlib.figure.Figure at 0x10ca76cd0>"
]
}
],
"prompt_number": 45
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"import random\n",
"random.seed(12345)\n",
"\n",
"funcs = ['py_mat_lstsqr', 'numba_mat_lstsqr', \n",
" 'py_lstsqr', 'numba_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 = np.asarray([x_i*np.random.randint(8,12)/10 for x_i in range(n)])\n",
" y = np.asarray([y_i*np.random.randint(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": 28
},
2014-05-07 07:04:41 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import matplotlib.pyplot as plt\n",
"\n",
2014-05-08 18:57:48 +00:00
"labels = [('py_mat_lstsqr', 'matrix equation in reg. (C)Python & Numpy'), \n",
" ('numba_mat_lstsqr', 'matrix equation in Numba'),\n",
" ('py_lstsqr', '\"classic least squares in reg. (C)Python'),\n",
" ('numba_lstsqr', 'classic least squares in Numba'),]\n",
2014-05-07 07:04:41 +00:00
"\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('time per computation in milliseconds [ms]')\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",
2014-05-08 18:57:48 +00:00
"\n",
"#max_perf = max( py/nu for py,nu in zip(times_n['py_lstsqr'],\n",
"# times_n['numba_lstsqr']) )\n",
"#min_perf = min( py/nu for py,nu in zip(times_n['py_lstsqr'],\n",
"# times_n['numba_lstsqr']) )\n",
"#\n",
"#ftext = 'Using Numba is {:.2f}x to '\\\n",
"# '{:.2f}x faster than regular (C)Python and Numpy'\\\n",
"# .format(min_perf, max_perf)\n",
"\n",
"plt.figtext(.14,.15, ftext, fontsize=11, ha='left')\n",
"plt.title('Performance of least square fit implementations in Numba and (C)Python+Numpy')\n",
2014-05-07 07:04:41 +00:00
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
2014-05-08 18:57:48 +00:00
"png": "iVBORw0KGgoAAAANSUhEUgAAAnwAAAIECAYAAACDnPP6AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXdYFcfXx7+79HLpXhBQiiAoUawYG1wVW+xREzUoWLBG\nxagRK1jA3kuMaLCg/uwtJjYUDNhQBBUBaXZRxF5ROO8fvnfDci9wQQGj83kenoednTlz5kzZc6fs\nckREYDAYDAaDwWB8sfAVrQCDwWAwGAwGo2xhDh+DwWAwGAzGFw5z+BgMBoPBYDC+cJjDx2AwGAwG\ng/GFwxw+BoPBYDAYjC8c5vAxGAwGg8FgfOF8lQ7f+/fvMWDAAJiZmYHneZw8ebKiVfpPsmPHDlSr\nVg3q6uoYMGCA0jg+Pj5o3bp1OWvGKFg3kZGR4Hked+/eLbEsOzs7BAcHl4GWitja2iIoKKhc8vpc\n4XkeW7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7k6upKR48epevXr9O2bdvIyMiI1q1bJ5JrYWFBGzdupLS0NBo+fDjp6elRmzZtaMOGDZSW\nlkYjR44kPT09oQ29evWKatSoQT169KALFy5QWloaBQUFkZaWFiUmJhLRvw8AV1dXOnz4MKWmplL/\n/v3JwMBAsNfFixeJ4zjas2ePaDwprn8/ffqU3N3dqVevXkI7lfeh/A/bvLw8cnNzo7p161J0dDRd\nvnyZfvzxRzI2NhbyUkXP4tq6MmQyGfn6+grXqoxHhbWfWbNm0cyZM6ly5cqCs/ExDl+9evXoxIkT\ndPXqVWrcuDHVrl2bmjZtSseP
2014-05-07 07:04:41 +00:00
"text": [
2014-05-08 18:57:48 +00:00
"<matplotlib.figure.Figure at 0x103b0fb10>"
2014-05-07 07:04:41 +00:00
]
}
],
2014-05-08 18:57:48 +00:00
"prompt_number": 33
2014-05-07 07:04:41 +00:00
},
2014-04-26 05:15:39 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
2014-03-25 19:36:28 +00:00
}
],
"metadata": {}
}
]
}