'added numpy notebook with last minute changes'

This commit is contained in:
Verjinia Metodieva 2024-08-29 17:40:16 +02:00
parent 8420e30651
commit f494d93b3f

View file

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "86b10564",
"metadata": {
"slideshow": {
@ -27,31 +27,32 @@
" print(txt)"
]
},
{
"cell_type": "markdown",
"id": "a5bbf650",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# NumPy views and copies\n",
"\n",
"- Operations that only require changing the metadata always do so, and return a **view**\n",
"- Operations that cannot be executed by changing the metadata create a new memory block, and return a **copy**"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "53bd92f9",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x)"
@ -83,14 +84,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "f1b82845",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 3]\n",
" [ 9 11]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(2, 2)\n",
"strides\t(64, 16)\n",
" \n"
]
}
],
"source": [
"# slice\n",
"y = x[0::2, 1::2]\n",
@ -111,14 +127,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "28ea1c71",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3 4 5 6 7 8 9 10 11]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(1, 12)\n",
"strides\t(96, 8)\n",
" \n"
]
}
],
"source": [
"z = x.reshape(1, 12)\n",
"print_info(z)"
@ -146,14 +176,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "4ff516cc",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 3]\n",
" [ 9 11]]\n"
]
}
],
"source": [
"print(y) # a view of x"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "46822b5a",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[101 103]\n",
" [109 111]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(2, 2)\n",
"strides\t(64, 16)\n",
" \n"
]
}
],
"source": [
"y += 100 \n",
"print_info(y)"
@ -161,14 +229,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "ad9a7950",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 101 2 103]\n",
" [ 4 5 6 7]\n",
" [ 8 109 10 111]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n",
"[[ 0 101 2 103 4 5 6 7 8 109 10 111]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(1, 12)\n",
"strides\t(96, 8)\n",
" \n"
]
}
],
"source": [
"print_info(x)\n",
"print_info(z)"
@ -183,14 +274,14 @@
}
},
"source": [
"Functions that take an array as an input should **avoid modifying it in place!***\n",
"Functions that take an array as an input should **avoid modifying it in place!**\n",
"\n",
"Always make a copy or be super extra clear in the docstring."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "aa25ac4b",
"metadata": {
"slideshow": {
@ -199,8 +290,9 @@
},
"outputs": [],
"source": [
"def robust_log(x, cte=1e-10):\n",
" \"\"\" Returns the log of an array, deals with values that are 0.\n",
"def robust_log(x, cte = 3):\n",
" \"\"\" \n",
" Returns the log of an array, deals with values that are equal to 0.\n",
"\n",
" `x` is expected to have non-negative values.\n",
" \"\"\"\n",
@ -212,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "471d9d6b",
"metadata": {
"slideshow": {
@ -221,53 +313,89 @@
},
"outputs": [],
"source": [
"a = np.array([[0.3, 0.01], [0, 1]])"
"a = np.array([[96, 0.01], [0, 1]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "6c05d356",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 1.]\n",
"\n",
"dtype\tfloat64\n",
"ndim\t1\n",
"shape\t(2,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"# This is a view of `a`\n",
"# a view of `a`\n",
"b = a[1, :]\n",
"print_info(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "9d96fb61",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([[ 4.56434819, -4.60517019],\n",
" [ 1.09861229, 0. ]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# what is the output?\n",
"robust_log(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "35d0327d",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([3., 1.])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# what is the output?\n",
"b # what about b??"
"np.set_printoptions(suppress=True)\n",
"b"
]
},
{
@ -284,7 +412,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "c5359eac",
"metadata": {
"slideshow": {
@ -293,7 +421,7 @@
},
"outputs": [],
"source": [
"def robust_log(x, cte=1e-10):\n",
"def robust_log(x, cte = 3):\n",
" \"\"\" Returns the log of an array, deals with values that are 0.\n",
"\n",
" `x` is expected to have non-negative values.\n",
@ -305,34 +433,63 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "0bf9b2d5",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 1.]\n"
]
},
{
"data": {
"text/plain": [
"array([[ 4.56434819, -4.60517019],\n",
" [ 1.09861229, 0. ]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[0.3, 0.01], [0, 1]])\n",
"a = np.array([[96, 0.01], [0, 1]])\n",
"b = a[1, :]\n",
"\n",
"#robust_sqrt(a)"
"print(b)\n",
"robust_log(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "895209ce",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"array([0., 1.])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a # what is the output? \n",
"# b"
"b"
]
},
{
@ -347,8 +504,7 @@
"# Copies\n",
"\n",
"- Operations that cannot be executed by changing the metadata create a new memory block, and return a **copy**\n",
"\n",
"- How to find out view or copy?"
"- can be forced by method .copy()"
]
},
{
@ -367,10 +523,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "fbcf3100",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x)"
@ -378,32 +550,69 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"id": "6c50e46e",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 0 11]\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(3,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"#print(x)\n",
"z = x[[0, 0, 2], [1, 0, 3]]\n",
"# Can you guess what's z equal to?\n",
"# what's z equal to?\n",
"\n",
"print_info(z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"id": "9d65a5c3",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1001 1000 1011]\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(3,)\n",
"strides\t(8,)\n",
" \n",
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"z += 1000\n",
"print_info(z)\n",
@ -444,7 +653,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
"version": "3.9.12"
}
},
"nbformat": 4,