diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa1..3d3faa6 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,12 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np" ] }, { @@ -34,11 +35,20 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.5\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(np.version.version)" ] }, { @@ -51,11 +61,66 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "a = np.random.randint(10, size=(2, 3, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.73773464, 0.84147322, 0.95380339, 0.19273015, 0.01911876],\n", + " [0.21828496, 0.79798639, 0.54946885, 0.05771115, 0.98940753],\n", + " [0.87341576, 0.1208124 , 0.05345454, 0.80709554, 0.99384958]],\n", + "\n", + " [[0.7146427 , 0.73889584, 0.58263739, 0.1552977 , 0.32963236],\n", + " [0.95065779, 0.57469346, 0.49023791, 0.85522008, 0.89872488],\n", + " [0.42587058, 0.09629422, 0.61043993, 0.30656913, 0.7780495 ]]])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aa = np.random.random(size=(2, 3, 5))\n", + "aa" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.80912459, 0.16359662, 0.72073499, 0.12707715, 0.08843276],\n", + " [0.2160909 , 0.56050739, 0.33386354, 0.6597112 , 0.2814734 ],\n", + " [0.77394185, 0.42737524, 0.3991704 , 0.14279601, 0.20689268]],\n", + "\n", + " [[0.77327668, 0.59291054, 0.25551097, 0.78261312, 0.26959345],\n", + " [0.00854966, 0.07189779, 0.74979198, 0.26445544, 0.78398933],\n", + " [0.98946892, 0.2181205 , 0.06410375, 0.67730014, 0.84130067]]])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aaa = np.random.rand(2,3,5)\n", + "aaa" ] }, { @@ -68,11 +133,26 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[6 1 0 0 8]\n", + " [2 7 5 5 0]\n", + " [5 3 6 0 1]]\n", + "\n", + " [[0 8 8 2 4]\n", + " [3 3 0 4 8]\n", + " [3 9 5 7 0]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(a)" ] }, { @@ -83,15 +163,6 @@ "### Assign the array to variable \"b\"\n" ] }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -102,11 +173,43 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "b = np.ones((5, 2, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "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)" ] }, { @@ -119,11 +222,23 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "size_comparison = (np.size(a)) == (np.size(b))\n", + "size_comparison" ] }, { @@ -136,11 +251,48 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 11, + "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[11]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m### [your code here]\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m ab_add = \u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43madd\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3\u001b[39m ab_add\n\u001b[32m 5\u001b[39m \u001b[38;5;66;03m#not possible: ValueError: operands could not be broadcast together with shapes (2,3,5) (5,2,3) \u001b[39;00m\n", + "\u001b[31mValueError\u001b[39m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "### [your code here]\n", + "ab_add = np.add(a, b)\n", + "ab_add\n", + "\n", + "#not possible: ValueError: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, - "outputs": [], + "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[34]\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": [ - "### [your code here]\n" + "a + b" ] }, { @@ -154,11 +306,30 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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.]]])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "c = np.transpose(b, axes=(1,2,0))\n", + "c" ] }, { @@ -171,11 +342,31 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 7., 2., 1., 1., 9.],\n", + " [ 3., 8., 6., 6., 1.],\n", + " [ 6., 4., 7., 1., 2.]],\n", + "\n", + " [[ 1., 9., 9., 3., 5.],\n", + " [ 4., 4., 1., 5., 9.],\n", + " [ 4., 10., 6., 8., 1.]]])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "d = a + c\n", + "d\n", + "#it works because both a and c have the same shape." ] }, { @@ -188,12 +379,36 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[6 1 0 0 8]\n", + " [2 7 5 5 0]\n", + " [5 3 6 0 1]]\n", + "\n", + " [[0 8 8 2 4]\n", + " [3 3 0 4 8]\n", + " [3 9 5 7 0]]]\n", + "[[[ 7. 2. 1. 1. 9.]\n", + " [ 3. 8. 6. 6. 1.]\n", + " [ 6. 4. 7. 1. 2.]]\n", + "\n", + " [[ 1. 9. 9. 3. 5.]\n", + " [ 4. 4. 1. 5. 9.]\n", + " [ 4. 10. 6. 8. 1.]]]\n" + ] + } + ], "source": [ "### [your code here]\n", - "\n" + "print(a)\n", + "print(d)\n", + "\n", + "#the array \"d\" adds 1 to each element from array \"a\", because the array \"b\" is all ones. a(0,0) adds c(0,0) and this becomes c(0,0)" ] }, { @@ -206,11 +421,30 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[6., 1., 0., 0., 8.],\n", + " [2., 7., 5., 5., 0.],\n", + " [5., 3., 6., 0., 1.]],\n", + "\n", + " [[0., 8., 8., 2., 4.],\n", + " [3., 3., 0., 4., 8.],\n", + " [3., 9., 5., 7., 0.]]])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "e = a * c\n", + "e" ] }, { @@ -224,12 +458,32 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "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": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "### [your code here]\n", - "\n" + "equal = e == a\n", + "equal\n", + "\n", + "#it is equal because c is all ones, which means you multiply each element of a by 1." ] }, { @@ -243,12 +497,36 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10.0\n", + "1.0\n", + "4.766666666666667\n", + "[[[ 7. 2. 1. 1. 9.]\n", + " [ 3. 8. 6. 6. 1.]\n", + " [ 6. 4. 7. 1. 2.]]\n", + "\n", + " [[ 1. 9. 9. 3. 5.]\n", + " [ 4. 4. 1. 5. 9.]\n", + " [ 4. 10. 6. 8. 1.]]]\n" + ] + } + ], "source": [ "### [your code here]\n", - "\n" + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "print(d_max)\n", + "print(d_min)\n", + "print(d_mean)\n", + "print(d)" ] }, { @@ -261,11 +539,12 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "f = np.empty([2, 3, 5])\n" ] }, { @@ -287,11 +566,95 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75. 25. 0. 0. 75.]\n", + " [ 25. 75. 75. 75. 0.]\n", + " [ 75. 25. 75. 0. 25.]]\n", + "\n", + " [[ 0. 75. 75. 25. 75.]\n", + " [ 25. 25. 0. 75. 75.]\n", + " [ 25. 100. 75. 75. 0.]]]\n" + ] + } + ], + "source": [ + "for row_i, row in enumerate(d[0]):\n", + " for col_i, item in enumerate(row):\n", + " if d_mean > item > d_min:\n", + " f[0, row_i, col_i] = 25\n", + " elif d_mean == item:\n", + " f[0,row_i, col_i] = 50\n", + " elif d_max == item:\n", + " f[0,row_i, col_i] = 100\n", + " elif d_min == item:\n", + " f[0,row_i, col_i] = 0\n", + " else:\n", + " f[0,row_i, col_i] = 75\n", + "\n", + "for row_i, row in enumerate(d[1]):\n", + " for col_i, item in enumerate(row):\n", + " if d_mean > item > d_min:\n", + " f[1,row_i, col_i] = 25\n", + " elif d_mean == item:\n", + " f[1,row_i, col_i] = 50\n", + " elif d_max == item:\n", + " f[1,row_i, col_i] = 100\n", + " elif d_min == item:\n", + " f[1,row_i, col_i] = 0\n", + " else:\n", + " f[1,row_i, col_i] = 75\n", + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75. 25. 0. 0. 75.]\n", + " [ 25. 75. 75. 75. 0.]\n", + " [ 75. 25. 75. 0. 25.]]\n", + "\n", + " [[ 0. 75. 75. 25. 75.]\n", + " [ 25. 25. 0. 75. 75.]\n", + " [ 25. 100. 75. 75. 0.]]]\n", + "[[[ 7. 2. 1. 1. 9.]\n", + " [ 3. 8. 6. 6. 1.]\n", + " [ 6. 4. 7. 1. 2.]]\n", + "\n", + " [[ 1. 9. 9. 3. 5.]\n", + " [ 4. 4. 1. 5. 9.]\n", + " [ 4. 10. 6. 8. 1.]]]\n" + ] + } + ], + "source": [ + "for layer_i, layer in enumerate(d):\n", + " for row_i, row in enumerate(layer):\n", + " for col_i, item in enumerate(row):\n", + " if d_mean > item > d_min:\n", + " f[layer_i, row_i, col_i] = 25\n", + " elif d_mean == item:\n", + " f[layer_i, row_i, col_i] = 50\n", + " elif d_max == item:\n", + " f[layer_i, row_i, col_i] = 100\n", + " elif d_min == item:\n", + " f[layer_i, row_i, col_i] = 0\n", + " else:\n", + " f[layer_i, row_i, col_i] = 75\n", + "\n", + "print(f)\n", + "print(d)" ] }, { @@ -325,11 +688,34 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 7. 2. 1. 1. 9.]\n", + " [ 3. 8. 6. 6. 1.]\n", + " [ 6. 4. 7. 1. 2.]]\n", + "\n", + " [[ 1. 9. 9. 3. 5.]\n", + " [ 4. 4. 1. 5. 9.]\n", + " [ 4. 10. 6. 8. 1.]]]\n", + "[[[ 75. 25. 0. 0. 75.]\n", + " [ 25. 75. 75. 75. 0.]\n", + " [ 75. 25. 75. 0. 25.]]\n", + "\n", + " [[ 0. 75. 75. 25. 75.]\n", + " [ 25. 25. 0. 75. 75.]\n", + " [ 25. 100. 75. 75. 0.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(d)\n", + "print(f)" ] }, { @@ -350,12 +736,57 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "### [your code here]" + "### [your code here]\n", + "g = np.empty([2, 3, 5], dtype=str)\n", + "\n", + "for layer_i, layer in enumerate(d):\n", + " for row_i, row in enumerate(layer):\n", + " for col_i, item in enumerate(row):\n", + " if d_mean > item > d_min:\n", + " g[layer_i, row_i, col_i] = \"B\"\n", + " elif d_mean == item:\n", + " g[layer_i, row_i, col_i] = \"C\"\n", + " elif d_max == item:\n", + " g[layer_i, row_i, col_i] = \"E\"\n", + " elif d_min == item:\n", + " g[layer_i, row_i, col_i] = \"A\"\n", + " else:\n", + " g[layer_i, row_i, col_i] = \"D\"" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['D' 'B' 'A' 'A' 'D']\n", + " ['B' 'D' 'D' 'D' 'A']\n", + " ['D' 'B' 'D' 'A' 'B']]\n", + "\n", + " [['A' 'D' 'D' 'B' 'D']\n", + " ['B' 'B' 'A' 'D' 'D']\n", + " ['B' 'E' 'D' 'D' 'A']]]\n" + ] + } + ], + "source": [ + "print(g)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -374,7 +805,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.3" } }, "nbformat": 4,