diff --git a/notebooks/numpy/index-slicing.ipynb b/notebooks/numpy/index-slicing.ipynb index 5006b10..27f828f 100644 --- a/notebooks/numpy/index-slicing.ipynb +++ b/notebooks/numpy/index-slicing.ipynb @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "id": "hAs4d-8M_xoD" }, @@ -79,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -114,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -149,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "id": "UCM-zVojardI" }, @@ -164,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -209,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -242,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -272,6 +272,17 @@ "Nótese la diferencia con las listas de python, en las que `l[:]` es la manera estándar de crear una *copia* de una lista `l`. En el caso de *numpy*, si se quiere realizar una copia, se ha de usar el método `copy` (por ejemplo, `v.copy()`)." ] }, + { + "cell_type": "code", + "source": [ + "v_copy = v.copy()" + ], + "metadata": { + "id": "RokeCr7534lp" + }, + "execution_count": 7, + "outputs": [] + }, { "cell_type": "markdown", "metadata": { @@ -301,13 +312,13 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "TykBJ9JI_xoI", - "outputId": "6b261758-0a49-4d1e-b0b3-d19d387d7588" + "outputId": "58d105e6-9471-4472-de7f-3386aa234404" }, "outputs": [ { @@ -318,7 +329,7 @@ ] }, "metadata": {}, - "execution_count": 9 + "execution_count": 8 } ], "source": [ @@ -326,6 +337,31 @@ "c2d[2]" ] }, + { + "cell_type": "code", + "source": [ + "print(c2d)" + ], + "metadata": { + "id": "S9ZL99sd4ENf", + "outputId": "b7206148-78c9-4ec0-9cd7-de711f992097", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n" + ] + } + ] + }, { "cell_type": "markdown", "metadata": { @@ -343,7 +379,7 @@ "base_uri": "https://localhost:8080/" }, "id": "25sSVC1l_xoI", - "outputId": "68ecf372-9812-41b2-c167-44a14b906882" + "outputId": "3df56d8e-c9c0-491b-9f03-878f2c698db3" }, "outputs": [ { @@ -378,7 +414,7 @@ "base_uri": "https://localhost:8080/" }, "id": "bizhjYPh_xoJ", - "outputId": "ca508277-b458-4655-ba2b-2f9382482e21" + "outputId": "3d06ac48-40da-4d11-f2a4-bb31eb004bcb" }, "outputs": [ { @@ -413,7 +449,7 @@ "base_uri": "https://localhost:8080/" }, "id": "ckTeln_g_xoK", - "outputId": "88eff21f-2f7d-45ca-8f7c-fa97f3455a1b" + "outputId": "be8e78f8-0f49-4e83-ac79-72059de1c2bf" }, "outputs": [ { @@ -447,13 +483,13 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VnD5R9kq_xoK", - "outputId": "07e6e879-f67b-4e32-8562-ef442b259b48" + "outputId": "0054b908-fcfc-420e-f938-945790c29643" }, "outputs": [ { @@ -465,7 +501,7 @@ ] }, "metadata": {}, - "execution_count": 13 + "execution_count": 14 } ], "source": [ @@ -483,7 +519,31 @@ }, { "cell_type": "code", - "execution_count": 14, + "source": [ + "l = list(range(10, 20))\n", + "print(list(enumerate(l)))" + ], + "metadata": { + "id": "9UtSBrCf4jKb", + "outputId": "a4d80e99-75a5-4c03-9240-e6dd47bfb6fd", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -529,7 +589,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -570,7 +630,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -613,6 +673,113 @@ ":::" ] }, + { + "cell_type": "code", + "source": [ + "# con fuerza bruta y np.ndenumerate\n", + "arr_enumerate = {k:v for v, k in np.ndenumerate(arr)}\n", + "idx = arr_enumerate[813]\n", + "print(idx)" + ], + "metadata": { + "id": "aWvsu08k6gFB", + "outputId": "5eab8374-cd16-4f01-c3a7-4db66491fa58", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(9, 4, 0, 1)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "arr[idx]" + ], + "metadata": { + "id": "TmS7L2Cg7BDc", + "outputId": "e56b65f9-ed87-49b5-b777-643197a1df4f", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "813" + ] + }, + "metadata": {}, + "execution_count": 24 + } + ] + }, + { + "cell_type": "code", + "source": [ + "813 % 25" + ], + "metadata": { + "id": "PG11_PJ87a6q", + "outputId": "979d0153-9a18-4b6e-cc20-d153b5737d3b", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 30, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "13" + ] + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "code", + "source": [ + "arr = np.arange(2100).reshape((25, 6, 7, 2))\n", + "n = 813\n", + "i = n // (6*7*2)\n", + "j = ()\n", + "k = 0\n", + "l = 0\n", + "print(arr[i, j, k, l])" + ], + "metadata": { + "id": "wxNMKZGt5sLt", + "outputId": "1cd73f28-a9cb-40e8-a7db-b682890b3e88", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "756\n" + ] + } + ] + }, { "cell_type": "markdown", "metadata": { @@ -624,7 +791,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -661,7 +828,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -697,7 +864,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -733,7 +900,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -768,7 +935,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -803,7 +970,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -831,33 +998,37 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "40WHNmgq_xoT", - "outputId": "683765b0-bc5b-455d-c5dd-af3d1c24af7c" + "outputId": "19bd8ef6-b9a9-465f-bdc0-efedbf1c5503" }, "outputs": [ { - "output_type": "execute_result", - "data": { - "text/plain": [ - "array([3, 6])" - ] - }, - "metadata": {}, - "execution_count": 27 + "output_type": "stream", + "name": "stdout", + "text": [ + "[3 6]\n", + "(2,)\n", + "[[3]\n", + " [6]]\n", + "(2, 1)\n" + ] } ], "source": [ - "c2d[:2, 2]" + "print(c2d[:2, 2])\n", + "print(c2d[:2, 2].shape)\n", + "print(c2d[:2, 2:])\n", + "print(c2d[:2, 2:].shape)" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -894,7 +1065,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "id": "RgvW7LRz_xoU", "outputId": "b31f7c84-f581-44d5-8297-4030c39b6a2d", @@ -932,7 +1103,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "id": "5a2v7IlMpb7r" }, @@ -944,7 +1115,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -988,6 +1159,41 @@ ":::" ] }, + { + "cell_type": "code", + "source": [ + "arr = np.arange(3*4*2).reshape((3, 4, 2))\n", + "arr2 = np.zeros((3, 4, 2))\n", + "assert arr.shape == (3, 4, 2)\n", + "assert arr2.shape == (3, 4, 2)\n", + "\n", + "subarr = arr[2, 3:4, :]\n", + "print(subarr)\n", + "print(subarr.shape)\n", + "\n", + "subarr2 = arr[:2, :3, :1]\n", + "print(subarr2.shape)" + ], + "metadata": { + "id": "vDXfjcpI-PUF", + "outputId": "17cc7afc-ddc7-4594-808f-aaf532beadb8", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[22 23]]\n", + "(1, 2)\n", + "(2, 3, 1)\n" + ] + } + ] + }, { "cell_type": "markdown", "metadata": { @@ -1010,7 +1216,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": { "id": "aCvfEAkM_xoV" }, @@ -1021,7 +1227,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1047,7 +1253,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1081,7 +1287,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1126,7 +1332,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1161,7 +1367,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1197,7 +1403,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1224,7 +1430,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1259,7 +1465,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1300,7 +1506,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1327,7 +1533,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1365,7 +1571,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1397,7 +1603,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1425,7 +1631,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1469,7 +1675,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -1502,7 +1708,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": { "id": "OuhXTz0R56pL" }, @@ -1557,8 +1763,7 @@ ], "metadata": { "colab": { - "provenance": [], - "toc_visible": true + "provenance": [] }, "kernelspec": { "display_name": "Python 3.9.1 64-bit",