Make notebooks compatible with both python 2 and python 3

main
Aurélien Geron 2016-02-19 12:22:42 +01:00
parent ec681a3174
commit 793c3a2574
3 changed files with 617 additions and 575 deletions

View File

@ -7,31 +7,32 @@
"# Machine Learning Notebooks\n", "# Machine Learning Notebooks\n",
"\n", "\n",
"Welcome to the Machine Learning Notebooks.\n", "Welcome to the Machine Learning Notebooks.\n",
"**This work is in progress.**\n",
"\n", "\n",
"## Tools\n", "## Tools\n",
"* [NumPy](tools_numpy.ipynb)\n", "* [NumPy](tools_numpy.ipynb)\n",
"* [Matplotlib](tools_matplotlib.ipynb)" "* [Matplotlib](tools_matplotlib.ipynb)\n",
"\n",
"**This work is in progress, more notebooks are coming soon...**"
] ]
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.11" "version": "3.5.1"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -20,7 +20,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"First we need to import the `matplotlib` library." "First let's make sure that this notebook works well in both python 2 and 3:"
] ]
}, },
{ {
@ -30,6 +30,26 @@
"collapsed": true "collapsed": true
}, },
"outputs": [], "outputs": [],
"source": [
"from __future__ import division\n",
"from __future__ import print_function\n",
"from __future__ import unicode_literals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we need to import the `matplotlib` library."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [ "source": [
"import matplotlib" "import matplotlib"
] ]
@ -43,7 +63,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 47, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -62,7 +82,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -85,7 +105,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -105,7 +125,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 6,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -125,7 +145,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 7,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -148,7 +168,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 8,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -178,7 +198,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 9,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -199,7 +219,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 10,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -221,7 +241,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 11,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -241,7 +261,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 12,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -263,7 +283,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 13,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -283,7 +303,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 14,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -308,7 +328,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 15,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -330,7 +350,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 16,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -365,7 +385,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 17,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -389,7 +409,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 18,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -423,7 +443,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 19,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -465,7 +485,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 20,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -483,7 +503,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 21,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -527,7 +547,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 22,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -559,7 +579,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 23,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -581,7 +601,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 24,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": false "scrolled": false
@ -608,7 +628,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 25,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -636,7 +656,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 26,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -660,7 +680,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 27,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -710,7 +730,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 28,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -755,7 +775,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 29,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -781,7 +801,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": 30,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -811,7 +831,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 31,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -838,7 +858,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 32,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -859,7 +879,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 33,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -880,7 +900,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 34,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -909,7 +929,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": 35,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -942,7 +962,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 35, "execution_count": 36,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -962,7 +982,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 36, "execution_count": 37,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -999,7 +1019,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 38,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -1008,7 +1028,7 @@
"import matplotlib.image as mpimg\n", "import matplotlib.image as mpimg\n",
"\n", "\n",
"img = mpimg.imread('my_square_function.png')\n", "img = mpimg.imread('my_square_function.png')\n",
"print img.shape, img.dtype" "print(img.shape, img.dtype)"
] ]
}, },
{ {
@ -1020,7 +1040,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 38, "execution_count": 39,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -1039,7 +1059,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 40,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -1059,14 +1079,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 40, "execution_count": 41,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"img = np.arange(100*100).reshape(100, 100)\n", "img = np.arange(100*100).reshape(100, 100)\n",
"print img\n", "print(img)\n",
"plt.imshow(img)\n", "plt.imshow(img)\n",
"plt.show()" "plt.show()"
] ]
@ -1080,7 +1100,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 41, "execution_count": 42,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": false "scrolled": false
@ -1100,7 +1120,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 42, "execution_count": 43,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
@ -1125,7 +1145,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": 44,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": false "scrolled": false
@ -1146,7 +1166,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 44, "execution_count": 45,
"metadata": { "metadata": {
"collapsed": true "collapsed": true
}, },
@ -1169,7 +1189,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": 46,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -1205,7 +1225,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 47,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -1227,21 +1247,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.11" "version": "3.5.1"
} }
}, },
"nbformat": 4, "nbformat": 4,

File diff suppressed because it is too large Load Diff