pm21-dragon/lectures/lecture-07/1 - numpy.ipynb

2883 lines
64 KiB
Plaintext
Raw Normal View History

2024-11-29 03:11:00 -05:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\"Alle Teilnehmer:innen biologischer Profilmodule werden zur Studienleistung angemeldet.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numpy introduction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# numpy basics and creating arrays\n",
"\n",
"Numpy is a widely used library for handling arrays of data, especially numerical data. It would not be an exageration to say it is fundamental to the Python data science ecosystem.\n",
"\n",
"The most important part of numpy is the numpy `array` type. A numpy `array` is conceptually similar to a Python `list` or `tuple` but each element has the same data type and the array has a fixed size.\n",
"\n",
"Typically `numpy` is imported as `np`, a conventional shorthand that saves a bit of typing."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 4,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can create an array from any sequence type, such as lists and tuples:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 5,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.array([1,2,3,4])\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can crate an array of `n` elements (from `0` to `n-1`) with the `arange` function."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 6,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(10)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 7,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(4,10)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 8,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(0,10,1)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 9,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 6, 8])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(4,10,2)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can create an array of `n` equally spaced elements from `start` to `stop` with `np.linspace`. For example, here `start` is `100`, `stop` is `120` and `n` is 11."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 12,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([100., 102., 104., 106., 108., 110., 112., 114., 116., 118., 120.])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.linspace(100, 120, 11)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also create arrays of zero or one with a given shape:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 13,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0., 0., 0.])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.zeros((3,))"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 14,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0.]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.zeros((3,5)) # shape parameter - (number of rows, number of columns) in this case"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 15,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.],\n",
" [1., 1., 1.]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.ones((5,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## array shape\n",
"\n",
"In addition to 1 dimensional numpy arrays which are very similar to lists or tuples, numpy arrays may also be 2 or more dimensions. The `shape` attribute of a numpy array may be used to get or set its number of dimensions and size."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 16,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(12)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 17,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"(12,)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.shape"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 18,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(12)\n",
"x.shape = (3,4)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 19,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.shape"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 20,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"ename": "ValueError",
"evalue": "cannot reshape array of size 12 into shape (3,2)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m x\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m2\u001b[39m)\n",
"\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 12 into shape (3,2)"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.shape = (3,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-12-02 04:47:50 -05:00
"The `ndim` attribute is the dimensionality of the array (and, thus, equal to length of the array's `shape` attribute):"
2024-11-29 03:11:00 -05:00
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 21,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.ndim"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 23,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 1],\n",
" [ 2, 3]],\n",
"\n",
" [[ 4, 5],\n",
" [ 6, 7]],\n",
"\n",
" [[ 8, 9],\n",
" [10, 11]]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(12)\n",
"x.shape = (3,2,2)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 24,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## array operations\n",
"\n",
"Numpy arrays support mathematical operations with other numpy arrays and with single numbers (\"scalars\").\n",
"\n",
"With scalars, the scalar is first converted to an array with the same shape as the numpy array and then an element-wise operation is performed.\n",
"\n",
"With other arrays of the same size, an element-wise operation is performed."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 25,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(10)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 26,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y = 2 * np.arange(10)\n",
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 27,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z = x + y\n",
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 28,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x + 3"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 29,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x + 3.0"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 30,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 31,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x/5"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 32,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"0.2"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"1/5"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 33,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 34,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"4*x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 35,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.array([ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27])/3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## array dtype\n",
"\n",
"Just like lists or tuples, every element in a numpy array has a data type. As mentioned above, however, every element in a numpy array has the same data type, and thus we can refer to the \"datatype of the array\". This can be set when the array is created with the `dtype` keyword argument and read from the `dtype` attribute:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 36,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(10)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 37,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.dtype"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 38,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(10, dtype=np.float64)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 39,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## array indexing and slicing\n",
"\n",
"Numpy arrays can be indexed and sliced just like other Python sequence types such as lists, tuples, and strings.\n",
"\n",
2024-12-02 04:47:50 -05:00
"Just like with python lists, the indices or slices can be read and written. In other words, numpy arrays are *mutable*."
2024-11-29 03:11:00 -05:00
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 40,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(10)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 41,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[4]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 42,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[:4]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 56,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[4:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 54,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"tmp = slice(4)\n",
"x[tmp]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 57,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"tmp = slice(4, 7)\n",
"x[tmp]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 58,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[4:7]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 59,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"tmp = slice(2, 7, 2)\n",
"x[tmp]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 60,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6, 8])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"tmp = slice(2, None, 2)\n",
"x[tmp]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 61,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6, 8])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[2::2]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 62,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 7])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[4::3]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 63,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6])"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[4:7]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 64,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"ename": "TypeError",
"evalue": "slice indices must be integers or None or have an __index__ method",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[64], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m x[\u001b[38;5;241m2\u001b[39m::\u001b[38;5;241m2.2\u001b[39m]\n",
"\u001b[0;31mTypeError\u001b[0m: slice indices must be integers or None or have an __index__ method"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[2::2.2]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 65,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 2.2, 4.4, 6.6, 8.8])"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.arange(0, 10, 2.2)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 66,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 2, 4, 6, 8])"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[::2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because numpy arrays can have 2 or more, dimensions, we can also index and slice them in higher dimensions. For two dimensional arrays, the first index is always the row index and the second index is always the column index."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 67,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(12)\n",
"x.shape = (3,4)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 68,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[1:, :]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 69,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [ 5, 6, 7],\n",
" [ 9, 10, 11]])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[:, 1:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 70,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 6, 7],\n",
" [10, 11]])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[1:, 2:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 71,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[4, 5, 6, 7]])"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[1:2:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 72,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 73,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 99, 99],\n",
" [ 8, 9, 99, 99]])"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[1:, 2:] = 99\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 74,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.dtype"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 75,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 99, 99],\n",
" [ 8, 9, 99, 99]])"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[1:, 2:] = 99.5\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 76,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 99, 99],\n",
" [ 8, 9, 99, 99]])"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[1:, 2:] = 99.9\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 77,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[99, 99, 99, 99],\n",
" [99, 99, 99, 99],\n",
" [99, 99, 99, 99]])"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[:, :] = 99.9\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References to arrays\n",
"\n",
"Remember that variable assignment in Python does not create a new object but only creates a variable which points to an existing object. This is very important with numpy."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 78,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19])"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(20)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 79,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"# Here we create a variable which references the first 10 elements of `x`.\n",
"y = x[:10]\n",
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 80,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"x[4] = 9999"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 81,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 9999, 5, 6, 7, 8, 9, 10,\n",
" 11, 12, 13, 14, 15, 16, 17, 18, 19])"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 82,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 9999, 5, 6, 7, 8, 9])"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 83,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([123, 123, 123, 123, 123, 123, 123, 123, 123, 123])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"# Now we assign all the elements of `y` to have the value of 123.\n",
"# We do this by creating a slice into the array `y` and assigning to it.\n",
"y[:] = 123\n",
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 84,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 10, 11, 12,\n",
" 13, 14, 15, 16, 17, 18, 19])"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"# How does this affect the original array `x`?\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 85,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"y[-1] = 999"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 86,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([123, 123, 123, 123, 123, 123, 123, 123, 123, 999, 10, 11, 12,\n",
" 13, 14, 15, 16, 17, 18, 19])"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 87,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"y[::2] = -1"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 88,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ -1, 123, -1, 123, -1, 123, -1, 123, -1, 999, 10, 11, 12,\n",
" 13, 14, 15, 16, 17, 18, 19])"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 89,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"x[:3] = -100"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 90,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-100, -100, -100, 123, -1, 123, -1, 123, -1, 999, 10,\n",
" 11, 12, 13, 14, 15, 16, 17, 18, 19])"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 91,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-100, -100, -100, 123, -1, 123, -1, 123, -1, 999])"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 92,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"z = y.copy()"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 93,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-100, -100, -100, 123, -1, 123, -1, 123, -1, 999])"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 94,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-100, -100, -100, 123, -1, 123, -1, 123, -1, 999])"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 95,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"y[:2] = -9999"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 96,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-9999, -9999, -100, 123, -1, 123, -1, 123, -1,\n",
" 999])"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 97,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-100, -100, -100, 123, -1, 123, -1, 123, -1, 999])"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 98,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"z = y[:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 99,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-9999, -9999, -100, 123, -1, 123, -1, 123, -1,\n",
" 999])"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 100,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-9999, -9999, -100, 123, -1, 123, -1, 123, -1,\n",
" 999])"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 101,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"y[:2] = 444444"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 102,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([444444, 444444, -100, 123, -1, 123, -1, 123,\n",
" -1, 999])"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 103,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([444444, 444444, -100, 123, -1, 123, -1, 123,\n",
" -1, 999])"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 104,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z = np.arange(12)\n",
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 105,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"zref = z[::3]\n",
"zref[:] = 999"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 106,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([999, 1, 2, 999, 4, 5, 999, 7, 8, 999, 10, 11])"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 107,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"zrefref = zref[::2]\n",
"zrefref[:] = -1000"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 108,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([-1000, 1, 2, 999, 4, 5, -1000, 7, 8,\n",
" 999, 10, 11])"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"z"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Array *slices* - a key difference between a numpy array and a Python list\n",
"\n",
"With a numpy array, a slice is created by `[:]` (e.g. `my_array[:]`). With a plain Python list, `[:]` will return a copy of the list.\n",
"\n",
"For both numpy arrays and Python lists, the `.copy()` method will make a copy, so this is preferred if you want to be sure you are making a copy."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 109,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"# First with a list\n",
"a = [1,2,3]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 110,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"b = a[:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 111,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"a[0] = 100"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 112,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"b"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 113,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"[100, 2, 3]"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"a"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 114,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"# Now with a numpy array\n",
"a = np.array([1,2,3])"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 115,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"b = a[:]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 116,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"a[0] = 100"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 117,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([100, 2, 3])"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"b"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 118,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([100, 2, 3])"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Efficient data processing with numpy\n",
"\n",
"Because operations on numpy arrays happen for all elements with a single Python expression, these can operations can be performed very fast and efficiently by the computer. For example, if `x` is a numpy array with 10,000 elements, we can avoid a Python for loop with 10,000 iterations by performing our work with numpy.\n",
"\n",
"Below we use the Jupyter \"magic command `%timeit`\" to measure how long a single expression takes, in this case performing an element-wise multiplication."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 119,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(10000, dtype=np.float64)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 120,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0.0000000e+00, 1.0000000e+00, 4.0000000e+00, ..., 9.9940009e+07,\n",
" 9.9960004e+07, 9.9980001e+07])"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x*x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 121,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.6 μs ± 15.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"%timeit x*x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 122,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"y = x*x\n",
"len(y)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 124,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
2024-12-02 04:47:50 -05:00
"assert y[2] == 4\n",
"assert y[3] == 9"
2024-11-29 03:11:00 -05:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's do the same as above with a Python `list`. We need to crease a list_mul function."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 125,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"def list_mul(a,b):\n",
" \"\"\"element-wise product of `a` and `b`\"\"\"\n",
" n = len(a)\n",
" assert n==len(b) \n",
" c = []\n",
" for i in range(n):\n",
" c.append(a[i] * b[i])\n",
" return c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now convert `x` to a list from a numpy array."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 126,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"x = list(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 127,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"type(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 128,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[:10]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 129,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"483 μs ± 24.9 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"%timeit list_mul(x,x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 130,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"y = list_mul(x,x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 131,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"assert y[3] == 9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Elementwise numpy operations\n",
"\n",
"Above you have already seen element-wise multiplication, which multiplies every element of two inputs. Similarly, other operations operate element wise on a single input array."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 132,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([1., 2., 3.])"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.sqrt( np.array([1, 4, 9]))"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 133,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 0.21666156, 0.43332312, 0.64998469, 0.86664625,\n",
" 1.08330781, 1.29996937, 1.51663094, 1.7332925 , 1.94995406,\n",
" 2.16661562, 2.38327719, 2.59993875, 2.81660031, 3.03326187,\n",
" 3.24992343, 3.466585 , 3.68324656, 3.89990812, 4.11656968,\n",
" 4.33323125, 4.54989281, 4.76655437, 4.98321593, 5.1998775 ,\n",
" 5.41653906, 5.63320062, 5.84986218, 6.06652374, 6.28318531])"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.linspace( 0, 2*np.pi, 30) "
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 134,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 1. , 0.97662056, 0.90757542, 0.79609307, 0.64738628,\n",
" 0.46840844, 0.26752834, 0.05413891, -0.161782 , -0.37013816,\n",
" -0.56118707, -0.72599549, -0.85685718, -0.94765317, -0.99413796,\n",
" -0.99413796, -0.94765317, -0.85685718, -0.72599549, -0.56118707,\n",
" -0.37013816, -0.161782 , 0.05413891, 0.26752834, 0.46840844,\n",
" 0.64738628, 0.79609307, 0.90757542, 0.97662056, 1. ])"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.cos( np.linspace( 0, 2*np.pi, 30) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## More numpy operations\n",
"\n",
"In addition to elementwise operations such as `np.cos(x)` or `x * y` where `x` and `y` are same-shaped arrays, numpy can also perform operations on entire arrays.\n",
"\n",
"Take for example the `mean()` function."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 135,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(10)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 136,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"4.5"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.mean(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also do the mean on a 2D array, either for the entire array or row-wise or column-wise:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 137,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4, 5],\n",
" [ 6, 7, 8, 9, 10, 11],\n",
" [12, 13, 14, 15, 16, 17],\n",
" [18, 19, 20, 21, 22, 23],\n",
" [24, 25, 26, 27, 28, 29]])"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.arange(30)\n",
"x.shape = (5,6)\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 138,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"14.5"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.mean(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 139,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([12., 13., 14., 15., 16., 17.])"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"# take the mean across the rows, (i.e. mean of each column), which is axis 0.\n",
"np.mean(x,axis=0)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 140,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 2.5, 8.5, 14.5, 20.5, 26.5])"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"# take the mean across the columns, which is axis 1.\n",
"np.mean(x,axis=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to `mean()`, numpy provides `std()`, `sum()`, and more. "
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 141,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"8.65544144839919"
]
},
"execution_count": 141,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.std(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 142,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"435"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.sum(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 143,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"29"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.max(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 144,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"14.5"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x.mean()"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 145,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"14.5"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.mean(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## argmin and argmax\n",
"\n",
"Important in many scientific computing applications are `argmin` and `argmax` functions. These return the index of the smallest or largest value, respectively."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 146,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 10, -10, 4, 3, 2, 100, 2, 2, -1])"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.array([0, 10, -10, 4, 3, 2, 100, 2, 2, -1])\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 147,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"min_idx = np.argmin(x)\n",
"min_idx"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 148,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"-10"
]
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[min_idx]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 149,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"-10"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.min(x)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 150,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 10, -10, 4, 3, 2, 100, 2, 2, -1])"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 151,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"max_idx = np.argmax(x)\n",
"max_idx"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 152,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x[max_idx]"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 153,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 100, 0, 4, 3, 2, 100, 2, 2, -1])"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"x = np.array([0, 100, 0, 4, 3, 2, 100, 2, 2, -1])\n",
"x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 154,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"np.argmax(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Because of its speed, numpy makes it possible to use Python for scientific computing.\n",
"\n",
"You can read more about numpy at its [User Guide](https://numpy.org/doc/1.26/user/index.html) and its [Reference Guide](https://numpy.org/doc/1.26/reference/index.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Live coding example: calculate distance between 2D points."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 155,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 156,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"image/png": "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
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"a = (10, 20)\n",
"b = (13, 24)\n",
"\n",
"plt.plot([a[0]], [a[1]], 'o', label='a')\n",
"plt.plot([b[0]], [b[1]], 'o', label='b')\n",
"plt.plot([5, 15, 15, 5, 5], [15, 15, 25, 25, 15], 'k-')\n",
"plt.legend();"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 157,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
2024-12-02 04:47:50 -05:00
"source": [
"def compute_distance(a,b):\n",
" dx = a[0] - b[0]\n",
" dy = a[1] - b[1]\n",
" return np.sqrt(dx*dx + dy*dy)"
]
2024-11-29 03:11:00 -05:00
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 158,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"compute_distance(a,b)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 159,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"assert compute_distance(a,b)==5.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See also https://twitter.com/MIT_CSAIL/status/1459932891765297153?s=20\n",
"\n",
"![FEA8OpTWYAAxNjx.png](FEA8OpTWYAAxNjx.png)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}