├── .gitattibutes ├── .gitignore ├── 01-python-tools ├── 01.01-python-overview.ipynb ├── 01.02-ipython-interpreter.ipynb ├── 01.03-ipython-notebook.ipynb └── 01.04-use-anaconda.ipynb ├── 02-python-essentials ├── 02.01-a-tour-of-python.ipynb ├── 02.02-python-data-types.ipynb ├── 02.03-numbers.ipynb ├── 02.04-strings.ipynb ├── 02.05-indexing-and-slicing.ipynb ├── 02.06-lists.ipynb ├── 02.07-mutable-and-immutable-data-types.ipynb ├── 02.08-tuples.ipynb ├── 02.09-speed-comparison-between-list-&-tuple.ipynb ├── 02.10-dictionaries.ipynb ├── 02.11-sets.ipynb ├── 02.12-frozen-sets.ipynb ├── 02.13-how-python-assignment-works.ipynb ├── 02.14-if-statement.ipynb ├── 02.15-loops.ipynb ├── 02.16-list-comprehension.ipynb ├── 02.17-functions.ipynb ├── 02.18-modules-and-packages.ipynb ├── 02.19-exceptions.ipynb ├── 02.20-warnings.ipynb └── 02.21-file-IO.ipynb ├── 03-numpy ├── 03.01-numpy-overview.ipynb ├── 03.02-matplotlib-basics.ipynb ├── 03.03-numpy-arrays.ipynb ├── 03.04-array-types.ipynb ├── 03.05-array-calculation-method.ipynb ├── 03.06-sorting-numpy-arrays.ipynb ├── 03.07-array-shapes.ipynb ├── 03.08-diagonals.ipynb ├── 03.09-data-to-&-from-string.ipynb ├── 03.10-array-attribute-&-method-overview-.ipynb ├── 03.11-array-creation-functions.ipynb ├── 03.12-matrix-object.ipynb ├── 03.13-general-functions.ipynb ├── 03.14-vectorizing-functions.ipynb ├── 03.15-binary-operators.ipynb ├── 03.16-universal-functions.ipynb ├── 03.17-choose.ipynb ├── 03.18-array-broadcasting.ipynb ├── 03.19-reading-and-writing-arrays.ipynb ├── 03.20-structured-arrays.ipynb ├── 03.21-record-arrays.ipynb ├── 03.22-memory-maps.ipynb └── 03.23-from-matlab-to-numpy.ipynb ├── 04-scipy ├── 04.01-scienticfic-python-overview.ipynb ├── 04.02-interpolation-with-scipy.ipynb ├── 04.03-statistics-with-scipy.ipynb ├── 04.04-curve-fitting.ipynb ├── 04.05-minimization-in-python.ipynb ├── 04.06-integration-in-python.ipynb ├── 04.07-ODEs.ipynb ├── 04.08-sparse-matrix.ipynb ├── 04.09-linear-algbra.ipynb ├── 04.10-sparse-linear-algebra.ipynb └── JANAF_CH4.txt ├── 05-advanced-python ├── 05.01-overview-of-the-sys-module.ipynb ├── 05.02-interacting-with-the-OS---os.ipynb ├── 05.03-comma-separated-values.ipynb ├── 05.04-regular-expression.ipynb ├── 05.05-datetime.ipynb ├── 05.06-sql-databases.ipynb ├── 05.07-object-relational-mappers.ipynb ├── 05.08-functions.ipynb ├── 05.09-iterators.ipynb ├── 05.10-generators.ipynb ├── 05.11-context-managers-and-the-with-statement.ipynb ├── 05.12-decorators.ipynb ├── 05.13-decorator-usage.ipynb ├── 05.14-the-operator-functools-itertools-toolz-fn-funcy-module.ipynb ├── 05.15-scope.ipynb ├── 05.16-dynamic-code-execution.ipynb └── my_database.sqlite ├── 06-matplotlib ├── 06.01-pyplot-tutorial.ipynb ├── 06.02-customizing-plots-with-style-sheets.ipynb ├── 06.03-working-with-text---basic.ipynb ├── 06.04-working-with-text---math-expression.ipynb ├── 06.05-image-tutorial.ipynb ├── 06.06-annotating-axes.ipynb ├── 06.07-legend.ipynb ├── 06.08-figures,-subplots,-axes-and-ticks.ipynb ├── 06.09-do-not-trust-the-defaults.ipynb ├── 06.10-different-plots.ipynb ├── artists_figure.png ├── artists_tree.png └── stinkbug.png ├── 07-interfacing-with-other-languages ├── 07-02-example.zip ├── 07-03-fib.zip ├── 07-04-extern.zip ├── 07-05-particle.zip ├── 07-06-cython-sum.zip ├── 07.01-introduction.ipynb ├── 07.02-python-extension-modules.ipynb ├── 07.03-cython-part-1.ipynb ├── 07.04-cython-part-2.ipynb ├── 07.05-cython-part-3.ipynb ├── 07.06-cython-part-4.ipynb ├── 07.07-profiling-with-annotations.ipynb ├── 07.08-ctypes.ipynb ├── fib_orig.c ├── fib_orig.html ├── fib_orig.png └── fib_orig.pyx ├── 08-object-oriented-programming ├── 08.01-oop-introduction.ipynb ├── 08.02-using-oop-model-a-forest-fire.ipynb ├── 08.03-what-is-a-object.ipynb ├── 08.04-writing-classes.ipynb ├── 08.05-special-method.ipynb ├── 08.06-properties.ipynb ├── 08.07-forest-fire-simulation.ipynb ├── 08.08-inheritance.ipynb ├── 08.09-super.ipynb ├── 08.10-refactoring-the-forest-fire-simutation.ipynb ├── 08.11-interfaces.ipynb ├── 08.12-public-private-special-in-python.ipynb └── 08.13-multiple-inheritance.ipynb ├── 09-theano ├── 09.01-introduction-and-installation.ipynb ├── 09.02-theano-basics.ipynb ├── 09.03-gpu-on-windows.ipynb ├── 09.04-graph-structures.ipynb ├── 09.05-configuration-settings-and-compiling-modes.ipynb ├── 09.06-conditions-in-theano.ipynb ├── 09.07-loop-with-scan.ipynb ├── 09.08-linear-regression.ipynb ├── 09.09-logistic-regression-.ipynb ├── 09.10-softmax-on-mnist.ipynb ├── 09.11-net-on-mnist.ipynb ├── 09.12-random-streams.ipynb ├── 09.13-modern-net-on-mnist.ipynb ├── 09.14-convolutional-net-on-mnist.ipynb ├── 09.15-tensor-basics.ipynb ├── 09.16-tensor-indexing.ipynb ├── 09.17-tensor-operator-and-elementwise-operations.ipynb ├── 09.18-tensor-nnet-.ipynb ├── 09.19-tensor-conv.ipynb ├── apply1.png ├── apply2.png ├── apply_no_opti.png ├── apply_opti.png ├── download_mnist.py └── load.py ├── 10-something-interesting ├── 10.01-maps-using-basemap.ipynb ├── 10.02-maps-using-cartopy.ipynb ├── 10.03-nba-data.ipynb ├── 10.04-louis-cha's-kungfu-world.ipynb ├── _Player.py ├── bangs.txt ├── kungfu.txt └── names.txt ├── 11-useful-tools ├── 11.01-pprint.ipynb ├── 11.02-pickle-and-cPickle.ipynb ├── 11.03-json.ipynb ├── 11.04-glob.ipynb ├── 11.05-shutil.ipynb ├── 11.06-gzip,-zipfile,-tarfile.ipynb ├── 11.07-logging.ipynb ├── 11.08-string.ipynb ├── 11.09-collections.ipynb └── 11.10-requests.ipynb ├── 12-pandas ├── 12.01-ten-minutes-to-pandas.ipynb ├── 12.02-series-in-pandas.ipynb └── 12.03-dataframe-in-pandas.ipynb ├── README.md ├── generate index.ipynb ├── generate_static_files.ipynb ├── generate_static_files.py ├── index.ipynb ├── index.md └── payment.jpeg /.gitattibutes: -------------------------------------------------------------------------------- 1 | # encoding=UTF-8 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /*/.ipynb_checkpoints/* 2 | /.ipynb_checkpoints/* 3 | *-checkpoint.ipynb 4 | *.pyc 5 | /*/mnist/* 6 | static-files/* 7 | /*/novels/* 8 | /*/*.model 9 | .DS_Store 10 | -------------------------------------------------------------------------------- /01-python-tools/01.01-python-overview.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Python 简介" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "## **Python** 历史" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "`Python` 的创始人为荷兰人吉多·范罗苏姆(`Guido van Rossum`)。1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ABC 语言的一种继承。之所以选中 `Python` 作为程序的名字,是因为他是 BBC 电视剧——蒙提·派森的飞行马戏团(`Monty Python's Flying Circus`)的爱好者。\n", 24 | "\n", 25 | "1991年,第一个 Python 编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。\n", 26 | "\n", 27 | "`Python 2.0` 于 2000 年 10 月 16 日发布,增加了实现完整的垃圾回收,并且支持 `Unicode`。\n", 28 | "\n", 29 | "`Python 3.0` 于 2008 年 12 月 3 日发布,此版不完全兼容之前的 `Python` 源代码。不过,很多新特性后来也被移植到旧的 `Python 2.6/2.7` 版本。" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "## 第一行Python代码" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "安装好 `Python` 之后,在命令行下输入:\n", 44 | "\n", 45 | " python\n", 46 | "\n", 47 | "就可以进入 `Python` 解释器的页面。\n", 48 | "\n", 49 | "按照惯例,第一行代码应该是输出 `\"hello world!\"`:" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "metadata": { 56 | "collapsed": false 57 | }, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "hello world!\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "print \"hello world!\"" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "相对与 `Java,C` 等语言,`Python` 仅仅使用一行语句就完成的了这个任务。\n", 76 | "\n", 77 | "可以将这句话的内容保存到一个文本文件中,并使用后缀名 `.py` 结尾,例如 `hello_world.py`,在命令行下运行这个程序:\n", 78 | "\n", 79 | " python hello_world.py\n", 80 | "\n", 81 | "也会输出 `\"hello world!\"` 的结果。" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "## Python 之禅" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "在 **Python** 解释器下输入 \n", 96 | "\n", 97 | "```import this```\n", 98 | "\n", 99 | "会出来这样一首小诗:" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 2, 105 | "metadata": { 106 | "collapsed": false 107 | }, 108 | "outputs": [ 109 | { 110 | "name": "stdout", 111 | "output_type": "stream", 112 | "text": [ 113 | "The Zen of Python, by Tim Peters\n", 114 | "\n", 115 | "Beautiful is better than ugly.\n", 116 | "Explicit is better than implicit.\n", 117 | "Simple is better than complex.\n", 118 | "Complex is better than complicated.\n", 119 | "Flat is better than nested.\n", 120 | "Sparse is better than dense.\n", 121 | "Readability counts.\n", 122 | "Special cases aren't special enough to break the rules.\n", 123 | "Although practicality beats purity.\n", 124 | "Errors should never pass silently.\n", 125 | "Unless explicitly silenced.\n", 126 | "In the face of ambiguity, refuse the temptation to guess.\n", 127 | "There should be one-- and preferably only one --obvious way to do it.\n", 128 | "Although that way may not be obvious at first unless you're Dutch.\n", 129 | "Now is better than never.\n", 130 | "Although never is often better than *right* now.\n", 131 | "If the implementation is hard to explain, it's a bad idea.\n", 132 | "If the implementation is easy to explain, it may be a good idea.\n", 133 | "Namespaces are one honking great idea -- let's do more of those!\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "import this" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "这首诗反映了**Python**的设计哲学——**Python**是一种追求优雅,明确,简单的编程语言,但事实上,产生这首诗的代码并没有写的那么简单易懂:" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 3, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "The Zen of Python, by Tim Peters\n", 160 | "\n", 161 | "Beautiful is better than ugly.\n", 162 | "Explicit is better than implicit.\n", 163 | "Simple is better than complex.\n", 164 | "Complex is better than complicated.\n", 165 | "Flat is better than nested.\n", 166 | "Sparse is better than dense.\n", 167 | "Readability counts.\n", 168 | "Special cases aren't special enough to break the rules.\n", 169 | "Although practicality beats purity.\n", 170 | "Errors should never pass silently.\n", 171 | "Unless explicitly silenced.\n", 172 | "In the face of ambiguity, refuse the temptation to guess.\n", 173 | "There should be one-- and preferably only one --obvious way to do it.\n", 174 | "Although that way may not be obvious at first unless you're Dutch.\n", 175 | "Now is better than never.\n", 176 | "Although never is often better than *right* now.\n", 177 | "If the implementation is hard to explain, it's a bad idea.\n", 178 | "If the implementation is easy to explain, it may be a good idea.\n", 179 | "Namespaces are one honking great idea -- let's do more of those!\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "s = \"\"\"Gur Mra bs Clguba, ol Gvz Crgref\n", 185 | "\n", 186 | "Ornhgvshy vf orggre guna htyl.\n", 187 | "Rkcyvpvg vf orggre guna vzcyvpvg.\n", 188 | "Fvzcyr vf orggre guna pbzcyrk.\n", 189 | "Pbzcyrk vf orggre guna pbzcyvpngrq.\n", 190 | "Syng vf orggre guna arfgrq.\n", 191 | "Fcnefr vf orggre guna qrafr.\n", 192 | "Ernqnovyvgl pbhagf.\n", 193 | "Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.\n", 194 | "Nygubhtu cenpgvpnyvgl orngf chevgl.\n", 195 | "Reebef fubhyq arire cnff fvyragyl.\n", 196 | "Hayrff rkcyvpvgyl fvyraprq.\n", 197 | "Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.\n", 198 | "Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.\n", 199 | "Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.\n", 200 | "Abj vf orggre guna arire.\n", 201 | "Nygubhtu arire vf bsgra orggre guna *evtug* abj.\n", 202 | "Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.\n", 203 | "Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.\n", 204 | "Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!\"\"\"\n", 205 | "\n", 206 | "d = {}\n", 207 | "for c in (65, 97):\n", 208 | " for i in range(26):\n", 209 | " d[chr(i+c)] = chr((i+13) % 26 + c)\n", 210 | "\n", 211 | "print \"\".join([d.get(c, c) for c in s])" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "> Life is short. Use Python." 219 | ] 220 | } 221 | ], 222 | "metadata": { 223 | "kernelspec": { 224 | "display_name": "Python 2", 225 | "language": "python", 226 | "name": "python2" 227 | }, 228 | "language_info": { 229 | "codemirror_mode": { 230 | "name": "ipython", 231 | "version": 2 232 | }, 233 | "file_extension": ".py", 234 | "mimetype": "text/x-python", 235 | "name": "python", 236 | "nbconvert_exporter": "python", 237 | "pygments_lexer": "ipython2", 238 | "version": "2.7.6" 239 | } 240 | }, 241 | "nbformat": 4, 242 | "nbformat_minor": 0 243 | } 244 | -------------------------------------------------------------------------------- /01-python-tools/01.03-ipython-notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Ipython notebook" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "在命令行下输入命令:\n", 15 | "\n", 16 | " ipython notebook\n", 17 | "\n", 18 | "会打开一个notebook本地服务器,一般地址是 http://localhost:8888\n", 19 | "\n", 20 | "**`ipython notebook`** 支持两种模式的cell:\n", 21 | "\n", 22 | "* Markdown\n", 23 | "* Code\n", 24 | "\n", 25 | "这里不做过多介绍。" 26 | ] 27 | } 28 | ], 29 | "metadata": { 30 | "kernelspec": { 31 | "display_name": "Python 2", 32 | "language": "python", 33 | "name": "python2" 34 | }, 35 | "language_info": { 36 | "codemirror_mode": { 37 | "name": "ipython", 38 | "version": 2 39 | }, 40 | "file_extension": ".py", 41 | "mimetype": "text/x-python", 42 | "name": "python", 43 | "nbconvert_exporter": "python", 44 | "pygments_lexer": "ipython2", 45 | "version": "2.7.10" 46 | } 47 | }, 48 | "nbformat": 4, 49 | "nbformat_minor": 0 50 | } 51 | -------------------------------------------------------------------------------- /02-python-essentials/02.02-python-data-types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python 数据类型" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 常用数据类型 Common Data Types" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "| 类型| 例子|\n", 22 | "| ----- | ----- |\n", 23 | "| 整数 | `-100` |\n", 24 | "| 浮点数 | `3.1416` |\n", 25 | "| 字符串 | `'hello'` |\n", 26 | "| 列表 | `[1, 1.2, 'hello']` |\n", 27 | "| 字典 | `{'dogs': 5, 'pigs': 3}`|\n", 28 | "| Numpy数组 | `array([1, 2, 3])`" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## 其他类型 Others" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "| 类型| 例子|\n", 43 | "| ------- | ----- |\n", 44 | "| 长整型 | `1000000000000L`\n", 45 | "| 布尔型 | `True, False`\n", 46 | "| 元组 | `('ring', 1000)`\n", 47 | "| 集合 | `{1, 2, 3}`\n", 48 | "| Pandas类型| `DataFrame, Series`\n", 49 | "| 自定义 | `Object Oriented Classes`" 50 | ] 51 | } 52 | ], 53 | "metadata": { 54 | "kernelspec": { 55 | "display_name": "Python 2", 56 | "language": "python", 57 | "name": "python2" 58 | }, 59 | "language_info": { 60 | "codemirror_mode": { 61 | "name": "ipython", 62 | "version": 2 63 | }, 64 | "file_extension": ".py", 65 | "mimetype": "text/x-python", 66 | "name": "python", 67 | "nbconvert_exporter": "python", 68 | "pygments_lexer": "ipython2", 69 | "version": "2.7.6" 70 | } 71 | }, 72 | "nbformat": 4, 73 | "nbformat_minor": 0 74 | } 75 | -------------------------------------------------------------------------------- /02-python-essentials/02.08-tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 元组" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 基本操作" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "与列表相似,元组`Tuple`也是个有序序列,但是元组是不可变的,用`()`生成。" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "(10, 11, 12, 13, 14)" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "t = (10, 11, 12, 13, 14)\n", 44 | "t" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "可以索引,切片:" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "10" 65 | ] 66 | }, 67 | "execution_count": 2, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "t[0]" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 3, 79 | "metadata": { 80 | "collapsed": false 81 | }, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "(11, 12)" 87 | ] 88 | }, 89 | "execution_count": 3, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "t[1:3]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "但是元组是不可变的:" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 4, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "ename": "TypeError", 114 | "evalue": "'tuple' object does not support item assignment", 115 | "output_type": "error", 116 | "traceback": [ 117 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 118 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 119 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 会报错\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 120 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "# 会报错\n", 126 | "t[0] = 1" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "## 单个元素的元组生成" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "由于`()`在表达式中被应用,只含有单个元素的元组容易和表达式混淆,所以采用下列方式定义只有一个元素的元组:" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 5, 146 | "metadata": { 147 | "collapsed": false 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "(10,)\n", 155 | "\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "a = (10,)\n", 161 | "print a\n", 162 | "print type(a)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 6, 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "a = (10)\n", 182 | "print type(a)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "将列表转换为元组:" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 7, 195 | "metadata": { 196 | "collapsed": false 197 | }, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "(10, 11, 12, 13, 14)" 203 | ] 204 | }, 205 | "execution_count": 7, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "a = [10, 11, 12, 13, 14]\n", 212 | "tuple(a)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "## 元组方法" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "由于元组是不可变的,所以只能有一些不可变的方法,例如计算元素个数 `count` 和元素位置 `index` ,用法与列表一样。" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 8, 232 | "metadata": { 233 | "collapsed": false 234 | }, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "1" 240 | ] 241 | }, 242 | "execution_count": 8, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "a.count(10)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 9, 254 | "metadata": { 255 | "collapsed": false 256 | }, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "2" 262 | ] 263 | }, 264 | "execution_count": 9, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "a.index(12)" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": {}, 276 | "source": [ 277 | "## 为什么需要元组" 278 | ] 279 | }, 280 | { 281 | "cell_type": "markdown", 282 | "metadata": {}, 283 | "source": [ 284 | "旧式字符串格式化中参数要用元组;\n", 285 | "\n", 286 | "在字典中当作键值;\n", 287 | "\n", 288 | "数据库的返回值……" 289 | ] 290 | } 291 | ], 292 | "metadata": { 293 | "kernelspec": { 294 | "display_name": "Python 2", 295 | "language": "python", 296 | "name": "python2" 297 | }, 298 | "language_info": { 299 | "codemirror_mode": { 300 | "name": "ipython", 301 | "version": 2 302 | }, 303 | "file_extension": ".py", 304 | "mimetype": "text/x-python", 305 | "name": "python", 306 | "nbconvert_exporter": "python", 307 | "pygments_lexer": "ipython2", 308 | "version": "2.7.10" 309 | } 310 | }, 311 | "nbformat": 4, 312 | "nbformat_minor": 0 313 | } 314 | -------------------------------------------------------------------------------- /02-python-essentials/02.09-speed-comparison-between-list-&-tuple.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 列表与元组的速度比较" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "IPython 中用 `magic` 命令 `%timeit` 来计时。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## 比较生成速度" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "1000000 loops, best of 3: 456 ns per loop\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "%timeit [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "10000000 loops, best of 3: 23 ns per loop\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "%timeit (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "可以看到,元组的生成速度要比列表的生成速度快得多,相差大概一个数量级。" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "## 比较遍历速度" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "产生内容相同的随机列表和元组:" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "from numpy.random import rand\n", 92 | "values = rand(10000,4)\n", 93 | "lst = [list(row) for row in values]\n", 94 | "tup = tuple(tuple(row) for row in values)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "100 loops, best of 3: 4.12 ms per loop\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | " %timeit for row in lst: list(row)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 5, 119 | "metadata": { 120 | "collapsed": false 121 | }, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "100 loops, best of 3: 2.07 ms per loop\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "%timeit for row in tup: tuple(row)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "在遍历上,元组和列表的速度表现差不多。" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "## 比较遍历和索引速度:" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 6, 152 | "metadata": { 153 | "collapsed": false 154 | }, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "The slowest run took 12.20 times longer than the fastest. This could mean that an intermediate result is being cached \n", 161 | "100 loops, best of 3: 3.73 ms per loop\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "%timeit for row in lst: a = row[0] + 1" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 7, 172 | "metadata": { 173 | "collapsed": false 174 | }, 175 | "outputs": [ 176 | { 177 | "name": "stdout", 178 | "output_type": "stream", 179 | "text": [ 180 | "100 loops, best of 3: 3.82 ms per loop\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "%timeit for row in tup: a = row[0] + 1" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "元组的生成速度会比列表快很多,迭代速度快一点,索引速度差不多。" 193 | ] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 2", 199 | "language": "python", 200 | "name": "python2" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 2 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython2", 212 | "version": "2.7.6" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 0 217 | } 218 | -------------------------------------------------------------------------------- /02-python-essentials/02.12-frozen-sets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 不可变集合" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "对应于元组(`tuple`)与列表(`list`)的关系,对于集合(`set`),**Python**提供了一种叫做不可变集合(`frozen set`)的数据结构。\n", 15 | "\n", 16 | "使用 `frozenset` 来进行创建:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": false 24 | }, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "frozenset({1, 2, 3, 'a'})" 30 | ] 31 | }, 32 | "execution_count": 1, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "s = frozenset([1, 2, 3, 'a', 1])\n", 39 | "s" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "与集合不同的是,不可变集合一旦创建就不可以改变。\n", 47 | "\n", 48 | "不可变集合的一个主要应用是用来作为字典的键,例如用一个字典来记录两个城市之间的距离:" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 2, 54 | "metadata": { 55 | "collapsed": false 56 | }, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "{frozenset({'Austin', 'New York'}): 1515,\n", 62 | " frozenset({'Austin', 'Los Angeles'}): 1233,\n", 63 | " frozenset({'Los Angeles', 'New York'}): 2498}" 64 | ] 65 | }, 66 | "execution_count": 2, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "flight_distance = {}\n", 73 | "city_pair = frozenset(['Los Angeles', 'New York'])\n", 74 | "flight_distance[city_pair] = 2498\n", 75 | "flight_distance[frozenset(['Austin', 'Los Angeles'])] = 1233\n", 76 | "flight_distance[frozenset(['Austin', 'New York'])] = 1515\n", 77 | "flight_distance" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "由于集合不分顺序,所以不同顺序不会影响查阅结果:" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 3, 90 | "metadata": { 91 | "collapsed": false 92 | }, 93 | "outputs": [ 94 | { 95 | "data": { 96 | "text/plain": [ 97 | "1515" 98 | ] 99 | }, 100 | "execution_count": 3, 101 | "metadata": {}, 102 | "output_type": "execute_result" 103 | } 104 | ], 105 | "source": [ 106 | "flight_distance[frozenset(['New York','Austin'])]" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 4, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "1515" 120 | ] 121 | }, 122 | "execution_count": 4, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "flight_distance[frozenset(['Austin','New York'])]" 129 | ] 130 | } 131 | ], 132 | "metadata": { 133 | "kernelspec": { 134 | "display_name": "Python 2", 135 | "language": "python", 136 | "name": "python2" 137 | }, 138 | "language_info": { 139 | "codemirror_mode": { 140 | "name": "ipython", 141 | "version": 2 142 | }, 143 | "file_extension": ".py", 144 | "mimetype": "text/x-python", 145 | "name": "python", 146 | "nbconvert_exporter": "python", 147 | "pygments_lexer": "ipython2", 148 | "version": "2.7.10" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 0 153 | } 154 | -------------------------------------------------------------------------------- /02-python-essentials/02.16-list-comprehension.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 列表推导式" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "循环可以用来生成列表:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "[100, 441, 16, 49, 144]\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "values = [10, 21, 4, 7, 12]\n", 34 | "squares = []\n", 35 | "for x in values:\n", 36 | " squares.append(x**2)\n", 37 | "print squares" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "列表推导式可以使用更简单的方法来创建这个列表:" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "[100, 441, 16, 49, 144]\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "values = [10, 21, 4, 7, 12]\n", 64 | "squares = [x**2 for x in values]\n", 65 | "print squares" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "还可以在列表推导式中加入条件进行筛选。" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "例如在上面的例子中,假如只想保留列表中不大于`10`的数的平方:" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": { 86 | "collapsed": false 87 | }, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "[100, 16, 49]\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "values = [10, 21, 4, 7, 12]\n", 99 | "squares = [x**2 for x in values if x <= 10]\n", 100 | "print squares" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "也可以使用推导式生成集合和字典:" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 4, 113 | "metadata": { 114 | "collapsed": false 115 | }, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "set([16, 49, 100])\n", 122 | "{10: 100, 4: 16, 7: 49}\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "square_set = {x**2 for x in values if x <= 10}\n", 128 | "print(square_set)\n", 129 | "square_dict = {x: x**2 for x in values if x <= 10}\n", 130 | "print(square_dict)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "再如,计算上面例子中生成的列表中所有元素的和:" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 5, 143 | "metadata": { 144 | "collapsed": false 145 | }, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "165\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "total = sum([x**2 for x in values if x <= 10])\n", 157 | "print(total)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "但是,**Python**会生成这个列表,然后在将它放到垃圾回收机制中(因为没有变量指向它),这毫无疑问是种浪费。\n", 165 | "\n", 166 | "为了解决这种问题,与xrange()类似,**Python**使用产生式表达式来解决这个问题:" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 6, 172 | "metadata": { 173 | "collapsed": false 174 | }, 175 | "outputs": [ 176 | { 177 | "name": "stdout", 178 | "output_type": "stream", 179 | "text": [ 180 | "165\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "total = sum(x**2 for x in values if x <= 10)\n", 186 | "print(total)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "与上面相比,只是去掉了括号,但这里并不会一次性的生成这个列表。" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "比较一下两者的用时:" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 7, 206 | "metadata": { 207 | "collapsed": true 208 | }, 209 | "outputs": [], 210 | "source": [ 211 | "x = range(1000000)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 8, 217 | "metadata": { 218 | "collapsed": false 219 | }, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "1 loops, best of 3: 3.86 s per loop\n" 226 | ] 227 | } 228 | ], 229 | "source": [ 230 | "%timeit total = sum([i**2 for i in x])" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 9, 236 | "metadata": { 237 | "collapsed": false 238 | }, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "1 loops, best of 3: 2.58 s per loop\n" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "%timeit total = sum(i**2 for i in x)" 250 | ] 251 | } 252 | ], 253 | "metadata": { 254 | "kernelspec": { 255 | "display_name": "Python 2", 256 | "language": "python", 257 | "name": "python2" 258 | }, 259 | "language_info": { 260 | "codemirror_mode": { 261 | "name": "ipython", 262 | "version": 2 263 | }, 264 | "file_extension": ".py", 265 | "mimetype": "text/x-python", 266 | "name": "python", 267 | "nbconvert_exporter": "python", 268 | "pygments_lexer": "ipython2", 269 | "version": "2.7.10" 270 | } 271 | }, 272 | "nbformat": 4, 273 | "nbformat_minor": 0 274 | } 275 | -------------------------------------------------------------------------------- /02-python-essentials/02.20-warnings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 警告" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "出现了一些需要让用户知道的问题,但又不想停止程序,这时候我们可以使用警告:\n", 15 | "\n", 16 | "首先导入警告模块:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": false 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "import warnings" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "在需要的地方,我们使用 `warnings` 中的 `warn` 函数:\n", 35 | "\n", 36 | " warn(msg, WarningType = UserWarning)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": { 43 | "collapsed": false 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stderr", 48 | "output_type": "stream", 49 | "text": [ 50 | "c:\\Anaconda\\lib\\site-packages\\IPython\\kernel\\__main__.py:4: RuntimeWarning: month (13) is not between 1 and 12\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "def month_warning(m):\n", 56 | " if not 1<= m <= 12:\n", 57 | " msg = \"month (%d) is not between 1 and 12\" % m\n", 58 | " warnings.warn(msg, RuntimeWarning)\n", 59 | "\n", 60 | "month_warning(13)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "有时候我们想要忽略特定类型的警告,可以使用 `warnings` 的 `filterwarnings` 函数:\n", 68 | "\n", 69 | " filterwarnings(action, category)\n", 70 | "\n", 71 | "将 `action` 设置为 `'ignore'` 便可以忽略特定类型的警告:" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 3, 77 | "metadata": { 78 | "collapsed": true 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "warnings.filterwarnings(action = 'ignore', category = RuntimeWarning)\n", 83 | "\n", 84 | "month_warning(13)" 85 | ] 86 | } 87 | ], 88 | "metadata": { 89 | "kernelspec": { 90 | "display_name": "Python 2", 91 | "language": "python", 92 | "name": "python2" 93 | }, 94 | "language_info": { 95 | "codemirror_mode": { 96 | "name": "ipython", 97 | "version": 2 98 | }, 99 | "file_extension": ".py", 100 | "mimetype": "text/x-python", 101 | "name": "python", 102 | "nbconvert_exporter": "python", 103 | "pygments_lexer": "ipython2", 104 | "version": "2.7.10" 105 | } 106 | }, 107 | "nbformat": 4, 108 | "nbformat_minor": 0 109 | } 110 | -------------------------------------------------------------------------------- /03-numpy/03.08-diagonals.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 对角线" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "这里,使用与之前不同的导入方法:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import numpy as np" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "使用numpy中的函数前,需要加上 `np.`:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": { 39 | "collapsed": false 40 | }, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "array([[11, 21, 31],\n", 46 | " [12, 22, 32],\n", 47 | " [13, 23, 33]])" 48 | ] 49 | }, 50 | "execution_count": 2, 51 | "metadata": {}, 52 | "output_type": "execute_result" 53 | } 54 | ], 55 | "source": [ 56 | "a = np.array([11,21,31,12,22,32,13,23,33])\n", 57 | "a.shape = 3,3\n", 58 | "a" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "查看它的对角线元素:" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "array([11, 22, 33])" 79 | ] 80 | }, 81 | "execution_count": 3, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "a.diagonal()" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "可以使用偏移来查看它的次对角线,正数表示右移,负数表示左移:" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "array([21, 32])" 108 | ] 109 | }, 110 | "execution_count": 4, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "a.diagonal(offset=1)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 5, 122 | "metadata": { 123 | "collapsed": false 124 | }, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "array([12, 23])" 130 | ] 131 | }, 132 | "execution_count": 5, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "a.diagonal(offset=-1)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "可以使用花式索引来得到对角线:" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 6, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "array([11, 22, 33])" 159 | ] 160 | }, 161 | "execution_count": 6, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "i = [0,1,2]\n", 168 | "a[i, i]" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "可以更新对角线的值:" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 7, 181 | "metadata": { 182 | "collapsed": false 183 | }, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "array([[ 2, 21, 31],\n", 189 | " [12, 2, 32],\n", 190 | " [13, 23, 2]])" 191 | ] 192 | }, 193 | "execution_count": 7, 194 | "metadata": {}, 195 | "output_type": "execute_result" 196 | } 197 | ], 198 | "source": [ 199 | "a[i, i] = 2\n", 200 | "a" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "修改次对角线的值:" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 8, 213 | "metadata": { 214 | "collapsed": false 215 | }, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "array([[ 2, 1, 31],\n", 221 | " [12, 2, 1],\n", 222 | " [13, 23, 2]])" 223 | ] 224 | }, 225 | "execution_count": 8, 226 | "metadata": {}, 227 | "output_type": "execute_result" 228 | } 229 | ], 230 | "source": [ 231 | "i = np.array([0,1])\n", 232 | "a[i, i + 1] = 1\n", 233 | "a" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 9, 239 | "metadata": { 240 | "collapsed": false 241 | }, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "array([[ 2, 1, 31],\n", 247 | " [-1, 2, 1],\n", 248 | " [13, -1, 2]])" 249 | ] 250 | }, 251 | "execution_count": 9, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "a[i + 1, i] = -1\n", 258 | "a" 259 | ] 260 | } 261 | ], 262 | "metadata": { 263 | "kernelspec": { 264 | "display_name": "Python 2", 265 | "language": "python", 266 | "name": "python2" 267 | }, 268 | "language_info": { 269 | "codemirror_mode": { 270 | "name": "ipython", 271 | "version": 2 272 | }, 273 | "file_extension": ".py", 274 | "mimetype": "text/x-python", 275 | "name": "python", 276 | "nbconvert_exporter": "python", 277 | "pygments_lexer": "ipython2", 278 | "version": "2.7.5" 279 | } 280 | }, 281 | "nbformat": 4, 282 | "nbformat_minor": 0 283 | } 284 | -------------------------------------------------------------------------------- /03-numpy/03.09-data-to-&-from-string.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 数组与字符串的转换" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## tostring 方法" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import numpy as np" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "a = np.array([[1,2],\n", 37 | " [3,4]], \n", 38 | " dtype = np.uint8)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "转化为字符串:" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": { 52 | "collapsed": false, 53 | "scrolled": true 54 | }, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "'\\x01\\x02\\x03\\x04'" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "a.tostring()" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "我们可以使用不同的顺序来转换字符串:" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'\\x01\\x03\\x02\\x04'" 89 | ] 90 | }, 91 | "execution_count": 4, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "a.tostring(order='F')" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "这里使用了**Fortran**的格式,按照列来读数据。" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "## fromstring 函数" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "可以使用 `fromstring` 函数从字符串中读出数据,不过要指定类型:" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 5, 124 | "metadata": { 125 | "collapsed": false 126 | }, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "array([1, 2, 3, 4], dtype=uint8)" 132 | ] 133 | }, 134 | "execution_count": 5, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "s = a.tostring()\n", 141 | "a = np.fromstring(s, \n", 142 | " dtype=np.uint8)\n", 143 | "a" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "此时,返回的数组是一维的,需要重新设定维度:" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 6, 156 | "metadata": { 157 | "collapsed": false 158 | }, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "array([[1, 2],\n", 164 | " [3, 4]], dtype=uint8)" 165 | ] 166 | }, 167 | "execution_count": 6, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "a.shape = 2,2\n", 174 | "a" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "对于文本文件,推荐使用\n", 182 | "- `loadtxt`\n", 183 | "- `genfromtxt`\n", 184 | "- `savetxt`\n", 185 | "\n", 186 | "对于二进制文本文件,推荐使用 \n", 187 | "- `save` \n", 188 | "- `load`\n", 189 | "- `savez`" 190 | ] 191 | } 192 | ], 193 | "metadata": { 194 | "kernelspec": { 195 | "display_name": "Python 2", 196 | "language": "python", 197 | "name": "python2" 198 | }, 199 | "language_info": { 200 | "codemirror_mode": { 201 | "name": "ipython", 202 | "version": 2 203 | }, 204 | "file_extension": ".py", 205 | "mimetype": "text/x-python", 206 | "name": "python", 207 | "nbconvert_exporter": "python", 208 | "pygments_lexer": "ipython2", 209 | "version": "2.7.9" 210 | } 211 | }, 212 | "nbformat": 4, 213 | "nbformat_minor": 0 214 | } 215 | -------------------------------------------------------------------------------- /03-numpy/03.12-matrix-object.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 矩阵" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "使用 `mat` 方法将 `2` 维数组转化为矩阵:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "matrix([[1, 2, 4],\n", 28 | " [2, 5, 3],\n", 29 | " [7, 8, 9]])" 30 | ] 31 | }, 32 | "execution_count": 1, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "import numpy as np\n", 39 | "a = np.array([[1,2,4],\n", 40 | " [2,5,3], \n", 41 | " [7,8,9]])\n", 42 | "A = np.mat(a)\n", 43 | "A" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "也可以使用 **Matlab** 的语法传入一个字符串来生成矩阵:" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 2, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "matrix([[1, 2, 4],\n", 64 | " [2, 5, 3],\n", 65 | " [7, 8, 9]])" 66 | ] 67 | }, 68 | "execution_count": 2, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "A = np.mat('1,2,4;2,5,3;7,8,9')\n", 75 | "A" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "利用分块创造新的矩阵:" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 3, 88 | "metadata": { 89 | "collapsed": false 90 | }, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "matrix([[ 1, 2, 10, 20],\n", 96 | " [ 3, 4, 30, 40],\n", 97 | " [10, 20, 1, 2],\n", 98 | " [30, 40, 3, 4]])" 99 | ] 100 | }, 101 | "execution_count": 3, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "a = np.array([[ 1, 2],\n", 108 | " [ 3, 4]])\n", 109 | "b = np.array([[10,20], \n", 110 | " [30,40]])\n", 111 | "\n", 112 | "np.bmat('a,b;b,a')" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "矩阵与向量的乘法:" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 4, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "array([[1],\n", 133 | " [2],\n", 134 | " [3]])" 135 | ] 136 | }, 137 | "execution_count": 4, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "x = np.array([[1], [2], [3]])\n", 144 | "x" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 5, 150 | "metadata": { 151 | "collapsed": false 152 | }, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "matrix([[17],\n", 158 | " [21],\n", 159 | " [50]])" 160 | ] 161 | }, 162 | "execution_count": 5, 163 | "metadata": {}, 164 | "output_type": "execute_result" 165 | } 166 | ], 167 | "source": [ 168 | "A * x" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "`A.I` 表示 `A` 矩阵的逆矩阵:" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 6, 181 | "metadata": { 182 | "collapsed": false 183 | }, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "[[ 1.00000000e+00 0.00000000e+00 0.00000000e+00]\n", 190 | " [ 0.00000000e+00 1.00000000e+00 2.08166817e-17]\n", 191 | " [ 2.22044605e-16 -8.32667268e-17 1.00000000e+00]]\n" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "print A * A.I" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "矩阵指数表示矩阵连乘:" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 7, 209 | "metadata": { 210 | "collapsed": false, 211 | "scrolled": true 212 | }, 213 | "outputs": [ 214 | { 215 | "name": "stdout", 216 | "output_type": "stream", 217 | "text": [ 218 | "[[ 6497 9580 9836]\n", 219 | " [ 7138 10561 10818]\n", 220 | " [18434 27220 27945]]\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "print A ** 4" 226 | ] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 2", 232 | "language": "python", 233 | "name": "python2" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 2 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython2", 245 | "version": "2.7.10" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 0 250 | } 251 | -------------------------------------------------------------------------------- /03-numpy/03.17-choose.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# choose 函数实现条件筛选" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "对于数组,我们有时候需要进行类似 `switch` 和 `case` 进行条件选择,此时使用 choose 函数十分方便:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import numpy as np" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "array([[11, 10, 11],\n", 39 | " [12, 11, 10],\n", 40 | " [11, 12, 12]])" 41 | ] 42 | }, 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "control = np.array([[1,0,1],\n", 50 | " [2,1,0],\n", 51 | " [1,2,2]])\n", 52 | "\n", 53 | "np.choose(control, [10, 11, 12])" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "在上面的例子中,`choose` 将 `0,1,2` 对应的值映射为了 `10, 11, 12`,这里的 `0,1,2` 表示对应的下标。\n", 61 | "\n", 62 | "事实上, `choose` 不仅仅能接受下标参数,还可以接受下标所在的位置:" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": { 69 | "collapsed": false 70 | }, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "array([[10, 1, 10],\n", 76 | " [23, 10, 5],\n", 77 | " [10, 27, 28]])" 78 | ] 79 | }, 80 | "execution_count": 3, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "i0 = np.array([[0,1,2],\n", 87 | " [3,4,5],\n", 88 | " [6,7,8]])\n", 89 | "i2 = np.array([[20,21,22],\n", 90 | " [23,24,25],\n", 91 | " [26,27,28]])\n", 92 | "control = np.array([[1,0,1],\n", 93 | " [2,1,0],\n", 94 | " [1,2,2]])\n", 95 | "\n", 96 | "np.choose(control, [i0, 10, i2])" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "这里,`control` 传入第一个 `1` 对应的是 10,传入的第一个 `0` 对应于 `i0` 相应位置的值即 `1`,剩下的以此类推。 \n", 104 | "\n", 105 | "下面的例子将数组中所有小于 `10` 的值变成了 `10`。" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 4, 111 | "metadata": { 112 | "collapsed": false 113 | }, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "array([[ True, True, True],\n", 119 | " [False, False, False],\n", 120 | " [False, False, False]], dtype=bool)" 121 | ] 122 | }, 123 | "execution_count": 4, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "a = np.array([[ 0, 1, 2], \n", 130 | " [10,11,12], \n", 131 | " [20,21,22]])\n", 132 | "\n", 133 | "a < 10" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 5, 139 | "metadata": { 140 | "collapsed": false 141 | }, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "array([[10, 10, 10],\n", 147 | " [10, 11, 12],\n", 148 | " [20, 21, 22]])" 149 | ] 150 | }, 151 | "execution_count": 5, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "np.choose(a < 10, (a, 10))" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "下面的例子将数组中所有小于 10 的值变成了 10,大于 15 的值变成了 15。" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 6, 170 | "metadata": { 171 | "collapsed": false 172 | }, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "array([[1, 1, 1],\n", 178 | " [0, 0, 0],\n", 179 | " [2, 2, 2]])" 180 | ] 181 | }, 182 | "execution_count": 6, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "a = np.array([[ 0, 1, 2], \n", 189 | " [10,11,12], \n", 190 | " [20,21,22]])\n", 191 | "\n", 192 | "lt = a < 10\n", 193 | "gt = a > 15\n", 194 | "\n", 195 | "choice = lt + 2 * gt\n", 196 | "choice" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 7, 202 | "metadata": { 203 | "collapsed": false 204 | }, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "array([[10, 10, 10],\n", 210 | " [10, 11, 12],\n", 211 | " [15, 15, 15]])" 212 | ] 213 | }, 214 | "execution_count": 7, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "np.choose(choice, (a, 10, 15))" 221 | ] 222 | } 223 | ], 224 | "metadata": { 225 | "kernelspec": { 226 | "display_name": "Python 2", 227 | "language": "python", 228 | "name": "python2" 229 | }, 230 | "language_info": { 231 | "codemirror_mode": { 232 | "name": "ipython", 233 | "version": 2 234 | }, 235 | "file_extension": ".py", 236 | "mimetype": "text/x-python", 237 | "name": "python", 238 | "nbconvert_exporter": "python", 239 | "pygments_lexer": "ipython2", 240 | "version": "2.7.10" 241 | } 242 | }, 243 | "nbformat": 4, 244 | "nbformat_minor": 0 245 | } 246 | -------------------------------------------------------------------------------- /03-numpy/03.21-record-arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 记录数组" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "记录数组(`record array`)与结构数组类似:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import numpy as np" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "质点类型:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "partical_dtype = np.dtype([('mass', 'float'), \n", 44 | " ('velocity', 'float')])" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "生成记录数组要使用 `numpy.rec` 里的 `fromrecords` 方法:" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": { 58 | "collapsed": true 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "from numpy import rec\n", 63 | "\n", 64 | "particals_rec = rec.fromrecords([(1,1), (1,2), (2,1), (1,3)], \n", 65 | " dtype = partical_dtype)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "rec.array([(1.0, 1.0), (1.0, 2.0), (2.0, 1.0), (1.0, 3.0)], \n", 79 | " dtype=[('mass', '所有的这些操作既可以在稀疏矩阵上使用,也可以在普通矩阵上使用。" 101 | ] 102 | } 103 | ], 104 | "metadata": { 105 | "kernelspec": { 106 | "display_name": "Python 2", 107 | "language": "python", 108 | "name": "python2" 109 | }, 110 | "language_info": { 111 | "codemirror_mode": { 112 | "name": "ipython", 113 | "version": 2 114 | }, 115 | "file_extension": ".py", 116 | "mimetype": "text/x-python", 117 | "name": "python", 118 | "nbconvert_exporter": "python", 119 | "pygments_lexer": "ipython2", 120 | "version": "2.7.6" 121 | } 122 | }, 123 | "nbformat": 4, 124 | "nbformat_minor": 0 125 | } 126 | -------------------------------------------------------------------------------- /04-scipy/JANAF_CH4.txt: -------------------------------------------------------------------------------- 1 | Methane (CH4) C1H4(g) 2 | T(K) Cp S -[G-H(Tr)]/T H-H(Tr) delta-f H delta-f G log Kf 3 | 0 0. 0. INFINITE -10.024 -66.911 -66.911 INFINITE 4 | 100 33.258 149.500 216.485 -6.698 -69.644 -64.353 33.615 5 | 200 33.473 172.577 189.418 -3.368 -72.027 -58.161 15.190 6 | 250 34.216 180.113 186.829 -1.679 -73.426 -54.536 11.395 7 | 298.15 35.639 186.251 186.251 0. -74.873 -50.768 8.894 8 | 300 35.708 186.472 186.252 0.066 -74.929 -50.618 8.813 9 | 350 37.874 192.131 186.694 1.903 -76.461 -46.445 6.932 10 | 400 40.500 197.356 187.704 3.861 -77.969 -42.054 5.492 11 | 450 43.374 202.291 189.053 5.957 -79.422 -37.476 4.350 12 | 500 46.342 207.014 190.614 8.200 -80.802 -32.741 3.420 13 | 600 52.227 215.987 194.103 13.130 -83.308 -22.887 1.993 14 | 700 57.794 224.461 197.840 18.635 -85.452 -12.643 0.943 15 | 800 62.932 232.518 201.675 24.675 -87.238 -2.115 0.138 16 | 900 67.601 240.205 205.532 31.205 -88.692 8.616 -0.500 17 | 1000 71.795 247.549 209.370 38.179 -89.849 19.492 -1.018 18 | 1100 75.529 254.570 213.162 45.549 -90.750 30.472 -1.447 19 | 1200 78.833 261.287 216.895 53.270 -91.437 41.524 -1.807 20 | 1300 81.744 267.714 220.558 61.302 -91.945 52.626 -2.115 21 | 1400 84.305 273.868 224.148 69.608 -92.308 63.761 -2.379 22 | 1500 86.556 279.763 227.660 78.153 -92.553 74.918 -2.609 23 | 1600 88.537 285.413 231.095 86.910 -92.703 86.088 -2.810 24 | 1700 90.283 290.834 234.450 95.853 -92.780 97.265 -2.989 25 | 1800 91.824 296.039 237.728 104.960 -92.797 108.445 -3.147 26 | 1900 93.188 301.041 240.930 114.212 -92.770 119.624 -3.289 27 | 2000 94.399 305.853 244.057 123.592 -92.709 130.802 -3.416 28 | 2100 95.477 310.485 247.110 133.087 -92.624 141.975 -3.531 29 | 2200 96.439 314.949 250.093 142.684 -92.521 153.144 -3.636 30 | 2300 97.301 319.255 253.007 152.371 -92.409 164.308 -3.732 31 | 2400 98.075 323.413 255.854 162.141 -92.291 175.467 -3.819 32 | 2500 98.772 327.431 258.638 171.984 -92.174 186.622 -3.899 33 | 2600 99.401 331.317 261.359 181.893 -92.060 197.771 -3.973 34 | 2700 99.971 335.080 264.020 191.862 -91.954 208.916 -4.042 35 | 2800 100.489 338.725 266.623 201.885 -91.857 220.058 -4.105 36 | 2900 100.960 342.260 269.171 211.958 -91.773 231.196 -4.164 37 | 3000 101.389 345.690 271.664 222.076 -91.705 242.332 -4.219 38 | 3100 101.782 349.021 274.106 232.235 -91.653 253.465 -4.271 39 | 3200 102.143 352.258 276.498 242.431 -91.621 264.598 -4.319 40 | 3300 102.474 355.406 278.842 252.662 -91.609 275.730 -4.364 41 | 3400 102.778 358.470 281.139 262.925 -91.619 286.861 -4.407 42 | 3500 103.060 361.453 283.391 273.217 -91.654 297.993 -4.447 43 | 3600 103.319 364.360 285.600 283.536 -91.713 309.127 -4.485 44 | 3700 103.560 367.194 287.767 293.881 -91.798 320.262 -4.521 45 | 3800 103.783 369.959 289.894 304.248 -91.911 331.401 -4.555 46 | 3900 103.990 372.658 291.982 314.637 -92.051 342.542 -4.588 47 | 4000 104.183 375.293 294.032 325.045 -92.222 353.687 -4.619 48 | 4100 104.363 377.868 296.045 335.473 -92.422 364.838 -4.648 49 | 4200 104.531 380.385 298.023 345.918 -92.652 375.993 -4.676 50 | 4300 104.688 382.846 299.967 356.379 -92.914 387.155 -4.703 51 | 4400 104.834 385.255 301.879 366.855 -93.208 398.322 -4.729 52 | 4500 104.972 387.612 303.758 377.345 -93.533 409.497 -4.753 53 | 4600 105.101 389.921 305.606 387.849 -93.891 420.679 -4.777 54 | 4700 105.223 392.182 307.424 398.365 -94.281 431.869 -4.800 55 | 4800 105.337 394.399 309.213 408.893 -94.702 443.069 -4.822 56 | 4900 105.445 396.572 310.973 419.432 -95.156 454.277 -4.843 57 | 5000 105.546 398.703 312.707 429.982 -95.641 465.495 -4.863 58 | 5100 105.642 400.794 314.414 440.541 -96.157 476.722 -4.883 59 | 5200 105.733 402.847 316.095 451.110 -96.703 487.961 -4.902 60 | 5300 105.818 404.861 317.750 461.688 -97.278 499.210 -4.920 61 | 5400 105.899 406.840 319.382 472.274 -97.882 510.470 -4.938 62 | 5500 105.976 408.784 320.990 482.867 -98.513 521.741 -4.955 63 | 5600 106.049 410.694 322.575 493.469 -99.170 533.025 -4.972 64 | 5700 106.118 412.572 324.137 504.077 -99.852 544.320 -4.988 65 | 5800 106.184 414.418 325.678 514.692 -100.557 555.628 -5.004 66 | 5900 106.247 416.234 327.197 525.314 -101.284 566.946 -5.019 67 | 6000 106.306 418.020 328.696 535.942 -102.032 578.279 -5.034 68 | -------------------------------------------------------------------------------- /05-advanced-python/05.05-datetime.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# datetime 模块" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import datetime as dt" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "`datetime` 提供了基础时间和日期的处理。" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## date 对象" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "可以使用 `date(year, month, day)` 产生一个 `date` 对象:" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "collapsed": true 47 | }, 48 | "outputs": [], 49 | "source": [ 50 | "d1 = dt.date(2007, 9, 25)\n", 51 | "d2 = dt.date(2008, 9, 25)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "可以格式化 `date` 对象的输出:" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 3, 64 | "metadata": { 65 | "collapsed": false 66 | }, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "2007-09-25\n", 73 | "Tuesday, 09/25/07\n", 74 | "Tue, 09-25-2007\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "print d1\n", 80 | "print d1.strftime('%A, %m/%d/%y')\n", 81 | "print d1.strftime('%a, %m-%d-%Y')" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "可以看两个日期相差多久:" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "366 days, 0:00:00\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "print d2 - d1" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "返回的是一个 `timedelta` 对象:" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 5, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "366\n", 129 | "0\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "d = d2 - d1\n", 135 | "print d.days\n", 136 | "print d.seconds" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "查看今天的日期:" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 6, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "2015-09-10\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "print dt.date.today()" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "## time 对象" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "可以使用 `time(hour, min, sec, us)` 产生一个 `time` 对象:" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 7, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [], 186 | "source": [ 187 | "t1 = dt.time(15, 38)\n", 188 | "t2 = dt.time(18)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "改变显示格式:" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 8, 201 | "metadata": { 202 | "collapsed": false 203 | }, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "15:38:00\n", 210 | "03:38, PM\n", 211 | "15:38:00, PM\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "print t1\n", 217 | "print t1.strftime('%I:%M, %p')\n", 218 | "print t1.strftime('%H:%M:%S, %p')" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "metadata": {}, 224 | "source": [ 225 | "因为没有具体的日期信息,所以 `time` 对象不支持减法操作。" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "## datetime 对象" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "可以使用 `datetime(year, month, day, hr, min, sec, us)` 来创建一个 `datetime` 对象。 \n", 240 | "\n", 241 | "获得当前时间:" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 9, 247 | "metadata": { 248 | "collapsed": false 249 | }, 250 | "outputs": [ 251 | { 252 | "name": "stdout", 253 | "output_type": "stream", 254 | "text": [ 255 | "2015-09-10 20:58:50.148000\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "d1 = dt.datetime.now()\n", 261 | "print d1" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "给当前的时间加上 `30` 天,`timedelta` 的参数是 `timedelta(day, hr, min, sec, us)`:" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 10, 274 | "metadata": { 275 | "collapsed": false 276 | }, 277 | "outputs": [ 278 | { 279 | "name": "stdout", 280 | "output_type": "stream", 281 | "text": [ 282 | "2015-10-10 20:58:50.148000\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "d2 = d1 + dt.timedelta(30)\n", 288 | "print d2" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "除此之外,我们还可以通过一些指定格式的字符串来创建 `datetime` 对象:" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 11, 301 | "metadata": { 302 | "collapsed": false 303 | }, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "2001-02-10 00:00:00\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "print dt.datetime.strptime('2/10/01', '%m/%d/%y')" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": {}, 320 | "source": [ 321 | "## datetime 格式字符表" 322 | ] 323 | }, 324 | { 325 | "cell_type": "markdown", 326 | "metadata": {}, 327 | "source": [ 328 | "字符|含义\n", 329 | "--|--\n", 330 | "`%a` | 星期英文缩写\n", 331 | "`%A` | 星期英文\n", 332 | "`%w` | 一星期的第几天,`[0(sun),6]`\n", 333 | "`%b` | 月份英文缩写\n", 334 | "`%B` | 月份英文\n", 335 | "`%d` | 日期,`[01,31]`\n", 336 | "`%H` | 小时,`[00,23]`\n", 337 | "`%I` | 小时,`[01,12]`\n", 338 | "`%j` | 一年的第几天,`[001,366]`\n", 339 | "`%m` | 月份,`[01,12]`\n", 340 | "`%M` | 分钟,`[00,59]`\n", 341 | "`%p` | AM 和 PM\n", 342 | "`%S` | 秒钟,`[00,61]` (大概是有闰秒的存在)\n", 343 | "`%U` | 一年中的第几个星期,星期日为第一天,`[00,53]`\n", 344 | "`%W` | 一年中的第几个星期,星期一为第一天,`[00,53]`\n", 345 | "`%y` | 没有世纪的年份\n", 346 | "`%Y` | 完整的年份" 347 | ] 348 | } 349 | ], 350 | "metadata": { 351 | "kernelspec": { 352 | "display_name": "Python 2", 353 | "language": "python", 354 | "name": "python2" 355 | }, 356 | "language_info": { 357 | "codemirror_mode": { 358 | "name": "ipython", 359 | "version": 2 360 | }, 361 | "file_extension": ".py", 362 | "mimetype": "text/x-python", 363 | "name": "python", 364 | "nbconvert_exporter": "python", 365 | "pygments_lexer": "ipython2", 366 | "version": "2.7.10" 367 | } 368 | }, 369 | "nbformat": 4, 370 | "nbformat_minor": 0 371 | } 372 | -------------------------------------------------------------------------------- /05-advanced-python/05.06-sql-databases.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# SQL 数据库" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "`Python` 提供了一系列标准的数据库的 API,这里我们介绍 sqlite 数据库的用法,其他的数据库的用法大同小异:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import sqlite3 as db" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "首先我们要建立或者连接到一个数据库上:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "connection = db.connect(\"my_database.sqlite\")" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "不同的数据库有着不同的连接方法,例如 cx-oracle 数据库的链接方式为:\n", 51 | "\n", 52 | " connection = db.connect(username, password, host, port, 'XE')\n", 53 | "\n", 54 | "一旦建立连接,我们可以利用它的 `cursor()` 来执行 SQL 语句:" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "cursor = connection.cursor()\n", 66 | "cursor.execute(\"\"\"CREATE TABLE IF NOT EXISTS orders(\n", 67 | " order_id TEXT PRIMARY KEY,\n", 68 | " date TEXT,\n", 69 | " symbol TEXT,\n", 70 | " quantity INTEGER,\n", 71 | " price NUMBER)\"\"\")\n", 72 | "cursor.execute(\"\"\"INSERT INTO orders VALUES\n", 73 | " ('A0001', '2013-12-01', 'AAPL', 1000, 203.4)\"\"\")\n", 74 | "connection.commit()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "不过为了安全起见,一般不将数据内容写入字符串再传入,而是使用这样的方式:" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 4, 87 | "metadata": { 88 | "collapsed": true 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "orders = [\n", 93 | " (\"A0002\",\"2013-12-01\",\"MSFT\",1500,167.5),\n", 94 | " (\"A0003\",\"2013-12-02\",\"GOOG\",1500,167.5)\n", 95 | "]\n", 96 | "cursor.executemany(\"\"\"INSERT INTO orders VALUES\n", 97 | " (?, ?, ?, ?, ?)\"\"\", orders)\n", 98 | "connection.commit()" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "cx-oracle 数据库使用不同的方式:\n", 106 | "\n", 107 | " cursor.executemany(\"\"\"INSERT INTO orders VALUES\n", 108 | " (:order_id, :date, :symbol, :quantity, :price)\"\"\",\n", 109 | " orders)\n", 110 | "\n", 111 | "查看支持的数据库格式:" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "'qmark'" 125 | ] 126 | }, 127 | "execution_count": 5, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "db.paramstyle" 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": {}, 139 | "source": [ 140 | "在 `query` 语句执行之后,我们需要进行 `commit`,否则数据库将不会接受这些变化,如果想撤销某个 `commit`,可以使用 `rollback()` 方法撤销到上一次 `commit()` 的结果:\n", 141 | "\n", 142 | " try:\n", 143 | " ... # perform some operations\n", 144 | " except:\n", 145 | " connection.rollback()\n", 146 | " raise\n", 147 | " else:\n", 148 | " connection.commit()\n", 149 | "\n", 150 | "使用 `SELECT` 语句对数据库进行查询:" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 6, 156 | "metadata": { 157 | "collapsed": false 158 | }, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "(u'A0002', u'2013-12-01', u'MSFT', 1500, 167.5)\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "stock = 'MSFT'\n", 170 | "cursor.execute(\"\"\"SELECT *\n", 171 | " FROM orders\n", 172 | " WHERE symbol=?\n", 173 | " ORDER BY quantity\"\"\", (stock,))\n", 174 | "for row in cursor:\n", 175 | " print row" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "`cursor.fetchone()` 返回下一条内容, `cursor.fetchall()` 返回所有查询到的内容组成的列表(可能非常大):" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 7, 188 | "metadata": { 189 | "collapsed": false 190 | }, 191 | "outputs": [ 192 | { 193 | "data": { 194 | "text/plain": [ 195 | "[(u'A0001', u'2013-12-01', u'AAPL', 1000, 203.4)]" 196 | ] 197 | }, 198 | "execution_count": 7, 199 | "metadata": {}, 200 | "output_type": "execute_result" 201 | } 202 | ], 203 | "source": [ 204 | "stock = 'AAPL'\n", 205 | "cursor.execute(\"\"\"SELECT *\n", 206 | " FROM orders\n", 207 | " WHERE symbol=?\n", 208 | " ORDER BY quantity\"\"\", (stock,))\n", 209 | "cursor.fetchall()" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "关闭数据库:" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 8, 222 | "metadata": { 223 | "collapsed": true 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "cursor.close()\n", 228 | "connection.close()" 229 | ] 230 | } 231 | ], 232 | "metadata": { 233 | "kernelspec": { 234 | "display_name": "Python 2", 235 | "language": "python", 236 | "name": "python2" 237 | }, 238 | "language_info": { 239 | "codemirror_mode": { 240 | "name": "ipython", 241 | "version": 2 242 | }, 243 | "file_extension": ".py", 244 | "mimetype": "text/x-python", 245 | "name": "python", 246 | "nbconvert_exporter": "python", 247 | "pygments_lexer": "ipython2", 248 | "version": "2.7.10" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 0 253 | } 254 | -------------------------------------------------------------------------------- /05-advanced-python/05.07-object-relational-mappers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 对象关系映射" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "数据库中的记录可以与一个 `Python` 对象对应。\n", 15 | "\n", 16 | "例如对于上一节中的数据库:\n", 17 | "\n", 18 | "Order|Date|Stock|Quantity|Price\n", 19 | "--|--|--|--|--\n", 20 | "A0001|2013-12-01|AAPL|1000|203.4\n", 21 | "A0002|2013-12-01|MSFT|1500|167.5\n", 22 | "A0003|2013-12-02|GOOG|1500|167.5\n", 23 | "\n", 24 | "可以用一个类来描述:\n", 25 | "\n", 26 | "Attr.|Method\n", 27 | "--|--\n", 28 | "Order id| Cost\n", 29 | "Date|\n", 30 | "Stock|\n", 31 | "Quant.|\n", 32 | "Price|\n", 33 | "\n", 34 | "可以使用 `sqlalchemy` 来实现这种对应:" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 1, 40 | "metadata": { 41 | "collapsed": true 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "from sqlalchemy.ext.declarative import declarative_base\n", 46 | "from sqlalchemy import Column, Date, Float, Integer, String\n", 47 | "\n", 48 | "Base = declarative_base()\n", 49 | "\n", 50 | "class Order(Base):\n", 51 | " __tablename__ = 'orders'\n", 52 | " \n", 53 | " order_id = Column(String, primary_key=True)\n", 54 | " date = Column(Date)\n", 55 | " symbol = Column(String)\n", 56 | " quantity = Column(Integer)\n", 57 | " price = Column(Float)\n", 58 | " \n", 59 | " def get_cost(self):\n", 60 | " return self.quantity*self.price" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "生成一个 `Order` 对象:" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 2, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "import datetime\n", 79 | "order = Order(order_id='A0004', date=datetime.date.today(), symbol='MSFT', quantity=-1000, price=187.54)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "调用方法:" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 3, 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "-187540.0" 100 | ] 101 | }, 102 | "execution_count": 3, 103 | "metadata": {}, 104 | "output_type": "execute_result" 105 | } 106 | ], 107 | "source": [ 108 | "order.get_cost()" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "使用上一节生成的数据库产生一个 `session`:" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 4, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "from sqlalchemy import create_engine\n", 127 | "from sqlalchemy.orm import sessionmaker\n", 128 | "\n", 129 | "engine = create_engine(\"sqlite:///my_database.sqlite\") # 相当于 connection\n", 130 | "Session = sessionmaker(bind=engine) # 相当于 cursor\n", 131 | "session = Session()" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "使用这个 `session` 向数据库中添加刚才生成的对象:" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 5, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [], 148 | "source": [ 149 | "session.add(order)\n", 150 | "session.commit()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "显示是否添加成功:" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 6, 163 | "metadata": { 164 | "collapsed": false 165 | }, 166 | "outputs": [ 167 | { 168 | "name": "stdout", 169 | "output_type": "stream", 170 | "text": [ 171 | "(u'A0001', u'2013-12-01', u'AAPL', 1000, 203.4)\n", 172 | "(u'A0002', u'2013-12-01', u'MSFT', 1500, 167.5)\n", 173 | "(u'A0003', u'2013-12-02', u'GOOG', 1500, 167.5)\n", 174 | "(u'A0004', u'2015-09-10', u'MSFT', -1000, 187.54)\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "for row in engine.execute(\"SELECT * FROM orders\"):\n", 180 | " print row" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "使用 `filter` 进行查询,返回的是 `Order` 对象的列表:" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 7, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "A0001 2013-12-01 203400.0\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "for order in session.query(Order).filter(Order.symbol==\"AAPL\"):\n", 207 | " print order.order_id, order.date, order.get_cost()" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "返回列表的第一个:" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 8, 220 | "metadata": { 221 | "collapsed": true 222 | }, 223 | "outputs": [], 224 | "source": [ 225 | "order_2 = session.query(Order).filter(Order.order_id=='A0002').first()" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 9, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [ 235 | { 236 | "data": { 237 | "text/plain": [ 238 | "u'MSFT'" 239 | ] 240 | }, 241 | "execution_count": 9, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "order_2.symbol" 248 | ] 249 | } 250 | ], 251 | "metadata": { 252 | "kernelspec": { 253 | "display_name": "Python 2", 254 | "language": "python", 255 | "name": "python2" 256 | }, 257 | "language_info": { 258 | "codemirror_mode": { 259 | "name": "ipython", 260 | "version": 2 261 | }, 262 | "file_extension": ".py", 263 | "mimetype": "text/x-python", 264 | "name": "python", 265 | "nbconvert_exporter": "python", 266 | "pygments_lexer": "ipython2", 267 | "version": "2.7.10" 268 | } 269 | }, 270 | "nbformat": 4, 271 | "nbformat_minor": 0 272 | } 273 | -------------------------------------------------------------------------------- /05-advanced-python/my_database.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/05-advanced-python/my_database.sqlite -------------------------------------------------------------------------------- /06-matplotlib/artists_figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/06-matplotlib/artists_figure.png -------------------------------------------------------------------------------- /06-matplotlib/artists_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/06-matplotlib/artists_tree.png -------------------------------------------------------------------------------- /06-matplotlib/stinkbug.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/06-matplotlib/stinkbug.png -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07-02-example.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/07-interfacing-with-other-languages/07-02-example.zip -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07-03-fib.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/07-interfacing-with-other-languages/07-03-fib.zip -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07-04-extern.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/07-interfacing-with-other-languages/07-04-extern.zip -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07-05-particle.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/07-interfacing-with-other-languages/07-05-particle.zip -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07-06-cython-sum.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/07-interfacing-with-other-languages/07-06-cython-sum.zip -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07.01-introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 简介" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 使用 Python 和另一种语言混编的好处" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "至少有以下四个原因:\n", 22 | "\n", 23 | "1. `Best of both worlds` - 结合两种语言的优点:已经优化和测试过的代码库 + Python 的灵活\n", 24 | "- `Python as glue` - **Python** 作为连接的桥梁,将很多其他语言的模块结合到一个大型程序中\n", 25 | "- `Speed up Python` - 使用一个更快的语言帮助加速 **Python**\n", 26 | "- `Division of labor` - 各司其职,让各个语言做各自更擅长的事情,例如 **Fortran** 进行数组计算,**Python** 处理测试,文件读写,文本处理,数据整理,GUI 生成,HTTP 服务等等。" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "## 语言扩展工具" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "### 打包已有的代码和其他语言的库" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "- 使用手写的扩展模块\n", 48 | "- `Cython` - **C/C++**\n", 49 | "- `SWIG` - **C/C++**\n", 50 | "- `f2py` - **Fortran**\n", 51 | "- `ctypes` - 其他语言库" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "### 加速 Python" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "- 使用手写的扩展模块\n", 66 | "- `Cython`\n", 67 | "- `Weave`\n", 68 | "- `Shedskin` 和其他模块" 69 | ] 70 | } 71 | ], 72 | "metadata": { 73 | "kernelspec": { 74 | "display_name": "Python 2", 75 | "language": "python", 76 | "name": "python2" 77 | }, 78 | "language_info": { 79 | "codemirror_mode": { 80 | "name": "ipython", 81 | "version": 2 82 | }, 83 | "file_extension": ".py", 84 | "mimetype": "text/x-python", 85 | "name": "python", 86 | "nbconvert_exporter": "python", 87 | "pygments_lexer": "ipython2", 88 | "version": "2.7.9" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 0 93 | } 94 | -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/07.07-profiling-with-annotations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 生成编译注释" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Writing fib_orig.pyx\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "%%file fib_orig.pyx\n", 27 | "def fib(n):\n", 28 | " a,b = 1,1\n", 29 | " for i in range(n):\n", 30 | " a,b = a+b, a\n", 31 | " return a" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "!cython -a fib_orig.pyx" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "在浏览器中打开 `fib_orig.html` 可以查看内容,`windows` 下打开网页使用:" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [ 60 | "!start fib_orig.html" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "`linux` 下使用:\n", 68 | "```\n", 69 | "open fib_orig.html\n", 70 | "```\n", 71 | "\n", 72 | "其界面可能如图所示:\n", 73 | "![界面](fib_orig.png)\n", 74 | "\n", 75 | "点击某一行可以查看该 `Python` 代码对应的 `C` 代码。" 76 | ] 77 | } 78 | ], 79 | "metadata": { 80 | "kernelspec": { 81 | "display_name": "Python 2", 82 | "language": "python", 83 | "name": "python2" 84 | }, 85 | "language_info": { 86 | "codemirror_mode": { 87 | "name": "ipython", 88 | "version": 2 89 | }, 90 | "file_extension": ".py", 91 | "mimetype": "text/x-python", 92 | "name": "python", 93 | "nbconvert_exporter": "python", 94 | "pygments_lexer": "ipython2", 95 | "version": "2.7.10" 96 | } 97 | }, 98 | "nbformat": 4, 99 | "nbformat_minor": 0 100 | } 101 | -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/fib_orig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/07-interfacing-with-other-languages/fib_orig.png -------------------------------------------------------------------------------- /07-interfacing-with-other-languages/fib_orig.pyx: -------------------------------------------------------------------------------- 1 | def fib(n): 2 | a,b = 1,1 3 | for i in range(n): 4 | a,b = a+b, a 5 | return a -------------------------------------------------------------------------------- /08-object-oriented-programming/08.01-oop-introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 简介" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 属性 attributes" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "属性是与对象绑定的一组数据,可以只读,只写,或者读写,使用时不加括号,例如:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "f = file(\"new_file\", 'w')" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "显示模式属性:" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "metadata": { 46 | "collapsed": false 47 | }, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "'w'" 53 | ] 54 | }, 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "f.mode" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "是否关闭:" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "False" 82 | ] 83 | }, 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "f.closed" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "`mode` 是只读属性,所以这样会报错:" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": { 104 | "collapsed": false 105 | }, 106 | "outputs": [ 107 | { 108 | "ename": "TypeError", 109 | "evalue": "readonly attribute", 110 | "output_type": "error", 111 | "traceback": [ 112 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 113 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 114 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmode\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 115 | "\u001b[1;31mTypeError\u001b[0m: readonly attribute" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "f.mode = 'r'" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "获取属性不需要加括号:" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 5, 133 | "metadata": { 134 | "collapsed": false 135 | }, 136 | "outputs": [ 137 | { 138 | "ename": "TypeError", 139 | "evalue": "'str' object is not callable", 140 | "output_type": "error", 141 | "traceback": [ 142 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 143 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 144 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 145 | "\u001b[1;31mTypeError\u001b[0m: 'str' object is not callable" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "f.mode()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "## 方法 method" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "方法是与属性绑定的一组函数,需要使用括号,作用于对象本身:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 6, 170 | "metadata": { 171 | "collapsed": true 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "f.write('Hi.\\n')\n", 176 | "f.seek(0)\n", 177 | "f.write('Hola!\\n')\n", 178 | "f.close()" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 7, 184 | "metadata": { 185 | "collapsed": true 186 | }, 187 | "outputs": [], 188 | "source": [ 189 | "!rm new_file" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "## 使用 OPP 的原因" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "- 构建自己的类型来模拟真实世界的对象\n", 204 | "- 处理抽象对象\n", 205 | "- 容易复用和扩展\n", 206 | "- 理解其他 OPP 代码\n", 207 | "- GUI 通常使用 OPP 规则编写\n", 208 | "- ..." 209 | ] 210 | } 211 | ], 212 | "metadata": { 213 | "kernelspec": { 214 | "display_name": "Python 2", 215 | "language": "python", 216 | "name": "python2" 217 | }, 218 | "language_info": { 219 | "codemirror_mode": { 220 | "name": "ipython", 221 | "version": 2 222 | }, 223 | "file_extension": ".py", 224 | "mimetype": "text/x-python", 225 | "name": "python", 226 | "nbconvert_exporter": "python", 227 | "pygments_lexer": "ipython2", 228 | "version": "2.7.10" 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 0 233 | } 234 | -------------------------------------------------------------------------------- /08-object-oriented-programming/08.03-what-is-a-object.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 什么是对象?" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "在 `Python` 中,几乎所有的东西都是对象。\n", 15 | "\n", 16 | "整数是对象:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": true 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "a = 257" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": { 34 | "collapsed": false 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "int" 41 | ] 42 | }, 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "type(a)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "metadata": { 56 | "collapsed": false 57 | }, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "53187032L" 63 | ] 64 | }, 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "id(a)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "`b` 和 `a` 是同一个对象:" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": { 85 | "collapsed": true 86 | }, 87 | "outputs": [], 88 | "source": [ 89 | "b = a" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 5, 95 | "metadata": { 96 | "collapsed": false 97 | }, 98 | "outputs": [ 99 | { 100 | "data": { 101 | "text/plain": [ 102 | "53187032L" 103 | ] 104 | }, 105 | "execution_count": 5, 106 | "metadata": {}, 107 | "output_type": "execute_result" 108 | } 109 | ], 110 | "source": [ 111 | "id(b)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 6, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "53186960L" 125 | ] 126 | }, 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "c = 258\n", 134 | "id(c)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "函数:" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 7, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "def foo():\n", 153 | " print 'hi'" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 8, 159 | "metadata": { 160 | "collapsed": false 161 | }, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "function" 167 | ] 168 | }, 169 | "execution_count": 8, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "type(foo)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 9, 181 | "metadata": { 182 | "collapsed": false 183 | }, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "63632664L" 189 | ] 190 | }, 191 | "execution_count": 9, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "id(foo)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "`type` 函数本身也是对象:" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 10, 210 | "metadata": { 211 | "collapsed": false 212 | }, 213 | "outputs": [ 214 | { 215 | "data": { 216 | "text/plain": [ 217 | "type" 218 | ] 219 | }, 220 | "execution_count": 10, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "type(type)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 11, 232 | "metadata": { 233 | "collapsed": false 234 | }, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "506070640L" 240 | ] 241 | }, 242 | "execution_count": 11, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "id(type)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "只有一些保留的关键词不是对象:" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 12, 261 | "metadata": { 262 | "collapsed": false 263 | }, 264 | "outputs": [ 265 | { 266 | "ename": "SyntaxError", 267 | "evalue": "invalid syntax (, line 1)", 268 | "output_type": "error", 269 | "traceback": [ 270 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m id(if)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "id(if)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 13, 281 | "metadata": { 282 | "collapsed": false 283 | }, 284 | "outputs": [ 285 | { 286 | "ename": "SyntaxError", 287 | "evalue": "invalid syntax (, line 1)", 288 | "output_type": "error", 289 | "traceback": [ 290 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m id(+)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "id(+)" 296 | ] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 2", 302 | "language": "python", 303 | "name": "python2" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 2 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython2", 315 | "version": "2.7.10" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 0 320 | } 321 | -------------------------------------------------------------------------------- /08-object-oriented-programming/08.04-writing-classes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 定义 class" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## 基本形式" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "`class` 定义如下:\n", 22 | "```python\n", 23 | "class ClassName(ParentClass):\n", 24 | " \"\"\"class docstring\"\"\"\n", 25 | " def method(self):\n", 26 | " return\n", 27 | "```\n", 28 | "\n", 29 | "- `class` 关键词在最前面\n", 30 | "- `ClassName` 通常采用 `CamelCase` 记法\n", 31 | "- 括号中的 `ParentClass` 用来表示继承关系\n", 32 | "- 冒号不能缺少\n", 33 | "- `\"\"\"\"\"\"` 中的内容表示 `docstring`,可以省略\n", 34 | "- 方法定义与函数定义十分类似,不过多了一个 `self` 参数表示这个对象本身\n", 35 | "- `class` 中的方法要进行缩进" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 1, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "class Forest(object):\n", 47 | " \"\"\" Forest can grow trees which eventually die.\"\"\"\n", 48 | " pass" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "其中 `object` 是最基本的类型。\n", 56 | "\n", 57 | "查看帮助:" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 2, 63 | "metadata": { 64 | "collapsed": false 65 | }, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | " Forest()\n", 72 | "\n", 73 | "Forest can grow trees which eventually die.\n", 74 | "\n", 75 | "\n", 76 | "Methods:\n", 77 | "\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "import numpy as np\n", 83 | "np.info(Forest)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 3, 89 | "metadata": { 90 | "collapsed": false 91 | }, 92 | "outputs": [], 93 | "source": [ 94 | "forest = Forest()" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 4, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "<__main__.Forest at 0x3cda358>" 108 | ] 109 | }, 110 | "execution_count": 4, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "forest" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "## 添加方法和属性" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "可以直接添加属性(有更好的替代方式):" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 5, 136 | "metadata": { 137 | "collapsed": true 138 | }, 139 | "outputs": [], 140 | "source": [ 141 | "forest.trees = np.zeros((150, 150), dtype=bool)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 6, 147 | "metadata": { 148 | "collapsed": false 149 | }, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "array([[False, False, False, ..., False, False, False],\n", 155 | " [False, False, False, ..., False, False, False],\n", 156 | " [False, False, False, ..., False, False, False],\n", 157 | " ..., \n", 158 | " [False, False, False, ..., False, False, False],\n", 159 | " [False, False, False, ..., False, False, False],\n", 160 | " [False, False, False, ..., False, False, False]], dtype=bool)" 161 | ] 162 | }, 163 | "execution_count": 6, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "forest.trees" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 7, 175 | "metadata": { 176 | "collapsed": true 177 | }, 178 | "outputs": [], 179 | "source": [ 180 | "forest2 = Forest()" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "`forest2` 没有这个属性:" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 8, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [ 197 | { 198 | "ename": "AttributeError", 199 | "evalue": "'Forest' object has no attribute 'trees'", 200 | "output_type": "error", 201 | "traceback": [ 202 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 203 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 204 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mforest2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrees\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 205 | "\u001b[1;31mAttributeError\u001b[0m: 'Forest' object has no attribute 'trees'" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "forest2.trees" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "添加方法时,默认第一个参数是对象本身,一般为 `self`,可能用到也可能用不到,然后才是其他的参数:" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 9, 223 | "metadata": { 224 | "collapsed": true 225 | }, 226 | "outputs": [], 227 | "source": [ 228 | "class Forest(object):\n", 229 | " \"\"\" Forest can grow trees which eventually die.\"\"\"\n", 230 | " def grow(self):\n", 231 | " print \"the tree is growing!\"\n", 232 | " \n", 233 | " def number(self, num=1):\n", 234 | " if num == 1:\n", 235 | " print 'there is 1 tree.'\n", 236 | " else:\n", 237 | " print 'there are', num, 'trees.'" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 10, 243 | "metadata": { 244 | "collapsed": false 245 | }, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "the tree is growing!\n", 252 | "there are 12 trees.\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "forest = Forest()\n", 258 | "\n", 259 | "forest.grow()\n", 260 | "forest.number(12)" 261 | ] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Python 2", 267 | "language": "python", 268 | "name": "python2" 269 | } 270 | }, 271 | "nbformat": 4, 272 | "nbformat_minor": 0 273 | } 274 | -------------------------------------------------------------------------------- /08-object-oriented-programming/08.08-inheritance.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 继承" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "一个类定义的基本形式如下:\n", 15 | "```python\n", 16 | "class ClassName(ParentClass):\n", 17 | " \"\"\"class docstring\"\"\"\n", 18 | " def method(self):\n", 19 | " return\n", 20 | "```\n", 21 | "\n", 22 | "- `class` 关键词在最前面\n", 23 | "- `ClassName` 通常采用 `CamelCase` 记法\n", 24 | "- 括号中的 `ParentClass` 用来表示继承关系\n", 25 | "- 冒号不能缺少\n", 26 | "- `\"\"\"\"\"\"` 中的内容表示 `docstring`,可以省略\n", 27 | "- 方法定义与函数定义十分类似,不过多了一个 `self` 参数表示这个对象本身\n", 28 | "- `class` 中的方法要进行缩进\n", 29 | "\n", 30 | "在里面有一个 `ParentClass` 项,用来进行继承,被继承的类是父类,定义的这个类是子类。\n", 31 | "对于子类来说,继承意味着它可以使用所有父类的方法和属性,同时还可以定义自己特殊的方法和属性。\n", 32 | "\n", 33 | "假设我们有这样一个父类:" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": { 40 | "collapsed": true 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "class Leaf(object):\n", 45 | " def __init__(self, color=\"green\"):\n", 46 | " self.color = color\n", 47 | " def fall(self):\n", 48 | " print \"Splat!\"" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "测试:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 2, 61 | "metadata": { 62 | "collapsed": false, 63 | "scrolled": true 64 | }, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "green\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "leaf = Leaf()\n", 76 | "\n", 77 | "print leaf.color" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": { 84 | "collapsed": false 85 | }, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "Splat!\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "leaf.fall()" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "现在定义一个子类,继承自 `Leaf`:" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": { 110 | "collapsed": true 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "class MapleLeaf(Leaf):\n", 115 | " def change_color(self):\n", 116 | " if self.color == \"green\":\n", 117 | " self.color = \"red\"" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "继承父类的所有方法:" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 5, 130 | "metadata": { 131 | "collapsed": false 132 | }, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "green\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "mleaf = MapleLeaf()\n", 144 | "\n", 145 | "print mleaf.color" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 6, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "Splat!\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "mleaf.fall()" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "但是有自己独有的方法,父类中没有:" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 7, 177 | "metadata": { 178 | "collapsed": false 179 | }, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "red\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "mleaf.change_color()\n", 191 | "\n", 192 | "print mleaf.color" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "如果想对父类的方法进行修改,只需要在子类中重定义这个类即可:" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 8, 205 | "metadata": { 206 | "collapsed": true 207 | }, 208 | "outputs": [], 209 | "source": [ 210 | "class MapleLeaf(Leaf):\n", 211 | " def change_color(self):\n", 212 | " if self.color == \"green\":\n", 213 | " self.color = \"red\"\n", 214 | " def fall(self):\n", 215 | " self.change_color()\n", 216 | " print \"Plunk!\"" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 9, 222 | "metadata": { 223 | "collapsed": false, 224 | "scrolled": true 225 | }, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "green\n", 232 | "Plunk!\n", 233 | "red\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "mleaf = MapleLeaf()\n", 239 | "\n", 240 | "print mleaf.color\n", 241 | "mleaf.fall()\n", 242 | "print mleaf.color" 243 | ] 244 | } 245 | ], 246 | "metadata": { 247 | "kernelspec": { 248 | "display_name": "Python 2", 249 | "language": "python", 250 | "name": "python2" 251 | }, 252 | "language_info": { 253 | "codemirror_mode": { 254 | "name": "ipython", 255 | "version": 2 256 | }, 257 | "file_extension": ".py", 258 | "mimetype": "text/x-python", 259 | "name": "python", 260 | "nbconvert_exporter": "python", 261 | "pygments_lexer": "ipython2", 262 | "version": "2.7.10" 263 | } 264 | }, 265 | "nbformat": 4, 266 | "nbformat_minor": 0 267 | } 268 | -------------------------------------------------------------------------------- /08-object-oriented-programming/08.12-public-private-special-in-python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 共有,私有和特殊方法和属性" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "- 我们之前已经见过 `special` 方法和属性,即以 `__` 开头和结尾的方法和属性\n", 15 | "- 私有方法和属性,以 `_` 开头,不过不是真正私有,而是可以调用的,但是不会被代码自动完成所记录(即 Tab 键之后不会显示)\n", 16 | "- 其他都是共有的方法和属性\n", 17 | "- 以 `__` 开头不以 `__` 结尾的属性是更加特殊的方法,调用方式也不同:" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "metadata": { 24 | "collapsed": true 25 | }, 26 | "outputs": [], 27 | "source": [ 28 | "class MyClass(object):\n", 29 | " def __init__(self):\n", 30 | " print \"I'm special!\"\n", 31 | " def _private(self):\n", 32 | " print \"I'm private!\"\n", 33 | " def public(self):\n", 34 | " print \"I'm public!\"\n", 35 | " def __really_special(self):\n", 36 | " print \"I'm really special!\"" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": { 43 | "collapsed": false, 44 | "scrolled": true 45 | }, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "I'm special!\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "m = MyClass()" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": { 63 | "collapsed": false, 64 | "scrolled": true 65 | }, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "I'm public!\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "m.public()" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "metadata": { 83 | "collapsed": false, 84 | "scrolled": true 85 | }, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "I'm private!\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "m._private()" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "注意调用方式:" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 5, 109 | "metadata": { 110 | "collapsed": false 111 | }, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "I'm really special!\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "m._MyClass__really_special()" 123 | ] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 2", 129 | "language": "python", 130 | "name": "python2" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 2 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython2", 142 | "version": "2.7.10" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 0 147 | } 148 | -------------------------------------------------------------------------------- /08-object-oriented-programming/08.13-multiple-inheritance.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 多重继承" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "多重继承,指的是一个类别可以同时从多于一个父类继承行为与特征的功能,`Python` 是支持多重继承的:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "class Leaf(object):\n", 26 | " def __init__(self, color='green'):\n", 27 | " self.color = color\n", 28 | "\n", 29 | "class ColorChangingLeaf(Leaf):\n", 30 | " def change(self, new_color='brown'):\n", 31 | " self.color = new_color\n", 32 | "\n", 33 | "class DeciduousLeaf(Leaf):\n", 34 | " def fall(self):\n", 35 | " print \"Plunk!\"\n", 36 | "\n", 37 | "class MapleLeaf(ColorChangingLeaf, DeciduousLeaf):\n", 38 | " pass" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "在上面的例子中, `MapleLeaf` 就使用了多重继承,它可以使用两个父类的方法:" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 2, 51 | "metadata": { 52 | "collapsed": false 53 | }, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "yellow\n", 60 | "Plunk!\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "leaf = MapleLeaf()\n", 66 | "\n", 67 | "leaf.change(\"yellow\")\n", 68 | "print leaf.color\n", 69 | "\n", 70 | "leaf.fall()" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "如果同时实现了不同的接口,那么,最后使用的方法以继承的顺序为准,放在前面的优先继承:" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": { 84 | "collapsed": true 85 | }, 86 | "outputs": [], 87 | "source": [ 88 | "class Leaf(object):\n", 89 | " def __init__(self, color='green'):\n", 90 | " self.color = color\n", 91 | "\n", 92 | "class ColorChangingLeaf(Leaf):\n", 93 | " def change(self, new_color='brown'):\n", 94 | " self.color = new_color \n", 95 | " def fall(self):\n", 96 | " print \"Spalt!\"\n", 97 | "\n", 98 | "class DeciduousLeaf(Leaf):\n", 99 | " def fall(self):\n", 100 | " print \"Plunk!\"\n", 101 | "\n", 102 | "class MapleLeaf(ColorChangingLeaf, DeciduousLeaf):\n", 103 | " pass" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": { 110 | "collapsed": false 111 | }, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "Spalt!\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "leaf = MapleLeaf()\n", 123 | "leaf.fall()" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 5, 129 | "metadata": { 130 | "collapsed": true 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "class MapleLeaf(DeciduousLeaf, ColorChangingLeaf):\n", 135 | " pass" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 6, 141 | "metadata": { 142 | "collapsed": false 143 | }, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "Plunk!\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "leaf = MapleLeaf()\n", 155 | "leaf.fall()" 156 | ] 157 | }, 158 | { 159 | "cell_type": "markdown", 160 | "metadata": {}, 161 | "source": [ 162 | "事实上,这个顺序可以通过该类的 `__mro__` 属性或者 `mro()` 方法来查看:" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 7, 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "outputs": [ 172 | { 173 | "data": { 174 | "text/plain": [ 175 | "(__main__.MapleLeaf,\n", 176 | " __main__.DeciduousLeaf,\n", 177 | " __main__.ColorChangingLeaf,\n", 178 | " __main__.Leaf,\n", 179 | " object)" 180 | ] 181 | }, 182 | "execution_count": 7, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "MapleLeaf.__mro__" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 8, 194 | "metadata": { 195 | "collapsed": false 196 | }, 197 | "outputs": [ 198 | { 199 | "data": { 200 | "text/plain": [ 201 | "[__main__.MapleLeaf,\n", 202 | " __main__.DeciduousLeaf,\n", 203 | " __main__.ColorChangingLeaf,\n", 204 | " __main__.Leaf,\n", 205 | " object]" 206 | ] 207 | }, 208 | "execution_count": 8, 209 | "metadata": {}, 210 | "output_type": "execute_result" 211 | } 212 | ], 213 | "source": [ 214 | "MapleLeaf.mro()" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "考虑更复杂的例子:" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 9, 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "class A(object):\n", 233 | " pass\n", 234 | "\n", 235 | "class B(A):\n", 236 | " pass\n", 237 | "\n", 238 | "class C(A):\n", 239 | " pass\n", 240 | "\n", 241 | "class C1(C):\n", 242 | " pass\n", 243 | "\n", 244 | "class B1(B):\n", 245 | " pass\n", 246 | "\n", 247 | "class D(B1, C):\n", 248 | " pass" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "调用顺序:" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 10, 261 | "metadata": { 262 | "collapsed": false 263 | }, 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "[__main__.D, __main__.B1, __main__.B, __main__.C, __main__.A, object]" 269 | ] 270 | }, 271 | "execution_count": 10, 272 | "metadata": {}, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "source": [ 277 | "D.mro()" 278 | ] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 2", 284 | "language": "python", 285 | "name": "python2" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 2 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython2", 297 | "version": "2.7.10" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 0 302 | } 303 | -------------------------------------------------------------------------------- /09-theano/09.01-introduction-and-installation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Theano 简介及其安装" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# 简介" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "`Theano` 是一个 `Python` 科学计算库,允许我们进行符号运算,并在 `CPU` 和 `GPU` 上执行。\n", 22 | "\n", 23 | "它最初由 `Montreal` 大学的机器学习研究者们所开发,用来进行机器学习的计算。\n", 24 | "\n", 25 | "按照[官网](http://deeplearning.net/software/theano/)上的说明,它拥有以下几个方面的特点:\n", 26 | "\n", 27 | "- 与 **Numpy, Scipy** 的紧密结合\n", 28 | "- **GPU** 加速\n", 29 | "- 高效的符号计算\n", 30 | "- 速度和稳定性\n", 31 | "- 动态生成 **C** 代码" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "## 使用 anaconda 安装 theano" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "`windows` 下,使用 `anaconda` 安装 `theano` 的命令为:\n", 46 | "\n", 47 | " conda install mingw libpython\n", 48 | " pip install theano\n", 49 | " \n", 50 | "`linux` 下,使用 `anaconda` 安装的命令为\n", 51 | " \n", 52 | " conda install theano\n", 53 | "\n", 54 | "安装好之后,还需要安装 `Cuda` 并进行 `GPU` 环境的配置,否则是不能利用 `GPU` 进行计算的,推荐使用 `linux/mac` 进行配置,具体方法可以参考[官网](http://deeplearning.net/software/theano/)上的配置说明。\n", 55 | "\n", 56 | "查看安装的版本:" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 1, 62 | "metadata": { 63 | "collapsed": false, 64 | "scrolled": true 65 | }, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "'0.7.0.dev-54186290a97186b9c6b76317e007844529a352f4'" 71 | ] 72 | }, 73 | "execution_count": 1, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "import theano\n", 80 | "\n", 81 | "theano.__version__" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "查看当前使用的 device:" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 2, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "'cpu'" 102 | ] 103 | }, 104 | "execution_count": 2, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "theano.config.device" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "运行测试:" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 3, 123 | "metadata": { 124 | "collapsed": false 125 | }, 126 | "outputs": [ 127 | { 128 | "name": "stderr", 129 | "output_type": "stream", 130 | "text": [ 131 | "/usr/local/lib/python2.7/dist-packages/theano/misc/pycuda_init.py:34: UserWarning: PyCUDA import failed in theano.misc.pycuda_init\n", 132 | " warnings.warn(\"PyCUDA import failed in theano.misc.pycuda_init\")\n", 133 | "....................S..............." 134 | ] 135 | }, 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "Theano version 0.7.0.dev-54186290a97186b9c6b76317e007844529a352f4\n", 141 | "theano is installed in /usr/local/lib/python2.7/dist-packages/theano\n", 142 | "NumPy version 1.10.1\n", 143 | "NumPy relaxed strides checking option: True\n", 144 | "NumPy is installed in /usr/lib/python2.7/dist-packages/numpy\n", 145 | "Python version 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]\n", 146 | "nose version 1.3.7\n" 147 | ] 148 | }, 149 | { 150 | "name": "stderr", 151 | "output_type": "stream", 152 | "text": [ 153 | "\n", 154 | "----------------------------------------------------------------------\n", 155 | "Ran 37 tests in 37.919s\n", 156 | "\n", 157 | "OK (SKIP=1)\n" 158 | ] 159 | }, 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "" 164 | ] 165 | }, 166 | "execution_count": 3, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "theano.test()" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "这里我已经在本地 `Windows` 配好了 `GPU` 的设置,如果没有配好,显示的结果可能不一样。\n", 180 | "\n", 181 | "`Windows` 下第一次运行可能会显示 `DEBUG: nvcc STDOUT` 等内容,**`Just ignore it!`**" 182 | ] 183 | } 184 | ], 185 | "metadata": { 186 | "kernelspec": { 187 | "display_name": "Python 2", 188 | "language": "python", 189 | "name": "python2" 190 | }, 191 | "language_info": { 192 | "codemirror_mode": { 193 | "name": "ipython", 194 | "version": 2 195 | }, 196 | "file_extension": ".py", 197 | "mimetype": "text/x-python", 198 | "name": "python", 199 | "nbconvert_exporter": "python", 200 | "pygments_lexer": "ipython2", 201 | "version": "2.7.6" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 0 206 | } 207 | -------------------------------------------------------------------------------- /09-theano/09.06-conditions-in-theano.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Theano 条件语句" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "`theano` 中提供了两种条件语句,`ifelse` 和 `switch`,两者都是用于在符号变量上使用条件语句:\n", 15 | "\n", 16 | "- `ifelse(condition, var1, var2)`\n", 17 | " - 如果 `condition` 为 `true`,返回 `var1`,否则返回 `var2`\n", 18 | "- `switch(tensor, var1, var2)`\n", 19 | " - Elementwise `ifelse` 操作,更一般化\n", 20 | "- `switch` 会计算两个输出,而 `ifelse` 只会根据给定的条件,计算相应的输出。\n", 21 | "\n", 22 | "`ifelse` 需要从 `theano.ifelse` 中导入,而 `switch` 在 `theano.tensor` 模块中。" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 1, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [ 32 | { 33 | "name": "stderr", 34 | "output_type": "stream", 35 | "text": [ 36 | "Using gpu device 1: Tesla K10.G2.8GB (CNMeM is disabled)\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "import theano, time\n", 42 | "import theano.tensor as T\n", 43 | "import numpy as np\n", 44 | "from theano.ifelse import ifelse" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "假设我们有两个标量参数:$a, b$,和两个矩阵 $\\mathbf{x, y}$,定义函数为:\n", 52 | "\n", 53 | "$$ \n", 54 | "\\mathbf z = f(a, b,\\mathbf{x, y}) = \\left\\{ \n", 55 | "\\begin{aligned}\n", 56 | " \\mathbf x & ,\\ a <= b\\\\\n", 57 | " \\mathbf y & ,\\ a > b\n", 58 | "\\end{aligned}\n", 59 | "\\right.\n", 60 | "$$\n", 61 | "\n", 62 | "定义变量:" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 2, 68 | "metadata": { 69 | "collapsed": true 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "a, b = T.scalars('a', 'b')\n", 74 | "x, y = T.matrices('x', 'y')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "用 `ifelse` 构造,小于等于用 `T.lt()`,大于等于用 `T.gt()`:" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 3, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "z_ifelse = ifelse(T.lt(a, b), x, y)\n", 93 | "\n", 94 | "f_ifelse = theano.function([a, b, x, y], z_ifelse)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "用 `switch` 构造:" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 4, 107 | "metadata": { 108 | "collapsed": true 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "z_switch = T.switch(T.lt(a, b), x, y)\n", 113 | "\n", 114 | "f_switch = theano.function([a, b, x, y], z_switch)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "测试数据:" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 5, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "val1 = 0.\n", 133 | "val2 = 1.\n", 134 | "big_mat1 = np.ones((10000, 1000), dtype=theano.config.floatX)\n", 135 | "big_mat2 = np.ones((10000, 1000), dtype=theano.config.floatX)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | "比较两者的运行速度:" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 6, 148 | "metadata": { 149 | "collapsed": false 150 | }, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "time spent evaluating both values 0.638598 sec\n", 157 | "time spent evaluating one value 0.461249 sec\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "n_times = 10\n", 163 | "\n", 164 | "tic = time.clock()\n", 165 | "for i in xrange(n_times):\n", 166 | " f_switch(val1, val2, big_mat1, big_mat2)\n", 167 | "print 'time spent evaluating both values %f sec' % (time.clock() - tic)\n", 168 | "\n", 169 | "tic = time.clock()\n", 170 | "for i in xrange(n_times):\n", 171 | " f_ifelse(val1, val2, big_mat1, big_mat2)\n", 172 | "print 'time spent evaluating one value %f sec' % (time.clock() - tic)" 173 | ] 174 | } 175 | ], 176 | "metadata": { 177 | "kernelspec": { 178 | "display_name": "Python 2", 179 | "language": "python", 180 | "name": "python2" 181 | }, 182 | "language_info": { 183 | "codemirror_mode": { 184 | "name": "ipython", 185 | "version": 2 186 | }, 187 | "file_extension": ".py", 188 | "mimetype": "text/x-python", 189 | "name": "python", 190 | "nbconvert_exporter": "python", 191 | "pygments_lexer": "ipython2", 192 | "version": "2.7.6" 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 0 197 | } 198 | -------------------------------------------------------------------------------- /09-theano/09.12-random-streams.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Theano 随机数流变量" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stderr", 19 | "output_type": "stream", 20 | "text": [ 21 | "Using gpu device 1: Tesla C2075 (CNMeM is disabled)\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import theano\n", 27 | "import theano.tensor as T\n", 28 | "import numpy as np" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "`Theano` 的随机数变量由 `theano.sandbox.rng_mrg` 中的 `MRG_RandomStreams` 实现(`sandbox` 表示是实验代码):" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "from theano.sandbox.rng_mrg import MRG_RandomStreams" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "新建一个 `MRG_RandomStreams(seed=12345, use_cuda=None)` 实例:" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "srng = MRG_RandomStreams()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "它支持以下方法:\n", 72 | "\n", 73 | "- `normal(size, avg=0.0, std=1.0, ndim=None, dtype=None, nstreams=None)` \n", 74 | " - 产生指定形状的、服从正态分布 $N(avg, std)$ 的随机数变量,默认为标准正态分布 \n", 75 | "- `uniform(size, low=0.0, high=1.0, ndim=None, dtype=None, nstreams=None)`\n", 76 | " - 产生指定形状的、服从均匀分布 $U(low, high)$ 的随机数变量,默认为 0-1 之间的均匀分布\n", 77 | "- `binomial(size=None, n=1, p=0.5, ndim=None, dtype='int64', nstreams=None)`\n", 78 | " - 产生指定形状的、服从二项分布 $B(n,p)$ 的随机数变量\n", 79 | "- `multinomial(size=None, n=1, pvals=None, ndim=None, dtype='int64', nstreams=None)`\n", 80 | " - 产生指定形状的、服从多项分布的随机数变量\n", 81 | "\n", 82 | "与 np.random.random 不同,它产生的是随机数变量,而不是随机数数组,因此可以将 `size` 作为参数传给它:" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 4, 88 | "metadata": { 89 | "collapsed": false 90 | }, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "[ 0.10108768 -1.64354193 0.71042836 -0.77760422 0.06291872]\n", 97 | "[ 0.23193923 0.71880513 0.03122572 0.97318739 0.99260223]\n", 98 | "[0 1 0 1 1]\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "rand_size = T.vector(dtype=\"int64\")\n", 104 | "\n", 105 | "rand_normal = srng.normal(rand_size.shape)\n", 106 | "rand_uniform = srng.uniform(rand_size.shape)\n", 107 | "rand_binomial = srng.binomial(rand_size.shape)\n", 108 | "\n", 109 | "f_rand = theano.function(inputs = [rand_size], \n", 110 | " outputs = [rand_normal, rand_uniform, rand_binomial])\n", 111 | "\n", 112 | "print f_rand(range(5))[0]\n", 113 | "print f_rand(range(5))[1]\n", 114 | "print f_rand(range(5))[2]" 115 | ] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 2", 121 | "language": "python", 122 | "name": "python2" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 2 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython2", 134 | "version": "2.7.6" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 0 139 | } 140 | -------------------------------------------------------------------------------- /09-theano/09.16-tensor-indexing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Theano tensor 模块:索引" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stderr", 19 | "output_type": "stream", 20 | "text": [ 21 | "Using gpu device 1: Tesla C2075 (CNMeM is disabled)\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "import theano\n", 27 | "import theano.tensor as T\n", 28 | "import numpy as np" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## 简单索引" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "`tensor` 模块完全支持 `numpy` 中的简单索引:" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "[1 3 5 7]\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "t = T.arange(9)\n", 62 | "\n", 63 | "print t[1::2].eval()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "`numpy` 结果:" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "[1 3 5 7]\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "n = np.arange(9)\n", 90 | "\n", 91 | "print n[1::2]" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "## mask 索引" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "`tensor` 模块虽然支持简单索引,但并不支持 `mask` 索引,例如这样的做法是错误的:" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 4, 111 | "metadata": { 112 | "collapsed": false 113 | }, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "[[[0 1 2]\n", 120 | " [0 1 2]\n", 121 | " [0 1 2]]\n", 122 | "\n", 123 | " [[0 1 2]\n", 124 | " [0 1 2]\n", 125 | " [3 4 5]]\n", 126 | "\n", 127 | " [[3 4 5]\n", 128 | " [3 4 5]\n", 129 | " [3 4 5]]]\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "t = T.arange(9).reshape((3,3))\n", 135 | "\n", 136 | "print t[t > 4].eval()" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "`numpy` 中的结果:" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 5, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "[5 6 7 8]\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "n = np.arange(9).reshape((3,3))\n", 163 | "\n", 164 | "print n[n > 4]" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "要想像 `numpy` 一样得到正确结果,我们需要使用这样的方法:" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 6, 177 | "metadata": { 178 | "collapsed": false 179 | }, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "[5 6 7 8]\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "print t[(t > 4).nonzero()].eval()" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "## 使用索引进行赋值" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "`tensor` 模块不支持直接使用索引赋值,例如 `a[5] = b, a[5]+=b` 等是不允许的。\n", 205 | "\n", 206 | "不过可以考虑用 `set_subtensor` 和 `inc_subtensor` 来实现类似的功能:" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "### T.set_subtensor(x, y)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "实现类似 r[10:] = 5 的功能:" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 7, 226 | "metadata": { 227 | "collapsed": false 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "r = T.vector()\n", 232 | "\n", 233 | "new_r = T.set_subtensor(r[10:], 5)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "### T.inc_subtensor(x, y)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "实现类似 r[10:] += 5 的功能:" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 8, 253 | "metadata": { 254 | "collapsed": true 255 | }, 256 | "outputs": [], 257 | "source": [ 258 | "r = T.vector()\n", 259 | "\n", 260 | "new_r = T.inc_subtensor(r[10:], 5)" 261 | ] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Python 2", 267 | "language": "python", 268 | "name": "python2" 269 | }, 270 | "language_info": { 271 | "codemirror_mode": { 272 | "name": "ipython", 273 | "version": 2 274 | }, 275 | "file_extension": ".py", 276 | "mimetype": "text/x-python", 277 | "name": "python", 278 | "nbconvert_exporter": "python", 279 | "pygments_lexer": "ipython2", 280 | "version": "2.7.6" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 0 285 | } 286 | -------------------------------------------------------------------------------- /09-theano/09.18-tensor-nnet-.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Theano tensor 模块:nnet 子模块" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "`nnet` 是 `tensor` 模块中与神经网络 `Neural Networks` 相关的子模块。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [ 24 | { 25 | "name": "stderr", 26 | "output_type": "stream", 27 | "text": [ 28 | "Using gpu device 1: Tesla C2075 (CNMeM is disabled)\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "import theano\n", 34 | "from theano import tensor as T" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## Sigmoid 函数" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "共有三种 `sigmoid`:\n", 49 | "\n", 50 | "- `T.nnet.sigmoid(x)`\n", 51 | "- `T.nnet.ultra_sigmoid(x)`\n", 52 | "- `T.nnet.hard_sigmoid(x)`\n", 53 | "\n", 54 | "精度和时间:\n", 55 | "\n", 56 | "`sigmoid > ultra_fast_sigmoid > hard_sigmoid`\n", 57 | "\n", 58 | "函数图像:\n", 59 | "\n", 60 | "![](http://deeplearning.net/software/theano/_images/sigmoid_prec.png)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 2, 66 | "metadata": { 67 | "collapsed": false 68 | }, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "sigmoid(((W \\dot x) + b))\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "x, y, b = T.dvectors('x', 'y', 'b')\n", 80 | "W = T.dmatrix('W')\n", 81 | "y = T.nnet.sigmoid(T.dot(W, x) + b)\n", 82 | "\n", 83 | "print theano.pprint(y)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "## 其他" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "`T.nnet.softplus(x)` 返回 \n", 98 | "\n", 99 | "$$\\operatorname{softplus}(x) = \\log_e{\\left(1 + \\exp(x)\\right)}$$\n", 100 | "\n", 101 | "会解决在 1 附近自定义函数值不准的问题。" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 3, 107 | "metadata": { 108 | "collapsed": false 109 | }, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "softplus(((W \\dot x) + b))\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "x,y,b = T.dvectors('x','y','b')\n", 121 | "W = T.dmatrix('W')\n", 122 | "y = T.nnet.softplus(T.dot(W,x) + b)\n", 123 | "\n", 124 | "print theano.pprint(y)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "`T.nnet.softplus(x)` 返回 \n", 132 | "\n", 133 | "$$\n", 134 | "\\operatorname{softmax}_{ij}(x) = \\frac{\\exp{x_{ij}}}{\\sum_k\\exp(x_{ik})}\n", 135 | "$$\n", 136 | "\n", 137 | "当 `softmax` 作用到矩阵时,它会按照行进行计算。\n", 138 | "\n", 139 | "不过,下面\n", 140 | "的代码计算性能上更加稳定:\n", 141 | "\n", 142 | "```\n", 143 | "e_x = exp(x - x.max(axis=1, keepdims=True))\n", 144 | "out = e_x / e_x.sum(axis=1, keepdims=True)\n", 145 | "```" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 4, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "Softmax(((W \\dot x) + b))\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "x,y,b = T.dvectors('x','y','b')\n", 165 | "W = T.dmatrix('W')\n", 166 | "y = T.nnet.softmax(T.dot(W,x) + b)\n", 167 | "\n", 168 | "print theano.pprint(y)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "`T.nnet.relu(x, alpha=0)` 返回这样一个函数:\n", 176 | "\n", 177 | "$$\n", 178 | "f(x_i) = \\left\\{\n", 179 | "\\begin{aligned}\n", 180 | "x_i, & \\ x_i > 0 \\\\\n", 181 | "\\alpha x_i, & \\ otherwise\n", 182 | "\\end{aligned}\\right.\n", 183 | "$$" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "## 损失函数" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "metadata": {}, 196 | "source": [ 197 | "`T.nnet.binary_crossentropy(output, target)` 二类交叉熵:\n", 198 | "\n", 199 | "$$\n", 200 | "\\text{crossentropy}(t,o) = -(t\\cdot log(o) + (1 - t) \\cdot log(1 - o))\n", 201 | "$$" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 5, 207 | "metadata": { 208 | "collapsed": false 209 | }, 210 | "outputs": [], 211 | "source": [ 212 | "x, y, b, c = T.dvectors('x', 'y', 'b', 'c')\n", 213 | "W = T.dmatrix('W')\n", 214 | "V = T.dmatrix('V')\n", 215 | "h = T.nnet.sigmoid(T.dot(W, x) + b)\n", 216 | "x_recons = T.nnet.sigmoid(T.dot(V, h) + c)\n", 217 | "recon_cost = T.nnet.binary_crossentropy(x_recons, x).mean()" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "`T.nnet.categorical_crossentropy(coding_dist, true_dist)` 多类交叉熵\n", 225 | "\n", 226 | "$$\n", 227 | "H(p,q) = - \\sum_x p(x) \\log(q(x))\n", 228 | "$$" 229 | ] 230 | } 231 | ], 232 | "metadata": { 233 | "kernelspec": { 234 | "display_name": "Python 2", 235 | "language": "python", 236 | "name": "python2" 237 | }, 238 | "language_info": { 239 | "codemirror_mode": { 240 | "name": "ipython", 241 | "version": 2 242 | }, 243 | "file_extension": ".py", 244 | "mimetype": "text/x-python", 245 | "name": "python", 246 | "nbconvert_exporter": "python", 247 | "pygments_lexer": "ipython2", 248 | "version": "2.7.6" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 0 253 | } 254 | -------------------------------------------------------------------------------- /09-theano/09.19-tensor-conv.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Theano tensor 模块:conv 子模块" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "`conv` 是 `tensor` 中处理卷积神经网络的子模块。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## 卷积" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "这里只介绍二维卷积:\n", 29 | "\n", 30 | "`T.nnet.conv2d(input, filters, input_shape=None, filter_shape=None, border_mode='valid', subsample=(1, 1), filter_flip=True, image_shape=None, **kwargs)`\n", 31 | "\n", 32 | "`conv2d` 函数接受两个输入:\n", 33 | "\n", 34 | "- `4D` 张量 `input`,其形状如下:\n", 35 | " \n", 36 | " `[b, ic, i0, i1]`\n", 37 | " \n", 38 | " \n", 39 | "- `4D` 张量 `filter` ,其形状如下:\n", 40 | " \n", 41 | " `[oc, ic, f0, f1]`\n", 42 | " \n", 43 | "`border_mode` 控制输出大小:\n", 44 | "\n", 45 | "- `'valid'`:输出形状:\n", 46 | "\n", 47 | " `[b, oc, i0 - f0 + 1, i1 - f1 + 1]`\n", 48 | " \n", 49 | "- `'full'`:输出形状:\n", 50 | "\n", 51 | " `[b, oc, i0 + f0 - 1, i1 + f1 - 1]`" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "## 池化" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "池化操作:\n", 66 | "\n", 67 | "`T.signal.downsample.max_pool_2d(input, ds, ignore_border=None, st=None, padding=(0, 0), mode='max')`\n", 68 | "\n", 69 | "`input` 池化操作在其最后两维进行。\n", 70 | "\n", 71 | "`ds` 是池化区域的大小,用长度为 2 的元组表示。\n", 72 | "\n", 73 | "`ignore_border` 设为 `Ture` 时,`(5, 5)` 在 `(2, 2)` 的池化下会变成 `(2, 2)`(5 % 2 == 1,多余的 1 个被舍去了),否则是 `(3, 3)`。" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "## MNIST 卷积神经网络形状详解" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "```python\n", 88 | "def model(X, w, w2, w3, w4, p_drop_conv, p_drop_hidden):\n", 89 | " \n", 90 | " # X: 128 * 1 * 28 * 28\n", 91 | " # w: 32 * 1 * 3 * 3\n", 92 | " # full mode\n", 93 | " # l1a: 128 * 32 * (28 + 3 - 1) * (28 + 3 - 1)\n", 94 | " l1a = rectify(conv2d(X, w, border_mode='full'))\n", 95 | " # l1a: 128 * 32 * 30 * 30\n", 96 | " # ignore_border False\n", 97 | " # l1: 128 * 32 * (30 / 2) * (30 / 2)\n", 98 | " l1 = max_pool_2d(l1a, (2, 2), ignore_border=False)\n", 99 | " l1 = dropout(l1, p_drop_conv)\n", 100 | "\n", 101 | " # l1: 128 * 32 * 15 * 15\n", 102 | " # w2: 64 * 32 * 3 * 3\n", 103 | " # valid mode\n", 104 | " # l2a: 128 * 64 * (15 - 3 + 1) * (15 - 3 + 1)\n", 105 | " l2a = rectify(conv2d(l1, w2)) \n", 106 | " # l2a: 128 * 64 * 13 * 13\n", 107 | " # l2: 128 * 64 * (13 / 2 + 1) * (13 / 2 + 1)\n", 108 | " l2 = max_pool_2d(l2a, (2, 2), ignore_border=False)\n", 109 | " l2 = dropout(l2, p_drop_conv)\n", 110 | "\n", 111 | " # l2: 128 * 64 * 7 * 7\n", 112 | " # w3: 128 * 64 * 3 * 3\n", 113 | " # l3a: 128 * 128 * (7 - 3 + 1) * (7 - 3 + 1)\n", 114 | " l3a = rectify(conv2d(l2, w3))\n", 115 | " # l3a: 128 * 128 * 5 * 5\n", 116 | " # l3b: 128 * 128 * (5 / 2 + 1) * (5 / 2 + 1)\n", 117 | " l3b = max_pool_2d(l3a, (2, 2), ignore_border=False) \n", 118 | " # l3b: 128 * 128 * 3 * 3\n", 119 | " # l3: 128 * (128 * 3 * 3)\n", 120 | " l3 = T.flatten(l3b, outdim=2)\n", 121 | " l3 = dropout(l3, p_drop_conv)\n", 122 | " \n", 123 | " # l3: 128 * (128 * 3 * 3)\n", 124 | " # w4: (128 * 3 * 3) * 625\n", 125 | " # l4: 128 * 625\n", 126 | " l4 = rectify(T.dot(l3, w4))\n", 127 | " l4 = dropout(l4, p_drop_hidden)\n", 128 | "\n", 129 | " # l5: 128 * 625\n", 130 | " # w5: 625 * 10\n", 131 | " # pyx: 128 * 10\n", 132 | " pyx = softmax(T.dot(l4, w_o))\n", 133 | " return l1, l2, l3, l4, pyx\n", 134 | "```" 135 | ] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 2", 141 | "language": "python", 142 | "name": "python2" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 2 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython2", 154 | "version": "2.7.6" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 0 159 | } 160 | -------------------------------------------------------------------------------- /09-theano/apply1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/09-theano/apply1.png -------------------------------------------------------------------------------- /09-theano/apply2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/09-theano/apply2.png -------------------------------------------------------------------------------- /09-theano/apply_no_opti.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/09-theano/apply_no_opti.png -------------------------------------------------------------------------------- /09-theano/apply_opti.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/09-theano/apply_opti.png -------------------------------------------------------------------------------- /09-theano/download_mnist.py: -------------------------------------------------------------------------------- 1 | import os 2 | import os.path 3 | import urllib 4 | import gzip 5 | import shutil 6 | 7 | if not os.path.exists('mnist'): 8 | os.mkdir('mnist') 9 | 10 | def download_and_gzip(name): 11 | if not os.path.exists(name + '.gz'): 12 | urllib.urlretrieve('http://yann.lecun.com/exdb/' + name + '.gz', name + '.gz') 13 | if not os.path.exists(name): 14 | with gzip.open(name + '.gz', 'rb') as f_in, open(name, 'wb') as f_out: 15 | shutil.copyfileobj(f_in, f_out) 16 | 17 | download_and_gzip('mnist/train-images-idx3-ubyte') 18 | download_and_gzip('mnist/train-labels-idx1-ubyte') 19 | download_and_gzip('mnist/t10k-images-idx3-ubyte') 20 | download_and_gzip('mnist/t10k-labels-idx1-ubyte') -------------------------------------------------------------------------------- /09-theano/load.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | 4 | datasets_dir = './' 5 | 6 | def one_hot(x,n): 7 | if type(x) == list: 8 | x = np.array(x) 9 | x = x.flatten() 10 | o_h = np.zeros((len(x),n)) 11 | o_h[np.arange(len(x)),x] = 1 12 | return o_h 13 | 14 | def mnist(ntrain=60000,ntest=10000,onehot=True): 15 | data_dir = os.path.join(datasets_dir,'mnist/') 16 | fd = open(os.path.join(data_dir,'train-images-idx3-ubyte')) 17 | loaded = np.fromfile(file=fd,dtype=np.uint8) 18 | trX = loaded[16:].reshape((60000,28*28)).astype(float) 19 | 20 | fd = open(os.path.join(data_dir,'train-labels-idx1-ubyte')) 21 | loaded = np.fromfile(file=fd,dtype=np.uint8) 22 | trY = loaded[8:].reshape((60000)) 23 | 24 | fd = open(os.path.join(data_dir,'t10k-images-idx3-ubyte')) 25 | loaded = np.fromfile(file=fd,dtype=np.uint8) 26 | teX = loaded[16:].reshape((10000,28*28)).astype(float) 27 | 28 | fd = open(os.path.join(data_dir,'t10k-labels-idx1-ubyte')) 29 | loaded = np.fromfile(file=fd,dtype=np.uint8) 30 | teY = loaded[8:].reshape((10000)) 31 | 32 | trX = trX/255. 33 | teX = teX/255. 34 | 35 | trX = trX[:ntrain] 36 | trY = trY[:ntrain] 37 | 38 | teX = teX[:ntest] 39 | teY = teY[:ntest] 40 | 41 | if onehot: 42 | trY = one_hot(trY, 10) 43 | teY = one_hot(teY, 10) 44 | else: 45 | trY = np.asarray(trY) 46 | teY = np.asarray(teY) 47 | 48 | return trX,teX,trY,teY -------------------------------------------------------------------------------- /10-something-interesting/bangs.txt: -------------------------------------------------------------------------------- 1 | 少林派 2 | 武当派 3 | 峨嵋派 4 | 华山派 5 | 昆仑派 6 | 崆峒派 7 | 逍遥派 8 | 星宿派 9 | 无量剑派 10 | 灵鹫宫 11 | 神农帮 12 | 姑苏慕容 13 | 一品堂 14 | 大理段氏 15 | 天龙寺 16 | 大轮寺 17 | 丐帮 18 | 万劫谷 19 | 曼佗山庄 20 | 聚贤庄 21 | 全真教 22 | 桃花岛 23 | 白驼山 24 | 仙霞派 25 | 铁掌帮 26 | 西藏密宗 27 | 古墓派 28 | 鹰爪门 29 | 绝情谷 30 | 万兽山庄 31 | 明教 32 | 波斯明教 33 | 天鹰教 34 | 西域少林 35 | 金刚门 36 | 巨鲸帮 37 | 海沙派 38 | 神拳门 39 | 三门帮 40 | 巫山帮 41 | 五凤刀 42 | 断魂枪 43 | 玉真观 44 | 鄱阳帮 45 | 铁剑门 46 | 五毒教 47 | 龙游帮 48 | 石梁派 49 | 青竹帮 50 | 点苍派 51 | 金龙帮 52 | 仙都派 53 | 渤海派 54 | 恶虎沟 55 | 金刀寨 56 | 雪山派 57 | 长乐帮 58 | 六合丁氏 59 | 金乌派 60 | 侠客岛 61 | 铁叉会 62 | 上清观 63 | 青龙门 64 | 快刀门 65 | 鹤笔门 66 | 万马庄 67 | 梅花拳 68 | 天地会 69 | 沐王府 70 | 神龙教 71 | 王屋派 72 | 八卦门 73 | 太极门 74 | 天龙门 75 | 五虎派 76 | 韦陀门 77 | 八仙剑 78 | 九龙派 79 | 药王门 80 | 塞北白家堡 81 | 鹰爪雁行门 82 | 华拳门 83 | 猴拳大圣门 84 | 三才剑 85 | 黑龙门 86 | 二郎拳 87 | 燕青拳 88 | 昆仑刀 89 | 醉八仙 90 | 金刚拳 91 | 鸭形门 92 | 先天拳 93 | 地堂拳 94 | 双子门 95 | 五湖门 96 | 玄指门 97 | 五台派 98 | 七青门 99 | 玉笔山庄 100 | 福建莆田少林寺 101 | 福威镖局 102 | 青城派 103 | 洛阳金刀门 104 | 五仙教 105 | 百药门 106 | 天河帮 107 | 衡山派 108 | 泰山派 109 | 嵩山派 110 | 恒山派 111 | 日月神教 112 | 白蛟帮 113 | 山东黑风会 114 | 湘西排教 115 | 红花会 116 | 血刀门 -------------------------------------------------------------------------------- /10-something-interesting/kungfu.txt: -------------------------------------------------------------------------------- 1 | 少林长拳 2 | 少林罗汉拳 3 | 少林擒拿十八打 4 | 一指禅 5 | 易筋经 6 | 大金刚掌 7 | 袈裟伏魔功 8 | 伏魔禅杖 9 | 多罗叶指 10 | 无相劫指 11 | 龙功 12 | 少林握石掌 13 | 达摩掌 14 | 大韦陀杵 15 | 少林降魔刀 16 | 少林伏虎掌 17 | 袖里乾坤 18 | 少林拦云手 19 | 少林快掌 20 | 千手如来掌 21 | 一拍二散 22 | 天竺佛指 23 | 劈空神拳 24 | 慈悲刀 25 | 大智无定指 26 | 神拳八打 27 | 韦陀棍 28 | 去烦恼指 29 | 大力金刚指 30 | 般若掌 31 | 摩诃指 32 | 寂灭抓 33 | 罗汉伏魔神功 34 | 达摩剑 35 | 如影随形腿 36 | 韦陀拳 37 | 罗汉剑 38 | 因陀罗抓 39 | 拈花指 40 | 大慈大悲千叶手 41 | 燃木刀 42 | 日月鞭 43 | 林九阳 44 | 刚伏魔圈 45 | 普渡仗 46 | 金刚护体神功 47 | 大力魔爪功 48 | 少林龙爪功 49 | 少林虎爪功 50 | 金刚禅狮子吼 51 | 躲刚拳 52 | 斩蛟拳 53 | 韦陀伏魔剑 54 | 大力金刚抓 55 | 少林降魔掌 56 | 韦陀掌 57 | 少林醉拳 58 | 少林疯魔杖 59 | 夜叉棍 60 | 取经棍 61 | 查拳 62 | 潭腿 63 | 花拳 64 | 洪门拳 65 | 燕青拳 66 | 鲁智深醉跌 67 | 中平枪 68 | 六合刀 69 | 六合拳 70 | 六合鞭 71 | 六合枪 72 | 赤尻连拳 73 | 变势掌 74 | 少林猴拳 75 | 十八罗汉阵 76 | 太极剑 77 | 太极拳 78 | 柔云剑 79 | 绕指柔剑 80 | 玄虚刀 81 | 武当长拳 82 | 武当绵掌 83 | 武当推云手 84 | 武当九阳 85 | 倚天屠龙功 86 | 纯阳无极功 87 | 太极神功 88 | 神门十三剑 89 | 虎爪绝户手 90 | 武当两仪剑 91 | 羽衣刀 92 | 奇门三才刀 93 | 四象六合刀 94 | 灵宝拳 95 | 上清拳 96 | 上清剑 97 | 武当鹤嘴劲 98 | 武当震山掌 99 | 无极玄功拳 100 | 玄玄刀 101 | 破金锤 102 | 太极刀 103 | 真武七截阵 104 | 梯云纵 105 | 狻冰功 106 | 芙蓉金针 107 | 降龙十八掌 108 | 打狗棒 109 | 逍遥拳 110 | 锁喉擒拿手 111 | 缠丝擒拿手 112 | 龙爪擒拿手 113 | 丐帮混元牌 114 | 丐帮摄心术 115 | 蟹钳功 116 | 排云双掌 117 | 打狗阵 118 | 漫天洒金针 119 | 青竹镖 120 | 北冥神功 121 | 八荒六合唯我独尊功 122 | 小无相功 123 | 抽髓掌 124 | 化功大法 125 | 天山六阳掌 126 | 天山折梅手 127 | 天山杖 128 | 三阴蜈蚣爪 129 | 天羽奇剑 130 | 如意刀 131 | 逍遥龟息功 132 | 寒袖拂穴 133 | 五斗米神功 134 | 地火功 135 | 白虹掌 136 | 周公剑 137 | 传音搜魂大法 138 | 凌波微步 139 | 月影舞步 140 | 采燕功 141 | 凭虚临风 142 | 飞星术 143 | 穿心钉 144 | 极乐刺 145 | 三笑逍遥散 146 | 无形粉 147 | 碧磷粉 148 | 铄心弹 149 | 连珠腐尸毒 150 | 段家剑 151 | 六脉神剑 152 | 一阳指 153 | 枯荣禅功 154 | 天龙佛门狮子吼 155 | 修罗刀 156 | 五罗轻烟掌 157 | 盘根错节十八斧 158 | 哀牢山三十六剑 159 | 九阴真经 160 | 先天气功 161 | 全真基剑 162 | 左右双手互搏 163 | 空明拳 164 | 同归剑 165 | 金关玉锁二十四决 166 | 三花聚顶掌 167 | 全真收筋缩骨法 168 | 太乙拳 169 | 太乙神功 170 | 太乙枪 171 | 全真排云手 172 | 全真出云手 173 | 全真鞭拳 174 | 泥鳅功 175 | 太乙元笔 176 | 昊天掌 177 | 全真鸳鸯连环腿 178 | 一剑化三清 179 | 冲天掌 180 | 九阴神抓 181 | 易筋锻骨术 182 | 大伏魔拳 183 | 阴寒箭掌 184 | 羽衣刀 185 | 天罡北斗阵 186 | 七星聚会 187 | 金雁功 188 | 飞絮功 189 | 上天梯 190 | 天罡北斗步 191 | 玉女心经 192 | 玉女剑 193 | 玉女素心剑 194 | 古墓九阴 195 | 天罗地网掌 196 | 手挥五弦 197 | 赤练神掌 198 | 金铃银索 199 | 五毒神掌 200 | 三无三不手 201 | 黯然销魂掌 202 | 拂尘功 203 | 美女拳 204 | 美女剑 205 | 天罗地网势 206 | 迎风拂柳步 207 | 玉蜂针 208 | 冰魄银针 209 | 弹指神通 210 | 落英神剑 211 | 落英神剑掌 212 | 八卦劈空掌 213 | 碧海潮生曲 214 | 兰花拂穴手 215 | 九阴白骨爪 216 | 九阴摧心掌 217 | 毒龙银鞭 218 | 玉箫剑 219 | 玉漏催银剑 220 | 旋风扫叶腿 221 | 灵鳌步 222 | 桃花燕双飞 223 | 桃花附骨针 224 | 蛤蟆功 225 | 灵蛇拳 226 | 灵蛇杖 227 | 逆转九阴 228 | 神驼雪山掌 229 | 黄沙万里鞭 230 | 白驼天龙掌 231 | 金刚穿掌 232 | 白驼疯魔杖 233 | 透骨打穴 234 | 蚯蚓功 235 | 合扇刀 236 | 瞬息千里 237 | 峨眉九阳 238 | 回风拂柳剑 239 | 回风拂柳刀 240 | 金顶绵掌 241 | 飘雪穿云掌 242 | 灭剑 243 | 绝剑 244 | 四象掌 245 | 佛光普照掌 246 | 乱披风剑 247 | 大佛掌 248 | 阴阳太极拳 249 | 鸭形掌 250 | 昆仑基剑 251 | 昆仑两仪剑 252 | 无色无声剑 253 | 迅雷剑 254 | 玉碎昆冈剑 255 | 雨打飞花剑 256 | 天山雪飘掌 257 | 清凉扇 258 | 混沌剑阵 259 | 云龙三折 260 | 雪山入门十三式 261 | 雪山基剑 262 | 金乌刀 263 | 神扑鬼跌三绝手 264 | 雪影擒拿手 265 | 踏雪无痕 266 | 血杀刀 267 | 灭仙掌 268 | 血海魔功 269 | 血刀经 270 | 夜战八方刀 271 | 批纸削腐刀 272 | 雪遁步行 273 | 九阳神功 274 | 乾坤大挪移 275 | 青翼寒冰绵掌 276 | 白眉鹰爪 277 | 千蛛万毒手 278 | 圣火令神功 279 | 蚊须针 280 | 七星针 281 | 葵花宝典 282 | 吸星大法 283 | 吸功入地 284 | 玄天指 285 | 辟邪剑谱 286 | 石鼓打穴笔 287 | 一字电剑 288 | 七弦无形剑 289 | 小十八拿 290 | 泼墨披麻剑 291 | 蓝砂手 292 | 独孤九剑 293 | 华山基剑 294 | 紫霞神功 295 | 反两仪刀 296 | 冲灵剑 297 | 华拳 298 | 华山破玉拳 299 | 华山伏虎拳 300 | 华山混元功 301 | 华山混元掌 302 | 淑女剑 303 | 抱元劲 304 | 夺命连环三仙剑 305 | 鹰蛇生死搏 306 | 铁指决 307 | 华山劈石拳 308 | 华山长拳十段锦 309 | 宁氏无双剑 310 | 养吾剑 311 | 希夷剑 312 | 华山玉女十九式 313 | 玉女金针十三剑 314 | 狂风快剑 315 | 嵩山基剑 316 | 嵩山寒冰神掌 317 | 大嵩阳神掌 318 | 托塔手 319 | 仙鹤手 320 | 大阴阳掌 321 | 九曲剑 322 | 衡山基剑 323 | 飘湘十八剑 324 | 芙蓉剑 325 | 紫盖剑 326 | 石廪剑 327 | 天柱剑 328 | 祝融剑 329 | 衡山五神剑 330 | 回风落雁三十六式 331 | 百变千幻云雾十三式 332 | 恒山基剑 333 | 万法剑 334 | 天长掌 335 | 泰山基剑 336 | 岱宗如何 337 | 泰山十八盘 338 | 五大夫剑 339 | 快活三剑 340 | 神行百变 341 | 太玄经等《侠客行》二十四式 342 | 铁掌 343 | 铁砂掌 344 | 黑煞掌 345 | 毒蛇神掌 346 | 裘氏通背拳 347 | 阴阳倒乱刀 348 | 泼水杖 349 | 水上漂 350 | 渔网阵 351 | 连城决 352 | 唐诗剑 353 | 神照经 354 | 言家拳 355 | 无影神拳 356 | 五云手 357 | 碧针清掌 358 | 摩天铁袖功 359 | 火焰刀 360 | 龙象波若功 361 | 大手印 362 | 控鹤功 363 | 五轮大法 364 | 狂风迅雷功 365 | 无上大力杵 366 | 飞掷金杵 367 | 百花错拳 368 | 天山三分剑 369 | 斗转星移 370 | 参合指 371 | 慕容剑 372 | 柳絮剑 373 | 柴刀十八路 374 | 鹤蛇八打 375 | 壁虎游身功 376 | 青城基剑 377 | 雷公轰 378 | 青字九打 379 | 城字十八破 380 | 蜀道难牌法 381 | 九宫剑 382 | 无影幻腿 383 | 青城催心掌 384 | 松风剑 385 | 风雷掌 386 | 青蜂钉 387 | 八仙剑 388 | 八仙迷踪拳 389 | 铁拐阵 390 | 天王补心针 391 | 越女剑 392 | 空空拳 393 | 朱氏分筋错骨手 394 | 地堂鞭 395 | 南山掌 396 | 南山刀 397 | 柯氏降魔杖 398 | 三才剑 399 | 八卦游身掌 400 | 八卦八阵掌 401 | 八卦刀 402 | 内八卦功 403 | 七伤拳 404 | 飞龙剑 405 | 飞龙拳 406 | 醉八仙拳 407 | 醉双剑 408 | 神门拳 409 | 青灵剑 410 | 五虎断门刀 411 | 五虎拳 412 | 伏魔剑 413 | 心意气混元功 414 | 普门杖法 415 | 天龙剑 416 | 追命毒龙锥 417 | 胡家刀 418 | 胡家拳经 419 | 飞天神行 420 | 四象步 421 | 苗家剑 422 | 辟邪剑 423 | 林家剑 424 | 翻天掌 425 | 金蛇剑 426 | 独臂刀 427 | 金蛇游身拳 428 | 金蛇擒鹤拳 429 | 金蛇锥 430 | 混元霹雳手 431 | 混元气童子功 432 | 化骨绵掌 433 | 美女三招 434 | 百胜鞭 435 | 玄冥神掌 436 | 大力鹰爪功 437 | 搏狮手 438 | 劈卦刀 439 | 青龙刀 440 | 拦路断门刀 441 | 雁行功 442 | 鸳鸯夫妻刀 443 | 田家快刀 444 | 飞沙走石十三式 445 | 五行拳 446 | 五行六合拳 447 | 夺魄鞭 448 | 夺魄指 449 | 断魂刀 450 | 野狐拳 451 | 子午透骨钉 452 | 武胜刀 453 | 沐家拳 454 | 奈何功 455 | 碧凝神爪 456 | 朱砂掌 457 | 鹤形拳 458 | 雷震剑 459 | 大洪拳 460 | 黑砂掌 461 | 醉打山门拳 462 | 快活三十掌 463 | 孟家神拳 464 | 四面刀 465 | 鸭行拳 466 | 蝎尾鞭 467 | 软红珠索 468 | 金铁蜈钩 469 | 毒蟾砂 470 | 呼延枪 471 | 呼延十八鞭 472 | 震山三十掌 473 | 丁家擒拿手 474 | 金龙鞭 475 | 江南陆家刀 476 | 冷月剑 477 | 孔雀开屏剑 478 | 释迦掷象功 479 | 岳家枪 480 | 岳家散手 481 | 杨家枪 482 | 太祖长拳 483 | 十八路齐眉棍 484 | -------------------------------------------------------------------------------- /10-something-interesting/names.txt: -------------------------------------------------------------------------------- 1 | 飞狐外传 2 | 马行空 马春花 徐铮 商宝震 何思豪 阎基 田归农 苗人凤 南仁通 补锅匠 脚夫 车夫 蒋调侯 店伴 钟兆文 钟兆英 钟兆能 南兰 苗若兰 商老太 平四 胡斐 张总管 上官铁生 王剑杰 陈禹 古若般 殷仲翔 福康安 赵半山 孙刚峰 欧阳公政 钟四嫂 钟小二 钟阿四 胖商人 瘦商人 凤南天 凤七 哈赤大师 蛇皮张 邝宝官 凤一鸣 大汉 孙伏虎 尉迟连 杨宾 中年武师 袁紫衣 刘鹤真 崔百胜 曹猛 蓝秦 易吉 王仲萍 同桌后生 张飞雄 程灵素 姜铁山 薛鹊 王铁匠 姜小铁 田青文 慕容景岳 张管家 聂钺 上官 褚轰 汪铁鹗 周铁鹤 曾铁鸥 秦耐之 姬晓峰 张九 任通武 蔡威 汤沛 无青子 海兰弼 相国夫人 黄希节 文醉翁 周隆 郭玉堂 齐伯涛 石万嗔 陈家洛 大智禅师 陈高波 安提督宗雄 桑飞虹 倪不大 倪不小 常赫志 常伯志 心砚 石双英 刘之余 童怀道 李廷豹 木文察 司徒雷 无尘道长 德布 李沅芷 余鱼同 谢不当 吕小妹 俞朝奉 王剑英 西灵道人 3 | 雪山飞狐 4 | 于管家 田青文 左书僮 右书僮 平阿四 阮士忠 刘元鹤 杜希孟 周云阳 郑三娘 宝树 苗若兰 苗人凤 范帮主 殷吉 胡一刀 胡夫人 胡斐 陶子安 陶百岁 曹云奇 琴儿 熊元献 静智大师 赛总管 5 | 连诚诀 6 | 卜垣 丁典 万震山 马大鸣 万圭 水福 水岱 血刀老祖 孙均 冯坦 平工头 刘乘风 水笙 言达平 汪啸风 张姓老者 吴坎 沈城 花铁干 陆天抒 狄云 周圻 空心菜 宝象和尚 桃红 耿天霜 高管家 铁匠 戚芳 凌退思 凌霜华 鱼贩头子 菊友 梅念笙 戚长发 鲁坤 教书先生 7 | 天龙八部 8 | 刀白凤 丁春秋 马夫人 马五德 小翠 于光豪 巴天石 不平道人 邓百川 风波恶 甘宝宝 公冶乾 木婉清 包不同 天狼子 太皇太后 王语嫣 乌老大 无崖子 云岛主 云中鹤 止清 白世镜 天山童姥 本参 本观 本相 本因 出尘子 冯阿三 古笃诚 少林老僧 过彦之 兰剑 平婆婆 石清露 石嫂 司空玄 司马林 玄慈 玄寂 玄苦 玄难 玄生 玄痛 叶二娘 左子穆 耶律莫哥 李春来 李傀儡 李秋水 刘竹庄 祁六三 乔峰 萧峰 全冠清 朴者和尚 阮星竹 许卓诚 朱丹臣 竹剑 阿碧 阿洪 阿胜 西夏宫女 阿朱 阿紫 波罗星 陈孤雁 何望海 鸠摩智 来福儿 耶律洪基 努儿海 宋长老 苏星河 苏辙 完颜阿古打 吴长风 枯荣长老 辛双清 严妈妈 余婆婆 岳老三 张全祥 单伯山 单季山 单叔山 单小山 单正 段延庆 段誉 段正淳 段正明 范禹 宗赞王子 范骅 苟读 和里布 孟师叔 华赫艮 耶律涅鲁古 耶律重元 郁光标 卓不凡 范百龄 哈大霸 姜师叔 吴光胜 梦姑 神山上人 神音 狮鼻子 室里 项长老 姚伯当 幽草 赵钱孙 黄眉和尚 哲罗星 钟灵 钟万仇 高升泰 龚光杰 贾老者 康广陵 秦红棉 容子矩 桑土公 唐光雄 奚长老 徐长老 诸保昆 崔百泉 崔绿华 符敏仪 赵洵 菊剑 梅剑 萧远山 虚竹 游骥 聋哑婆婆 游驹 游坦之 程青霜 傅思归 葛光佩 缘根 鲍千灵 智光大师 褚万里 瑞婆婆 端木元 黎夫人 慕容博 慕容复 谭公 赫连铁树 谭婆 谭青 摘星子 慧方 慧观 慧净 慧真 穆贵妃 薛慕华 吴领军 易大彪 9 | 射雕英雄传 10 | 马青雄 马钰 小沙弥 木华黎 丘处机 沈青刚 书记 一灯大师 书生 王处一 王罕 尹志平 包惜弱 冯衡 术赤 天竺僧人 孙不二 札木合 华筝 李萍 刘玄处 刘瑛姑 吕文德 完颜洪烈 乔寨主 曲三 曲傻姑 全金发 汤祖德 朱聪 陈玄风 完颜洪熙 陆乘风 陆冠英 沙通天 农夫 吴青烈 赤老温 杨康 灵智上人 杨铁心 余兆兴 张阿生 张十五 忽都虎 欧阳峰 欧阳克 拖雷 者勒米 周伯通 段天德 郭靖 郭啸天 郝大通 洪七公 侯通海 姜文 柯镇恶 枯木 南希仁 胖妇人 胖丐 胖子 哑梢公 都史 钱青健 桑昆 铁木真 盖运聪 黄蓉 黄药师 梁长老 梁子翁 梅超风 渔人 博尔忽 博尔术 程瑶迦 韩宝驹 韩小莹 鲁有脚 穆念慈 彭长老 彭连虎 童子 窝阔台 简长老 焦木和尚 简管家 裘千仞 裘千丈 瘦丐 察合台 谭处端 黎生 樵子 酸儒文人 11 | 白马啸西风 12 | 李三 霍元龙 史仲俊 陈达海 上官虹 李文秀 丁同 马家骏 苏普 苏鲁克 车尔库 阿曼 桑斯 云强盗 全强盗 宁强盗 瓦尔拉齐 13 | 鹿鼎记 14 | 九难 卫周祚 马喇 马佑 马宝 马博仁 于八 马超兴 小桂子 小玄子 玄烨 马齐 心溪 韦小宝 韦春花 毛文珠 巴泰 方怡 风际中 邓炳春 云素梅 王潭 方大洪?无根道人 五符 元义方 巴郎星 王武通 王进宝 王琪 双儿 史松 冯难敌 邝天雄 平威 白寒松 白寒枫 卢一峰 归辛树 玄真道人 司徒鹤 对喀纳 冯锡范 孙思克 归钟 归二娘 玉林 司徒伯雷 汤若望 李自成 老吴 守备 米思翰 江百胜 齐元凯 华伯斯基 西奥图三世 刘一舟 沐剑声 庄夫人 许雪亭 多隆 齐洛诺夫 祁清彪 关安基 吕留良 陈珂 李西华 吕葆中 吕毅中 行颠 庄廷龙 庄允城 陆高轩 杜立德 吴之荣 苏菲亚 陈圆圆 罕贴摩 吴大鹏 沐剑屏 吴三桂 阿济赤 阿尔尼 张淡月 苏荃 苏冈 吴六奇 李式开 李力世 陈近南 吴应熊 杨溢之 佟国纲 吴立身 张康年 张勇 张妈 吴宝宇 何惕守 劳太监 明珠 费要多罗 柳燕 图海道 杰书 郎师傅 净清 净济 林兴珠 图尔布青 林永超 柳大洪 呼巴音 昌齐 郑克爽 赵齐贤 茅十八 建宁公主 洪朝 姚春 施琅 皇甫阁 胡逸之 南怀仁 钟志灵 神照上人 洪安通 胡德第 姚必达 赵良栋 查继左 胖头陀 郝太监 徐天川 陶红英 索额图 教士 陶师傅 高里津 敖彪 高颜超 钱老本 海大富 殷锦 贾老六 笔贴式 顾炎武 夏国相 桑结 晦聪禅师 章老三 黄甫 黄金魁 崔瞎子 黄宗羲 菊芳 彭参将 葛尔丹 程维藩 温有方 温有道 舒化龙 曾柔 富春 葛通 路副将 雷一啸 瘦头陀 蕊初 瑞栋 蔡德忠 察尔珠 潘先生 澄光 澄通 澄观 澄心 澄识 樊纲 慕天颜 鳌拜 巴颜法师 行痴 15 | 笑傲江湖 16 | 卜沉 丁坚 丁勉 上官云 万大平 于人豪 于嫂 不戒和尚 长青子 仇松年 丹青生 邓八公 方人智 方生 方证 天门道人 计无施 木高峰 风清扬 丛不弃 王伯奋 王诚 王二叔 天乙道人 王夫人 王家驹 王家骏 王元霸 王仲强 白二 白熊 天松道人 乐厚 令狐冲 宁中则 平夫人 平一指 申人俊 史镖头 东方不败 史登达 司马大 田伯光 仪和 仪琳 仪清 玉玑子 玉灵道人 玉磬子 玉音子 玉钟子 左冷禅 成不忧 齐堂主 吉人通 冲虚道长 老不死 老头子 刘菁 刘芹 刘正风 米为义 农妇 成高道人 曲非烟 曲洋 任我行 英颚 西宝 向大年 向问天 陈七 陈歪嘴 迟百诚 狄镖头 狄修 杜长老 何三七 季镖头 定静师太 劳德诺 陆伯 陆大有 任盈盈 沙天江 秃笔翁 吴柏英 吴天德 辛国梁 严三星 杨莲亭 余沧海 余人彦 岳灵珊 张夫人 张金鏊 定逸 建除 林平之 林远图 林震南 罗人杰 易国梓 易师爷 易堂主 英白罗 英长老 岳不群 郑镖头 郑萼 周孤桐 费彬 封不平 洪人雄 侯人英 觉月 施戴子 施令威 闻先生 哑婆婆 钟镇 祝镖头 祖千秋 高克新 高明根 贾布 贾人达 夏老拳师 秦娟 秦伟帮 桑三娘 桃干仙 桃根仙 桃花仙 桃实仙 桃叶仙 桃枝仙 陶钧 莫大 崔镖头 黄伯流 黄国柏 黄钟公 假东方不败 梁发 绿竹翁 游迅 葛长老 震山子 黑白子 黑熊 麻衣汉子 鲁连荣 舒奇 童百熊 鲍大楚 解风 蓝凤凰 谭迪人 清虚道人 17 | 书剑恩仇录 18 | 大痴 卫春华 马善均 于万亭 大癫 马敬侠 大苦 上官毅山 万庆澜 马大挺 马真? 文泰来 心砚 天镜 木卓伦 无尘道人 元伤 元痛 元悲 方有德 王维扬 王道 贝人龙 尹章垓 冯辉 石双英 玉如意 平旺先 孙大善 龙骏 白振 天虹禅师 孙老三 孙克通 安健刚 成璜 兆惠 关明梅 李可秀 朱祖荫 李沅芷 吴国栋 迟玄 沈德潜 杨成协 余鱼同 陈家洛 张安官 玛米尔 陆菲青 孟健雄 陈正德 宋善朋 张召重 纳斯尔丁•阿凡提 言伯乾 汪浩天 阿里 宋天保 和珅 武铭 凯别兴 阿凡提妻 郑板桥 哈合台 周阿三 袁枚 范中恩 章进 周绮 忽伦大虎 和尔大 罗信 周仲英 晴画 呼音克 周英杰 赵半山 忽伦二虎 皇太后 骆冰 胡老爷 唐六爷 阎世章 阎世魁 徐天宏 忽伦三虎 顾金标 桑拉巴 袁士霄 教长 钱正伦 梅良鸣 曹司朋 忽伦四虎 曹能 常伯志 常赫志 韩文冲 韩春霖 蒋天寿 瑞芳 周大奶奶 焦文期 蒋四根 蒋士铨 褚圆 曾图南 喀丝丽 童兆和 乾隆皇帝 覃天丞 彭三春 腾一雷 瑞大林 福康安 霍青桐 德鄂 霍阿伊 戴永明 19 | 神雕侠侣 20 | 子聪 丁大全 人厨子 九死生 马钰 小棒头 大头鬼 一灯大师 马光佐 小龙女 尹志平 丘处机 王处一 王十三 公孙止 小王将军 王志坦 王惟忠 无常鬼 尹克西 天竺僧 少妇 孙婆婆 公孙绿萼 孙不二 皮清云 申志凡 冯默风 讨债鬼 史伯威 史仲猛 史叔刚 史季强 史孟龙 尼摩星 李莫愁 达尔巴 刘处玄 朱子柳 圣因师太 曲傻姑 吕文德 祁志诚 李志常 刘瑛姑 吊死鬼 百草仙 陆鼎立 陆二娘 阿根 张志光 完颜萍 陆冠英 宋德方 陈大方 觉远大师 沙通天 张君宝 张一氓 陈老丐 张二叔 陆无双 杨过 灵智上人 武三通 武敦儒 武修文 武三娘 林朝英 耶律晋 耶律燕 耶律楚材 忽必烈 丧门鬼 狗头陀 青灵子 欧阳峰 耶律齐 周伯通 金轮法王 洪凌波 柔儿 郭破虏 侯通海 宋五 俏鬼 柯镇恶 点苍渔隐 赵志敬 洪七公 郭靖 郭芙 郭襄 姬清玄 笑脸鬼 鹿清笃 崔志方 鄂尔多 萨多 黄药师 黄蓉 程遥迦 鲁有脚 彭连虎 韩无垢 童大海 韩老丐 彭长老 瘦丐 程瑛 雷猛 裘千尺 蒙哥 煞神鬼 催命鬼 蓝天和 裘千仞 赫大通 潇湘子 霍都 樊一翁 藏边大丑 藏边二丑 藏边三丑 藏边四丑 藏边五丑 21 | 侠客行 22 | 丁不三 丁不四 丁珰 龙岛主 贝海石 木岛主 王掌柜 元澄道人 王万仞 王老六 尤得胜 天虚 风良 白阿绣 白自在 大悲老人 冯振武 孙万年 司徒横 汉子 石清 白万剑 石中玉 丑脸汉子 史小翠 石破天 李四 李大元 安奉日 米横野 李万山 老李 冲虚 吕正平 齐自勉 成自学 张三 吴道通 花万紫 西门观止 邱山风 陈冲之 汪万翼 杨光 闵柔 周牧 侍剑 呼延万善 范一飞 郑光芝 封万里 柯万钧 闻万夫 胡大哥 耿万钟 展飞 梁自进 梅文馨 梅芳姑 温仁厚 谢烟客 褚万春 廖自砺?高三娘子 照虚 解文豹 黄面道人 23 | 倚天屠龙记 24 | 卜泰 丁敏尹 马法通 卫天望 卫四娘 小翠 小虹 乌旺阿普 小凤 小昭 卫璧 王难姑 元广波 邓愈 方天劳 天鸣方丈 韦一笑 云鹤 无色 王八衰 方东白 五姑 贝锦仪 无相禅师 王保保 太虚子 史镖头 史火龙 史红石 平等宝树王 叶长青 孙三毁 白龟寿 陈友谅 冷谦 汤和 朱元璋 传功长老 祁天彪 纪晓芙 朱长龄 西华子 刘敖 阳顶天 齐心宝树王 李四摧 过三拳 李天恒 刚相 朱九真 乔福 苏梦清 司徒千钟 季长老 花云 吴良 吴祯 张无忌 麦鲸 何足道 执法长老 杜百当 杨逍 辛然 妙风使 邵鹤 邵燕 吴劲草 寿南山 吴六破 张中 何太冲 孟正鸿 灵虚 宋青书 张三丰 阿二 阿三 杨不悔 杨姐姐 宋远桥 张松溪 张翠山 苏习之 麦少帮主 周芷若 郑长老 宗维侠 范遥 拨速台 周颠 空闻 空智 空性 空见 空性 周五输 郑七灭 明月 武青婴 金花婆婆 武烈 易三娘 说不得 胡青牛 泉建男 郝密 闻苍松 哈总管 觉远 赵敏 赵一伤 封坛主 贺老三 郭襄 宫九佳?欧阳牧之 姚清泉 胡青羊 俞连舟 俞岱岩 都大锦 徐达 唐洋 高老者 圆真 成昆 唐文亮 高则成 流云使 俱明宝树王 夏胄 圆业 圆心 钱二败 殷野王 殷天正 殷无禄 殷无福 殷无寿 殷离 班淑娴 殷素素 殷梨亭 莫声谷 常敬之 常遇春 常胜宝树王 渡劫 渡难 渡厄 常金鹏 清风 鹿杖客 韩林儿 掌棒龙头 彭莹玉 谢逊 蒋涛 辉月使 程坛主 韩千叶 矮老者?掌钵龙头 简捷 詹春 静空 静照 静迦 静慧 庄铮 秦老五 察罕特穆尔 鲜于通 德成 潘天耕 颜恒 鹤笔翁?薛公远 圆音 静虚师太 静玄师太 摩诃巴思 灭绝师太 25 | 碧血剑 26 | 丁游 小菊 万里风 水鉴 义生 马公子 玉真子 水云道人 王师兄 归辛树 归二娘 孙仲寿 田见秀 龙德邻 孙仲尹 木桑道长 冯同知 宁完我 石骏 史秉文 史秉光 冯难敌 冯不摧 冯不破 白脸人 安大娘 刘芳亮 吕七 杨景亭 刘培生 安剑清 多尔衮 刘宗敏 红娘子 朱安国 老王 安小慧 齐云 张朝唐 张信 张康 杨鹏举 张春九 沙老大 闵子华 吴平 沙广天 孟伯飞 张若谷 孟铸 孟铮 李岩 单铁生 陈圆圆 宋献策 应松 岑其斯 何红药 李自成 阿九 何铁手 郑起云 范文程 罗大千 罗立如 彼得 若克琳 荣彩 洞玄 侯飞文 皇太极 祖大寿 倪浩 哑巴 胡桂南 胡老三 洪胜海 秦栋 钱通四 高师弟 袁承志 夏雪宜 崔秋山 黄真 崔希敏 曹化淳 黄须人 黄二毛子 梅剑和 温正 温南扬 焦公礼 程青竹 褚红柏 董开山 温方施 温方山 温方悟 温方达 温方义 焦宛儿 温青 温仪 鲍承先 谭文理 黎刚 潘秀达 穆人清 27 | 鸳鸯刀 28 | 书僮 任飞燕 刘於义 杨夫人 花剑影 林玉龙 周威信 卓天雄 逍遥子 袁夫人 袁冠南 常长风 盖一鸣 萧半和 萧中慧 杨中慧 29 | 越女剑 30 | 风胡子 勾践 文种 西施 阿青 范蠡 薛烛 -------------------------------------------------------------------------------- /11-useful-tools/11.01-pprint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# pprint 模块:打印 Python 对象" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "`pprint` 是 pretty printer 的缩写,用来打印 Python 数据结构,与 `print` 相比,它打印出来的结构更加整齐,便于阅读。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import pprint" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "生成一个 Python 对象:" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "data = (\n", 44 | " \"this is a string\", \n", 45 | " [1, 2, 3, 4], \n", 46 | " (\"more tuples\", 1.0, 2.3, 4.5), \n", 47 | " \"this is yet another string\"\n", 48 | " )" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "使用普通的 `print` 函数:" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": { 62 | "collapsed": false 63 | }, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "('this is a string', [1, 2, 3, 4], ('more tuples', 1.0, 2.3, 4.5), 'this is yet another string')\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print data" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "使用 `pprint` 模块中的 `pprint` 函数:" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 4, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "('this is a string',\n", 96 | " [1, 2, 3, 4],\n", 97 | " ('more tuples', 1.0, 2.3, 4.5),\n", 98 | " 'this is yet another string')\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "pprint.pprint(data)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "可以看到,这样打印出来的公式更加美观。" 111 | ] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 2", 117 | "language": "python", 118 | "name": "python2" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 2 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython2", 130 | "version": "2.7.6" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 0 135 | } 136 | -------------------------------------------------------------------------------- /11-useful-tools/11.04-glob.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# glob 模块:文件模式匹配" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import glob" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "`glob` 模块提供了方便的文件模式匹配方法。\n", 26 | "\n", 27 | "例如,找到所有以 `.ipynb` 结尾的文件名:" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": { 34 | "collapsed": false 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "['11.03 json.ipynb',\n", 41 | " '11.01 pprint.ipynb',\n", 42 | " '11.02 pickle and cpickle.ipynb',\n", 43 | " '11.04 glob.ipynb']" 44 | ] 45 | }, 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "glob.glob(\"*.ipynb\")" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "`glob` 函数支持三种格式的语法:\n", 60 | "\n", 61 | "- `*` 匹配单个或多个字符\n", 62 | "- `?` 匹配任意单个字符\n", 63 | "- `[]` 匹配指定范围内的字符,如:[0-9]匹配数字。\n", 64 | "\n", 65 | "假设我们要匹配第 09 节所有的 `.ipynb` 文件:" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "['../09. theano/09.05 configuration settings and compiling modes.ipynb',\n", 79 | " '../09. theano/09.03 gpu on windows.ipynb',\n", 80 | " '../09. theano/09.07 loop with scan.ipynb',\n", 81 | " '../09. theano/09.13 modern net on mnist.ipynb',\n", 82 | " '../09. theano/09.11 net on mnist.ipynb',\n", 83 | " '../09. theano/09.09 logistic regression .ipynb',\n", 84 | " '../09. theano/09.10 softmax on mnist.ipynb',\n", 85 | " '../09. theano/09.01 introduction and installation.ipynb',\n", 86 | " '../09. theano/09.02 theano basics.ipynb',\n", 87 | " '../09. theano/09.12 random streams.ipynb',\n", 88 | " '../09. theano/09.04 graph structures.ipynb',\n", 89 | " '../09. theano/09.14 convolutional net on mnist.ipynb',\n", 90 | " '../09. theano/09.08 linear regression.ipynb',\n", 91 | " '../09. theano/09.15 tensor module.ipynb',\n", 92 | " '../09. theano/09.06 conditions in theano.ipynb']" 93 | ] 94 | }, 95 | "execution_count": 3, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "glob.glob(\"../09*/*.ipynb\")" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "匹配数字开头的文件夹名:" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 4, 114 | "metadata": { 115 | "collapsed": false 116 | }, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "['../04. scipy',\n", 122 | " '../02. python essentials',\n", 123 | " '../07. interfacing with other languages',\n", 124 | " '../11. useful tools',\n", 125 | " '../05. advanced python',\n", 126 | " '../10. something interesting',\n", 127 | " '../03. numpy',\n", 128 | " '../06. matplotlib',\n", 129 | " '../08. object-oriented programming',\n", 130 | " '../01. python tools',\n", 131 | " '../09. theano']" 132 | ] 133 | }, 134 | "execution_count": 4, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "glob.glob(\"../[0-9]*\")" 141 | ] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 2", 147 | "language": "python", 148 | "name": "python2" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 2 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython2", 160 | "version": "2.7.6" 161 | } 162 | }, 163 | "nbformat": 4, 164 | "nbformat_minor": 0 165 | } 166 | -------------------------------------------------------------------------------- /11-useful-tools/11.05-shutil.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# shutil 模块:高级文件操作" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import shutil\n", 19 | "import os" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "`shutil` 是 `Python` 中的高级文件操作模块。" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "## 复制文件" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": { 40 | "collapsed": false 41 | }, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "True\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "with open(\"test.file\", \"w\") as f:\n", 53 | " pass\n", 54 | "\n", 55 | "print \"test.file\" in os.listdir(os.curdir)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "`shutil.copy(src, dst)` 将源文件复制到目标地址:" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 3, 68 | "metadata": { 69 | "collapsed": false 70 | }, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "True\n", 77 | "True\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "shutil.copy(\"test.file\", \"test.copy.file\")\n", 83 | "\n", 84 | "print \"test.file\" in os.listdir(os.curdir)\n", 85 | "print \"test.copy.file\" in os.listdir(os.curdir)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "如果目标地址中间的文件夹不存在则会报错:" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 4, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "[Errno 2] No such file or directory: 'my_test_dir/test.copy.file'\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "try:\n", 112 | " shutil.copy(\"test.file\", \"my_test_dir/test.copy.file\")\n", 113 | "except IOError as msg:\n", 114 | " print msg" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "另外的一个函数 `shutil.copyfile(src, dst)` 与 `shutil.copy` 使用方法一致,不过只是简单复制文件的内容,并不会复制文件本身的读写可执行权限,而 `shutil.copy` 则是完全复制。" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "## 复制文件夹" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "将文件转移到 `test_dir` 文件夹:" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 5, 141 | "metadata": { 142 | "collapsed": true 143 | }, 144 | "outputs": [], 145 | "source": [ 146 | "os.renames(\"test.file\", \"test_dir/test.file\")\n", 147 | "os.renames(\"test.copy.file\", \"test_dir/test.copy.file\")" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "使用 `shutil.copytree` 来复制文件夹:" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 6, 160 | "metadata": { 161 | "collapsed": false 162 | }, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "True" 168 | ] 169 | }, 170 | "execution_count": 6, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "shutil.copytree(\"test_dir/\", \"test_dir_copy/\")\n", 177 | "\n", 178 | "\"test_dir_copy\" in os.listdir(os.curdir)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "## 删除非空文件夹" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "`os.removedirs` 不能删除非空文件夹:" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 7, 198 | "metadata": { 199 | "collapsed": false 200 | }, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "[Errno 39] Directory not empty: 'test_dir_copy'\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "try:\n", 212 | " os.removedirs(\"test_dir_copy\")\n", 213 | "except Exception as msg:\n", 214 | " print msg" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "使用 `shutil.rmtree` 来删除非空文件夹:" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 8, 227 | "metadata": { 228 | "collapsed": false 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "shutil.rmtree(\"test_dir_copy\")" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "## 移动文件夹" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "`shutil.move` 可以整体移动文件夹,与 `os.rename` 功能差不多。" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "## 产生压缩文件" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "查看支持的压缩文件格式:" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 9, 266 | "metadata": { 267 | "collapsed": false 268 | }, 269 | "outputs": [ 270 | { 271 | "data": { 272 | "text/plain": [ 273 | "[('bztar', \"bzip2'ed tar-file\"),\n", 274 | " ('gztar', \"gzip'ed tar-file\"),\n", 275 | " ('tar', 'uncompressed tar file'),\n", 276 | " ('zip', 'ZIP file')]" 277 | ] 278 | }, 279 | "execution_count": 9, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "shutil.get_archive_formats()" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "产生压缩文件:\n", 293 | "\n", 294 | "`shutil.make_archive(basename, format, root_dir)`" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 10, 300 | "metadata": { 301 | "collapsed": false 302 | }, 303 | "outputs": [ 304 | { 305 | "data": { 306 | "text/plain": [ 307 | "'/home/lijin/notes-python/11. useful tools/test_archive.zip'" 308 | ] 309 | }, 310 | "execution_count": 10, 311 | "metadata": {}, 312 | "output_type": "execute_result" 313 | } 314 | ], 315 | "source": [ 316 | "shutil.make_archive(\"test_archive\", \"zip\", \"test_dir/\")" 317 | ] 318 | }, 319 | { 320 | "cell_type": "markdown", 321 | "metadata": {}, 322 | "source": [ 323 | "清理生成的文件和文件夹:" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 11, 329 | "metadata": { 330 | "collapsed": true 331 | }, 332 | "outputs": [], 333 | "source": [ 334 | "os.remove(\"test_archive.zip\")\n", 335 | "shutil.rmtree(\"test_dir/\")" 336 | ] 337 | } 338 | ], 339 | "metadata": { 340 | "kernelspec": { 341 | "display_name": "Python 2", 342 | "language": "python", 343 | "name": "python2" 344 | }, 345 | "language_info": { 346 | "codemirror_mode": { 347 | "name": "ipython", 348 | "version": 2 349 | }, 350 | "file_extension": ".py", 351 | "mimetype": "text/x-python", 352 | "name": "python", 353 | "nbconvert_exporter": "python", 354 | "pygments_lexer": "ipython2", 355 | "version": "2.7.6" 356 | } 357 | }, 358 | "nbformat": 4, 359 | "nbformat_minor": 0 360 | } 361 | -------------------------------------------------------------------------------- /11-useful-tools/11.07-logging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# logging 模块:记录日志" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "`logging` 模块可以用来记录日志:" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import logging" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "`logging` 的日志类型有以下几种:\n", 33 | "\n", 34 | "- `logging.critical(msg)`\n", 35 | "- `logging.error(msg)`\n", 36 | "- `logging.warning(msg)`\n", 37 | "- `logging.info(msg)`\n", 38 | "- `logging.debug(msg)`\n", 39 | "\n", 40 | "级别排序为:`CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET`\n", 41 | "\n", 42 | "默认情况下,`logging` 的日志级别为 `WARNING`,只有不低于 `WARNING` 级别的日志才会显示在命令行。" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [ 52 | { 53 | "name": "stderr", 54 | "output_type": "stream", 55 | "text": [ 56 | "CRITICAL:root:This is critical message\n", 57 | "ERROR:root:This is error message\n", 58 | "WARNING:root:This is warning message\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "logging.critical('This is critical message')\n", 64 | "logging.error('This is error message')\n", 65 | "logging.warning('This is warning message')\n", 66 | "\n", 67 | "# 不会显示\n", 68 | "logging.info('This is info message')\n", 69 | "logging.debug('This is debug message')" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "可以这样修改默认的日志级别:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 3, 82 | "metadata": { 83 | "collapsed": false 84 | }, 85 | "outputs": [ 86 | { 87 | "name": "stderr", 88 | "output_type": "stream", 89 | "text": [ 90 | "INFO:root:This is info message\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "logging.root.setLevel(level=logging.INFO)\n", 96 | "\n", 97 | "logging.info('This is info message')" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "可以通过 `logging.basicConfig()` 函数来改变默认的日志显示方式:" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 4, 110 | "metadata": { 111 | "collapsed": false 112 | }, 113 | "outputs": [ 114 | { 115 | "name": "stderr", 116 | "output_type": "stream", 117 | "text": [ 118 | "CRITICAL:this program:This is critical message\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')\n", 124 | "\n", 125 | "logger = logging.getLogger(\"this program\")\n", 126 | "\n", 127 | "logger.critical('This is critical message')" 128 | ] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 2", 134 | "language": "python", 135 | "name": "python2" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 2 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython2", 147 | "version": "2.7.6" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 0 152 | } 153 | -------------------------------------------------------------------------------- /11-useful-tools/11.08-string.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# string 模块:字符串处理" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import string" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "标点符号:" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "'!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~'" 39 | ] 40 | }, 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "string.punctuation" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "source": [ 56 | "字母表:" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": { 63 | "collapsed": false, 64 | "scrolled": true 65 | }, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", 72 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "print string.letters\n", 78 | "print string.ascii_letters" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "小写和大写:" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 4, 91 | "metadata": { 92 | "collapsed": false 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "abcdefghijklmnopqrstuvwxyz\n", 100 | "abcdefghijklmnopqrstuvwxyz\n", 101 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 102 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "print string.ascii_lowercase\n", 108 | "print string.lowercase\n", 109 | "\n", 110 | "print string.ascii_uppercase\n", 111 | "print string.uppercase" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "print string.lower" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "数字:" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 6, 143 | "metadata": { 144 | "collapsed": false 145 | }, 146 | "outputs": [ 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "'0123456789'" 151 | ] 152 | }, 153 | "execution_count": 6, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "string.digits" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "16 进制数字:" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 7, 172 | "metadata": { 173 | "collapsed": false 174 | }, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "'0123456789abcdefABCDEF'" 180 | ] 181 | }, 182 | "execution_count": 7, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "string.hexdigits" 189 | ] 190 | }, 191 | { 192 | "cell_type": "markdown", 193 | "metadata": {}, 194 | "source": [ 195 | "每个单词的首字符大写:" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 8, 201 | "metadata": { 202 | "collapsed": false 203 | }, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "'This Is A Big World'" 209 | ] 210 | }, 211 | "execution_count": 8, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "string.capwords(\"this is a big world\")" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "将指定的单词放到中央:" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 9, 230 | "metadata": { 231 | "collapsed": false 232 | }, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "' test '" 238 | ] 239 | }, 240 | "execution_count": 9, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "string.center(\"test\", 20)" 247 | ] 248 | } 249 | ], 250 | "metadata": { 251 | "kernelspec": { 252 | "display_name": "Python 2", 253 | "language": "python", 254 | "name": "python2" 255 | }, 256 | "language_info": { 257 | "codemirror_mode": { 258 | "name": "ipython", 259 | "version": 2 260 | }, 261 | "file_extension": ".py", 262 | "mimetype": "text/x-python", 263 | "name": "python", 264 | "nbconvert_exporter": "python", 265 | "pygments_lexer": "ipython2", 266 | "version": "2.7.6" 267 | } 268 | }, 269 | "nbformat": 4, 270 | "nbformat_minor": 0 271 | } 272 | -------------------------------------------------------------------------------- /11-useful-tools/11.09-collections.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# collections 模块:更多数据结构" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import collections" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "## 计数器" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "可以使用 `Counter(seq)` 对序列中出现的元素个数进行统计。\n", 33 | "\n", 34 | "例如,我们可以统计一段文本中出现的单词及其出现的次数:" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": { 41 | "collapsed": false 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Counter({'two': 2, 'one': 2, 'from': 1, 'i': 1, 'tree': 1, 'three': 1, 'china': 1, 'come': 1})\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "from string import punctuation\n", 54 | "\n", 55 | "sentence = \"One, two, three, one, two, tree, I come from China.\"\n", 56 | "\n", 57 | "words_count = collections.Counter(sentence.translate(None, punctuation).lower().split())\n", 58 | "\n", 59 | "print words_count" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "## 双端队列" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "双端队列支持从队头队尾出入队:" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 3, 79 | "metadata": { 80 | "collapsed": false 81 | }, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n", 88 | "9 8 7 6 5 4 3 2 1 0\n", 89 | "deque([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])\n", 90 | "9 8 7 6 5 4 3 2 1 0\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "dq = collections.deque()\n", 96 | "\n", 97 | "for i in xrange(10):\n", 98 | " dq.append(i)\n", 99 | " \n", 100 | "print dq\n", 101 | "\n", 102 | "for i in xrange(10):\n", 103 | " print dq.pop(), \n", 104 | "\n", 105 | "print \n", 106 | "\n", 107 | "for i in xrange(10):\n", 108 | " dq.appendleft(i)\n", 109 | " \n", 110 | "print dq\n", 111 | "\n", 112 | "for i in xrange(10):\n", 113 | " print dq.popleft()," 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "与列表相比,双端队列在队头的操作更快:" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 4, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "100 loops, best of 3: 598 ns per loop\n", 135 | "100 loops, best of 3: 291 ns per loop\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "lst = []\n", 141 | "dq = collections.deque()\n", 142 | "\n", 143 | "%timeit -n100 lst.insert(0, 10)\n", 144 | "%timeit -n100 dq.appendleft(10)" 145 | ] 146 | }, 147 | { 148 | "cell_type": "markdown", 149 | "metadata": {}, 150 | "source": [ 151 | "## 有序字典" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "字典的 `key` 按顺序排列:" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 5, 164 | "metadata": { 165 | "collapsed": false 166 | }, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "Regular Dict:\n", 173 | "A 1\n", 174 | "C 3\n", 175 | "B 2\n", 176 | "Ordered Dict:\n", 177 | "A 1\n", 178 | "B 2\n", 179 | "C 3\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "items = (\n", 185 | " ('A', 1),\n", 186 | " ('B', 2),\n", 187 | " ('C', 3)\n", 188 | ")\n", 189 | "\n", 190 | "regular_dict = dict(items)\n", 191 | "ordered_dict = collections.OrderedDict(items)\n", 192 | "\n", 193 | "print 'Regular Dict:'\n", 194 | "for k, v in regular_dict.items():\n", 195 | " print k, v\n", 196 | "\n", 197 | "print 'Ordered Dict:'\n", 198 | "for k, v in ordered_dict.items():\n", 199 | " print k, v" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "## 带默认值的字典" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "metadata": {}, 212 | "source": [ 213 | "对于 `Python` 自带的词典 `d`,当 `key` 不存在的时候,调用 `d[key]` 会报错,但是 `defaultdict` 可以为这样的 `key` 提供一个指定的默认值,我们只需要在定义时提供默认值的类型即可,如果 `key` 不存在返回指定类型的默认值:" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 6, 219 | "metadata": { 220 | "collapsed": false 221 | }, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "[]\n", 228 | "0\n", 229 | "0.0\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "dd = collections.defaultdict(list)\n", 235 | "\n", 236 | "print dd[\"foo\"]\n", 237 | "\n", 238 | "dd = collections.defaultdict(int)\n", 239 | "\n", 240 | "print dd[\"foo\"]\n", 241 | "\n", 242 | "dd = collections.defaultdict(float)\n", 243 | "\n", 244 | "print dd[\"foo\"]" 245 | ] 246 | } 247 | ], 248 | "metadata": { 249 | "kernelspec": { 250 | "display_name": "Python 2", 251 | "language": "python", 252 | "name": "python2" 253 | }, 254 | "language_info": { 255 | "codemirror_mode": { 256 | "name": "ipython", 257 | "version": 2 258 | }, 259 | "file_extension": ".py", 260 | "mimetype": "text/x-python", 261 | "name": "python", 262 | "nbconvert_exporter": "python", 263 | "pygments_lexer": "ipython2", 264 | "version": "2.7.6" 265 | } 266 | }, 267 | "nbformat": 4, 268 | "nbformat_minor": 0 269 | } 270 | -------------------------------------------------------------------------------- /11-useful-tools/11.10-requests.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# requests 模块:HTTP for Human" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": true 15 | }, 16 | "outputs": [], 17 | "source": [ 18 | "import requests" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "collapsed": true 25 | }, 26 | "source": [ 27 | "Python 标准库中的 `urllib2` 模块提供了你所需要的大多数 `HTTP` 功能,但是它的 `API` 不是特别方便使用。\n", 28 | "\n", 29 | "`requests` 模块号称 `HTTP for Human`,它可以这样使用:" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": { 36 | "collapsed": false 37 | }, 38 | "outputs": [], 39 | "source": [ 40 | "r = requests.get(\"http://httpbin.org/get\")\n", 41 | "r = requests.post('http://httpbin.org/post', data = {'key':'value'})\n", 42 | "r = requests.put(\"http://httpbin.org/put\")\n", 43 | "r = requests.delete(\"http://httpbin.org/delete\")\n", 44 | "r = requests.head(\"http://httpbin.org/get\")\n", 45 | "r = requests.options(\"http://httpbin.org/get\")" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## 传入 URL 参数" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "假如我们想访问 `httpbin.org/get?key=val`,我们可以使用 `params` 传入这些参数:" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "payload = {'key1': 'value1', 'key2': 'value2'}\n", 71 | "r = requests.get(\"http://httpbin.org/get\", params=payload)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "查看 `url` :" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": { 85 | "collapsed": false 86 | }, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "http://httpbin.org/get?key2=value2&key1=value1\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "print(r.url)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "## 读取响应内容" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "`Requests` 会自动解码来自服务器的内容。大多数 `unicode` 字符集都能被无缝地解码。" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "{\"message\":\"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.\",\"documentation_url\":\"https://developer.github.com/v3/activity/events/#list-public-events\"}\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "r = requests.get('https://github.com/timeline.json')\n", 131 | "\n", 132 | "print r.text" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "查看文字编码:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 6, 145 | "metadata": { 146 | "collapsed": false 147 | }, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "'utf-8'" 153 | ] 154 | }, 155 | "execution_count": 6, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "r.encoding" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "每次改变文字编码,`text` 的内容也随之变化:" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 7, 174 | "metadata": { 175 | "collapsed": false 176 | }, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "u'{\"message\":\"Hello there, wayfaring stranger. If you\\xe2\\x80\\x99re reading this then you probably didn\\xe2\\x80\\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.\",\"documentation_url\":\"https://developer.github.com/v3/activity/events/#list-public-events\"}'" 182 | ] 183 | }, 184 | "execution_count": 7, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "r.encoding = \"ISO-8859-1\"\n", 191 | "\n", 192 | "r.text" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "`Requests` 中也有一个内置的 `JSON` 解码器处理 `JSON` 数据:" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 8, 205 | "metadata": { 206 | "collapsed": false 207 | }, 208 | "outputs": [ 209 | { 210 | "data": { 211 | "text/plain": [ 212 | "{u'documentation_url': u'https://developer.github.com/v3/activity/events/#list-public-events',\n", 213 | " u'message': u'Hello there, wayfaring stranger. If you\\xe2\\x80\\x99re reading this then you probably didn\\xe2\\x80\\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.'}" 214 | ] 215 | }, 216 | "execution_count": 8, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "r.json()" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "如果 `JSON` 解码失败, `r.json` 就会抛出一个异常。" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "## 响应状态码" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": 9, 242 | "metadata": { 243 | "collapsed": false 244 | }, 245 | "outputs": [ 246 | { 247 | "data": { 248 | "text/plain": [ 249 | "407" 250 | ] 251 | }, 252 | "execution_count": 9, 253 | "metadata": {}, 254 | "output_type": "execute_result" 255 | } 256 | ], 257 | "source": [ 258 | "r = requests.get('http://httpbin.org/get')\n", 259 | "\n", 260 | "r.status_code" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "## 响应头" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 10, 273 | "metadata": { 274 | "collapsed": false 275 | }, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/plain": [ 280 | "'text/html'" 281 | ] 282 | }, 283 | "execution_count": 10, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "r.headers['Content-Type']" 290 | ] 291 | } 292 | ], 293 | "metadata": { 294 | "kernelspec": { 295 | "display_name": "Python 2", 296 | "language": "python", 297 | "name": "python2" 298 | }, 299 | "language_info": { 300 | "codemirror_mode": { 301 | "name": "ipython", 302 | "version": 2 303 | }, 304 | "file_extension": ".py", 305 | "mimetype": "text/x-python", 306 | "name": "python", 307 | "nbconvert_exporter": "python", 308 | "pygments_lexer": "ipython2", 309 | "version": "2.7.6" 310 | } 311 | }, 312 | "nbformat": 4, 313 | "nbformat_minor": 0 314 | } 315 | -------------------------------------------------------------------------------- /generate_static_files.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # # 将笔记转化为不同的文件格式 5 | 6 | # In[ ]: 7 | 8 | 9 | import os 10 | import os.path 11 | import nbconvert 12 | import glob 13 | 14 | 15 | # 检查路径是否存在,删除旧的文件: 16 | 17 | # In[ ]: 18 | 19 | 20 | if not os.path.exists('static-files'): 21 | os.mkdir('static-files') 22 | 23 | for n in glob.glob('static-files/*/*/*'): 24 | os.remove(n) 25 | 26 | 27 | # 文件夹: 28 | 29 | # In[ ]: 30 | 31 | 32 | folders = ['01-python-tools', 33 | '02-python-essentials', 34 | '03-numpy', 35 | '04-scipy', 36 | '05-advanced-python', 37 | '06-matplotlib', 38 | '07-interfacing-with-other-languages', 39 | '08-object-oriented-programming', 40 | '09-theano', 41 | '10-something-interesting', 42 | '11-useful-tools', 43 | '12-pandas' 44 | ] 45 | 46 | 47 | # 遍历文件夹得到所有的文件名: 48 | 49 | # In[ ]: 50 | 51 | 52 | file_names = [] 53 | 54 | for folder in folders: 55 | files = sorted(os.listdir(folder)) 56 | file_names += [os.path.join(folder, file_name) for file_name in files if file_name.endswith('.ipynb')] 57 | 58 | 59 | # In[ ]: 60 | 61 | 62 | def convert_to_files(names, to_format): 63 | target_dir = os.path.join("static-files", to_format) 64 | for folder in folders: 65 | if not os.path.exists(os.path.join(target_dir, folder)): 66 | os.makedirs(os.path.join(target_dir, folder)) 67 | 68 | for file_name in names: 69 | p = nbconvert.export(nbconvert.get_exporter(to_format), file_name) 70 | with open(os.path.join(target_dir, file_name[:-6] + p[1]["output_extension"]), 'w') as f: 71 | f.write(p[0].encode("utf-8")) 72 | print file_name 73 | 74 | 75 | # 转化 HTML 文件: 76 | 77 | # In[ ]: 78 | 79 | 80 | convert_to_files(file_names, "html") 81 | 82 | 83 | # 产生新目录: 84 | 85 | # In[ ]: 86 | 87 | 88 | with open('index.md') as f: 89 | text = f.read() 90 | with open(os.path.join("static-files", "html", "README.md"), "w") as g: 91 | g.write(text.replace(".ipynb", ".html")) 92 | 93 | -------------------------------------------------------------------------------- /payment.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijin-THU/notes-python/f56440db16e57ba29944e2eb1eb5e9bcfa1abf95/payment.jpeg --------------------------------------------------------------------------------