{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# numpy: Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Difference in Speed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's import the `numpy` module."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"[0 1 2 3 4 5 6 7 8 9]\n"
]
}
],
"source": [
"n = 10 # CHANGE ME\n",
"a1 = list(range(n))\n",
"a2 = np.arange(n)\n",
"\n",
"if n <= 10:\n",
" print(a1)\n",
" print(a2)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100000 loops, best of 3: 2.41 \u00b5s per loop\n"
]
}
],
"source": [
"%timeit [i**2 for i in a1]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 34.86 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 400 ns per loop\n"
]
}
],
"source": [
"%timeit a2**2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy Arrays: much less flexible, but:\n",
"\n",
"* much faster\n",
"* less memory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Why the difference?"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The gvmagic extension is already loaded. To reload it, use:\n",
" %reload_ext gvmagic\n"
]
}
],
"source": [
"# (This cell contains a bunch of voodoo that\n",
"# helps with the graphics below. You don't need to\n",
"# know what this does.)\n",
"\n",
"%load_ext gvmagic\n",
"from objgraph_helper import dot_refs"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%dotstr dot_refs([a1])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%dotstr dot_refs([a2])"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(8,)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2.strides"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ways to create a numpy array\n",
"\n",
"* Casting from a list"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array([1,2,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* `linspace`"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([-1. , -0.77777778, -0.55555556, -0.33333333, -0.11111111,\n",
" 0.11111111, 0.33333333, 0.55555556, 0.77777778, 1. ])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linspace(-1, 1, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* `zeros`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros((10,10), np.float64)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operations on arrays\n",
"\n",
"These propagate to all elements:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"a = np.array([1.2, 3, 4])\n",
"b = np.array([0.5, 0, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Addition, multiplication, power, .. are all elementwise:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1.7, 3. , 5. ])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a+b"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.6, 0. , 4. ])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1.09544512, 1. , 4. ])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a**b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Matrix multiplication is `np.dot(A, B)` for two 2D arrays."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Important Attributes\n",
"\n",
"Numpy arrays have two (most) important attributes:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(5, 4, 3)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.random.rand(5, 4, 3)\n",
"a.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `.shape` attribute contains the dimensionality array as a tuple. So the tuple `(5,4,3)` means that we're dealing with a three-dimensional array of size $5 \\times 4 \\times 3$.\n",
"\n",
"(`numpy.random.rand` just generates an array of random numbers of the given shape.)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other `dtype`s include `np.complex64`, `np.int32`, ..."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2+"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}