5437 lines
177 KiB
Plaintext
5437 lines
177 KiB
Plaintext
|
|
{
|
||
|
|
"cells": [
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"# Numpy - multidimensional data arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"J.R. Johansson (jrjohansson at gmail.com)\n",
|
||
|
|
"\n",
|
||
|
|
"The latest version of this [IPython notebook](http://ipython.org/notebook.html) lecture is available at [http://github.com/jrjohansson/scientific-python-lectures](http://github.com/jrjohansson/scientific-python-lectures).\n",
|
||
|
|
"\n",
|
||
|
|
"The other notebooks in this lecture series are indexed at [http://jrjohansson.github.io](http://jrjohansson.github.io)."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 1,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"# what is this line all about?!? Answer in lecture 4\n",
|
||
|
|
"%matplotlib inline\n",
|
||
|
|
"import matplotlib.pyplot as plt"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Introduction"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The `numpy` package (module) is used in almost all numerical computation using Python. It is a package that provide 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 is very good. \n",
|
||
|
|
"\n",
|
||
|
|
"To use `numpy` you need to import the module, using for example:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 2,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"from numpy import *"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"In the `numpy` package the terminology used for vectors, matrices and higher-dimensional data sets is *array*. \n",
|
||
|
|
"\n"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Creating `numpy` arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"There are a number of ways to initialize new numpy arrays, for example from\n",
|
||
|
|
"\n",
|
||
|
|
"* a Python list or tuples\n",
|
||
|
|
"* using functions that are dedicated to generating numpy arrays, such as `arange`, `linspace`, etc.\n",
|
||
|
|
"* reading data from files"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### From lists"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"For example, to create new vector and matrix arrays from Python lists we can use the `numpy.array` function."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 3,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([1, 2, 3, 4])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 3,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# a vector: the argument to the array function is a Python list\n",
|
||
|
|
"v = array([1,2,3,4])\n",
|
||
|
|
"\n",
|
||
|
|
"v"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 4,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2],\n",
|
||
|
|
" [3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 4,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# a matrix: the argument to the array function is a nested Python list\n",
|
||
|
|
"M = array([[1, 2], [3, 4]])\n",
|
||
|
|
"\n",
|
||
|
|
"M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The `v` and `M` objects are both of the type `ndarray` that the `numpy` module provides."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 5,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(numpy.ndarray, numpy.ndarray)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 5,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"type(v), type(M)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"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": 6,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(4,)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 6,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v.shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 7,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(2, 2)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 7,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The number of elements in the array is available through the `ndarray.size` property:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 8,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"4"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 8,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.size"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Equivalently, we could use the function `numpy.shape` and `numpy.size`"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 9,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(2, 2)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 9,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"shape(M)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 10,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"4"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 10,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"size(M)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"So far the `numpy.ndarray` looks awfully much like a Python list (or nested list). Why not simply use Python lists for computations instead of creating a new array type? \n",
|
||
|
|
"\n",
|
||
|
|
"There are several reasons:\n",
|
||
|
|
"\n",
|
||
|
|
"* Python lists are very general. They can contain any kind of object. They are dynamically typed. They do not support mathematical functions such as matrix and dot multiplications, etc. Implementing such functions for Python lists would not be very efficient because of the dynamic typing.\n",
|
||
|
|
"* Numpy arrays are **statically typed** and **homogeneous**. The type of the elements is determined when the array is created.\n",
|
||
|
|
"* Numpy arrays are memory efficient.\n",
|
||
|
|
"* Because of the static typing, fast implementation of mathematical functions such as multiplication and addition of `numpy` arrays can be implemented in a compiled language (C and Fortran is used).\n",
|
||
|
|
"\n",
|
||
|
|
"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": 11,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"dtype('int64')"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 11,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.dtype"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We get an error if we try to assign a value of the wrong type to an element in a numpy array:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 12,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"ename": "ValueError",
|
||
|
|
"evalue": "invalid literal for long() with base 10: 'hello'",
|
||
|
|
"output_type": "error",
|
||
|
|
"traceback": [
|
||
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||
|
|
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||
|
|
"\u001b[0;32m<ipython-input-12-a09d72434238>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mM\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||
|
|
"\u001b[0;31mValueError\u001b[0m: invalid literal for long() with base 10: 'hello'"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M[0,0] = \"hello\""
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"If we want, we can explicitly define the type of the array data when we create it, using the `dtype` keyword argument: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 13,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 1.+0.j, 2.+0.j],\n",
|
||
|
|
" [ 3.+0.j, 4.+0.j]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 13,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M = array([[1, 2], [3, 4]], dtype=complex)\n",
|
||
|
|
"\n",
|
||
|
|
"M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"### Using array-generating functions"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"For larger arrays it is impractical 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": {},
|
||
|
|
"source": [
|
||
|
|
"#### arange"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 14,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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 = arange(0, 10, 1) # arguments: start, stop, step\n",
|
||
|
|
"\n",
|
||
|
|
"x"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 15,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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 = arange(-1, 1, 0.1)\n",
|
||
|
|
"\n",
|
||
|
|
"x"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### linspace and logspace"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 16,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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",
|
||
|
|
"linspace(0, 10, 25)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 17,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### mgrid"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 18,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"x, y = mgrid[0:5, 0:5] # similar to meshgrid in MATLAB"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 19,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[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]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 19,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"x"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 20,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[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]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 20,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"y"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### random data"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 21,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"from numpy import random"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 22,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"#### diag"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 24,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 0, 0],\n",
|
||
|
|
" [0, 2, 0],\n",
|
||
|
|
" [0, 0, 3]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 24,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# a diagonal matrix\n",
|
||
|
|
"diag([1,2,3])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 25,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[0, 1, 0, 0],\n",
|
||
|
|
" [0, 0, 2, 0],\n",
|
||
|
|
" [0, 0, 0, 3],\n",
|
||
|
|
" [0, 0, 0, 0]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 25,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# diagonal with offset from the main diagonal\n",
|
||
|
|
"diag([1,2,3], k=1) "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### zeros and ones"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 26,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": [
|
||
|
|
"zeros((3,3))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 27,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": [
|
||
|
|
"ones((3,3))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## File I/O"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Comma-separated values (CSV)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"A very common file format for data files is comma-separated values (CSV), or related formats such as TSV (tab-separated values). To read data from such files into Numpy arrays we can use the `numpy.genfromtxt` function. For example, "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 28,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"1800 1 1 -6.1 -6.1 -6.1 1\r\n",
|
||
|
|
"1800 1 2 -15.4 -15.4 -15.4 1\r\n",
|
||
|
|
"1800 1 3 -15.0 -15.0 -15.0 1\r\n",
|
||
|
|
"1800 1 4 -19.3 -19.3 -19.3 1\r\n",
|
||
|
|
"1800 1 5 -16.8 -16.8 -16.8 1\r\n",
|
||
|
|
"1800 1 6 -11.4 -11.4 -11.4 1\r\n",
|
||
|
|
"1800 1 7 -7.6 -7.6 -7.6 1\r\n",
|
||
|
|
"1800 1 8 -7.1 -7.1 -7.1 1\r\n",
|
||
|
|
"1800 1 9 -10.1 -10.1 -10.1 1\r\n",
|
||
|
|
"1800 1 10 -9.5 -9.5 -9.5 1\r\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"!head stockholm_td_adj.dat"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 29,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"data = genfromtxt('stockholm_td_adj.dat')"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 30,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(77431, 7)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 30,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"data.shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 31,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"image/png": "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
|
||
|
|
"text/plain": [
|
||
|
|
"<matplotlib.figure.Figure at 0x108686390>"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "display_data"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"fig, ax = plt.subplots(figsize=(14,4))\n",
|
||
|
|
"ax.plot(data[:,0]+data[:,1]/12.0+data[:,2]/365, data[:,5])\n",
|
||
|
|
"ax.axis('tight')\n",
|
||
|
|
"ax.set_title('tempeatures in Stockholm')\n",
|
||
|
|
"ax.set_xlabel('year')\n",
|
||
|
|
"ax.set_ylabel('temperature (C)');"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Using `numpy.savetxt` we can store a Numpy array to a file in CSV format:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 32,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0.77872576, 0.40043577, 0.66254019],\n",
|
||
|
|
" [ 0.60410063, 0.4791374 , 0.8237106 ],\n",
|
||
|
|
" [ 0.96856318, 0.15459644, 0.96082399]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 32,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M = random.rand(3,3)\n",
|
||
|
|
"\n",
|
||
|
|
"M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 33,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"savetxt(\"random-matrix.csv\", M)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 34,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"7.787257639287014088e-01 4.004357670697732408e-01 6.625401863466899854e-01\r\n",
|
||
|
|
"6.041006328761111543e-01 4.791373994963619154e-01 8.237105968088237473e-01\r\n",
|
||
|
|
"9.685631757740569281e-01 1.545964379103705877e-01 9.608239852111523094e-01\r\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"!cat random-matrix.csv"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 35,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"0.77873 0.40044 0.66254\r\n",
|
||
|
|
"0.60410 0.47914 0.82371\r\n",
|
||
|
|
"0.96856 0.15460 0.96082\r\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"savetxt(\"random-matrix.csv\", M, fmt='%.5f') # fmt specifies the format\n",
|
||
|
|
"\n",
|
||
|
|
"!cat random-matrix.csv"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Numpy's native file format"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Useful when storing and reading back numpy array data. Use the functions `numpy.save` and `numpy.load`:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 36,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"random-matrix.npy: data\r\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"save(\"random-matrix.npy\", M)\n",
|
||
|
|
"\n",
|
||
|
|
"!file random-matrix.npy"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 37,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0.77872576, 0.40043577, 0.66254019],\n",
|
||
|
|
" [ 0.60410063, 0.4791374 , 0.8237106 ],\n",
|
||
|
|
" [ 0.96856318, 0.15459644, 0.96082399]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 37,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"load(\"random-matrix.npy\")"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## More properties of the numpy arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 38,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"8"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 38,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.itemsize # bytes per element"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 39,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"72"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 39,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.nbytes # number of bytes"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 40,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 40,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.ndim # number of dimensions"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Manipulating arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Indexing"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can index elements in an array using square brackets and indices:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 41,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"1"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 41,
|
||
|
|
"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": 42,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"0.47913739949636192"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 42,
|
||
|
|
"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": {},
|
||
|
|
"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": 43,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0.77872576, 0.40043577, 0.66254019],\n",
|
||
|
|
" [ 0.60410063, 0.4791374 , 0.8237106 ],\n",
|
||
|
|
" [ 0.96856318, 0.15459644, 0.96082399]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 43,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 44,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0.60410063, 0.4791374 , 0.8237106 ])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 44,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M[1]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The same thing can be achieved with using `:` instead of an index: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 45,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"We can assign new values to elements in an array using indexing:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 47,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"M[0,0] = 1"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 48,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"# also works for rows and columns\n",
|
||
|
|
"M[1,:] = 0\n",
|
||
|
|
"M[:,2] = -1"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 50,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"### Index slicing"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([2, 3])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 52,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A[1:3]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"We can omit any of the three parameters in `M[lower:upper:step]`:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 54,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"Negative indices counts from the end of the array (positive index from the beginning):"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 58,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"A = array([1,2,3,4,5])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 59,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"Index slicing works exactly the same way for multidimensional arrays:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 61,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": {},
|
||
|
|
"source": [
|
||
|
|
"### Fancy indexing"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Fancy indexing is the name for when an array or list is used in-place of an index: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 64,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[10, 11, 12, 13, 14],\n",
|
||
|
|
" [20, 21, 22, 23, 24],\n",
|
||
|
|
" [30, 31, 32, 33, 34]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 64,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"row_indices = [1, 2, 3]\n",
|
||
|
|
"A[row_indices]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 65,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([11, 22, 34])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 65,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"col_indices = [1, 2, -1] # remember, index -1 means the last element\n",
|
||
|
|
"A[row_indices, col_indices]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can also use index masks: If the index mask is an Numpy array of data type `bool`, then an element is selected (True) or not (False) depending on the value of the index mask at the position of each element: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 66,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 1, 2, 3, 4])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 66,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"B = array([n for n in range(5)])\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 67,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 67,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"row_mask = array([True, False, True, False, False])\n",
|
||
|
|
"B[row_mask]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 68,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 68,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# same thing\n",
|
||
|
|
"row_mask = array([1,0,1,0,0], dtype=bool)\n",
|
||
|
|
"B[row_mask]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"This feature is very useful to conditionally select elements from an array, using for example comparison operators:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 69,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ,\n",
|
||
|
|
" 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 69,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"x = arange(0, 10, 0.5)\n",
|
||
|
|
"x"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 70,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([False, False, False, False, False, False, False, False, False,\n",
|
||
|
|
" False, False, True, True, True, True, False, False, False,\n",
|
||
|
|
" False, False], dtype=bool)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 70,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"mask = (5 < x) * (x < 7.5)\n",
|
||
|
|
"\n",
|
||
|
|
"mask"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 71,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 5.5, 6. , 6.5, 7. ])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 71,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"x[mask]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Functions for extracting data from arrays and creating arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### where"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The index mask can be converted to position index using the `where` function"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 72,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(array([11, 12, 13, 14]),)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 72,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"indices = where(mask)\n",
|
||
|
|
"\n",
|
||
|
|
"indices"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 73,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 5.5, 6. , 6.5, 7. ])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 73,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"x[indices] # this indexing is equivalent to the fancy indexing x[mask]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### diag"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"With the diag function we can also extract the diagonal and subdiagonals of an array:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 74,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0, 11, 22, 33, 44])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 74,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"diag(A)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 75,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([10, 21, 32, 43])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 75,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"diag(A, -1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### take"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The `take` function is similar to fancy indexing described above:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 76,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([-3, -2, -1, 0, 1, 2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 76,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v2 = arange(-3,3)\n",
|
||
|
|
"v2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 77,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([-2, 0, 2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 77,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"row_indices = [1, 3, 5]\n",
|
||
|
|
"v2[row_indices] # fancy indexing"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 78,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([-2, 0, 2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 78,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v2.take(row_indices)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"But `take` also works on lists and other objects:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 79,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([-2, 0, 2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 79,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"take([-3, -2, -1, 0, 1, 2], row_indices)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### choose"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Constructs an array by picking elements from several arrays:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 80,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 5, -2, 5, -2])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 80,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"which = [1, 0, 1, 0]\n",
|
||
|
|
"choices = [[-2,-2,-2,-2], [5,5,5,5]]\n",
|
||
|
|
"\n",
|
||
|
|
"choose(which, choices)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Linear algebra"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Vectorizing code is the key to writing efficient numerical calculation with Python/Numpy. That means that as much as possible of a program should be formulated in terms of matrix and vector operations, like matrix-matrix multiplication."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Scalar-array operations"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can use the usual arithmetic operators to multiply, add, subtract, and divide arrays with scalar numbers."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 81,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"v1 = arange(0, 5)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 82,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 2, 4, 6, 8])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 82,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v1 * 2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 83,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([2, 3, 4, 5, 6])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 83,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v1 + 2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 84,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(array([[ 0, 2, 4, 6, 8],\n",
|
||
|
|
" [20, 22, 24, 26, 28],\n",
|
||
|
|
" [40, 42, 44, 46, 48],\n",
|
||
|
|
" [60, 62, 64, 66, 68],\n",
|
||
|
|
" [80, 82, 84, 86, 88]]), array([[ 2, 3, 4, 5, 6],\n",
|
||
|
|
" [12, 13, 14, 15, 16],\n",
|
||
|
|
" [22, 23, 24, 25, 26],\n",
|
||
|
|
" [32, 33, 34, 35, 36],\n",
|
||
|
|
" [42, 43, 44, 45, 46]]))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 84,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A * 2, A + 2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Element-wise array-array operations"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"When we add, subtract, multiply and divide arrays with each other, the default behaviour is **element-wise** operations:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 85,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0, 1, 4, 9, 16],\n",
|
||
|
|
" [ 100, 121, 144, 169, 196],\n",
|
||
|
|
" [ 400, 441, 484, 529, 576],\n",
|
||
|
|
" [ 900, 961, 1024, 1089, 1156],\n",
|
||
|
|
" [1600, 1681, 1764, 1849, 1936]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 85,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A * A # element-wise multiplication"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 86,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0, 1, 4, 9, 16])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 86,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v1 * v1"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"If we multiply arrays with compatible shapes, we get an element-wise multiplication of each row:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 87,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"((5, 5), (5,))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 87,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A.shape, v1.shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 88,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0, 1, 4, 9, 16],\n",
|
||
|
|
" [ 0, 11, 24, 39, 56],\n",
|
||
|
|
" [ 0, 21, 44, 69, 96],\n",
|
||
|
|
" [ 0, 31, 64, 99, 136],\n",
|
||
|
|
" [ 0, 41, 84, 129, 176]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 88,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A * v1"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Matrix algebra"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"What about matrix multiplication? There are two ways. We can either use the `dot` function, which applies a matrix-matrix, matrix-vector, or inner vector multiplication to its two arguments: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 89,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 300, 310, 320, 330, 340],\n",
|
||
|
|
" [1300, 1360, 1420, 1480, 1540],\n",
|
||
|
|
" [2300, 2410, 2520, 2630, 2740],\n",
|
||
|
|
" [3300, 3460, 3620, 3780, 3940],\n",
|
||
|
|
" [4300, 4510, 4720, 4930, 5140]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 89,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"dot(A, A)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 90,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 30, 130, 230, 330, 430])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 90,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"dot(A, v1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 91,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"30"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 91,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"dot(v1, v1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Alternatively, we can cast the array objects to the type `matrix`. This changes the behavior of the standard arithmetic operators `+, -, *` to use matrix algebra."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 92,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"M = matrix(A)\n",
|
||
|
|
"v = matrix(v1).T # make it a column vector"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 93,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[0],\n",
|
||
|
|
" [1],\n",
|
||
|
|
" [2],\n",
|
||
|
|
" [3],\n",
|
||
|
|
" [4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 93,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 94,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 300, 310, 320, 330, 340],\n",
|
||
|
|
" [1300, 1360, 1420, 1480, 1540],\n",
|
||
|
|
" [2300, 2410, 2520, 2630, 2740],\n",
|
||
|
|
" [3300, 3460, 3620, 3780, 3940],\n",
|
||
|
|
" [4300, 4510, 4720, 4930, 5140]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 94,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M * M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 95,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 30],\n",
|
||
|
|
" [130],\n",
|
||
|
|
" [230],\n",
|
||
|
|
" [330],\n",
|
||
|
|
" [430]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 95,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M * v"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 96,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[30]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 96,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# inner product\n",
|
||
|
|
"v.T * v"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 97,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 30],\n",
|
||
|
|
" [131],\n",
|
||
|
|
" [232],\n",
|
||
|
|
" [333],\n",
|
||
|
|
" [434]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 97,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# with matrix objects, standard matrix algebra applies\n",
|
||
|
|
"v + M*v"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"If we try to add, subtract or multiply objects with incompatible shapes we get an error:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 98,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"v = matrix([1,2,3,4,5,6]).T"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 99,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"((5, 5), (6, 1))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 99,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"shape(M), shape(v)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 100,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"ename": "ValueError",
|
||
|
|
"evalue": "shapes (5,5) and (6,1) not aligned: 5 (dim 1) != 6 (dim 0)",
|
||
|
|
"output_type": "error",
|
||
|
|
"traceback": [
|
||
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||
|
|
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||
|
|
"\u001b[0;32m<ipython-input-100-995fb48ad0cc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mM\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||
|
|
"\u001b[0;32m/Users/rob/miniconda/envs/py27-spl/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.pyc\u001b[0m in \u001b[0;36m__mul__\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 339\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 340\u001b[0m \u001b[0;31m# This promotes 1-D vectors to row vectors\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 341\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0masmatrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 342\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misscalar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'__rmul__'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 343\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||
|
|
"\u001b[0;31mValueError\u001b[0m: shapes (5,5) and (6,1) not aligned: 5 (dim 1) != 6 (dim 0)"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M * v"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"See also the related functions: `inner`, `outer`, `cross`, `kron`, `tensordot`. Try for example `help(kron)`."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Array/Matrix transformations"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Above we have used the `.T` to transpose the matrix object `v`. We could also have used the `transpose` function to accomplish the same thing. \n",
|
||
|
|
"\n",
|
||
|
|
"Other mathematical functions that transform matrix objects are:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 101,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 0.+1.j, 0.+2.j],\n",
|
||
|
|
" [ 0.+3.j, 0.+4.j]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 101,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"C = matrix([[1j, 2j], [3j, 4j]])\n",
|
||
|
|
"C"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 102,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 0.-1.j, 0.-2.j],\n",
|
||
|
|
" [ 0.-3.j, 0.-4.j]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 102,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"conjugate(C)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Hermitian conjugate: transpose + conjugate"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 103,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 0.-1.j, 0.-3.j],\n",
|
||
|
|
" [ 0.-2.j, 0.-4.j]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 103,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"C.H"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can extract the real and imaginary parts of complex-valued arrays using `real` and `imag`:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 104,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 0., 0.],\n",
|
||
|
|
" [ 0., 0.]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 104,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"real(C) # same as: C.real"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 105,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 1., 2.],\n",
|
||
|
|
" [ 3., 4.]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 105,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"imag(C) # same as: C.imag"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Or the complex argument and absolute value"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 106,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0.78539816, 1.10714872],\n",
|
||
|
|
" [ 1.24904577, 1.32581766]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 106,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"angle(C+1) # heads up MATLAB Users, angle is used instead of arg"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 107,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 1., 2.],\n",
|
||
|
|
" [ 3., 4.]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 107,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"abs(C)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Matrix computations"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### Inverse"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 108,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 0.+2.j , 0.-1.j ],\n",
|
||
|
|
" [ 0.-1.5j, 0.+0.5j]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 108,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"linalg.inv(C) # equivalent to C.I "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 109,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"matrix([[ 1.00000000e+00+0.j, 4.44089210e-16+0.j],\n",
|
||
|
|
" [ 0.00000000e+00+0.j, 1.00000000e+00+0.j]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 109,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"C.I * C"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### Determinant"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 110,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(2.0000000000000004+0j)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 110,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"linalg.det(C)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 111,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(0.50000000000000011+0j)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 111,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"linalg.det(C.I)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Data processing"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Often it is useful to store datasets in Numpy arrays. Numpy provides a number of functions to calculate statistics of datasets in arrays. \n",
|
||
|
|
"\n",
|
||
|
|
"For example, let's calculate some properties from the Stockholm temperature dataset used above."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 112,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(77431, 7)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 112,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# reminder, the temperature dataset is stored in the data variable:\n",
|
||
|
|
"shape(data)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### mean"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 113,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"6.1971096847515854"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 113,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# the temperature data is in column 3\n",
|
||
|
|
"mean(data[:,3])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The daily mean temperature in Stockholm over the last 200 years has been about 6.2 C."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### standard deviations and variance"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 114,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(8.2822716213405734, 68.596023209663414)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 114,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"std(data[:,3]), var(data[:,3])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### min and max"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 115,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"-25.800000000000001"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 115,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# lowest daily average temperature\n",
|
||
|
|
"data[:,3].min()"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 116,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"28.300000000000001"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 116,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# highest daily average temperature\n",
|
||
|
|
"data[:,3].max()"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"#### sum, prod, and trace"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 117,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 117,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"d = arange(0, 10)\n",
|
||
|
|
"d"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 118,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"45"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 118,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# sum up all elements\n",
|
||
|
|
"sum(d)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 119,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"3628800"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 119,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# product of all elements\n",
|
||
|
|
"prod(d+1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 120,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 120,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# cumulative sum\n",
|
||
|
|
"cumsum(d)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 121,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 1, 2, 6, 24, 120, 720, 5040,\n",
|
||
|
|
" 40320, 362880, 3628800])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 121,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# cumulative product\n",
|
||
|
|
"cumprod(d+1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 122,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"110"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 122,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# same as: diag(A).sum()\n",
|
||
|
|
"trace(A)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Computations on subsets of arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can compute with subsets of the data in an array using indexing, fancy indexing, and the other methods of extracting data from an array (described above).\n",
|
||
|
|
"\n",
|
||
|
|
"For example, let's go back to the temperature dataset:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 123,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"1800 1 1 -6.1 -6.1 -6.1 1\r\n",
|
||
|
|
"1800 1 2 -15.4 -15.4 -15.4 1\r\n",
|
||
|
|
"1800 1 3 -15.0 -15.0 -15.0 1\r\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"!head -n 3 stockholm_td_adj.dat"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The dataformat is: year, month, day, daily average temperature, low, high, location.\n",
|
||
|
|
"\n",
|
||
|
|
"If we are interested in the average temperature only in a particular month, say February, then we can create a index mask and use it to select only the data for that month using:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 124,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.,\n",
|
||
|
|
" 12.])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 124,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"unique(data[:,1]) # the month column takes values from 1 to 12"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 125,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"mask_feb = data[:,1] == 2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 126,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"-3.2121095707365961"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 126,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# the temperature data is in column 3\n",
|
||
|
|
"mean(data[mask_feb,3])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"With these tools we have very powerful data processing capabilities at our disposal. For example, to extract the average monthly average temperatures for each month of the year only takes a few lines of code: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 127,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"image/png": "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
|
||
|
|
"text/plain": [
|
||
|
|
"<matplotlib.figure.Figure at 0x109f94e50>"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "display_data"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"months = arange(1,13)\n",
|
||
|
|
"monthly_mean = [mean(data[data[:,1] == month, 3]) for month in months]\n",
|
||
|
|
"\n",
|
||
|
|
"fig, ax = plt.subplots()\n",
|
||
|
|
"ax.bar(months, monthly_mean)\n",
|
||
|
|
"ax.set_xlabel(\"Month\")\n",
|
||
|
|
"ax.set_ylabel(\"Monthly avg. temp.\");"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### Calculations with higher-dimensional data"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"When functions such as `min`, `max`, etc. are applied to a multidimensional arrays, it is sometimes useful to apply the calculation to the entire array, and sometimes only on a row or column basis. Using the `axis` argument we can specify how these functions should behave: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 128,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0.2850926 , 0.17302017, 0.17748378],\n",
|
||
|
|
" [ 0.80070487, 0.45527067, 0.61277451],\n",
|
||
|
|
" [ 0.11372793, 0.43608703, 0.87010206]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 128,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"m = random.rand(3,3)\n",
|
||
|
|
"m"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 129,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"0.87010206156754955"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 129,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# global max\n",
|
||
|
|
"m.max()"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 130,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0.80070487, 0.45527067, 0.87010206])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 130,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# max in each column\n",
|
||
|
|
"m.max(axis=0)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 131,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 0.2850926 , 0.80070487, 0.87010206])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 131,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# max in each row\n",
|
||
|
|
"m.max(axis=1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Many other functions and methods in the `array` and `matrix` classes accept the same (optional) `axis` keyword argument."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Reshaping, resizing and stacking arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"The shape of an Numpy array can be modified without copying the underlying data, which makes it a fast operation even for large arrays."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 132,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"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": 132,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 133,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"n, m = A.shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 134,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n",
|
||
|
|
" 32, 33, 34, 40, 41, 42, 43, 44]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 134,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"B = A.reshape((1,n*m))\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 135,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n",
|
||
|
|
" 32, 33, 34, 40, 41, 42, 43, 44]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 135,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"B[0,0:5] = 5 # modify the array\n",
|
||
|
|
"\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 136,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 5, 5, 5, 5, 5],\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": 136,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A # and the original variable is also changed. B is only a different view of the same data"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can also use the function `flatten` to make a higher-dimensional array into a vector. But this function creates a copy of the data."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 137,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([ 5, 5, 5, 5, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n",
|
||
|
|
" 32, 33, 34, 40, 41, 42, 43, 44])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 137,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"B = A.flatten()\n",
|
||
|
|
"\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 138,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([10, 10, 10, 10, 10, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n",
|
||
|
|
" 32, 33, 34, 40, 41, 42, 43, 44])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 138,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"B[0:5] = 10\n",
|
||
|
|
"\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 139,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 5, 5, 5, 5, 5],\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": 139,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A # now A has not changed, because B's data is a copy of A's, not referring to the same data"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Adding a new dimension: newaxis"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"With `newaxis`, we can insert new dimensions in an array, for example converting a vector to a column or row matrix:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 140,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"v = array([1,2,3])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 141,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(3,)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 141,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"shape(v)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 142,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1],\n",
|
||
|
|
" [2],\n",
|
||
|
|
" [3]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 142,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# make a column matrix of the vector v\n",
|
||
|
|
"v[:, newaxis]"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 143,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(3, 1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 143,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# column matrix\n",
|
||
|
|
"v[:,newaxis].shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 144,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(1, 3)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 144,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# row matrix\n",
|
||
|
|
"v[newaxis,:].shape"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Stacking and repeating arrays"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Using function `repeat`, `tile`, `vstack`, `hstack`, and `concatenate` we can create larger vectors and matrices from smaller ones:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### tile and repeat"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 145,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"a = array([[1, 2], [3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 146,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 146,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# repeat each element 3 times\n",
|
||
|
|
"repeat(a, 3)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 147,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2, 1, 2, 1, 2],\n",
|
||
|
|
" [3, 4, 3, 4, 3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 147,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# tile the matrix 3 times \n",
|
||
|
|
"tile(a, 3)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### concatenate"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 148,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"b = array([[5, 6]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 149,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2],\n",
|
||
|
|
" [3, 4],\n",
|
||
|
|
" [5, 6]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 149,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"concatenate((a, b), axis=0)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 150,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2, 5],\n",
|
||
|
|
" [3, 4, 6]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 150,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"concatenate((a, b.T), axis=1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"### hstack and vstack"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 151,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2],\n",
|
||
|
|
" [3, 4],\n",
|
||
|
|
" [5, 6]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 151,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"vstack((a,b))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 152,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2, 5],\n",
|
||
|
|
" [3, 4, 6]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 152,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"hstack((a,b.T))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Copy and \"deep copy\""
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"To achieve high performance, assignments in Python usually do not copy the underlying objects. This is important for example when objects are passed between functions, to avoid an excessive amount of memory copying when it is not necessary (technical term: pass by reference). "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 153,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[1, 2],\n",
|
||
|
|
" [3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 153,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A = array([[1, 2], [3, 4]])\n",
|
||
|
|
"\n",
|
||
|
|
"A"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 154,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"# now B is referring to the same array data as A \n",
|
||
|
|
"B = A "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 155,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[10, 2],\n",
|
||
|
|
" [ 3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 155,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# changing B affects A\n",
|
||
|
|
"B[0,0] = 10\n",
|
||
|
|
"\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 156,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[10, 2],\n",
|
||
|
|
" [ 3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 156,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"If we want to avoid this behavior, so that when we get a new completely independent object `B` copied from `A`, then we need to do a so-called \"deep copy\" using the function `copy`:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 157,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"B = copy(A)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 158,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[-5, 2],\n",
|
||
|
|
" [ 3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 158,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# now, if we modify B, A is not affected\n",
|
||
|
|
"B[0,0] = -5\n",
|
||
|
|
"\n",
|
||
|
|
"B"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 159,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[10, 2],\n",
|
||
|
|
" [ 3, 4]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 159,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"A"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Iterating over array elements"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Generally, we want to avoid iterating over the elements of arrays whenever we can (at all costs). The reason is that in a interpreted language like Python (or MATLAB), iterations are really slow compared to vectorized operations. \n",
|
||
|
|
"\n",
|
||
|
|
"However, sometimes iterations are unavoidable. For such cases, the Python `for` loop is the most convenient way to iterate over an array:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 160,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"1\n",
|
||
|
|
"2\n",
|
||
|
|
"3\n",
|
||
|
|
"4\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"v = array([1,2,3,4])\n",
|
||
|
|
"\n",
|
||
|
|
"for element in v:\n",
|
||
|
|
" print(element)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 161,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"('row', array([1, 2]))\n",
|
||
|
|
"1\n",
|
||
|
|
"2\n",
|
||
|
|
"('row', array([3, 4]))\n",
|
||
|
|
"3\n",
|
||
|
|
"4\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M = array([[1,2], [3,4]])\n",
|
||
|
|
"\n",
|
||
|
|
"for row in M:\n",
|
||
|
|
" print(\"row\", row)\n",
|
||
|
|
" \n",
|
||
|
|
" for element in row:\n",
|
||
|
|
" print(element)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"When we need to iterate over each element of an array and modify its elements, it is convenient to use the `enumerate` function to obtain both the element and its index in the `for` loop: "
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 162,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"('row_idx', 0, 'row', array([1, 2]))\n",
|
||
|
|
"('col_idx', 0, 'element', 1)\n",
|
||
|
|
"('col_idx', 1, 'element', 2)\n",
|
||
|
|
"('row_idx', 1, 'row', array([3, 4]))\n",
|
||
|
|
"('col_idx', 0, 'element', 3)\n",
|
||
|
|
"('col_idx', 1, 'element', 4)\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"for row_idx, row in enumerate(M):\n",
|
||
|
|
" print(\"row_idx\", row_idx, \"row\", row)\n",
|
||
|
|
" \n",
|
||
|
|
" for col_idx, element in enumerate(row):\n",
|
||
|
|
" print(\"col_idx\", col_idx, \"element\", element)\n",
|
||
|
|
" \n",
|
||
|
|
" # update the matrix M: square each element\n",
|
||
|
|
" M[row_idx, col_idx] = element ** 2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 163,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 1, 4],\n",
|
||
|
|
" [ 9, 16]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 163,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# each element in M is now squared\n",
|
||
|
|
"M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Vectorizing functions"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"As mentioned several times by now, to get good performance we should try to avoid looping over elements in our vectors and matrices, and instead use vectorized algorithms. The first step in converting a scalar algorithm to a vectorized algorithm is to make sure that the functions we write work with vector inputs."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 164,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"def Theta(x):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" Scalar implementation of the Heaviside step function.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" if x >= 0:\n",
|
||
|
|
" return 1\n",
|
||
|
|
" else:\n",
|
||
|
|
" return 0"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 165,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"ename": "ValueError",
|
||
|
|
"evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
|
||
|
|
"output_type": "error",
|
||
|
|
"traceback": [
|
||
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||
|
|
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||
|
|
"\u001b[0;32m<ipython-input-165-6658efdd2f22>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mTheta\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||
|
|
"\u001b[0;32m<ipython-input-164-9a0cb13d93d4>\u001b[0m in \u001b[0;36mTheta\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mScalar\u001b[0m \u001b[0mimplemenation\u001b[0m \u001b[0mof\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mHeaviside\u001b[0m \u001b[0mstep\u001b[0m \u001b[0mfunction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \"\"\"\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||
|
|
"\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"Theta(array([-3,-2,-1,0,1,2,3]))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"OK, that didn't work because we didn't write the `Theta` function so that it can handle a vector input... \n",
|
||
|
|
"\n",
|
||
|
|
"To get a vectorized version of Theta we can use the Numpy function `vectorize`. In many cases it can automatically vectorize a function:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 166,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"Theta_vec = vectorize(Theta)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 167,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 0, 0, 1, 1, 1, 1])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 167,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"Theta_vec(array([-3,-2,-1,0,1,2,3]))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"We can also implement the function to accept a vector input from the beginning (requires more effort but might give better performance):"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 168,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"def Theta(x):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" Vector-aware implementation of the Heaviside step function.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" return 1 * (x >= 0)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 169,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([0, 0, 0, 1, 1, 1, 1])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 169,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"Theta(array([-3,-2,-1,0,1,2,3]))"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 170,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"(0, 1)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 170,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"# still works for scalars as well\n",
|
||
|
|
"Theta(-1.2), Theta(2.6)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Using arrays in conditions"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"When using arrays in conditions,for example `if` statements and other boolean expressions, one needs to use `any` or `all`, which requires that any or all elements in the array evaluates to `True`:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 171,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 1, 4],\n",
|
||
|
|
" [ 9, 16]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 171,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 172,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"at least one element in M is larger than 5\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"if (M > 5).any():\n",
|
||
|
|
" print(\"at least one element in M is larger than 5\")\n",
|
||
|
|
"else:\n",
|
||
|
|
" print(\"no element in M is larger than 5\")"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 173,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"all elements in M are not larger than 5\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"if (M > 5).all():\n",
|
||
|
|
" print(\"all elements in M are larger than 5\")\n",
|
||
|
|
"else:\n",
|
||
|
|
" print(\"all elements in M are not larger than 5\")"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Type casting"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"Since Numpy arrays are *statically typed*, the type of an array does not change once created. But we can explicitly cast an array of some type to another using the `astype` functions (see also the similar `asarray` function). This always creates a new array of new type:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 174,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"dtype('int64')"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 174,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M.dtype"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 175,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ 1., 4.],\n",
|
||
|
|
" [ 9., 16.]])"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 175,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M2 = M.astype(float)\n",
|
||
|
|
"\n",
|
||
|
|
"M2"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 176,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"dtype('float64')"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 176,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M2.dtype"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 177,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"text/plain": [
|
||
|
|
"array([[ True, True],\n",
|
||
|
|
" [ True, True]], dtype=bool)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 177,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"M3 = M.astype(bool)\n",
|
||
|
|
"\n",
|
||
|
|
"M3"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Further reading"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"* 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."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Versions"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 178,
|
||
|
|
"metadata": {
|
||
|
|
"collapsed": false,
|
||
|
|
"jupyter": {
|
||
|
|
"outputs_hidden": false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"data": {
|
||
|
|
"application/json": {
|
||
|
|
"Software versions": [
|
||
|
|
{
|
||
|
|
"module": "Python",
|
||
|
|
"version": "2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"module": "IPython",
|
||
|
|
"version": "3.2.1"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"module": "OS",
|
||
|
|
"version": "Darwin 14.1.0 x86_64 i386 64bit"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"module": "numpy",
|
||
|
|
"version": "1.9.2"
|
||
|
|
}
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"text/html": [
|
||
|
|
"<table><tr><th>Software</th><th>Version</th></tr><tr><td>Python</td><td>2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]</td></tr><tr><td>IPython</td><td>3.2.1</td></tr><tr><td>OS</td><td>Darwin 14.1.0 x86_64 i386 64bit</td></tr><tr><td>numpy</td><td>1.9.2</td></tr><tr><td colspan='2'>Sat Aug 15 11:02:09 2015 JST</td></tr></table>"
|
||
|
|
],
|
||
|
|
"text/latex": [
|
||
|
|
"\\begin{tabular}{|l|l|}\\hline\n",
|
||
|
|
"{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n",
|
||
|
|
"Python & 2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)] \\\\ \\hline\n",
|
||
|
|
"IPython & 3.2.1 \\\\ \\hline\n",
|
||
|
|
"OS & Darwin 14.1.0 x86\\_64 i386 64bit \\\\ \\hline\n",
|
||
|
|
"numpy & 1.9.2 \\\\ \\hline\n",
|
||
|
|
"\\hline \\multicolumn{2}{|l|}{Sat Aug 15 11:02:09 2015 JST} \\\\ \\hline\n",
|
||
|
|
"\\end{tabular}\n"
|
||
|
|
],
|
||
|
|
"text/plain": [
|
||
|
|
"Software versions\n",
|
||
|
|
"Python 2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]\n",
|
||
|
|
"IPython 3.2.1\n",
|
||
|
|
"OS Darwin 14.1.0 x86_64 i386 64bit\n",
|
||
|
|
"numpy 1.9.2\n",
|
||
|
|
"Sat Aug 15 11:02:09 2015 JST"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
"execution_count": 178,
|
||
|
|
"metadata": {},
|
||
|
|
"output_type": "execute_result"
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"%reload_ext version_information\n",
|
||
|
|
"\n",
|
||
|
|
"%version_information numpy"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"metadata": {
|
||
|
|
"kernelspec": {
|
||
|
|
"display_name": "Python (pyscixx)",
|
||
|
|
"language": "python",
|
||
|
|
"name": "pyscixx"
|
||
|
|
},
|
||
|
|
"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.12.11"
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"nbformat": 4,
|
||
|
|
"nbformat_minor": 4
|
||
|
|
}
|