'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": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"id": "86b10564", "id": "86b10564",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
@ -27,31 +27,32 @@
" print(txt)" " 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", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"id": "53bd92f9", "id": "53bd92f9",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "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": [ "source": [
"x = np.arange(12).reshape(3, 4).copy()\n", "x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x)" "print_info(x)"
@ -83,14 +84,29 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 6,
"id": "f1b82845", "id": "f1b82845",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "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": [ "source": [
"# slice\n", "# slice\n",
"y = x[0::2, 1::2]\n", "y = x[0::2, 1::2]\n",
@ -111,14 +127,28 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 7,
"id": "28ea1c71", "id": "28ea1c71",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "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": [ "source": [
"z = x.reshape(1, 12)\n", "z = x.reshape(1, 12)\n",
"print_info(z)" "print_info(z)"
@ -146,29 +176,90 @@
}, },
{ {
"cell_type": "code", "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", "id": "46822b5a",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "subslide" "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": [ "source": [
"y += 100\n", "y += 100 \n",
"print_info(y)" "print_info(y)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 10,
"id": "ad9a7950", "id": "ad9a7950",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "subslide" "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": [ "source": [
"print_info(x)\n", "print_info(x)\n",
"print_info(z)" "print_info(z)"
@ -183,14 +274,14 @@
} }
}, },
"source": [ "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", "\n",
"Always make a copy or be super extra clear in the docstring." "Always make a copy or be super extra clear in the docstring."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 12,
"id": "aa25ac4b", "id": "aa25ac4b",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
@ -199,8 +290,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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",
" Returns the log of an array, deals with values that are equal to 0.\n",
"\n", "\n",
" `x` is expected to have non-negative values.\n", " `x` is expected to have non-negative values.\n",
" \"\"\"\n", " \"\"\"\n",
@ -212,7 +304,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 13,
"id": "471d9d6b", "id": "471d9d6b",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
@ -221,53 +313,89 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"a = np.array([[0.3, 0.01], [0, 1]])" "a = np.array([[96, 0.01], [0, 1]])"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 14,
"id": "6c05d356", "id": "6c05d356",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "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": [ "source": [
"# This is a view of `a`\n", "# a view of `a`\n",
"b = a[1, :]\n", "b = a[1, :]\n",
"print_info(b)" "print_info(b)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 15,
"id": "9d96fb61", "id": "9d96fb61",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "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": [ "source": [
"# what is the output?\n",
"robust_log(a)" "robust_log(a)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 16,
"id": "35d0327d", "id": "35d0327d",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "slide_type": "fragment"
} }
}, },
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"array([3., 1.])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# what is the output?\n", "np.set_printoptions(suppress=True)\n",
"b # what about b??" "b"
] ]
}, },
{ {
@ -284,7 +412,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"id": "c5359eac", "id": "c5359eac",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
@ -293,7 +421,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "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", " \"\"\" Returns the log of an array, deals with values that are 0.\n",
"\n", "\n",
" `x` is expected to have non-negative values.\n", " `x` is expected to have non-negative values.\n",
@ -305,34 +433,63 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 18,
"id": "0bf9b2d5", "id": "0bf9b2d5",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "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": [ "source": [
"a = np.array([[0.3, 0.01], [0, 1]])\n", "a = np.array([[96, 0.01], [0, 1]])\n",
"b = a[1, :]\n", "b = a[1, :]\n",
"\n", "print(b)\n",
"#robust_sqrt(a)" "robust_log(a)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 19,
"id": "895209ce", "id": "895209ce",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "slide_type": "fragment"
} }
}, },
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"array([0., 1.])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"a # what is the output? \n", "b"
"# b"
] ]
}, },
{ {
@ -347,8 +504,7 @@
"# Copies\n", "# Copies\n",
"\n", "\n",
"- Operations that cannot be executed by changing the metadata create a new memory block, and return a **copy**\n", "- Operations that cannot be executed by changing the metadata create a new memory block, and return a **copy**\n",
"\n", "- can be forced by method .copy()"
"- How to find out view or copy?"
] ]
}, },
{ {
@ -367,10 +523,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 20,
"id": "fbcf3100", "id": "fbcf3100",
"metadata": {}, "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": [ "source": [
"x = np.arange(12).reshape(3, 4).copy()\n", "x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x)" "print_info(x)"
@ -378,32 +550,69 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 21,
"id": "6c50e46e", "id": "6c50e46e",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "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": [ "source": [
"#print(x)\n", "#print(x)\n",
"z = x[[0, 0, 2], [1, 0, 3]]\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", "\n",
"print_info(z)" "print_info(z)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 22,
"id": "9d65a5c3", "id": "9d65a5c3",
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "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": [ "source": [
"z += 1000\n", "z += 1000\n",
"print_info(z)\n", "print_info(z)\n",
@ -444,7 +653,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.3" "version": "3.9.12"
} }
}, },
"nbformat": 4, "nbformat": 4,