From 57113d593519ef6b4d06364b5242ab7cef077e79 Mon Sep 17 00:00:00 2001 From: ArisGoulas Date: Sun, 16 Apr 2023 17:55:03 +0100 Subject: [PATCH 1/2] lab done --- your-code/main.ipynb | 404 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 336 insertions(+), 68 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..0c0d1a8 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "import numpy as np" ] }, { @@ -28,11 +29,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.23.5\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(np.version.version)" ] }, { @@ -45,29 +55,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Method 1" + "# Method 1\n", + "a = np.random.randint(100, size = (2, 3, 5))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Method 2" + "# Method 2\n", + "a = np.random.rand(2, 3, 5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Method 3" + "# Method 3\n", + "a = np.random.random((2, 3, 5))" ] }, { @@ -79,11 +92,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.03411167 0.79162265 0.55477677 0.19838684 0.24865201]\n", + " [0.52787572 0.06859103 0.49002902 0.8517779 0.88414649]\n", + " [0.12426406 0.3830888 0.17270849 0.97297669 0.38435765]]\n", + "\n", + " [[0.58639823 0.73016611 0.76112317 0.23730907 0.85051669]\n", + " [0.70012508 0.36131528 0.44515629 0.55358958 0.65688702]\n", + " [0.65638377 0.54428704 0.52371581 0.66137845 0.36270527]]]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(a)" ] }, { @@ -95,11 +123,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "b = np.ones((5, 2, 3), dtype = int)" ] }, { @@ -111,11 +140,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 10, + "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": [ + "# your code here\n", + "print(b)" ] }, { @@ -127,11 +178,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(a.size == b.size)" ] }, { @@ -143,11 +203,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "it doesn't work because a and b have different shapes\n" + ] + } + ], "source": [ - "# your answer here" + "# your answer here\n", + "# no\n", + "# different shapes\n", + "if np.shape(a) == np.shape(b):\n", + " d = a + b\n", + " print(\"it works because a and b have the same shapes\")\n", + "else:\n", + " print(\"it doesn't work because a and b have different shapes\")" ] }, { @@ -159,11 +234,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[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" + ] + } + ], + "source": [ + "# your code here\n", + "c = b.transpose(1, 2, 0)\n", + "np.shape(c)\n", + "\n", + "print(c)" ] }, { @@ -175,11 +268,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "it works because a and c have the same shapes\n", + "\n", + "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", + " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", + " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", + "\n", + " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", + " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", + " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "if np.shape(a) == np.shape(c):\n", + " d = a + c\n", + " print(\"it works because a and c have the same shapes\")\n", + "else:\n", + " print(\"you can't add a and c because they have different shapes\")\n", + "\n", + "print()\n", + " \n", + "print(d)" ] }, { @@ -191,11 +309,53 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.03411167 0.79162265 0.55477677 0.19838684 0.24865201]\n", + " [0.52787572 0.06859103 0.49002902 0.8517779 0.88414649]\n", + " [0.12426406 0.3830888 0.17270849 0.97297669 0.38435765]]\n", + "\n", + " [[0.58639823 0.73016611 0.76112317 0.23730907 0.85051669]\n", + " [0.70012508 0.36131528 0.44515629 0.55358958 0.65688702]\n", + " [0.65638377 0.54428704 0.52371581 0.66137845 0.36270527]]]\n", + "\n", + "\n", + "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", + " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", + " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", + "\n", + " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", + " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", + " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n" + ] + }, + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code/answer here\n", + "print(a)\n", + "\n", + "print()\n", + "print()\n", + "\n", + "print(d)\n", + "\n", + "\n", + "# d equals to the sum of a and transposed b" ] }, { @@ -207,11 +367,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "e = a * c" ] }, { @@ -223,11 +384,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]],\n", + "\n", + " [[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]]])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code/answer here\n", + "e == a\n", + "\n", + "# it does because c is made out of ones" ] }, { @@ -239,11 +420,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean = d.mean()" ] }, { @@ -255,11 +439,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "f = np.empty((2, 3, 5))" ] }, { @@ -275,11 +460,38 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", + " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", + " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", + "\n", + " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", + " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", + " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n", + "\n", + "[[[ 0. 75. 75. 25. 25.]\n", + " [ 75. 25. 25. 75. 75.]\n", + " [ 25. 25. 25. 100. 25.]]\n", + "\n", + " [[ 75. 75. 75. 25. 75.]\n", + " [ 75. 25. 25. 75. 75.]\n", + " [ 75. 75. 75. 75. 25.]]]\n" + ] + } + ], + "source": [ + "# your code here\n", + "f[(d > d_min) & (d < d_mean)] = 25\n", + "f[d == d_mean] = 50\n", + "f[(d > d_mean) & (d < d_max)] = 75\n", + "f[d == d_min] = 0\n", + "f[d == d_max] = 100" ] }, { @@ -309,11 +521,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", + " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", + " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", + "\n", + " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", + " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", + " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n", + "\n", + "[[[ 0. 75. 75. 25. 25.]\n", + " [ 75. 25. 25. 75. 75.]\n", + " [ 25. 25. 25. 100. 25.]]\n", + "\n", + " [[ 75. 75. 75. 25. 75.]\n", + " [ 75. 25. 25. 75. 75.]\n", + " [ 75. 75. 75. 75. 25.]]]\n" + ] + } + ], + "source": [ + "# your code here\n", + "print(d)\n", + "\n", + "print()\n", + "\n", + "print(f)" ] }, { @@ -333,19 +572,48 @@ "**Note**: you don't have to use Numpy in this question." ] }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['A' 'D' 'D' 'B' 'B']\n", + " ['D' 'B' 'B' 'D' 'D']\n", + " ['B' 'B' 'B' 'E' 'B']]\n", + "\n", + " [['D' 'D' 'D' 'B' 'D']\n", + " ['D' 'B' 'B' 'D' 'D']\n", + " ['D' 'D' 'D' 'D' 'B']]]\n" + ] + } + ], + "source": [ + "# your code here\n", + "f_str = np.empty_like(d, dtype=str)\n", + "f_str[(d > d_min) & (d < d_mean)] = \"B\"\n", + "f_str[d == d_mean] = \"C\"\n", + "f_str[(d > d_mean) & (d < d_max)] = \"D\"\n", + "f_str[d == d_min] = \"A\"\n", + "f_str[d == d_max] = \"E\"\n", + "\n", + "print(f)" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -359,7 +627,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.9" } }, "nbformat": 4, From fa89804bd5db06670919432a871f46d66582c618 Mon Sep 17 00:00:00 2001 From: ArisGoulas Date: Wed, 21 Jun 2023 15:12:52 +0100 Subject: [PATCH 2/2] lab revised after bootcamp --- your-code/main.ipynb | 362 ++++++++++++++++++++----------------------- 1 file changed, 168 insertions(+), 194 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0c0d1a8..94a8270 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,10 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# your code here\n", "import numpy as np" ] }, @@ -29,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -41,8 +40,7 @@ } ], "source": [ - "# your code here\n", - "print(np.version.version)" + "print(np.__version__)" ] }, { @@ -55,32 +53,64 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ - "# Method 1\n", + "# syntax: random.randint(low, high=None, size=None, dtype=int); if high is None (the default), then results are from [0, low)\n", + "# integers, from \"discrete uniform\" distribution, [low, high)\n", + "\n", "a = np.random.randint(100, size = (2, 3, 5))" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ - "# Method 2\n", + "# syntax: random.rand(d0, d1, ..., dn)\n", + "# floats, from uniform distribution, [0, 1)\n", + "\n", "a = np.random.rand(2, 3, 5)" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 53, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# syntax: random.randn(d0, d1, ..., dn)\n", + "# floats, from \"standard normal\" distribution\n", + "\n", + "a = np.random.randn(2, 3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ - "# Method 3\n", - "a = np.random.random((2, 3, 5))" + "# syntax: random.random_sample(size=None)\n", + "# floats, \"continuous uniform\" distribtuion, [0.0, 1.0)\n", + "\n", + "a = np.random.random_sample(size = (2, 3, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "# syntax: random.random(size=None)\n", + "# floats, [0.0, 1.0)\n", + "\n", + "a = np.random.random(size = (2, 3, 5))" ] }, { @@ -92,25 +122,24 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[[0.03411167 0.79162265 0.55477677 0.19838684 0.24865201]\n", - " [0.52787572 0.06859103 0.49002902 0.8517779 0.88414649]\n", - " [0.12426406 0.3830888 0.17270849 0.97297669 0.38435765]]\n", + "[[[0.41685763 0.86218193 0.40424214 0.65916744 0.38822751]\n", + " [0.40456356 0.6913081 0.72844646 0.045383 0.50828095]\n", + " [0.376908 0.09780231 0.83434537 0.75355341 0.95188708]]\n", "\n", - " [[0.58639823 0.73016611 0.76112317 0.23730907 0.85051669]\n", - " [0.70012508 0.36131528 0.44515629 0.55358958 0.65688702]\n", - " [0.65638377 0.54428704 0.52371581 0.66137845 0.36270527]]]\n" + " [[0.46131671 0.32621905 0.89250888 0.74166322 0.90079063]\n", + " [0.78921746 0.42424457 0.9193221 0.60638787 0.24101044]\n", + " [0.41183555 0.55754807 0.38693262 0.37329723 0.90172869]]]\n" ] } ], "source": [ - "# your code here\n", "print(a)" ] }, @@ -123,12 +152,13 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ - "# your code here\n", - "b = np.ones((5, 2, 3), dtype = int)" + "# syntax: ones(shape, dtype=None, order=\"C\", *, like=None)\n", + "\n", + "b = np.ones(shape = (5, 2, 3), dtype = int)" ] }, { @@ -140,7 +170,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 58, "metadata": {}, "outputs": [ { @@ -165,7 +195,6 @@ } ], "source": [ - "# your code here\n", "print(b)" ] }, @@ -178,20 +207,24 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 60, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "# your code here\n", - "print(a.size == b.size)" + "a.size == b.size\n", + "\n", + "# yes, a and b have the same size" ] }, { @@ -203,26 +236,25 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 61, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "it doesn't work because a and b have different shapes\n" + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[61], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n", + "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " ] } ], "source": [ - "# your answer here\n", - "# no\n", - "# different shapes\n", - "if np.shape(a) == np.shape(b):\n", - " d = a + b\n", - " print(\"it works because a and b have the same shapes\")\n", - "else:\n", - " print(\"it doesn't work because a and b have different shapes\")" + "a + b\n", + "\n", + "# no you cannot add a and b because they have a different shape" ] }, { @@ -234,29 +266,16 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 85, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[[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" - ] - } - ], + "outputs": [], "source": [ - "# your code here\n", - "c = b.transpose(1, 2, 0)\n", - "np.shape(c)\n", + "# syntax: transpose(a, axes=None)\n", + "# axes: tuple or list of ints, optional\n", + "# if specified, it must be a tuple or list which contains a permutation of [0,1,…,N-1] where N is the number of axes of a\n", + "# The i’th axis of the returned array will correspond to the axis numbered axes[i] of the input\n", "\n", - "print(c)" + "c = np.transpose(b, axes=[1, 2, 0]) # i could also do c = b.reshape(a.shape)" ] }, { @@ -268,36 +287,13 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 73, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "it works because a and c have the same shapes\n", - "\n", - "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", - " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", - " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", - "\n", - " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", - " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", - " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n" - ] - } - ], + "outputs": [], "source": [ - "# your code/answer here\n", - "if np.shape(a) == np.shape(c):\n", - " d = a + c\n", - " print(\"it works because a and c have the same shapes\")\n", - "else:\n", - " print(\"you can't add a and c because they have different shapes\")\n", + "d = a + c\n", "\n", - "print()\n", - " \n", - "print(d)" + "# it works because a and c have the same shape" ] }, { @@ -309,53 +305,38 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[[0.03411167 0.79162265 0.55477677 0.19838684 0.24865201]\n", - " [0.52787572 0.06859103 0.49002902 0.8517779 0.88414649]\n", - " [0.12426406 0.3830888 0.17270849 0.97297669 0.38435765]]\n", + "[[[0.41685763 0.86218193 0.40424214 0.65916744 0.38822751]\n", + " [0.40456356 0.6913081 0.72844646 0.045383 0.50828095]\n", + " [0.376908 0.09780231 0.83434537 0.75355341 0.95188708]]\n", "\n", - " [[0.58639823 0.73016611 0.76112317 0.23730907 0.85051669]\n", - " [0.70012508 0.36131528 0.44515629 0.55358958 0.65688702]\n", - " [0.65638377 0.54428704 0.52371581 0.66137845 0.36270527]]]\n", + " [[0.46131671 0.32621905 0.89250888 0.74166322 0.90079063]\n", + " [0.78921746 0.42424457 0.9193221 0.60638787 0.24101044]\n", + " [0.41183555 0.55754807 0.38693262 0.37329723 0.90172869]]] \n", + " \n", "\n", + "[[[1.41685763 1.86218193 1.40424214 1.65916744 1.38822751]\n", + " [1.40456356 1.6913081 1.72844646 1.045383 1.50828095]\n", + " [1.376908 1.09780231 1.83434537 1.75355341 1.95188708]]\n", "\n", - "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", - " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", - " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", - "\n", - " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", - " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", - " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n" + " [[1.46131671 1.32621905 1.89250888 1.74166322 1.90079063]\n", + " [1.78921746 1.42424457 1.9193221 1.60638787 1.24101044]\n", + " [1.41183555 1.55754807 1.38693262 1.37329723 1.90172869]]]\n" ] - }, - { - "data": { - "text/plain": [ - "numpy.ndarray" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "# your code/answer here\n", - "print(a)\n", - "\n", - "print()\n", - "print()\n", + "print(a, \"\\n \\n\")\n", "\n", "print(d)\n", "\n", - "\n", - "# d equals to the sum of a and transposed b" + "# the values of d differ by 1 as compared to the values of a (as d is the product of addition of c -only ones- to a)" ] }, { @@ -367,11 +348,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ - "# your code here\n", "e = a * c" ] }, @@ -384,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 80, "metadata": {}, "outputs": [ { @@ -399,16 +379,15 @@ " [ True, True, True, True, True]]])" ] }, - "execution_count": 17, + "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# your code/answer here\n", "e == a\n", "\n", - "# it does because c is made out of ones" + "# it does because c contains only ones" ] }, { @@ -420,13 +399,14 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ - "# your code here\n", "d_max = d.max()\n", + "\n", "d_min = d.min()\n", + "\n", "d_mean = d.mean()" ] }, @@ -439,12 +419,25 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 84, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here\n", - "f = np.empty((2, 3, 5))" + "# syntax empty(shape, dtype=float, order='C', *, like=None)\n", + "# return a new array of given shape and type, without initializing entries\n", + "\n", + "f = np.empty((d.shape))" ] }, { @@ -460,37 +453,18 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 86, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", - " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", - " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", - "\n", - " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", - " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", - " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n", - "\n", - "[[[ 0. 75. 75. 25. 25.]\n", - " [ 75. 25. 25. 75. 75.]\n", - " [ 25. 25. 25. 100. 25.]]\n", - "\n", - " [[ 75. 75. 75. 25. 75.]\n", - " [ 75. 25. 25. 75. 75.]\n", - " [ 75. 75. 75. 75. 25.]]]\n" - ] - } - ], + "outputs": [], "source": [ - "# your code here\n", "f[(d > d_min) & (d < d_mean)] = 25\n", - "f[d == d_mean] = 50\n", + "\n", "f[(d > d_mean) & (d < d_max)] = 75\n", + "\n", + "f[d == d_mean] = 50\n", + "\n", "f[d == d_min] = 0\n", + "\n", "f[d == d_max] = 100" ] }, @@ -521,38 +495,38 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[[1.03411167 1.79162265 1.55477677 1.19838684 1.24865201]\n", - " [1.52787572 1.06859103 1.49002902 1.8517779 1.88414649]\n", - " [1.12426406 1.3830888 1.17270849 1.97297669 1.38435765]]\n", + "[[[1.41685763 1.86218193 1.40424214 1.65916744 1.38822751]\n", + " [1.40456356 1.6913081 1.72844646 1.045383 1.50828095]\n", + " [1.376908 1.09780231 1.83434537 1.75355341 1.95188708]]\n", "\n", - " [[1.58639823 1.73016611 1.76112317 1.23730907 1.85051669]\n", - " [1.70012508 1.36131528 1.44515629 1.55358958 1.65688702]\n", - " [1.65638377 1.54428704 1.52371581 1.66137845 1.36270527]]]\n", + " [[1.46131671 1.32621905 1.89250888 1.74166322 1.90079063]\n", + " [1.78921746 1.42424457 1.9193221 1.60638787 1.24101044]\n", + " [1.41183555 1.55754807 1.38693262 1.37329723 1.90172869]]] \n", + " \n", "\n", - "[[[ 0. 75. 75. 25. 25.]\n", - " [ 75. 25. 25. 75. 75.]\n", - " [ 25. 25. 25. 100. 25.]]\n", + "[[[ 25. 75. 25. 75. 25.]\n", + " [ 25. 75. 75. 0. 25.]\n", + " [ 25. 25. 75. 75. 100.]]\n", "\n", - " [[ 75. 75. 75. 25. 75.]\n", - " [ 75. 25. 25. 75. 75.]\n", - " [ 75. 75. 75. 75. 25.]]]\n" + " [[ 25. 25. 75. 75. 75.]\n", + " [ 75. 25. 75. 75. 25.]\n", + " [ 25. 25. 25. 25. 75.]]]\n" ] } ], "source": [ - "# your code here\n", - "print(d)\n", + "print(d, \"\\n \\n\")\n", "\n", - "print()\n", + "print(f)\n", "\n", - "print(f)" + "# yes" ] }, { @@ -574,31 +548,31 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[[['A' 'D' 'D' 'B' 'B']\n", - " ['D' 'B' 'B' 'D' 'D']\n", - " ['B' 'B' 'B' 'E' 'B']]\n", + "[[['B' 'D' 'B' 'D' 'B']\n", + " ['B' 'D' 'D' 'A' 'B']\n", + " ['B' 'B' 'D' 'D' 'E']]\n", "\n", - " [['D' 'D' 'D' 'B' 'D']\n", - " ['D' 'B' 'B' 'D' 'D']\n", - " ['D' 'D' 'D' 'D' 'B']]]\n" + " [['B' 'B' 'D' 'D' 'D']\n", + " ['D' 'B' 'D' 'D' 'B']\n", + " ['B' 'B' 'B' 'B' 'D']]]\n" ] } ], "source": [ - "# your code here\n", - "f_str = np.empty_like(d, dtype=str)\n", - "f_str[(d > d_min) & (d < d_mean)] = \"B\"\n", - "f_str[d == d_mean] = \"C\"\n", - "f_str[(d > d_mean) & (d < d_max)] = \"D\"\n", - "f_str[d == d_min] = \"A\"\n", - "f_str[d == d_max] = \"E\"\n", + "f = f.astype(str)\n", + "\n", + "f[(d > d_min) & (d < d_mean)] = \"B\"\n", + "f[(d > d_mean) & (d < d_max)] = \"D\"\n", + "f[d == d_mean] = \"C\"\n", + "f[d == d_min] = \"A\"\n", + "f[d == d_max] = \"E\"\n", "\n", "print(f)" ] @@ -627,7 +601,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.10.10" } }, "nbformat": 4,