{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lesson 3 - Shape Manipulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Original Tutorial may be found [here](http://wiki.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing the shape of an array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Original Tutorial may be found [here](http://wiki.scipy.org/Tentative_NumPy_Tutorial#head-1ddf70136d56cfa02b4ce306264ed13398327f25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An array has a shape given by the number of elements along each axis:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.floor(10*np.random.random((3,4)))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 6., 5., 5.],\n", " [ 1., 6., 1., 6.],\n", " [ 0., 5., 2., 2.]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(3L, 4L)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The shape of an array can be changed with various commands:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 3., 6., 5., 5., 1., 6., 1., 6., 0., 5., 2., 2.])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel() # flatten the array" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a.shape = (6, 2)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 6.],\n", " [ 5., 5.],\n", " [ 1., 6.],\n", " [ 1., 6.],\n", " [ 0., 5.],\n", " [ 2., 2.]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 5., 1., 1., 0., 2.],\n", " [ 6., 5., 6., 6., 5., 2.]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.transpose()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `reshape` (non-mutative) method returns its argument with a modified shape, whereas the `resize` (mutative) method modifies the array itself:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 6.],\n", " [ 5., 5.],\n", " [ 1., 6.],\n", " [ 1., 6.],\n", " [ 0., 5.],\n", " [ 2., 2.]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = a.resize((2,6))" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 6., 5., 5., 1., 6.],\n", " [ 1., 6., 0., 5., 2., 2.]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the `resize` method has mutated `a`. It does not return anything. It just mutate (a side effect)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = a.reshape(3,-1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 6., 5., 5., 1., 6.],\n", " [ 1., 6., 0., 5., 2., 2.]])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 3., 6., 5., 5.],\n", " [ 1., 6., 1., 6.],\n", " [ 0., 5., 2., 2.]])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the `reshape` method does not mutate `a`. It merely return the reshaped array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stacking together different arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Original Tutorial may be found [here](http://wiki.scipy.org/Tentative_NumPy_Tutorial#head-2dbfc93288ea06a02f270f2f9e6930ffbdd78b87)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.floor(10*np.random.random((2,2)))" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 9.],\n", " [ 7., 7.]])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = np.floor(10*np.random.random((2,2)))" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 9., 5.],\n", " [ 4., 1.]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 9.],\n", " [ 7., 7.],\n", " [ 9., 5.],\n", " [ 4., 1.]])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack((a, b))" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 9., 9., 5.],\n", " [ 7., 7., 4., 1.]])" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack((a, b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function `column_stack` stacks 1D arrays as columns into a 2D array. It is equivalent to `vstack` only for 1D arrays:" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 9., 9., 5.],\n", " [ 7., 7., 4., 1.]])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.column_stack((a,b)) # With 2D arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create two 1D arrays." ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = np.array((4., 2.))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = np.array((2., 8.))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is to quickly illustrate using the `newaxis` argument to increase a dimension for the array." ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 4.],\n", " [ 2.]])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:,np.newaxis]" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 2.],\n", " [ 8.]])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[:,np.newaxis]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's `column_stack` them!" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 4., 2.],\n", " [ 2., 8.]])" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 4.],\n", " [ 2.],\n", " [ 2.],\n", " [ 8.]])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack((a[:, np.newaxis], b[:, np.newaxis])) # The behavior of vstack is different" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "let's try `concatenate`:" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 4., 2.])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 2., 8.])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 4., 2., 2., 8.])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.concatenate((a, b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sort of similar to column stack." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "let's try to create array using the `r_[]` and `c_[]`" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 0, 4])" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.r_[1:4, 0, 4]" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [2, 3],\n", " [3, 4]])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.c_[1:4, 2:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Splitting one array into serveral smaller ones" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using `hsplit`, you can split an array along its horizontal axis, either by specifying the number of equally shaped arrays to return, or by specifying the columns after which the division should occur:" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = np.floor(10*np.random.random((2,12)))" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 9., 9., 4., 3., 4., 5., 3., 8., 0., 6., 6., 8.],\n", " [ 7., 8., 9., 7., 1., 6., 8., 6., 5., 2., 6., 7.]])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a_hsplit = np.hsplit(a, 3) # Split a into 3" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9. 9. 4. 3.]\n", " [ 7. 8. 9. 7.]]\n", "\n", "[[ 4. 5. 3. 8.]\n", " [ 1. 6. 8. 6.]]\n", "\n", "[[ 0. 6. 6. 8.]\n", " [ 5. 2. 6. 7.]]\n", "\n" ] } ], "source": [ "for i in a_hsplit:\n", " print i\n", " print \"\"" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a_vsplit = np.vsplit(a,2) # Split a into 2" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9. 9. 4. 3. 4. 5. 3. 8. 0. 6. 6. 8.]]\n", "\n", "[[ 7. 8. 9. 7. 1. 6. 8. 6. 5. 2. 6. 7.]]\n", "\n" ] } ], "source": [ "for i in a_vsplit:\n", " print i\n", " print " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Recall axis 0 is out-most). Split along axis 1 (column)." ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a_as0 = np.array_split(a, 3, axis=1)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9. 9. 4. 3.]\n", " [ 7. 8. 9. 7.]]\n", "\n", "[[ 4. 5. 3. 8.]\n", " [ 1. 6. 8. 6.]]\n", "\n", "[[ 0. 6. 6. 8.]\n", " [ 5. 2. 6. 7.]]\n", "\n" ] } ], "source": [ "for i in a_as0:\n", " print i\n", " print \"\" " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Recall axis 0 is out-most). Split along axis 0 (row)." ] }, { "cell_type": "code", "execution_count": 116, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a_as1 = np.array_split(a, 2, axis=0)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9. 9. 4. 3. 4. 5. 3. 8. 0. 6. 6. 8.]]\n", "\n", "[[ 7. 8. 9. 7. 1. 6. 8. 6. 5. 2. 6. 7.]]\n", "\n" ] } ], "source": [ "for i in a_as1:\n", " print i\n", " print " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conclusion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have now completed [Lesson 3 - Shape Manipulation](http://wiki.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }