diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa1..126b600 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "import numpy as np" ] }, { @@ -34,11 +34,105 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.5\n", + "{\n", + " \"Compilers\": {\n", + " \"c\": {\n", + " \"name\": \"clang\",\n", + " \"linker\": \"ld64\",\n", + " \"version\": \"15.0.0\",\n", + " \"commands\": \"cc\"\n", + " },\n", + " \"cython\": {\n", + " \"name\": \"cython\",\n", + " \"linker\": \"cython\",\n", + " \"version\": \"3.0.12\",\n", + " \"commands\": \"cython\"\n", + " },\n", + " \"c++\": {\n", + " \"name\": \"clang\",\n", + " \"linker\": \"ld64\",\n", + " \"version\": \"15.0.0\",\n", + " \"commands\": \"c++\"\n", + " }\n", + " },\n", + " \"Machine Information\": {\n", + " \"host\": {\n", + " \"cpu\": \"aarch64\",\n", + " \"family\": \"aarch64\",\n", + " \"endian\": \"little\",\n", + " \"system\": \"darwin\"\n", + " },\n", + " \"build\": {\n", + " \"cpu\": \"aarch64\",\n", + " \"family\": \"aarch64\",\n", + " \"endian\": \"little\",\n", + " \"system\": \"darwin\"\n", + " }\n", + " },\n", + " \"Build Dependencies\": {\n", + " \"blas\": {\n", + " \"name\": \"accelerate\",\n", + " \"found\": true,\n", + " \"version\": \"unknown\",\n", + " \"detection method\": \"system\",\n", + " \"include directory\": \"unknown\",\n", + " \"lib directory\": \"unknown\",\n", + " \"openblas configuration\": \"unknown\",\n", + " \"pc file directory\": \"unknown\"\n", + " },\n", + " \"lapack\": {\n", + " \"name\": \"accelerate\",\n", + " \"found\": true,\n", + " \"version\": \"unknown\",\n", + " \"detection method\": \"system\",\n", + " \"include directory\": \"unknown\",\n", + " \"lib directory\": \"unknown\",\n", + " \"openblas configuration\": \"unknown\",\n", + " \"pc file directory\": \"unknown\"\n", + " }\n", + " },\n", + " \"Python Information\": {\n", + " \"path\": \"/private/var/folders/qn/7t0vq3ts721cmgt0tgrtgzl80000gn/T/build-env-rsa2b_ts/bin/python\",\n", + " \"version\": \"3.13\"\n", + " },\n", + " \"SIMD Extensions\": {\n", + " \"baseline\": [\n", + " \"NEON\",\n", + " \"NEON_FP16\",\n", + " \"NEON_VFPV4\",\n", + " \"ASIMD\"\n", + " ],\n", + " \"found\": [\n", + " \"ASIMDHP\"\n", + " ],\n", + " \"not found\": [\n", + " \"ASIMDFHM\"\n", + " ]\n", + " }\n", + "}\n", + "None\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/benpaffrath/.pyenv/versions/3.13.3/envs/ironhack/lib/python3.13/site-packages/numpy/__config__.py:155: UserWarning: Install `pyyaml` for better output\n", + " warnings.warn(\"Install `pyyaml` for better output\", stacklevel=1)\n" + ] + } + ], + "source": [ + "print(np.__version__)\n", + "print(np.show_config())" ] }, { @@ -51,11 +145,14 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "np.random.random((2, 3, 5))\n", + "np.random.randint(10, size=(2, 3, 5))\n", + "\n", + "a = np.random.uniform(2, 20, size=(2, 3, 5))\n" ] }, { @@ -68,11 +165,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[17.78770969 16.21433609 13.95646197 11.78175653 6.99476122]\n", + " [ 6.85297114 19.31745251 8.25740376 19.49018648 6.75049046]\n", + " [17.25830367 7.17397522 4.85972144 12.84526518 15.16929822]]\n", + "\n", + " [[15.69770222 9.99543174 5.21324885 19.03113387 5.91285572]\n", + " [19.98667265 3.11842363 11.74839075 13.67004585 8.25739244]\n", + " [ 8.60237435 19.14564046 3.55084748 19.95483262 12.72122979]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a)" ] }, { @@ -85,11 +196,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "b = np.ones((5, 2, 3))" ] }, { @@ -102,11 +213,32 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]]\n" + ] + } + ], + "source": [ + "print(b)" ] }, { @@ -119,11 +251,21 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30 30\n", + "True\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(np.size(a), np.size(b))\n", + "print(np.size(a) == np.size(b))" ] }, { @@ -136,11 +278,25 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[12]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n", + "\u001b[31mValueError\u001b[39m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "a + b\n", + "\n", + "# no because they have not the same shape" ] }, { @@ -154,11 +310,22 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5) (5, 2, 3)\n", + "True\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a.shape, b.shape)\n", + "c = np.transpose(b, (1, 2, 0))\n", + "print(a.shape == c.shape)" ] }, { @@ -171,11 +338,13 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "d = a + c\n", + "\n", + "# It does work now, because both arrays have the same shape" ] }, { @@ -188,12 +357,45 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 35, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a [[[17.78770969 16.21433609 13.95646197 11.78175653 6.99476122]\n", + " [ 6.85297114 19.31745251 8.25740376 19.49018648 6.75049046]\n", + " [17.25830367 7.17397522 4.85972144 12.84526518 15.16929822]]\n", + "\n", + " [[15.69770222 9.99543174 5.21324885 19.03113387 5.91285572]\n", + " [19.98667265 3.11842363 11.74839075 13.67004585 8.25739244]\n", + " [ 8.60237435 19.14564046 3.55084748 19.95483262 12.72122979]]]\n", + "---\n", + "c [[[1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]]\n", + "\n", + " [[1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]]]\n", + "---\n", + "d [[[18.78770969 17.21433609 14.95646197 12.78175653 7.99476122]\n", + " [ 7.85297114 20.31745251 9.25740376 20.49018648 7.75049046]\n", + " [18.25830367 8.17397522 5.85972144 13.84526518 16.16929822]]\n", + "\n", + " [[16.69770222 10.99543174 6.21324885 20.03113387 6.91285572]\n", + " [20.98667265 4.11842363 12.74839075 14.67004585 9.25739244]\n", + " [ 9.60237435 20.14564046 4.55084748 20.95483262 13.72122979]]]\n" + ] + } + ], + "source": [ + "print(\"a\", a)\n", + "print(\"---\")\n", + "print(\"d\", d)\n", + "\n", + "# Each float from array a was added with the ones from array c. The value is in d. It has still the same order." ] }, { @@ -206,11 +408,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "e = a * c" ] }, { @@ -224,12 +426,35 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[17.78770969 16.21433609 13.95646197 11.78175653 6.99476122]\n", + " [ 6.85297114 19.31745251 8.25740376 19.49018648 6.75049046]\n", + " [17.25830367 7.17397522 4.85972144 12.84526518 15.16929822]]\n", + "\n", + " [[15.69770222 9.99543174 5.21324885 19.03113387 5.91285572]\n", + " [19.98667265 3.11842363 11.74839075 13.67004585 8.25739244]\n", + " [ 8.60237435 19.14564046 3.55084748 19.95483262 12.72122979]]]\n", + "[[[17.78770969 16.21433609 13.95646197 11.78175653 6.99476122]\n", + " [ 6.85297114 19.31745251 8.25740376 19.49018648 6.75049046]\n", + " [17.25830367 7.17397522 4.85972144 12.84526518 15.16929822]]\n", + "\n", + " [[15.69770222 9.99543174 5.21324885 19.03113387 5.91285572]\n", + " [19.98667265 3.11842363 11.74839075 13.67004585 8.25739244]\n", + " [ 8.60237435 19.14564046 3.55084748 19.95483262 12.72122979]]]\n" + ] + } + ], + "source": [ + "print(a)\n", + "print(e)\n", + "\n", + "# The values inside the multidimensional arrays are the same, because each value of array a is multiplied by 1" ] }, { @@ -243,12 +468,23 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20.98667265031494 4.118423627767713 13.04387720031215\n" + ] + } + ], + "source": [ + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "print(d_max, d_min, d_mean)\n" ] }, { @@ -261,11 +497,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "f = np.empty((2, 3, 5))" ] }, { @@ -287,11 +523,25 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "for i1, e1 in enumerate(d):\n", + " for i2, e2 in enumerate(e1):\n", + " for i3, e3 in enumerate(e2):\n", + " val = 100\n", + "\n", + " if e3 > d_min and e3 < d_mean:\n", + " val = 25\n", + " elif e3 > d_mean and e3 < d_max:\n", + " val = 75\n", + " elif e3 == d_mean:\n", + " val = 50\n", + " elif e3 == d_min:\n", + " val = 0\n", + " \n", + " f[i1, i2, i3] = val" ] }, { @@ -325,11 +575,35 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d [[[18.78770969 17.21433609 14.95646197 12.78175653 7.99476122]\n", + " [ 7.85297114 20.31745251 9.25740376 20.49018648 7.75049046]\n", + " [18.25830367 8.17397522 5.85972144 13.84526518 16.16929822]]\n", + "\n", + " [[16.69770222 10.99543174 6.21324885 20.03113387 6.91285572]\n", + " [20.98667265 4.11842363 12.74839075 14.67004585 9.25739244]\n", + " [ 9.60237435 20.14564046 4.55084748 20.95483262 13.72122979]]]\n", + "---\n", + "f [[[ 75. 75. 75. 25. 25.]\n", + " [ 25. 75. 25. 75. 25.]\n", + " [ 75. 25. 25. 75. 75.]]\n", + "\n", + " [[ 75. 25. 25. 75. 25.]\n", + " [100. 0. 25. 75. 25.]\n", + " [ 25. 75. 25. 75. 75.]]]\n" + ] + } + ], + "source": [ + "print(\"d\", d)\n", + "print(\"---\")\n", + "print(\"f\", f)" ] }, { @@ -350,17 +624,49 @@ }, { "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]" + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['D' 'D' 'D' 'B' 'B']\n", + " ['B' 'D' 'B' 'D' 'B']\n", + " ['D' 'B' 'B' 'D' 'D']]\n", + "\n", + " [['D' 'B' 'B' 'D' 'B']\n", + " ['E' 'A' 'B' 'D' 'B']\n", + " ['B' 'D' 'B' 'D' 'D']]]\n" + ] + } + ], + "source": [ + "g = np.empty((2, 3, 5), dtype=str)\n", + "\n", + "for i1, e1 in enumerate(d):\n", + " for i2, e2 in enumerate(e1):\n", + " for i3, e3 in enumerate(e2):\n", + " val = \"E\"\n", + "\n", + " if e3 > d_min and e3 < d_mean:\n", + " val = \"B\"\n", + " elif e3 > d_mean and e3 < d_max:\n", + " val = \"D\"\n", + " elif e3 == d_mean:\n", + " val = \"C\"\n", + " elif e3 == d_min:\n", + " val = \"A\"\n", + " \n", + " g[i1, i2, i3] = val\n", + "\n", + "print(g) " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "ironhack", "language": "python", "name": "python3" }, @@ -374,7 +680,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.3" } }, "nbformat": 4,