{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using `numpy`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import itertools as it\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**: Create the following 12 by 12 multiplicaiton table using `numpy`.\n", " \n", "```python\n", "array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],\n", " [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],\n", " [ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],\n", " [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],\n", " [ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],\n", " [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],\n", " [ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],\n", " [ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],\n", " [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],\n", " [ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],\n", " [ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])\n", "```" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],\n", " [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],\n", " [ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],\n", " [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],\n", " [ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],\n", " [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],\n", " [ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],\n", " [ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],\n", " [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],\n", " [ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],\n", " [ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([[x*y for x in range(1, 13)] for y in range(1,13)])" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],\n", " [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],\n", " [ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],\n", " [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],\n", " [ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],\n", " [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],\n", " [ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],\n", " [ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],\n", " [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],\n", " [ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],\n", " [ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.fromfunction(lambda i, j: (i+1)*(j+1), shape=(12,12), dtype='int')" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],\n", " [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],\n", " [ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],\n", " [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],\n", " [ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],\n", " [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],\n", " [ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],\n", " [ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],\n", " [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],\n", " [ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],\n", " [ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.outer(np.arange(1,13), np.arange(1,13))" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],\n", " [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],\n", " [ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],\n", " [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],\n", " [ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],\n", " [ 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],\n", " [ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],\n", " [ 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],\n", " [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],\n", " [ 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],\n", " [ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]])" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(1,13)[:, None] * np.arange(1, 13)[None,:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reductions and the axis argument" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.arange(12).reshape((3,4))" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum()" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([12, 15, 18, 21])" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum(axis=0)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 6, 22, 38])" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.sum(axis=1)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = np.arange(24).reshape((2,3,4))" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]],\n", "\n", " [[12, 13, 14, 15],\n", " [16, 17, 18, 19],\n", " [20, 21, 22, 23]]])" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3, 4)" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.shape" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[12, 14, 16, 18],\n", " [20, 22, 24, 26],\n", " [28, 30, 32, 34]])" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.sum(axis=0)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[12, 15, 18, 21],\n", " [48, 51, 54, 57]])" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.sum(axis=1)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 6, 22, 38],\n", " [54, 70, 86]])" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.sum(axis=2)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 66, 210])" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.sum(axis=(1,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Splitting arrays" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.arange(10)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8, 9])]" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.split(x, 5)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([0, 0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9, 9])]" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.split(np.lib.pad(x, (1,1), 'edge'), 3)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = np.arange(12).reshape((3,4))" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 119, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y1, y2 = np.split(y, 2, 1)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [4, 5],\n", " [8, 9]])" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y1" ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y1, y2, y3 = np.split(y, [1,3], 1)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2],\n", " [ 5, 6],\n", " [ 9, 10]])" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y2" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y1, y2, y3 = np.split(y, 3, 0)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 8, 9, 10, 11]])" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y3" ] }, { "cell_type": "code", "execution_count": 125, "metadata": { "collapsed": true }, "outputs": [], "source": [ "z = np.arange(24).reshape((2,3,4))" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]],\n", "\n", " [[12, 13, 14, 15],\n", " [16, 17, 18, 19],\n", " [20, 21, 22, 23]]])" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3, 4)" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z.shape" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3]\n", " [ 4 5 6 7]\n", " [ 8 9 10 11]]\n", "[[12 13 14 15]\n", " [16 17 18 19]\n", " [20 21 22 23]]\n" ] } ], "source": [ "for item in np.split(z, 2, axis=0):\n", " print(item.squeeze())" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 1 2 3]\n", " [12 13 14 15]]\n", "[[ 4 5 6 7]\n", " [16 17 18 19]]\n", "[[ 8 9 10 11]\n", " [20 21 22 23]]\n" ] } ], "source": [ "for item in np.split(z, 3, axis=1):\n", " print(item.squeeze())" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 4 8]\n", " [12 16 20]]\n", "[[ 1 5 9]\n", " [13 17 21]]\n", "[[ 2 6 10]\n", " [14 18 22]]\n", "[[ 3 7 11]\n", " [15 19 23]]\n" ] } ], "source": [ "for item in np.split(z, 4, axis=2):\n", " print(item.squeeze())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combining arrays" ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = np.ones((2,3), 'int')" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [1, 1, 1]])" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]])" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.vstack([x, x])" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1]])" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.hstack([x, x])" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1, 1],\n", " [1, 1],\n", " [1, 1]],\n", "\n", " [[1, 1],\n", " [1, 1],\n", " [1, 1]]])" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dstack([x,x])" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]])" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.concatenate([x, x], axis=0)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1]])" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.concatenate([x, x], axis=1)" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.r_[x, np.zeros((1,3))]" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1., 0.],\n", " [ 1., 1., 1., 0.]])" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.r_['-1', x, np.zeros((2,1))]" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 1]])" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.r_['0', x, x]" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 1, 1, 1, 1],\n", " [1, 1, 1, 1, 1, 1]])" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.r_['-1', x, x]" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 1.5, 2. , 2.5, 3. , 4. , 5. , 6. ])" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.r_[1:3:5j, 4:7]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }