clears output of views vs copies interactive exercise

This commit is contained in:
Guillermo Aguilar 2025-09-23 18:31:01 +02:00
parent 4930de7f0c
commit 018c883648

View file

@ -10,7 +10,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"id": "4ccf18f3", "id": "4ccf18f3",
"metadata": { "metadata": {
"scrolled": true "scrolled": true
@ -39,28 +39,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"id": "8f62b9a8", "id": "8f62b9a8",
"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",
"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": [ "source": [
"x = np.arange(12).reshape(3, 4).copy()\n", "x = np.arange(12).reshape(3, 4).copy()\n",
"print(x)\n", "print(x)\n",
@ -69,19 +51,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"id": "b68308e8", "id": "b68308e8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 8 9 10 11]]\n"
]
}
],
"source": [ "source": [
"y = x[::2, :]\n", "y = x[::2, :]\n",
"print(y)" "print(y)"
@ -89,43 +62,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "3737da3c", "id": "3737da3c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"\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": [ "source": [
"print_info(y, x)" "print_info(y, x)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "85feedb0", "id": "85feedb0",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6 7]\n"
]
}
],
"source": [ "source": [
"y = x[1, :]\n", "y = x[1, :]\n",
"print(y)" "print(y)"
@ -143,18 +93,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": null,
"id": "4957469c", "id": "4957469c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5 9 2]\n"
]
}
],
"source": [ "source": [
"y = x[[1, 2, 0], [1, 1, 2]]\n", "y = x[[1, 2, 0], [1, 1, 2]]\n",
"print(y)" "print(y)"
@ -172,19 +114,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": null,
"id": "aa8effeb", "id": "aa8effeb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3]\n",
" [ 8 9 10 11]]\n"
]
}
],
"source": [ "source": [
"# Get the first and third row\n", "# Get the first and third row\n",
"y = x[[0, 2], :]\n", "y = x[[0, 2], :]\n",
@ -203,23 +136,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": null,
"id": "d1649515", "id": "d1649515",
"metadata": {}, "metadata": {},
"outputs": [ "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"
]
}
],
"source": [ "source": [
"y = x.reshape((6, 2))\n", "y = x.reshape((6, 2))\n",
"print(y)" "print(y)"
@ -237,18 +157,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": null,
"id": "15b0d7ad", "id": "15b0d7ad",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 3 4 5 6 7 8 9 10 11]\n"
]
}
],
"source": [ "source": [
"y = x.ravel()\n", "y = x.ravel()\n",
"print(y)" "print(y)"
@ -266,18 +178,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": null,
"id": "38cc1ef3", "id": "38cc1ef3",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 4 8 1 5 9 2 6 10 3 7 11]\n"
]
}
],
"source": [ "source": [
"y = x.T.ravel()\n", "y = x.T.ravel()\n",
"print(y)" "print(y)"
@ -285,43 +189,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": null,
"id": "f6c40dce", "id": "f6c40dce",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Is it a view? False\n",
"\n",
"dtype\tint64\n",
"ndim\t1\n",
"shape\t(12,)\n",
"strides\t(8,)\n",
" \n"
]
}
],
"source": [ "source": [
"print_info(y, x)" "print_info(y, x)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": null,
"id": "b7d0cc63", "id": "b7d0cc63",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 3 5 7 9 11]\n"
]
}
],
"source": [ "source": [
"# Get all the odd elements\n", "# Get all the odd elements\n",
"y = x[(x % 2) == 1]\n", "y = x[(x % 2) == 1]\n",