{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# numpy: Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's import the `numpy` module." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[0 1 2 3 4 5 6 7 8 9]\n" ] } ], "source": [ "n = 10 # CHANGE ME\n", "a1 = list(range(n))\n", "a2 = np.arange(n)\n", "\n", "if n <= 10:\n", " print(a1)\n", " print(a2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100000 loops, best of 3: 2.36 \u00b5s per loop\n" ] } ], "source": [ "%timeit [i**2 for i in a1]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000000 loops, best of 3: 1.06 \u00b5s per loop\n" ] } ], "source": [ "%timeit a2**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy Arrays: much less flexible, but:\n", "\n", "* much faster\n", "* less memory" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "Ways to create a numpy array:\n", "\n", "* Casting from a list" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `linspace`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([-1. , -0.77777778, -0.55555556, -0.33333333, -0.11111111,\n", " 0.11111111, 0.33333333, 0.55555556, 0.77777778, 1. ])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(-1, 1, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `zeros`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", " [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((10,10), np.float64)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "Operations on arrays propagate to all elements:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "a = np.array([1.2, 3, 4])\n", "b = np.array([0.5, 0, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Addition, multiplication, power, .. are all elementwise:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.7, 3. , 5. ])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.6, 0. , 4. ])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*b" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1.09544512, 1. , 4. ])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a**b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matrix multiplication is `np.dot(A, B)` for two 2D arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "Numpy arrays have two (most) important attributes:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(5, 4, 3)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.random.rand(5, 4, 3)\n", "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `.shape` attribute contains the dimensionality array as a tuple. So the tuple `(5,4,3)` means that we're dealing with a three-dimensional array of size $5 \\times 4 \\times 3$.\n", "\n", "(`numpy.random.rand` just generates an array of random numbers of the given shape.)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other `dtype`s include `np.complex64`, `np.int32`, ..." ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }