ASPP 2024 material

This commit is contained in:
Pietro Berkes 2024-08-27 15:27:53 +03:00
commit 1f6bc07c51
90 changed files with 91689 additions and 0 deletions

View file

@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "774cc021",
"metadata": {},
"source": [
"# Is it a view or a copy?\n",
"\n",
"- For every operations on the array `x`, decide whether it returns a view of `x`, or a copy\n",
"- If it's a view, derive the new values for the metadata (ndim, shape, strides)\n",
"- You can verify your answers with the function `print_info` defined below"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ccf18f3",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"\n",
"def is_view(a, x):\n",
" \"\"\" Is `a` a view of `x`? \"\"\"\n",
" return a.base is x\n",
"\n",
"\n",
"def print_info(a, x):\n",
" txt = f\"\"\"\n",
"Is it a view? {is_view(a, x)}\n",
"\n",
"dtype\\t{a.dtype}\n",
"ndim\\t{a.ndim}\n",
"shape\\t{a.shape}\n",
"strides\\t{a.strides}\n",
" \"\"\"\n",
" print(a)\n",
" print(txt)\n",
"\n",
"\n",
"x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b68308e8",
"metadata": {},
"outputs": [],
"source": [
"y = x[::2, :]\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85feedb0",
"metadata": {},
"outputs": [],
"source": [
"y = x[1, :]\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbbb9a7f",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"y = x[1]\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4957469c",
"metadata": {},
"outputs": [],
"source": [
"y = x[[1, 2, 0], [1, 1, 2]]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa8effeb",
"metadata": {},
"outputs": [],
"source": [
"# Get the first and third row\n",
"y = x[[0, 2], :]\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1649515",
"metadata": {},
"outputs": [],
"source": [
"y = x.reshape((6, 2))\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15b0d7ad",
"metadata": {},
"outputs": [],
"source": [
"y = x.ravel()\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38cc1ef3",
"metadata": {},
"outputs": [],
"source": [
"y = x.T.ravel()\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7d0cc63",
"metadata": {},
"outputs": [],
"source": [
"# Get all the odd elements\n",
"y = x[(x % 2) == 1]\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4474f8cf",
"metadata": {},
"outputs": [],
"source": [
"y = x + 2\n",
"#print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "866e842a",
"metadata": {},
"outputs": [],
"source": [
"y = np.sort(x, axis=1)\n",
"#print_info(y ,x)"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "774cc021",
"metadata": {},
"source": [
"# Is it a view or a copy?\n",
"\n",
"- For every operations on the array `x`, decide whether it returns a view of `x`, or a copy\n",
"- If it's a view, derive the new values for the metadata (ndim, shape, strides)\n",
"- You can verify your answers with the function `print_info` defined below"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ccf18f3",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"\n",
"def is_view(a, x):\n",
" \"\"\" Is `a` a view of `x`? \"\"\"\n",
" return a.base is x\n",
"\n",
"\n",
"def print_info(a, x):\n",
" txt = f\"\"\"\n",
"Is it a view? {is_view(a, x)}\n",
"\n",
"dtype\\t{a.dtype}\n",
"ndim\\t{a.ndim}\n",
"shape\\t{a.shape}\n",
"strides\\t{a.strides}\n",
" \"\"\"\n",
" print(a)\n",
" print(txt)\n",
"\n",
"\n",
"x = np.arange(12).reshape(3, 4).copy()\n",
"print_info(x, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b68308e8",
"metadata": {},
"outputs": [],
"source": [
"y = x[::2, :]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85feedb0",
"metadata": {},
"outputs": [],
"source": [
"y = x[1, :]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbbb9a7f",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"y = x[1]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4957469c",
"metadata": {},
"outputs": [],
"source": [
"y = x[[1, 2, 0], [1, 1, 2]]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa8effeb",
"metadata": {},
"outputs": [],
"source": [
"# Get the first and third row\n",
"y = x[[0, 2], :]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1649515",
"metadata": {},
"outputs": [],
"source": [
"y = x.reshape((6, 2))\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15b0d7ad",
"metadata": {},
"outputs": [],
"source": [
"y = x.ravel()\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38cc1ef3",
"metadata": {},
"outputs": [],
"source": [
"y = x.T.ravel()\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7d0cc63",
"metadata": {},
"outputs": [],
"source": [
"# Get all the odd elements\n",
"y = x[(x % 2) == 1]\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4474f8cf",
"metadata": {},
"outputs": [],
"source": [
"y = x + 2\n",
"print_info(y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "866e842a",
"metadata": {},
"outputs": [],
"source": [
"y = np.sort(x, axis=1)\n",
"print_info(y ,x)"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}