python_reference/benchmarks/timeit_tests.ipynb

2301 lines
282 KiB
Plaintext
Raw Normal View History

2014-03-25 19:36:28 +00:00
{
"metadata": {
2014-04-13 23:22:30 +00:00
"name": "",
2014-05-07 07:04:41 +00:00
"signature": "sha256:a2b1ea908157604888db3edc69f70ebb7f6f25d46bbb813b419c0879633a8962"
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-04-14 18:28:42 +00:00
"Sebastian Raschka \n",
"last updated: 04/14/2014 \n",
"\n",
2014-04-14 18:30:27 +00:00
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb)"
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",
"via Twitter: [@rasbt](https://twitter.com/rasbt) \n",
"or Email: [bluewoodtree@gmail.com](mailto:bluewoodtree@gmail.com)\n",
"<hr>"
]
},
2014-04-14 18:28:42 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
2014-04-13 23:44:19 +00:00
"# Python benchmarks via `timeit`"
]
},
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",
"- [Cython vs. regular (C)Python](#cython)"
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 07:04:41 +00:00
"100 loops, best of 3: 5.67 ms per loop\n",
"100 loops, best of 3: 3.9 ms per loop"
2014-03-25 19:36:28 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 3
},
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": [],
"prompt_number": 55
},
{
"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",
"ftext = 'binary op. % is {:.2f}x faster than .format()'\\\n",
" .format(times_n['test_format'][-1]\\\n",
" /times_n['test_binaryop'][-1])\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnIAAAIECAYAAACdVcNJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXdYFNf3/9+79CooAQtKEQWkq9hFVDQaY4x+sINgiy22\nxBZFARVT/NklMZaIBhsaC3ZjQcRuBBRQEATsiKgoNmA5vz/4MrKwdFgEz+t59tG5c++ZM2fuzr45\nc+8dERERGIZhGIZhmBqHuLodYBiGYRiGYcoHCzmGYRiGYZgaCgs5hmEYhmGYGgoLOYZhGIZhmBoK\nCzmGYRiGYZgaCgs5hmEYhmGYGgoLOabGkZ2djVGjRkFPTw9isRihoaHV7VKNZPfu3WjatCkUFRUx\natSoUrfz8fFBs2bNitwuynZISAisra2hrKyMbt26Vc5J1EACAgKgpKQkt+OV9zp/yojFYmzfvr26\n3agSnJ2dMXbs2Cqx7enpiR49elSJbab6YCHHVAmenp4Qi8UQi8VQUlKCsbExJkyYgOfPn1fY9j//\n/IMdO3bg0KFDePLkCdq3b18JHn9eSCQSjBo1CkOGDMH9+/exatWqctuaOXMmLl++XKLtCRMmoHXr\n1khMTMTevXsrfA6VgZmZGXx9fStsR1FREVu3bi1V3SFDhuDRo0cVPmZpqMzrXJUEBgZCLC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XqTJ8+nUxNTYkoNxsoEono5MmTwv7MzExq1KiRcL7Pnz8nkUhEISEhRZ4/UzPgyQ5M\nlRESEgItLS2oq6vDxsYGZmZm
"text": [
"<matplotlib.figure.Figure at 0x10610c550>"
]
}
],
"prompt_number": 56
},
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 07:04:41 +00:00
"100000 loops, best of 3: 1.56 \u00b5s per loop\n",
"1000000 loops, best of 3: 362 ns per loop"
2014-04-13 23:22:30 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 07:04:41 +00:00
"prompt_number": 1
},
{
"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": [],
"prompt_number": 2
},
{
"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",
" 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 reversing methods')\n",
"max_perf = max( [times_n['reverse_join'][i]/times_n['reverse_slizing'][i]\n",
" for i in range(len(times_n['reverse_join']))] )\n",
"min_perf = min( [times_n['reverse_join'][i]/times_n['reverse_slizing'][i]\n",
" for i in range(len(times_n['reverse_join']))] )\n",
"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",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnIAAAIECAYAAACdVcNJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXdYVMfXx793KdKkCKKiIiBSRGBBiiUKAoKgomKPCAhi\ngua1xBJjVCCWGGvsieUn1hg09lhRsBuxoSIgSLGiWFBAOuf9g3BlKbIgLsX5PM8+7MydOXPuuffO\nHmbOzOWIiMBgMBgMBoPBaHAI6loBBoPBYDAYDEbNYI4cg8FgMBgMRgOFOXIMBoPBYDAYDRTmyDEY\nDAaDwWA0UJgjx2AwGAwGg9FAYY4cg8FgMBgMRgOFOXKMBk9BQQF8fX2hoaEBgUCAc+fO1bVKDZI9\ne/agffv2kJaWhq+vr9j1goKC0KFDh0rTlcmOiIhAp06dICsrCwcHh9o5iQZISEgIZGRk6lqNekty\ncjIEAgEuXbpU16p8FnR0dLBgwYLPItve3h7+/v6fRTaj/sAcOYZE8PHxgUAggEAggIyMDHR0dBAQ\nEIDXr19/suy///4bf/75J44cOYLU1FR07dq1FjT+sigsLISvry9GjBiBR48eYeXKlTWWNX36dPz7\n779Vyg4ICICVlRWSkpKwb9++Tz6H2kBfXx/BwcGfLEdaWhrbtm0Tq+yIESPw9OnTT26zsaKtrY3U\n1FTY2NjUtSqfxPz586Grq1sun+M4cBz3Wdr8nLIZ9QfpulaA8eXQs2dPhIaGoqCgANeuXYO/vz8e\nPXqEI0eO1EheXl4eZGVlER8fj9atW6NLly6fpF+JvC+Rp0+fIisrC66urmjVqtUnyVJUVISiouJH\nZRMREhIS8NNPP6F169Y1bouIUFhYCGnp2unKautHj+M4VLXXeonucnJykJOTq5V2q0tRUREAQCCo\nm//pxXnmBAIBNDU1JaQRg9HwYCNyDIkhIyMDTU1NaGlpwd3dHZMmTcLx48eRm5sLANi9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sbJo5cya1bduWvyeysrKIiKhdu3akpqZGv/76KyUkJFBoaCjJyMiI3LtV\n9WdFRUVkYWFBNjY2dPXqVbp16xb17t2blJWV+fOtys6Mhgtz5BgSwdvbm5ycnPh0dHQ06enpUdeu\nXYmouDP7448/ROqcPXuWOI6j9PR0Iir+ETA0NCwnOzAwkPT19fl0VlYWNWnShNavXy9SbtCgQeTg\n4MCnOY6jsWPHipTZsmULcRxHUVFRfN6SJUuI4zi6ceMGn7dixQrS0ND46DkPGDBA5EfDzs6OhEKh\nSJmAgADeBkREbdu2pf/7v/+rUF5WVhYpKCjQiRMnRPK3bt1Kqqqqleoxe/Zsatu2LeXn5/N5UVFR\nxHEcnTt3jojK/1hWRGZmJsnJydGmTZtE8q2srMo5cqWvR2WyyzodgYGB1KVLF5EyRUVF1L59e/rt\nt9+I6MP1uXDhAl8mMTGRBAIBxcXFidQNDg4WsTfHcTRp0iSRMsbGxvTjjz/yaX19fQoODq7UBiWo\nqKhQSEhIpcelpaVp69atInkV6V6SX9aR4ziObt68yeeVOE0ljsqsWbNIV1eXioqK+DLHjx8Xy5ET\nCAT06NEjPi88PJzk5OTo7du3ImXHjBlDAwcOJCKiN2/ekLy8PO3Zs4c/PmHCBOrWrRufrukzfOPG\nDeI4jpKTkyvUuTJHbsWKFXyZwsJCatq0KW3YsIGIiE6ePEkcx9GDBw/4Mq9fvyYFBQWxHLmDBw/y\neXv27CGO42jfvn183v79+4njON4Z8/b2phEjRojIysnJIQUFBTpw4AAREc2bN490dHTKtdmuXTsa\nMGCASJ6rqyuNHDmSiMTrz06d
"text": [
"<matplotlib.figure.Figure at 0x105b68be0>"
]
}
],
"prompt_number": 5
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 07:04:41 +00:00
"1000000 loops, best of 3: 1.15 \u00b5s per loop\n",
"1000000 loops, best of 3: 436 ns per loop"
2014-04-14 18:28:42 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-07 07:04:41 +00:00
"prompt_number": 70
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"funcs = ['string_add', 'string_join']\n",
"\n",
"orders_n = [10**n for n in range(1, 9)]\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(test_chars)' %f, \n",
" 'from __main__ import %s, test_chars' %f)\n",
" .repeat(repeat=10, number=1000)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 71
},
{
"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 = [('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",
" 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 reversing methods')\n",
"ftext = 'new_str += char is {:.2f}x faster than \"\".join(chars)'\\\n",
" .format(times_n['string_add'][-1]\\\n",
" /times_n['string_join'][-1])\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAnMAAAIECAYAAAByl6h3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdYFNfXB/DvLB0EQRAUEEGQ3hVBUFywl6hRk1hQrFGM\nNZZYMIBGY0xsMUZjxYI9GktiBbGBXVARkGJHFCMYBUWE+/7By/xcacsufc7nefaRnXLvPbPDcpx7\nZpdjjDEQQgghhJA6SVTTAyCEEEIIIbKjZI4QQgghpA6jZI4QQgghpA6jZI4QQgghpA6jZI4QQggh\npA6jZI4QQgghpA6jZI7UeR8+fMDIkSOhp6cHkUiEs2fP1vSQ6qS9e/fC3NwcioqKGDlypNT7BQcH\no2XLlqU+L63tyMhI2NvbQ1lZGb6+vpUTRB0UGhoKJSWlmh5GrXX//n2IRCJERUXV9FCqhKmpKRYu\nXFglbYvFYowZM6ZK2ia1CyVzpFoMHz4cIpEIIpEISk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Cw0NLXW9oqIi27Jli8SyksZetPzTZI7j\nOHbjxg1+WVHiVJSszJkzh5mZmbGCggJ+m2PHjkmVzIlEIvbo0SN+2enTp5mqqip79eqVxLYjRoxg\nffv2ZYwxlpmZydTU1NjevXv59d988w3z9PTkn8v6O3z9+nXGcRy7f/9+iWMuLZlbvnw5v01+fj7T\n1NRk69atY4wxduLECcZxHEtJSeG3efnyJVNXV5cqmTt48CC/bO/evYzjOLZ//35+2YEDBxjHcXxC\n5u/vzwYOHCjR1rt375i6ujr7
"text": [
"<matplotlib.figure.Figure at 0x1064c4dd8>"
]
}
],
"prompt_number": 73
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",
"def plus_operator():\n",
" return 'a' + str(1) + str(2) \n",
" \n",
"def format_method():\n",
" return 'a{}{}'.format(1,2)\n",
" \n",
"def binary_operator():\n",
" return 'a%s%s' %(1,2)\n",
"\n",
"%timeit plus_operator()\n",
"%timeit format_method()\n",
"%timeit binary_operator()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 764 ns per loop\n",
"1000000 loops, best of 3: 494 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000000 loops, best of 3: 79.3 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 17
},
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",
"%timeit no_int.isdigit()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 401 ns per loop\n",
"100000 loops, best of 3: 3.04 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000000 loops, best of 3: 92.1 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000000 loops, best of 3: 96.3 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 5
},
{
"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",
"%timeit no_float.replace('.','',1).isdigit()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 400 ns per loop\n",
"1000000 loops, best of 3: 1.15 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000000 loops, best of 3: 452 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000000 loops, best of 3: 394 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"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",
"\n",
"def reverse_func(my_list):\n",
" new_list = my_list[:]\n",
" new_list.reverse()\n",
" return new_list\n",
" \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",
"%timeit reverse_func([1,2,3,4,5])\n",
"%timeit reversed_func([1,2,3,4,5])\n",
"%timeit reverse_slizing([1,2,3,4,5])\n",
"\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.4 GHz Intel Core Duo\n",
"# 8 GB 1067 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
2014-04-14 15:48:34 +00:00
"1000000 loops, best of 3: 930 ns per loop\n",
"1000000 loops, best of 3: 1.89 \u00b5s per loop"
2014-04-13 23:44:19 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-04-14 15:48:34 +00:00
"1000000 loops, best of 3: 775 ns per loop"
2014-04-13 23:44:19 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-04-14 15:48:34 +00:00
"prompt_number": 1
},
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": [
"import timeit\n",
"\n",
"def cond_loop():\n",
" even_nums = []\n",
" for i in range(100):\n",
" if i % 2 == 0:\n",
" even_nums.append(i)\n",
" return even_nums\n",
"\n",
"def list_compr():\n",
" even_nums = [i for i in range(100) if i % 2 == 0]\n",
" return even_nums\n",
" \n",
"def filter_func():\n",
" even_nums = list(filter((lambda x: x % 2 != 0), range(100)))\n",
" return even_nums\n",
"\n",
"%timeit cond_loop()\n",
"%timeit list_compr()\n",
"%timeit filter_func()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 14.4 \u00b5s per loop\n",
"100000 loops, best of 3: 12 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 23.9 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 14
},
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",
" 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",
" 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",
" 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",
" 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",
" 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-02 01:34:45 +00:00
"10000 loops, best of 3: 28 \u00b5s per loop"
2014-05-02 01:25:08 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 26.2 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"10000 loops, best of 3: 26.5 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 22.8 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"10000 loops, best of 3: 33.3 \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-02 01:34:45 +00:00
"1000 loops, best of 3: 242 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 239 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"1000 loops, best of 3: 203 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"10000 loops, best of 3: 184 \u00b5s per loop"
2014-05-02 01:25:08 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"1000 loops, best of 3: 350 \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-02 01:34:45 +00:00
"1000 loops, best of 3: 262 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 370 \u00b5s per loop"
2014-05-02 01:25:08 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"1000 loops, best of 3: 502 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"1000 loops, best of 3: 422 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
2014-05-02 01:34:45 +00:00
"1000 loops, best of 3: 373 \u00b5s per loop"
2014-04-14 15:48:34 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-05-02 01:34:45 +00:00
"prompt_number": 25
},
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",
"n = 1000\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"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",
" return a_set"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def set_compr(n):\n",
" return {i for i in range(n) if i % 3 == 0}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"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": [
"10000 loops, best of 3: 136 \u00b5s per loop\n",
"10000 loops, best of 3: 113 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 22
},
{
"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": [],
"prompt_number": 23
},
{
"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": [],
"prompt_number": 24
},
{
"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": [
"10000 loops, best of 3: 129 \u00b5s per loop\n",
"10000 loops, best of 3: 111 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 25
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-04-25 17:05:32 +00:00
"prompt_number": 26
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-04-25 17:05:32 +00:00
"prompt_number": 27
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-04-25 17:05:32 +00:00
"10000 loops, best of 3: 121 \u00b5s per loop\n",
"10000 loops, best of 3: 127 \u00b5s per loop"
2014-04-24 21:25:45 +00:00
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
2014-04-25 17:05:32 +00:00
"prompt_number": 28
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-01 20:07:40 +00:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import timeit\n",
"\n",
"samples = list(range(1000000))\n",
"\n",
"%timeit(sum(samples))\n",
"%timeit(np.sum(samples))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 loops, best of 3: 18.3 ms per loop\n",
"10 loops, best of 3: 136 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit(list(range(1000000)))\n",
"%timeit(np.arange(1000000))\n",
"\n",
"# note that in Python range() is implemented as xrange()\n",
"# with lazy evaluation (generator)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 82.6 ms per loop\n",
"100 loops, best of 3: 5.35 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import statistics\n",
"\n",
"%timeit(statistics.mean(samples))\n",
"%timeit(np.mean(samples))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 1.14 s per loop\n",
"10 loops, best of 3: 141 ms per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 14
},
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)]"
]
},
{
"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": [],
"prompt_number": 62
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext cythonmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 58
},
{
"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": [],
"prompt_number": 63
},
{
"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": [],
"prompt_number": 64
},
{
"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 = [('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",
"ftext = 'Cython % is {:.2f}x faster than (C)Python'\\\n",
" .format(times_n['py_lstsqr'][-1]\\\n",
" /times_n['cy_lstsqr'][-1])\n",
"plt.figtext(.14,.75, ftext, fontsize=11, ha='left')\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+/AAAIABJREFUeJzsnXdYFcfXx7976SAdL9KkC4iAqFiRJnYTe0FRsRBjSzQa\nNf5UsBB7jZhELB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21fitYYOHUpubm509OhRSk5Opu3bt5Oenh6tX79eILdevXr022+/0f37\n92nMmDGkpaVFHTp0oE2bNtH9+/dp/PjxpKWlxbeh9+/fk5OTE/Xp04cuX75M9+/fp7CwMFJTU6Nb\nt24R0ccHmpubG8XExNC9e/do2LBhpKOjw9vrypUrxHEc7d27VzCelNW/X79+TV5eXjRgwAC+nRb0\nocKOXH5+PjVv3pzc3d3pzJkzdP36derfvz/p6+vz15JFz7LaujR8fHwoODiYP5ZlPCrK3r17y3RG\niCQ/HEUikcAhuXv3LolEIrpw4QJlZmbStGnTyMLCgrdXwY8/S0tL0tfXp4ULF9K9e/dox44dpKKi\nImgjso7vxsbGtG7dOnrw4AE/jpTU14hk79taWlrUuXNnio+Pp2vXrlHTpk2pbdu2fJ74+HhSUlKi\n6dOn0507d2jPnj1kZWVVpu1+/fVXqlu3rlw2L2Dp0qVkZWXFH0tz5J4/f04cx1F4eDhlZmaSvr4+\nbdq0iT+fl5dHlpaWtGjRolLHl6FDh5Kuri4NHDiQEhIS6OzZs2RtbS0Y/w8cOEBKSkq0YMECunv3\nLm3fvp309fUF7UyetliZMEeu
"text": [
"<matplotlib.figure.Figure at 0x108b50668>"
]
}
],
"prompt_number": 67
},
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": {}
}
]
}