2024-heraklion-data/exercises/.ipynb_checkpoints/view_or_copy-checkpoint.ipynb

463 lines
8 KiB
Plaintext
Raw Permalink Normal View History

2024-08-27 14:27:53 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 107,
"id": "4ccf18f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"\n",
"def is_view(a):\n",
" return a.base is not None\n",
"\n",
"\n",
"def print_info(a):\n",
" txt = f\"\"\"\n",
"Is it a view? {is_view(a)}\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)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "b68308e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(2, 4)\n",
"strides\t(64, 8)\n",
" \n"
]
}
],
"source": [
"y = x[::2, :]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"id": "85feedb0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6 7]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(4,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[1, :]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "dbbb9a7f",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6 7]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(4,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[1]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "fc63ad8c",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 4 11]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(2,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[[1, 2], [0, 3]]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "aa8effeb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(2, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"# Get the first and third row\n",
"y = x[[0, 2], :]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "4474f8cf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2 3 4 5]\n",
" [ 6 7 8 9]\n",
" [10 11 12 13]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"y = x + 2\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "4957469c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5 9 2]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(3,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[[1, 2, 0], [1, 1, 2]]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "d1649515",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1]\n",
" [ 2 3]\n",
" [ 4 5]\n",
" [ 6 7]\n",
" [ 8 9]\n",
" [10 11]]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(6, 2)\n",
"strides\t(16, 8)\n",
" \n"
]
}
],
"source": [
"y = x.reshape((6, 2))\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"id": "c5caeb74",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 4]\n",
" [ 8 1]\n",
" [ 5 9]\n",
" [ 2 6]\n",
" [10 3]\n",
" [ 7 11]]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(6, 2)\n",
"strides\t(16, 8)\n",
" \n"
]
}
],
"source": [
"y = x.T.reshape((6, 2))\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "15b0d7ad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11]\n",
"\n",
"Is it a view? True\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(12,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x.ravel()\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "38cc1ef3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 1 5 9 2 6 10 3 7 11]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(12,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x.T.ravel()\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "b7d0cc63",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 2 4 6 8 10]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(6,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [
"y = x[(x % 2) == 0]\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "866e842a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t2\n",
"shape\t(3, 4)\n",
"strides\t(32, 8)\n",
" \n"
]
}
],
"source": [
"y = np.sort(x, axis=1)\n",
"print_info(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a074c89b",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "320cfb50",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}