{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# PH2150- Scientific Computing Skills\n", "\n", "## Numpy - multidimensional data arrays\n", "\n", "\n", "The `numpy` package is used in almost all numerical computation using *Python*. If you are looking at a *Python* textbook and it does not contain a section on *NumPy* then it is probably the wrong book for you. *NumPy* is a package that provides high-performance vector, matrix and higher-dimensional data structures for *Python*. It is implemented in *C* and *Fortran* so when calculations are vectorized (formulated with vectors and matrices), performance can be very fast. \n", "\n", "In the `numpy` package the terminology used for vectors, matrices and higher-dimensional data sets is an *array*. And the object that is created is the numpy.ndarray (N-dimensional array).\n", "\n", "These notes contain a brief introduction to some of the properties and usage of the `numpy` array, for a more complete description:\n", "\n", "https://docs.scipy.org/doc/numpy/index.html\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "To use `numpy` you need to import the module, using for example:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import numpy as np # here we use the alias np which is common practice" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Creating `numpy` arrays\n", "\n", "There are a number of ways to create new numpy arrays, for example from:\n", "\n", "* a *Python* list or tuples\n", "* using functions to generate numpy arrays, such as `np.arange`, `np.linspace`.\n", "* reading data from files, `np.loadtxt` see Problem sheet 3." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### From lists\n", "\n", "For example, to create new vector and matrix arrays from Python lists we can use the `numpy.array` function." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a vector: the argument to the array function is a Python list\n", "v = np.array([1,2,3,4])\n", "v\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a matrix: the argument to the array function is a nested Python list\n", "M = np.array([[1, 2], [3, 4]])\n", "M" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The `v` and `M` objects are both of the type `ndarray` that the `numpy` module provides." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, numpy.ndarray)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(v), type(M)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The difference between the `v` and `M` arrays is only their shapes. We can get information about the shape of an array by using the `ndarray.shape` property." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vshape= (4,) \n", "Mshape= (2, 2)\n" ] } ], "source": [ "print ('vshape=',v.shape,'\\nMshape=', M.shape) #\\n gives new line" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The number of elements in the array is available through the `ndarray.size` property:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "v_size= 4 \n", "M_size= 4\n" ] } ], "source": [ "print ('v_size=',v.size,'\\nM_size=', M.size) #\\n gives new line" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Equivalently, we could use the function `numpy.shape` and `numpy.size`" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of M = (2, 2) \n", "Size of M = 4\n" ] } ], "source": [ "print('Shape of M =',np.shape(M),'\\nSize of M =',np.size(M))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Using the `dtype` (data type) property of an `ndarray`, we can see what type the data of an array has:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(dtype('int32'), dtype('int32'))" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M.dtype,v.dtype" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can explicitly define the type of the array data when we create it, using the `dtype` keyword argument: " ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1.+0.j 2.+0.j]\n", " [3.+0.j 4.+0.j]]\n" ] }, { "data": { "text/plain": [ "dtype('complex128')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M = np.array([[1, 2], [3, 4]], dtype=complex)\n", "\n", "print(M)\n", "\n", "M.dtype" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The `numpy.ndarray` has the following properties:\n", "\n", "* The number of elements in an array is fixed. \n", "* You cannot add elements to an array once it is created, or remove them.\n", "* The elements of an array must all be of the same type, such as all floats or all integers. \n", "* You cannot mix elements of different types in the same array and you cannot change the type of the elements once an array is created.\n", "\n", "\n", "This is much more restrictive than the *list* data container that we have already seen, why would we ever use an array if lists are more flexible?\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "* Lists do not support mathematical functions such as matrix and dot multiplications, etc.\n", "* Implementing such functions for Python lists would not be very efficient because of the dynamic typing (the data type at each element can be changed).\n", "* The restriction that Numpy arrays are **statically typed** (fixed when created) and **homogeneous** (all the same data type) allows them to be more memory efficient.\n", "* The static typing, also allows fast implementation of mathematical functions such as multiplication and addition of `numpy` arrays, which can be implemented in a compiled language (C and Fortran is used)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Common data types that can be used with `dtype` are: `int`, `float`, `complex`, `bool`, `object`, etc.\n", "\n", "We can also explicitly define the bit size of the data types, for example: `int64`, `int16`, `float128`, `complex128`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Using array-generating functions\n", "\n", "For larger arrays it is inpractical to initialize the data manually, using explicit python lists. Instead we can use one of the many functions in `numpy` that generate arrays of different forms. Some of the more common are:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### arange" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create a range\n", "\n", "x = np.arange(0, 10, 1) # arguments: start, stop, step\n", "\n", "x" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ -1.00000000e+00, -9.00000000e-01, -8.00000000e-01,\n", " -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,\n", " -4.00000000e-01, -3.00000000e-01, -2.00000000e-01,\n", " -1.00000000e-01, -2.22044605e-16, 1.00000000e-01,\n", " 2.00000000e-01, 3.00000000e-01, 4.00000000e-01,\n", " 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,\n", " 8.00000000e-01, 9.00000000e-01])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.arange(-1, 1, 0.1) # arguments: start, stop, step\n", "\n", "x" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### linspace and logspace" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.41666667, 0.83333333, 1.25 ,\n", " 1.66666667, 2.08333333, 2.5 , 2.91666667,\n", " 3.33333333, 3.75 , 4.16666667, 4.58333333,\n", " 5. , 5.41666667, 5.83333333, 6.25 ,\n", " 6.66666667, 7.08333333, 7.5 , 7.91666667,\n", " 8.33333333, 8.75 , 9.16666667, 9.58333333, 10. ])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# using linspace, both end points ARE included\n", "np.linspace(0, 10, 25) # (start, stop, num=50, endpoint=True, retstep=False, dtype=None)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1.00000000e+00, 3.03773178e+00, 9.22781435e+00,\n", " 2.80316249e+01, 8.51525577e+01, 2.58670631e+02,\n", " 7.85771994e+02, 2.38696456e+03, 7.25095809e+03,\n", " 2.20264658e+04])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "logspace(0, 10, 10, base=e) # (start, stop, num=50, endpoint=True, base=10.0, dtype=None)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### mgrid" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x=\n", " [[0 0 0 0 0]\n", " [1 1 1 1 1]\n", " [2 2 2 2 2]\n", " [3 3 3 3 3]\n", " [4 4 4 4 4]]\n", "\n", "y=\n", " [[0 1 2 3 4]\n", " [0 1 2 3 4]\n", " [0 1 2 3 4]\n", " [0 1 2 3 4]\n", " [0 1 2 3 4]]\n" ] } ], "source": [ "x, y = np.mgrid[0:5, 0:5] # similar to meshgrid in MATLAB\n", "\n", "print ('x=\\n',x)\n", "\n", "print('\\ny=\\n',y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### random data\n", "\n", "Numpy contains a sub package `random` that contains many function for generating arrays of random numbers, that we will return to later in the course, below are a few examples." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "from numpy import random" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.92932506, 0.19684255, 0.736434 , 0.18125714, 0.70905038],\n", " [ 0.18803573, 0.9312815 , 0.1284532 , 0.38138008, 0.36646481],\n", " [ 0.53700462, 0.02361381, 0.97760688, 0.73296701, 0.23042324],\n", " [ 0.9024635 , 0.20860922, 0.67729644, 0.68386687, 0.49385729],\n", " [ 0.95876515, 0.29341553, 0.37520629, 0.29194432, 0.64102804]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# uniform random numbers in [0,1]\n", "random.rand(5,5)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.117907 , -1.57016164, 0.78256246, 1.45386709, 0.54744436],\n", " [ 2.30356897, -0.28352021, -0.9087325 , 1.2285279 , -1.00760167],\n", " [ 0.72216801, 0.77507299, -0.37793178, -0.31852241, 0.84493629],\n", " [-0.10682252, 1.15930142, -0.47291444, -0.69496967, -0.58912034],\n", " [ 0.34513487, -0.92389516, -0.216978 , 0.42153272, 0.86650101]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# standard normal distributed random numbers\n", "random.randn(5,5)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### zeros and ones\n", "\n", "Sometimes you just want to start with an array filled with either just zeros or ones. With a particular shape. As this is quite a common requirement functions are built in to do this." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0., 0., 0.],\n", " [ 0., 0., 0.],\n", " [ 0., 0., 0.]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros((3,3))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1.],\n", " [ 1., 1., 1.],\n", " [ 1., 1., 1.]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((3,3))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Manipulating arrays\n", "\n", "### Indexing\n", "\n", "We can index elements in an array using square brackets and indices:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a matrix: the argument to the array function is a nested Python list\n", "M = np.array([[1, 2], [3, 4]])\n", "M" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# v is a vector, and has only one dimension, taking one index\n", "v[0]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# M is a matrix, or a 2 dimensional array, taking two indices \n", "M[1,1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "If we omit an index of a multidimensional array it returns the whole row (or, in general, a N-1 dimensional array) " ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 4])" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The same thing can be achieved with using `:` instead of an index: " ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.60410063, 0.4791374 , 0.8237106 ])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[1,:] # row 1" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 0.40043577, 0.4791374 , 0.15459644])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M[:,1] # column 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can assign new values to elements in an array using indexing:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "M[0,0] = 1" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 0.40043577, 0.66254019],\n", " [ 0.60410063, 0.4791374 , 0.8237106 ],\n", " [ 0.96856318, 0.15459644, 0.96082399]])" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "# also works for rows and columns\n", "M[1,:] = 0\n", "M[:,2] = -1" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 0.40043577, -1. ],\n", " [ 0. , 0. , -1. ],\n", " [ 0.96856318, 0.15459644, -1. ]])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Index slicing\n", "\n", "Index slicing is the technical name for the syntax `M[lower:upper:step]` to extract part of an array:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array([1,2,3,4,5])\n", "A" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 3])" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Array slices are *mutable*: if they are assigned a new value the original array from which the slice was extracted is modified:" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, -3, 4, 5])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1:3] = [-2,-3]\n", "\n", "A" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "We can omit any of the three parameters in `M[lower:upper:step]`:" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, -3, 4, 5])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[::] # lower, upper, step all take the default values" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -3, 5])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[::2] # step is 2, lower and upper defaults to the beginning and end of the array" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, -3])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:3] # first three elements" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 5])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[3:] # elements from index 3" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Negative indices counts from the end of the array (positive index from the begining):" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "A = array([1,2,3,4,5])" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[-1] # the last element in the array" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[-3:] # the last three elements" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Index slicing works exactly the same way for multidimensional arrays:" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [10, 11, 12, 13, 14],\n", " [20, 21, 22, 23, 24],\n", " [30, 31, 32, 33, 34],\n", " [40, 41, 42, 43, 44]])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array([[n+m*10 for n in range(5)] for m in range(5)])\n", "\n", "A" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13],\n", " [21, 22, 23],\n", " [31, 32, 33]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# a block from the original array\n", "A[1:4, 1:4]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4],\n", " [20, 22, 24],\n", " [40, 42, 44]])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# strides\n", "A[::2, ::2]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Further reading\n", "\n", "* http://numpy.scipy.org\n", "* http://scipy.org/Tentative_NumPy_Tutorial\n", "* http://scipy.org/NumPy_for_Matlab_Users - A Numpy guide for MATLAB users." ] } ], "metadata": { "celltoolbar": "Slideshow", "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.7.0" } }, "nbformat": 4, "nbformat_minor": 1 }