diff --git a/index.ipynb b/index.ipynb index a9cb620..1f27ceb 100644 --- a/index.ipynb +++ b/index.ipynb @@ -7,31 +7,32 @@ "# Machine Learning Notebooks\n", "\n", "Welcome to the Machine Learning Notebooks.\n", - "**This work is in progress.**\n", "\n", "## Tools\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": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.5.1" } }, "nbformat": 4, diff --git a/tools_matplotlib.ipynb b/tools_matplotlib.ipynb index 5538fb5..bf5d64c 100644 --- a/tools_matplotlib.ipynb +++ b/tools_matplotlib.ipynb @@ -20,7 +20,7 @@ "cell_type": "markdown", "metadata": {}, "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 }, "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": [ "import matplotlib" ] @@ -43,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -62,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -85,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true @@ -105,7 +125,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -125,7 +145,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -148,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -178,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -199,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -221,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -241,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -263,7 +283,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -283,7 +303,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": { "collapsed": false, "scrolled": true @@ -308,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": { "collapsed": false, "scrolled": true @@ -330,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "metadata": { "collapsed": false, "scrolled": true @@ -365,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -389,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": { "collapsed": false }, @@ -423,7 +443,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": { "collapsed": false, "scrolled": true @@ -465,7 +485,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -483,7 +503,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": { "collapsed": false, "scrolled": true @@ -527,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 22, "metadata": { "collapsed": false }, @@ -559,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 23, "metadata": { "collapsed": false }, @@ -581,7 +601,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 24, "metadata": { "collapsed": false, "scrolled": false @@ -608,7 +628,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 25, "metadata": { "collapsed": false }, @@ -636,7 +656,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 26, "metadata": { "collapsed": false }, @@ -660,7 +680,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 27, "metadata": { "collapsed": false, "scrolled": true @@ -710,7 +730,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 28, "metadata": { "collapsed": false }, @@ -755,7 +775,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 29, "metadata": { "collapsed": false }, @@ -781,7 +801,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 30, "metadata": { "collapsed": false, "scrolled": true @@ -811,7 +831,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 31, "metadata": { "collapsed": false }, @@ -838,7 +858,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 32, "metadata": { "collapsed": false }, @@ -859,7 +879,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 33, "metadata": { "collapsed": false }, @@ -880,7 +900,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 34, "metadata": { "collapsed": false, "scrolled": true @@ -909,7 +929,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 35, "metadata": { "collapsed": false }, @@ -942,7 +962,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 36, "metadata": { "collapsed": false }, @@ -962,7 +982,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 37, "metadata": { "collapsed": false, "scrolled": true @@ -999,7 +1019,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "metadata": { "collapsed": false }, @@ -1008,7 +1028,7 @@ "import matplotlib.image as mpimg\n", "\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", - "execution_count": 38, + "execution_count": 39, "metadata": { "collapsed": false }, @@ -1039,7 +1059,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 40, "metadata": { "collapsed": false }, @@ -1059,14 +1079,14 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = np.arange(100*100).reshape(100, 100)\n", - "print img\n", + "print(img)\n", "plt.imshow(img)\n", "plt.show()" ] @@ -1080,7 +1100,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 42, "metadata": { "collapsed": false, "scrolled": false @@ -1100,7 +1120,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 43, "metadata": { "collapsed": false, "scrolled": true @@ -1125,7 +1145,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 44, "metadata": { "collapsed": false, "scrolled": false @@ -1146,7 +1166,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 45, "metadata": { "collapsed": true }, @@ -1169,7 +1189,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 46, "metadata": { "collapsed": false }, @@ -1205,7 +1225,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 47, "metadata": { "collapsed": false }, @@ -1227,21 +1247,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.5.1" } }, "nbformat": 4, diff --git a/tools_numpy.ipynb b/tools_numpy.ipynb index 515d02f..5788905 100644 --- a/tools_numpy.ipynb +++ b/tools_numpy.ipynb @@ -8,7 +8,7 @@ "*NumPy is the fundamental library for scientific computing with Python. NumPy is centered around a powerful N-dimensional array object, and it also contains useful linear algebra, Fourier transform, and random number functions.*\n", "\n", "## Creating arrays\n", - "First let's import `numpy`:" + "First let's make sure that this notebook works both in python 2 and 3:" ] }, { @@ -18,6 +18,26 @@ "collapsed": true }, "outputs": [], + "source": [ + "from __future__ import division\n", + "from __future__ import print_function\n", + "from __future__ import unicode_literals" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's import `numpy`. Most people import it as `np`:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], "source": [ "import numpy as np" ] @@ -38,13 +58,13 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.zeros(5)" + "print(np.zeros(5))" ] }, { @@ -56,13 +76,13 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.zeros((3,4))" + "print(np.zeros((3,4)))" ] }, { @@ -83,17 +103,17 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.zeros((3,4))\n", - "print a\n", - "print \"Shape:\", a.shape\n", - "print \"Rank:\", a.ndim\n", - "print \"Size:\", a.size" + "print(a)\n", + "print(\"Shape:\", a.shape)\n", + "print(\"Rank:\", a.ndim)\n", + "print(\"Size:\", a.size)" ] }, { @@ -106,13 +126,13 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.zeros((2,3,4))" + "print(np.zeros((2,3,4)))" ] }, { @@ -125,13 +145,13 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print type(np.zeros((3,4)))" + "print(type(np.zeros((3,4))))" ] }, { @@ -146,13 +166,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.ones((3,4))" + "print(np.ones((3,4)))" ] }, { @@ -165,13 +185,13 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.full((3,4), np.pi)" + "print(np.full((3,4), np.pi))" ] }, { @@ -184,14 +204,14 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ - "print np.empty((2,3))" + "print(np.empty((2,3)))" ] }, { @@ -204,15 +224,15 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = np.array([[1,2,3,4], [10, 20, 30, 40]])\n", - "print type(b)\n", - "print b" + "print(type(b))\n", + "print(b)" ] }, { @@ -225,14 +245,14 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ - "print np.arange(1, 5)" + "print(np.arange(1, 5))" ] }, { @@ -244,13 +264,13 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.arange(1.0, 5.0)" + "print(np.arange(1.0, 5.0))" ] }, { @@ -262,13 +282,13 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.arange(1, 5, 0.5)" + "print(np.arange(1, 5, 0.5))" ] }, { @@ -280,15 +300,15 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.arange(0, 5/3.0, 1/3.0) # depending on floating point errors, the max value is 4/3.0 or 5/3.0.\n", - "print np.arange(0, 5/3.0, 0.333333333)\n", - "print np.arange(0, 5/3.0, 0.333333334)\n" + "print(np.arange(0, 5/3, 1/3)) # depending on floating point errors, the max value is 4/3 or 5/3.\n", + "print(np.arange(0, 5/3, 0.333333333))\n", + "print(np.arange(0, 5/3, 0.333333334))\n" ] }, { @@ -301,13 +321,13 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.linspace(0, 5/3.0, 6)" + "print(np.linspace(0, 5/3, 6))" ] }, { @@ -321,13 +341,13 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.random.rand(3,4)" + "print(np.random.rand(3,4))" ] }, { @@ -339,13 +359,13 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.random.randn(3,4)" + "print(np.random.randn(3,4))" ] }, { @@ -357,7 +377,7 @@ }, { "cell_type": "code", - "execution_count": 153, + "execution_count": 19, "metadata": { "collapsed": false, "scrolled": false @@ -386,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -395,7 +415,7 @@ "def my_function(z, y, x):\n", " return x * y + z\n", "\n", - "print np.fromfunction(my_function, (3, 2, 10))" + "print(np.fromfunction(my_function, (3, 2, 10)))" ] }, { @@ -428,7 +448,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": { "collapsed": false, "scrolled": true @@ -436,10 +456,10 @@ "outputs": [], "source": [ "c = np.arange(1, 5)\n", - "print c.dtype, c\n", + "print(c.dtype, c)\n", "\n", "c = np.arange(1.0, 5.0)\n", - "print c.dtype, c" + "print(c.dtype, c)" ] }, { @@ -451,21 +471,21 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d = np.arange(1, 5, dtype=np.complex64)\n", - "print d.dtype, d" + "print(d.dtype, d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Available data types include `int8`, `int16`, `int32`, `int64`, `uint8`/`16`/`32`/`64`, `float16`/`32`/`64` and `complex64`/`128`. Check out [the documentation](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html) for the full list.\n", + "Available data types include `int8`, `int16`, `int32`, `int64`, `uint8`|`16`|`32`|`64`, `float16`|`32`|`64` and `complex64`|`128`. Check out [the documentation](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html) for the full list.\n", "\n", "### `itemsize`\n", "The `itemsize` attribute returns the size (in bytes) of each item:" @@ -473,14 +493,14 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [], "source": [ "e = np.arange(1, 5, dtype=np.complex64)\n", - "print e.itemsize" + "print(e.itemsize)" ] }, { @@ -493,7 +513,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 24, "metadata": { "collapsed": false, "scrolled": false @@ -501,10 +521,10 @@ "outputs": [], "source": [ "f = np.array([[1,2],[1000, 2000]], dtype=np.int32)\n", - "print \"Array:\"\n", - "print f\n", - "print \"Raw data:\"\n", - "print [ord(item) for item in f.data]" + "print(\"Array:\")\n", + "print(f)\n", + "print(\"Raw data:\")\n", + "print(f.data.hex())" ] }, { @@ -523,19 +543,6 @@ "Changing the shape of an `ndarray` is as simple as setting its `shape` attribute. However, the array's size must remain the same." ] }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "g = np.arange(24)\n", - "print g\n", - "print \"Rank:\", g.ndim" - ] - }, { "cell_type": "code", "execution_count": 25, @@ -544,14 +551,27 @@ }, "outputs": [], "source": [ - "g.shape = (6, 4)\n", - "print g\n", - "print \"Rank:\", g.ndim" + "g = np.arange(24)\n", + "print(g)\n", + "print(\"Rank:\", g.ndim)" ] }, { "cell_type": "code", "execution_count": 26, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "g.shape = (6, 4)\n", + "print(g)\n", + "print(\"Rank:\", g.ndim)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, "metadata": { "collapsed": false, "scrolled": true @@ -559,8 +579,8 @@ "outputs": [], "source": [ "g.shape = (2, 3, 4)\n", - "print g\n", - "print \"Rank:\", g.ndim" + "print(g)\n", + "print(\"Rank:\", g.ndim)" ] }, { @@ -573,7 +593,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 28, "metadata": { "collapsed": false, "scrolled": true @@ -581,8 +601,8 @@ "outputs": [], "source": [ "g2 = g.reshape(4,6)\n", - "print g2\n", - "print \"Rank:\", g2.ndim" + "print(g2)\n", + "print(\"Rank:\", g2.ndim)" ] }, { @@ -594,14 +614,14 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [], "source": [ "g2[1, 2] = 999\n", - "print g2" + "print(g2)" ] }, { @@ -613,13 +633,13 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print g" + "print(g)" ] }, { @@ -632,14 +652,14 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [], "source": [ "g3 = g.ravel()\n", - "print g3" + "print(g3)" ] }, { @@ -647,12 +667,12 @@ "metadata": {}, "source": [ "## Arithmetic operations\n", - "All the usual arithmetic operators (`+`, `-`, `*`, `/`, `**`, etc.) can be used with `ndarray`s. They apply *elementwise*:" + "All the usual arithmetic operators (`+`, `-`, `*`, `/`, `//`, `**`, etc.) can be used with `ndarray`s. They apply *elementwise*:" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 32, "metadata": { "collapsed": false, "scrolled": false @@ -661,12 +681,13 @@ "source": [ "a = np.array([14, 23, 32, 41])\n", "b = np.array([5, 4, 3, 2])\n", - "print \"a + b =\", a + b\n", - "print \"a - b =\", a - b\n", - "print \"a * b =\", a * b\n", - "print \"a / b =\", a / b\n", - "print \"a % b =\", a % b\n", - "print \"a ** b =\", a ** b" + "print(\"a + b =\", a + b)\n", + "print(\"a - b =\", a - b)\n", + "print(\"a * b =\", a * b)\n", + "print(\"a / b =\", a / b)\n", + "print(\"a // b =\", a // b)\n", + "print(\"a % b =\", a % b)\n", + "print(\"a ** b =\", a ** b)" ] }, { @@ -697,14 +718,14 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [], "source": [ "h = np.arange(5).reshape(1, 1, 5)\n", - "print h" + "print(h)" ] }, { @@ -716,13 +737,13 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print h + [10, 20, 30, 40, 50] # same as: h + [[[10, 20, 30, 40, 50]]]" + "print(h + [10, 20, 30, 40, 50]) # same as: h + [[[10, 20, 30, 40, 50]]]" ] }, { @@ -735,14 +756,14 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [], "source": [ "k = np.arange(6).reshape(2, 3)\n", - "print k" + "print(k)" ] }, { @@ -754,13 +775,13 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print k + [[100], [200]] # same as: k + [[100, 100, 100], [200, 200, 200]]" + "print(k + [[100], [200]]) # same as: k + [[100, 100, 100], [200, 200, 200]]" ] }, { @@ -772,13 +793,13 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print k + [100, 200, 300] # after rule 1: [[100, 200, 300]], and after rule 2: [[100, 200, 300], [100, 200, 300]]" + "print(k + [100, 200, 300]) # after rule 1: [[100, 200, 300]], and after rule 2: [[100, 200, 300], [100, 200, 300]]" ] }, { @@ -790,13 +811,13 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print k + 1000 # same as: k + [[1000, 1000, 1000], [1000, 1000, 1000]]" + "print(k + 1000) # same as: k + [[1000, 1000, 1000], [1000, 1000, 1000]]" ] }, { @@ -809,16 +830,16 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [], "source": [ "try:\n", - " print k + [33, 44]\n", - "except ValueError, e:\n", - " print e" + " print(k + [33, 44])\n", + "except ValueError as e:\n", + " print(e)" ] }, { @@ -837,18 +858,6 @@ "When trying to combine arrays with different `dtype`s, NumPy will *upcast* to a type capable of handling all possible values (regardless of what the *actual* values are)." ] }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "k1 = np.arange(0, 5, dtype=np.uint8)\n", - "print k1.dtype, k1" - ] - }, { "cell_type": "code", "execution_count": 40, @@ -856,9 +865,21 @@ "collapsed": false }, "outputs": [], + "source": [ + "k1 = np.arange(0, 5, dtype=np.uint8)\n", + "print(k1.dtype, k1)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [], "source": [ "k2 = k1 + np.array([5, 6, 7, 8, 9], dtype=np.int8)\n", - "print k2.dtype, k2" + "print(k2.dtype, k2)" ] }, { @@ -870,14 +891,14 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [], "source": [ "k3 = k1 + 1.5\n", - "print k3.dtype, k3" + "print(k3.dtype, k3)" ] }, { @@ -896,14 +917,14 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [], "source": [ "m = np.array([20, -5, 30, 40])\n", - "print m < [15, 16, 35, 36]" + "print(m < [15, 16, 35, 36])" ] }, { @@ -915,13 +936,13 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print m < 25 # equivalent to m < [25, 25, 25, 25]" + "print(m < 25) # equivalent to m < [25, 25, 25, 25]" ] }, { @@ -933,13 +954,13 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print m[m < 25]" + "print(m[m < 25])" ] }, { @@ -961,15 +982,15 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.array([[-2.5, 3.1, 7], [10, 11, 12]])\n", - "print a\n", - "print \"mean =\", a.mean()" + "print(a)\n", + "print(\"mean =\", a.mean())" ] }, { @@ -983,14 +1004,14 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for func in (a.min, a.max, a.sum, a.prod, a.std, a.var):\n", - " print func.__name__, \"=\", func()" + " print(func.__name__, \"=\", func())" ] }, { @@ -1000,18 +1021,6 @@ "These functions accept an optional argument `axis` which lets you ask for the operation to be performed on elements along the given axis. For example:" ] }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c=np.arange(24).reshape(2,3,4)\n", - "print c" - ] - }, { "cell_type": "code", "execution_count": 48, @@ -1020,7 +1029,8 @@ }, "outputs": [], "source": [ - "print c.sum(axis=0) # sum across matrices" + "c=np.arange(24).reshape(2,3,4)\n", + "print(c)" ] }, { @@ -1031,7 +1041,18 @@ }, "outputs": [], "source": [ - "print c.sum(axis=1) # sum across rows" + "print(c.sum(axis=0)) # sum across matrices" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(c.sum(axis=1)) # sum across rows" ] }, { @@ -1043,14 +1064,14 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print c.sum(axis=(0,2)) # sum across matrices and columns\n", - "print 0+1+2+3 + 12+13+14+15, 4+5+6+7 + 16+17+18+19, 8+9+10+11 + 20+21+22+23" + "print(c.sum(axis=(0,2))) # sum across matrices and columns\n", + "print(0+1+2+3 + 12+13+14+15, 4+5+6+7 + 16+17+18+19, 8+9+10+11 + 20+21+22+23)" ] }, { @@ -1063,14 +1084,14 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.array([[-2.5, 3.1, 7], [10, 11, 12]])\n", - "print np.square(a)" + "print(np.square(a))" ] }, { @@ -1082,17 +1103,17 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print \"Original ndarray\"\n", - "print a\n", + "print(\"Original ndarray\")\n", + "print(a)\n", "for func in (np.abs, np.sqrt, np.exp, np.log, np.sign, np.ceil, np.modf, np.isnan, np.cos):\n", - " print \"\\n\", func.__name__\n", - " print func(a)" + " print(\"\\n\", func.__name__)\n", + " print(func(a))" ] }, { @@ -1105,7 +1126,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 54, "metadata": { "collapsed": false }, @@ -1113,18 +1134,7 @@ "source": [ "a = np.array([1, -2, 3, 4])\n", "b = np.array([2, 8, -1, 7])\n", - "print np.add(a, b) # equivalent to a + b" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print np.greater(a, b) # equivalent to a > b" + "print(np.add(a, b)) # equivalent to a + b" ] }, { @@ -1135,7 +1145,7 @@ }, "outputs": [], "source": [ - "print np.maximum(a, b)" + "print(np.greater(a, b)) # equivalent to a > b" ] }, { @@ -1146,7 +1156,18 @@ }, "outputs": [], "source": [ - "print np.copysign(a, b)" + "print(np.maximum(a, b))" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(np.copysign(a, b))" ] }, { @@ -1158,18 +1179,6 @@ "One-dimensional NumPy arrays can be accessed more or less like regular python arrays:" ] }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "a = np.array([1, 5, 3, 19, 13, 7, 3])\n", - "print a[3]" - ] - }, { "cell_type": "code", "execution_count": 58, @@ -1178,7 +1187,8 @@ }, "outputs": [], "source": [ - "print a[2:5]" + "a = np.array([1, 5, 3, 19, 13, 7, 3])\n", + "print(a[3])" ] }, { @@ -1189,7 +1199,7 @@ }, "outputs": [], "source": [ - "print a[2:-1]" + "print(a[2:5])" ] }, { @@ -1200,7 +1210,7 @@ }, "outputs": [], "source": [ - "print a[:2]" + "print(a[2:-1])" ] }, { @@ -1211,7 +1221,7 @@ }, "outputs": [], "source": [ - "print a[2::2]" + "print(a[:2])" ] }, { @@ -1222,7 +1232,18 @@ }, "outputs": [], "source": [ - "print a[::-1]" + "print(a[2::2])" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(a[::-1])" ] }, { @@ -1234,14 +1255,14 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a[3]=999\n", - "print a" + "print(a)" ] }, { @@ -1253,14 +1274,14 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a[2:5] = [997, 998, 999]\n", - "print a" + "print(a)" ] }, { @@ -1273,14 +1294,14 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a[2:5] = -1\n", - "print a" + "print(a)" ] }, { @@ -1292,7 +1313,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 67, "metadata": { "collapsed": false, "scrolled": false @@ -1301,8 +1322,8 @@ "source": [ "try:\n", " a[2:5] = [1,2,3,4,5,6] # too long\n", - "except ValueError, e:\n", - " print e" + "except ValueError as e:\n", + " print(e)" ] }, { @@ -1314,7 +1335,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 68, "metadata": { "collapsed": false }, @@ -1322,8 +1343,8 @@ "source": [ "try:\n", " del a[2:5]\n", - "except ValueError, e:\n", - " print e" + "except ValueError as e:\n", + " print(e)" ] }, { @@ -1335,7 +1356,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 69, "metadata": { "collapsed": false }, @@ -1343,19 +1364,19 @@ "source": [ "a_slice = a[2:6]\n", "a_slice[1] = 1000\n", - "print a # the original array was modified!" + "print(a) # the original array was modified!" ] }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a[3] = 2000\n", - "print a_slice # similarly, modifying the original array modifies the slice!" + "print(a_slice) # similarly, modifying the original array modifies the slice!" ] }, { @@ -1367,7 +1388,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 71, "metadata": { "collapsed": false }, @@ -1375,19 +1396,19 @@ "source": [ "another_slice = a[2:6].copy()\n", "another_slice[1] = 3000\n", - "print a # the original array is untouched" + "print(a) # the original array is untouched" ] }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a[3] = 4000\n", - "print another_slice # similary, modifying the original array does not affect the slice copy" + "print(another_slice) # similary, modifying the original array does not affect the slice copy" ] }, { @@ -1398,18 +1419,6 @@ "Multi-dimensional arrays can be accessed in a similar way by providing an index or slice for each axis, separated by commas:" ] }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b = np.arange(48).reshape(4, 12)\n", - "print b" - ] - }, { "cell_type": "code", "execution_count": 73, @@ -1418,7 +1427,8 @@ }, "outputs": [], "source": [ - "print b[1, 2] # row 1, col 2" + "b = np.arange(48).reshape(4, 12)\n", + "print(b)" ] }, { @@ -1429,7 +1439,7 @@ }, "outputs": [], "source": [ - "print b[1, :] # row 1, all columns" + "print(b[1, 2]) # row 1, col 2" ] }, { @@ -1440,7 +1450,18 @@ }, "outputs": [], "source": [ - "print b[:, 1] # all rows, column 1" + "print(b[1, :]) # row 1, all columns" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(b[:, 1]) # all rows, column 1" ] }, { @@ -1452,15 +1473,15 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 77, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ - "print b[1, :]\n", - "print b[1:2, :]" + "print(b[1, :])\n", + "print(b[1:2, :])" ] }, { @@ -1480,25 +1501,25 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 78, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ - "print b[(0,2), 2:5] # rows 0 and 2, columns 2 to 4 (5-1)" + "print(b[(0,2), 2:5]) # rows 0 and 2, columns 2 to 4 (5-1)" ] }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print b[:, (-1, 2, -1)] # all rows, columns -1 (last), 2 and -1 (again, and in this order)" + "print(b[:, (-1, 2, -1)]) # all rows, columns -1 (last), 2 and -1 (again, and in this order)" ] }, { @@ -1510,13 +1531,13 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print b[(-1, 2, -1, 2), (5, 9, 1, 9)] # returns a 1D array with b[-1, 5], b[2, 9], b[-1, 1] and b[2, 9] (again)" + "print(b[(-1, 2, -1, 2), (5, 9, 1, 9)]) # returns a 1D array with b[-1, 5], b[2, 9], b[-1, 1] and b[2, 9] (again)" ] }, { @@ -1527,18 +1548,6 @@ "Everything works just as well with higher dimensional arrays, but it's useful to look at a few examples:" ] }, - { - "cell_type": "code", - "execution_count": 80, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c = b.reshape(4,2,6)\n", - "print c" - ] - }, { "cell_type": "code", "execution_count": 81, @@ -1547,7 +1556,8 @@ }, "outputs": [], "source": [ - "print c[2, 1, 4] # matrix 2, row 1, col 4" + "c = b.reshape(4,2,6)\n", + "print(c)" ] }, { @@ -1558,7 +1568,18 @@ }, "outputs": [], "source": [ - "print c[2, :, 3] # matrix 2, all rows, col 3" + "print(c[2, 1, 4]) # matrix 2, row 1, col 4" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(c[2, :, 3]) # matrix 2, all rows, col 3" ] }, { @@ -1570,13 +1591,13 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print c[2, 1] # Return matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" + "print(c[2, 1]) # Return matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" ] }, { @@ -1587,17 +1608,6 @@ "You may also write an ellipsis (`...`) to ask that all non-specified axes be entirely included." ] }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print c[2, ...] # matrix 2, all rows, all columns. This is equivalent to c[2, :, :]" - ] - }, { "cell_type": "code", "execution_count": 85, @@ -1606,7 +1616,7 @@ }, "outputs": [], "source": [ - "print c[2, 1, ...] # matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" + "print(c[2, ...]) # matrix 2, all rows, all columns. This is equivalent to c[2, :, :]" ] }, { @@ -1617,19 +1627,30 @@ }, "outputs": [], "source": [ - "print c[2, ..., 3] # matrix 2, all rows, column 3. This is equivalent to c[2, :, 3]" + "print(c[2, 1, ...]) # matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]" ] }, { "cell_type": "code", "execution_count": 87, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(c[2, ..., 3]) # matrix 2, all rows, column 3. This is equivalent to c[2, :, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 88, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ - "print c[..., 3] # all matrices, all rows, column 3. This is equivalent to c[:, :, 3]" + "print(c[..., 3]) # all matrices, all rows, column 3. This is equivalent to c[:, :, 3]" ] }, { @@ -1640,18 +1661,6 @@ "You can also provide an `ndarray` of boolean values on one axis to specify the indices that you want to access." ] }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "b = np.arange(48).reshape(4, 12)\n", - "print b" - ] - }, { "cell_type": "code", "execution_count": 89, @@ -1660,8 +1669,8 @@ }, "outputs": [], "source": [ - "rows_on = np.array([True, False, True, False])\n", - "print b[rows_on, :] # Rows 0 and 2, all columns. Equivalent to b[(0, 2), :]" + "b = np.arange(48).reshape(4, 12)\n", + "print(b)" ] }, { @@ -1671,9 +1680,21 @@ "collapsed": false }, "outputs": [], + "source": [ + "rows_on = np.array([True, False, True, False])\n", + "print(b[rows_on, :]) # Rows 0 and 2, all columns. Equivalent to b[(0, 2), :]" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": { + "collapsed": false + }, + "outputs": [], "source": [ "cols_on = np.array([False, True, False] * 4)\n", - "print b[:, cols_on] # All rows, columns 1, 4, 7 and 10" + "print(b[:, cols_on]) # All rows, columns 1, 4, 7 and 10" ] }, { @@ -1684,17 +1705,6 @@ "You cannot use boolean indexing this way on multiple axes, but you can work around this by using the `ix_` function:" ] }, - { - "cell_type": "code", - "execution_count": 91, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print b[np.ix_(rows_on, cols_on)]" - ] - }, { "cell_type": "code", "execution_count": 92, @@ -1703,7 +1713,18 @@ }, "outputs": [], "source": [ - "print np.ix_(rows_on, cols_on)" + "print(b[np.ix_(rows_on, cols_on)])" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(np.ix_(rows_on, cols_on))" ] }, { @@ -1715,13 +1736,13 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 94, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print b[b % 3 == 1]" + "print(b[b % 3 == 1])" ] }, { @@ -1732,18 +1753,6 @@ "Iterating over `ndarray`s is very similar to iterating over regular python arrays. Note that iterating over multidimensional arrays is done with respect to the first axis." ] }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "c = np.arange(24).reshape(2, 3, 4) # A 3D array (composed of two 3x4 matrices)\n", - "print c" - ] - }, { "cell_type": "code", "execution_count": 95, @@ -1752,9 +1761,8 @@ }, "outputs": [], "source": [ - "for m in c:\n", - " print \"Item:\"\n", - " print m" + "c = np.arange(24).reshape(2, 3, 4) # A 3D array (composed of two 3x4 matrices)\n", + "print(c)" ] }, { @@ -1764,10 +1772,23 @@ "collapsed": false }, "outputs": [], + "source": [ + "for m in c:\n", + " print(\"Item:\")\n", + " print(m)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false + }, + "outputs": [], "source": [ "for i in range(len(c)): # Note that len(c) == c.shape[0]\n", - " print \"Item:\"\n", - " print c[i]" + " print(\"Item:\")\n", + " print(c[i])" ] }, { @@ -1779,14 +1800,14 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for i in c.flat:\n", - " print \"Item:\", i" + " print(\"Item:\", i)" ] }, { @@ -1799,7 +1820,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 99, "metadata": { "collapsed": false }, @@ -1808,12 +1829,12 @@ "q1 = np.full((3,4), 1.0)\n", "q2 = np.full((4,4), 2.0)\n", "q3 = np.full((3,4), 3.0)\n", - "print \"q1\"\n", - "print q1\n", - "print \"q2\"\n", - "print q2\n", - "print \"q3\"\n", - "print q3" + "print(\"q1\")\n", + "print(q1)\n", + "print(\"q2\")\n", + "print(q2)\n", + "print(\"q3\")\n", + "print(q3)" ] }, { @@ -1826,15 +1847,15 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 100, "metadata": { "collapsed": false }, "outputs": [], "source": [ "q4 = np.vstack((q1, q2, q3))\n", - "print q4\n", - "print \"shape =\", q4.shape" + "print(q4)\n", + "print(\"shape =\", q4.shape)" ] }, { @@ -1849,15 +1870,15 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 101, "metadata": { "collapsed": false }, "outputs": [], "source": [ "q5 = np.hstack((q1, q3))\n", - "print q5\n", - "print \"shape =\", q5.shape" + "print(q5)\n", + "print(\"shape =\", q5.shape)" ] }, { @@ -1869,7 +1890,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 102, "metadata": { "collapsed": false }, @@ -1877,8 +1898,8 @@ "source": [ "try:\n", " q5 = np.hstack((q1, q2, q3))\n", - "except ValueError, e:\n", - " print e" + "except ValueError as e:\n", + " print(e)" ] }, { @@ -1891,15 +1912,15 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 103, "metadata": { "collapsed": false }, "outputs": [], "source": [ "q7 = np.concatenate((q1, q2, q3), axis=0) # Equivalent to vstack\n", - "print q7\n", - "print \"shape =\", q7.shape" + "print(q7)\n", + "print(\"shape =\", q7.shape)" ] }, { @@ -1919,15 +1940,15 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 104, "metadata": { "collapsed": false }, "outputs": [], "source": [ "q8 = np.stack((q1, q3))\n", - "print q8\n", - "print \"shape =\", q8.shape" + "print(q8)\n", + "print(\"shape =\", q8.shape)" ] }, { @@ -1942,14 +1963,14 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 105, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r = np.arange(24).reshape(6,4)\n", - "print r" + "print(r)" ] }, { @@ -1961,19 +1982,19 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 106, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r1, r2, r3 = np.vsplit(r, 3)\n", - "print \"r1\"\n", - "print r1\n", - "print \"r2\"\n", - "print r2\n", - "print \"r3\"\n", - "print r3" + "print(\"r1\")\n", + "print(r1)\n", + "print(\"r2\")\n", + "print(r2)\n", + "print(\"r3\")\n", + "print(r3)" ] }, { @@ -1985,17 +2006,17 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 107, "metadata": { "collapsed": false }, "outputs": [], "source": [ "r4, r5 = np.hsplit(r, 2)\n", - "print \"r4\"\n", - "print r4\n", - "print \"r5\"\n", - "print r5" + "print(\"r4\")\n", + "print(r4)\n", + "print(\"r5\")\n", + "print(r5)" ] }, { @@ -2010,14 +2031,14 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 108, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = np.arange(24).reshape(4,2,3)\n", - "print t" + "print(t)" ] }, { @@ -2029,15 +2050,15 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 109, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t1 = t.transpose((1,2,0))\n", - "print t1\n", - "print \"shape =\", t1.shape" + "print(t1)\n", + "print(\"shape =\", t1.shape)" ] }, { @@ -2049,15 +2070,15 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 110, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t2 = t.transpose() # equivalent to t.transpose((2, 1, 0))\n", - "print t2\n", - "print \"shape =\", t2.shape" + "print(t2)\n", + "print(\"shape =\", t2.shape)" ] }, { @@ -2069,15 +2090,15 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 111, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t3 = t.swapaxes(0,1) # equivalent to t.transpose((1, 0, 2))\n", - "print t3\n", - "print \"shape =\", t3.shape" + "print(t3)\n", + "print(\"shape =\", t3.shape)" ] }, { @@ -2091,18 +2112,6 @@ "The `T` attribute is equivalent to calling `transpose()` when the rank is ≥2:" ] }, - { - "cell_type": "code", - "execution_count": 111, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "m1 = np.arange(10).reshape(2,5)\n", - "print m1" - ] - }, { "cell_type": "code", "execution_count": 112, @@ -2111,7 +2120,19 @@ }, "outputs": [], "source": [ - "print m1.T" + "m1 = np.arange(10).reshape(2,5)\n", + "print(m1)" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(m1.T)" ] }, { @@ -2121,19 +2142,6 @@ "The `T` attribute has no effect on rank 0 (empty) or rank 1 arrays:" ] }, - { - "cell_type": "code", - "execution_count": 113, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [], - "source": [ - "m2 = np.arange(5)\n", - "print m2" - ] - }, { "cell_type": "code", "execution_count": 114, @@ -2143,7 +2151,20 @@ }, "outputs": [], "source": [ - "print m2.T" + "m2 = np.arange(5)\n", + "print(m2)" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "print(m2.T)" ] }, { @@ -2153,18 +2174,6 @@ "We can get the desired transposition by first reshaping the 1D array to a single-row matrix (2D):" ] }, - { - "cell_type": "code", - "execution_count": 115, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "m2r = m2.reshape(1,5)\n", - "print m2r" - ] - }, { "cell_type": "code", "execution_count": 116, @@ -2173,7 +2182,19 @@ }, "outputs": [], "source": [ - "print m2r.T" + "m2r = m2.reshape(1,5)\n", + "print(m2r)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(m2r.T)" ] }, { @@ -2184,18 +2205,6 @@ "Let's create two matrices and execute a matrix dot product using the `dot` method." ] }, - { - "cell_type": "code", - "execution_count": 117, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "n1 = np.arange(10).reshape(2, 5)\n", - "print n1" - ] - }, { "cell_type": "code", "execution_count": 118, @@ -2204,8 +2213,8 @@ }, "outputs": [], "source": [ - "n2 = np.arange(15).reshape(5,3)\n", - "print n2" + "n1 = np.arange(10).reshape(2, 5)\n", + "print(n1)" ] }, { @@ -2216,7 +2225,19 @@ }, "outputs": [], "source": [ - "print n1.dot(n2)" + "n2 = np.arange(15).reshape(5,3)\n", + "print(n2)" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(n1.dot(n2))" ] }, { @@ -2236,7 +2257,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 121, "metadata": { "collapsed": false }, @@ -2245,18 +2266,18 @@ "import numpy.linalg as linalg\n", "\n", "m3 = np.array([[1,2,3],[5,7,11],[21,29,31]])\n", - "print m3" + "print(m3)" ] }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 122, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print linalg.inv(m3)" + "print(linalg.inv(m3))" ] }, { @@ -2268,13 +2289,13 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 123, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print linalg.pinv(m3)" + "print(linalg.pinv(m3))" ] }, { @@ -2287,13 +2308,13 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 124, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print m3.dot(linalg.inv(m3))" + "print(m3.dot(linalg.inv(m3)))" ] }, { @@ -2305,13 +2326,13 @@ }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 125, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print np.eye(3)" + "print(np.eye(3))" ] }, { @@ -2324,19 +2345,19 @@ }, { "cell_type": "code", - "execution_count": 125, + "execution_count": 126, "metadata": { "collapsed": false }, "outputs": [], "source": [ "q, r = linalg.qr(m3)\n", - "print \"q\"\n", - "print q\n", - "print \"r\"\n", - "print r\n", - "print \"q.r (equal to m3)\"\n", - "print q.dot(r)" + "print(\"q\")\n", + "print(q)\n", + "print(\"r\")\n", + "print(r)\n", + "print(\"q.r (equal to m3)\")\n", + "print(q.dot(r))" ] }, { @@ -2349,13 +2370,13 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 127, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print linalg.det(m3) # Computes the matrix determinant" + "print(linalg.det(m3)) # Computes the matrix determinant" ] }, { @@ -2368,19 +2389,19 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 128, "metadata": { "collapsed": false }, "outputs": [], "source": [ "eigenvalues, eigenvectors = linalg.eig(m3)\n", - "print \"Eigen values (λ)\"\n", - "print eigenvalues\n", - "print \"Eigen vectors (v)\"\n", - "print eigenvectors\n", - "print \"m3.v - λ*v (equals 0)\"\n", - "print m3.dot(eigenvectors) - eigenvalues * eigenvectors" + "print(\"Eigen values (λ)\")\n", + "print(eigenvalues)\n", + "print(\"Eigen vectors (v)\")\n", + "print(eigenvectors)\n", + "print(\"m3.v - λ*v (equals 0)\")\n", + "print(m3.dot(eigenvectors) - eigenvalues * eigenvectors)" ] }, { @@ -2393,19 +2414,19 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 129, "metadata": { "collapsed": false }, "outputs": [], "source": [ "m4 = np.array([[1,0,0,0,2], [0,0,3,0,0], [0,0,0,0,0], [0,2,0,0,0]])\n", - "print m4" + "print(m4)" ] }, { "cell_type": "code", - "execution_count": 129, + "execution_count": 130, "metadata": { "collapsed": false }, @@ -2414,14 +2435,14 @@ "U, S_diag, V = linalg.svd(m4)\n", "S = np.zeros((4, 5))\n", "S[np.diag_indices(4)] = S_diag\n", - "print \"U\"\n", - "print U\n", - "print \"Σ\"\n", - "print S\n", - "print \"V\"\n", - "print V\n", - "print \"U.Σ.V (equals m4)\"\n", - "print U.dot(S).dot(V) # equal to m4" + "print(\"U\")\n", + "print(U)\n", + "print(\"Σ\")\n", + "print(S)\n", + "print(\"V\")\n", + "print(V)\n", + "print(\"U.Σ.V (equals m4)\")\n", + "print(U.dot(S).dot(V)) # equal to m4" ] }, { @@ -2431,17 +2452,6 @@ "### Diagonal and trace" ] }, - { - "cell_type": "code", - "execution_count": 130, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print np.diag(m3) # the values in the diagonal of m3 (top left to bottom right)" - ] - }, { "cell_type": "code", "execution_count": 131, @@ -2450,7 +2460,18 @@ }, "outputs": [], "source": [ - "print np.trace(m3) # equivalent to np.diag(m3).sum()" + "print(np.diag(m3)) # the values in the diagonal of m3 (top left to bottom right)" + ] + }, + { + "cell_type": "code", + "execution_count": 132, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(np.trace(m3)) # equivalent to np.diag(m3).sum()" ] }, { @@ -2472,7 +2493,7 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": 133, "metadata": { "collapsed": false }, @@ -2481,7 +2502,7 @@ "coeffs = np.array([[2, 6], [5, 3]])\n", "depvars = np.array([6, -9])\n", "solution = linalg.solve(coeffs, depvars)\n", - "print solution" + "print(solution)" ] }, { @@ -2493,14 +2514,14 @@ }, { "cell_type": "code", - "execution_count": 133, + "execution_count": 134, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "print coeffs.dot(solution)\n", - "print depvars" + "print(coeffs.dot(solution))\n", + "print(depvars)" ] }, { @@ -2512,14 +2533,14 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": 135, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ - "print np.allclose(coeffs.dot(solution), depvars)" + "print(np.allclose(coeffs.dot(solution), depvars))" ] }, { @@ -2529,12 +2550,12 @@ "## Vectorization\n", "Instead of executing operations on individual array items, one at a time, your code is much more efficient if you try to stick to array operations. This is called *vectorization*. This way, you can benefit from NumPy's many optimizations.\n", "\n", - "For example, let's say we want to generate a 768x1024 array based on the formula $sin(xy/40)$. A **bad** option would be to do the math in python using nested loops:" + "For example, let's say we want to generate a 768x1024 array based on the formula $sin(xy/40.5)$. A **bad** option would be to do the math in python using nested loops:" ] }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 136, "metadata": { "collapsed": false }, @@ -2542,8 +2563,8 @@ "source": [ "import math\n", "data = np.empty((768, 1024))\n", - "for y in xrange(768):\n", - " for x in xrange(1024):\n", + "for y in range(768):\n", + " for x in range(1024):\n", " data[y, x] = math.sin(x*y/40.5) # BAD! Very inefficient." ] }, @@ -2556,7 +2577,7 @@ }, { "cell_type": "code", - "execution_count": 136, + "execution_count": 137, "metadata": { "collapsed": false }, @@ -2565,11 +2586,11 @@ "x_coords = np.arange(0, 1024) # [0, 1, 2, ..., 1023]\n", "y_coords = np.arange(0, 768) # [0, 1, 2, ..., 767]\n", "X, Y = np.meshgrid(x_coords, y_coords)\n", - "print \"X\"\n", - "print X\n", + "print(\"X\")\n", + "print(X)\n", "print\n", - "print \"Y\"\n", - "print Y" + "print(\"Y\")\n", + "print(Y)" ] }, { @@ -2583,7 +2604,7 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": 138, "metadata": { "collapsed": false }, @@ -2601,7 +2622,7 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": 139, "metadata": { "collapsed": false }, @@ -2627,7 +2648,7 @@ }, { "cell_type": "code", - "execution_count": 139, + "execution_count": 140, "metadata": { "collapsed": false, "scrolled": true @@ -2635,12 +2656,12 @@ "outputs": [], "source": [ "a = np.random.rand(2,3)\n", - "print a" + "print(a)" ] }, { "cell_type": "code", - "execution_count": 140, + "execution_count": 141, "metadata": { "collapsed": false }, @@ -2658,7 +2679,7 @@ }, { "cell_type": "code", - "execution_count": 141, + "execution_count": 142, "metadata": { "collapsed": false }, @@ -2666,7 +2687,7 @@ "source": [ "with open(\"my_array.npy\", \"rb\") as f:\n", " content = f.read()\n", - " print repr(content)" + " print(repr(content))" ] }, { @@ -2678,14 +2699,14 @@ }, { "cell_type": "code", - "execution_count": 142, + "execution_count": 143, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a_loaded = np.load(\"my_array.npy\")\n", - "print a_loaded" + "print(a_loaded)" ] }, { @@ -2698,7 +2719,7 @@ }, { "cell_type": "code", - "execution_count": 143, + "execution_count": 144, "metadata": { "collapsed": false }, @@ -2716,7 +2737,7 @@ }, { "cell_type": "code", - "execution_count": 144, + "execution_count": 145, "metadata": { "collapsed": false }, @@ -2724,7 +2745,7 @@ "source": [ "with open(\"my_array.csv\", \"rt\") as f:\n", " content = f.read()\n", - " print content" + " print(content)" ] }, { @@ -2736,7 +2757,7 @@ }, { "cell_type": "code", - "execution_count": 145, + "execution_count": 146, "metadata": { "collapsed": true }, @@ -2754,14 +2775,14 @@ }, { "cell_type": "code", - "execution_count": 146, + "execution_count": 147, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a_loaded = np.loadtxt(\"my_array.csv\", delimiter=\",\")\n", - "print a_loaded" + "print(a_loaded)" ] }, { @@ -2774,19 +2795,19 @@ }, { "cell_type": "code", - "execution_count": 147, + "execution_count": 148, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = np.arange(24, dtype=np.uint8).reshape(2, 3, 4)\n", - "print b" + "print(b)" ] }, { "cell_type": "code", - "execution_count": 148, + "execution_count": 149, "metadata": { "collapsed": true }, @@ -2804,7 +2825,7 @@ }, { "cell_type": "code", - "execution_count": 149, + "execution_count": 150, "metadata": { "collapsed": false }, @@ -2812,7 +2833,7 @@ "source": [ "with open(\"my_arrays.npz\", \"rb\") as f:\n", " content = f.read()\n", - " print repr(content)[:200], \"[...]\"" + " print(repr(content)[:200], \"[...]\")" ] }, { @@ -2824,7 +2845,7 @@ }, { "cell_type": "code", - "execution_count": 150, + "execution_count": 151, "metadata": { "collapsed": false }, @@ -2840,17 +2861,6 @@ "This is a dict-like object which loads the arrays lazily:" ] }, - { - "cell_type": "code", - "execution_count": 151, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "print my_arrays.keys()" - ] - }, { "cell_type": "code", "execution_count": 152, @@ -2859,7 +2869,18 @@ }, "outputs": [], "source": [ - "print my_arrays[\"my_a\"]" + "print(my_arrays.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": 153, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print(my_arrays[\"my_a\"])" ] }, { @@ -2873,21 +2894,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.1" } }, "nbformat": 4,