diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa1..623e1b7 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,19 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.4\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(np.version.version)" ] }, { @@ -51,11 +59,62 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A is an array of the following numbers: \n", + "3\n", + "[[[4 2 8 9 9]\n", + " [9 0 6 6 8]\n", + " [3 3 4 8 5]]\n", + "\n", + " [[9 3 5 4 4]\n", + " [8 1 4 5 5]\n", + " [4 2 3 2 8]]]\n", + "A2 is an array of the following numbers: \n", + "3\n", + "[[[0.58705264 0.10439797 0.10987155 0.87880466 0.19634468]\n", + " [0.01690683 0.90795606 0.46336664 0.29963763 0.22100271]\n", + " [0.64851394 0.07692552 0.87480797 0.77126095 0.9695411 ]]\n", + "\n", + " [[0.40088819 0.1784533 0.48114638 0.22217642 0.75097671]\n", + " [0.7116865 0.12059496 0.32261741 0.4235361 0.78969466]\n", + " [0.17073756 0.11060123 0.13605561 0.33102555 0.2068833 ]]]\n", + "A3 is an array of the following numbers: \n", + "3\n", + "[[[ 0.5121003 -0.46159845 1.61143687 0.40841123 0.20668906]\n", + " [ 0.98682028 -0.47623397 -1.72452115 0.03894566 0.12958093]\n", + " [ 1.62019624 0.92710677 -0.18568419 -1.70274415 1.14417101]]\n", + "\n", + " [[ 0.47493394 -2.08219872 -0.04255597 0.17259219 -0.77573742]\n", + " [-1.19144815 0.82735278 1.83961642 1.30307053 1.66074257]\n", + " [ 0.3060472 -0.87736169 -1.96939736 0.46438388 -0.84827314]]]\n" + ] + } + ], + "source": [ + "from random import randint, random\n", + "#method 1\n", + "a = np.random.randint(10, size = (2,3,5))\n", + "print(\"A is an array of the following numbers: \")\n", + "print(a.ndim)\n", + "print(a)\n", + "\n", + "#method 2\n", + "a2 = np.random.rand(2,3,5)\n", + "print(\"A2 is an array of the following numbers: \")\n", + "print(a2.ndim)\n", + "print(a2)\n", + "\n", + "#method 3\n", + "a3 = np.random.randn(2,3,5)\n", + "print(\"A3 is an array of the following numbers: \")\n", + "print(a3.ndim)\n", + "print(a3)" ] }, { @@ -68,11 +127,41 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[4 2 8 9 9]\n", + " [9 0 6 6 8]\n", + " [3 3 4 8 5]]\n", + "\n", + " [[9 3 5 4 4]\n", + " [8 1 4 5 5]\n", + " [4 2 3 2 8]]]\n", + "[[[0.58705264 0.10439797 0.10987155 0.87880466 0.19634468]\n", + " [0.01690683 0.90795606 0.46336664 0.29963763 0.22100271]\n", + " [0.64851394 0.07692552 0.87480797 0.77126095 0.9695411 ]]\n", + "\n", + " [[0.40088819 0.1784533 0.48114638 0.22217642 0.75097671]\n", + " [0.7116865 0.12059496 0.32261741 0.4235361 0.78969466]\n", + " [0.17073756 0.11060123 0.13605561 0.33102555 0.2068833 ]]]\n", + "[[[ 0.5121003 -0.46159845 1.61143687 0.40841123 0.20668906]\n", + " [ 0.98682028 -0.47623397 -1.72452115 0.03894566 0.12958093]\n", + " [ 1.62019624 0.92710677 -0.18568419 -1.70274415 1.14417101]]\n", + "\n", + " [[ 0.47493394 -2.08219872 -0.04255597 0.17259219 -0.77573742]\n", + " [-1.19144815 0.82735278 1.83961642 1.30307053 1.66074257]\n", + " [ 0.3060472 -0.87736169 -1.96939736 0.46438388 -0.84827314]]]\n" + ] + } + ], + "source": [ + "print(a)\n", + "print(a2)\n", + "print(a3)" ] }, { @@ -85,11 +174,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "b = np.ones((5,2,3))\n" ] }, { @@ -102,11 +191,35 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B is an array of the following numbers: \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", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(\"B is an array of the following numbers: \")\n", + "print(b)" ] }, { @@ -119,11 +232,24 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A and B have the same number of dimensions\n" + ] + } + ], "source": [ - "### [your code here]\n" + "if a.ndim == b.ndim:\n", + " print(\"A and B have the same number of dimensions\")\n", + "elif a.shape == b.shape:\n", + " print(\"A and B have the same shape\")\n", + "elif a.size == b.size:\n", + " print(\"A and B have the same size\")" ] }, { @@ -136,11 +262,23 @@ }, { "cell_type": "code", - "execution_count": 26, + "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[16]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28mprint\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" + "# We are not able to add a and b as they are not the same shape " ] }, { @@ -154,11 +292,53 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 20, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "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", + "C is an array of the following numbers: \n", + "[[[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", + "[[[ 5. 3. 9. 10. 10.]\n", + " [10. 1. 7. 7. 9.]\n", + " [ 4. 4. 5. 9. 6.]]\n", + "\n", + " [[10. 4. 6. 5. 5.]\n", + " [ 9. 2. 5. 6. 6.]\n", + " [ 5. 3. 4. 3. 9.]]]\n" + ] + } + ], + "source": [ + "arrayb1d = np.array(b)\n", + "print(arrayb1d)\n", + "c = np.reshape(arrayb1d, (2,3,5))\n", + "print(\"C is an array of the following numbers: \")\n", + "print(c)\n", + "print(a + c)\n", + "\n" ] }, { @@ -171,11 +351,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "d = a + c\n" ] }, { @@ -188,12 +368,35 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[4 2 8 9 9]\n", + " [9 0 6 6 8]\n", + " [3 3 4 8 5]]\n", + "\n", + " [[9 3 5 4 4]\n", + " [8 1 4 5 5]\n", + " [4 2 3 2 8]]]\n", + "[[[ 5. 3. 9. 10. 10.]\n", + " [10. 1. 7. 7. 9.]\n", + " [ 4. 4. 5. 9. 6.]]\n", + "\n", + " [[10. 4. 6. 5. 5.]\n", + " [ 9. 2. 5. 6. 6.]\n", + " [ 5. 3. 4. 3. 9.]]]\n" + ] + } + ], + "source": [ + "print(a)\n", + "print(d)\n", + "\n", + "#D is an array of the same shape as A, with each value + 1, because the original array that we added to it (b) was an array of ones." ] }, { @@ -206,11 +409,28 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 24, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "E is an array created by multiplying A and C: \n", + "[[[4. 2. 8. 9. 9.]\n", + " [9. 0. 6. 6. 8.]\n", + " [3. 3. 4. 8. 5.]]\n", + "\n", + " [[9. 3. 5. 4. 4.]\n", + " [8. 1. 4. 5. 5.]\n", + " [4. 2. 3. 2. 8.]]]\n" + ] + } + ], + "source": [ + "e = a * c\n", + "print(\"E is an array created by multiplying A and C: \")\n", + "print(e)\n" ] }, { @@ -224,12 +444,22 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "E is the same as A, because c consists of ones only\n" + ] + } + ], "source": [ - "### [your code here]\n", - "\n" + "if e.all() == a.all():\n", + " print(\"E is the same as A, because c consists of ones only\")\n", + "else:\n", + " print(\"E is not the same as A\")\n" ] }, { @@ -245,9 +475,24 @@ "cell_type": "code", "execution_count": 32, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max value in d = 10.0\n", + "Min value in d = 1.0\n", + "Mean value in d = 6.033333333333333\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "print(\"Max value in d =\", d_max)\n", + "d_min = d.min()\n", + "print(\"Min value in d =\", d_min)\n", + "d_mean = d.mean()\n", + "print(\"Mean value in d =\", d_mean)\n", "\n" ] }, @@ -263,9 +508,18 @@ "cell_type": "code", "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "F is an empy array of the shape 2x3x5\n" + ] + } + ], "source": [ - "### [your code here]\n" + "f = np.empty((2,3,5))\n", + "print(\"F is an empy array of the shape 2x3x5\")\n" ] }, { @@ -287,11 +541,26 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "for i in range(d.shape[0]):\n", + " for j in range(d.shape[1]):\n", + " for k in range(d.shape[2]):\n", + " # Get the current value\n", + " value = d[i, j, k]\n", + " \n", + " # Apply conditions and update f\n", + " if value > d_min and value < d_mean:\n", + " f[i, j, k] = 25\n", + " elif value > d_mean and value < d_max:\n", + " f[i, j, k] = 75\n", + " elif value == d_mean:\n", + " f[i, j, k] = 50\n", + " elif value == d_max:\n", + " f[i, j, k] = 100\n", + " \n" ] }, { @@ -325,11 +594,39 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "D array (original):\n", + "[[[ 5. 3. 9. 10. 10.]\n", + " [10. 1. 7. 7. 9.]\n", + " [ 4. 4. 5. 9. 6.]]\n", + "\n", + " [[10. 4. 6. 5. 5.]\n", + " [ 9. 2. 5. 6. 6.]\n", + " [ 5. 3. 4. 3. 9.]]]\n", + "\n", + "F is an array of the following numbers:\n", + "[[[ 25. 25. 75. 100. 100.]\n", + " [100. 0. 75. 75. 75.]\n", + " [ 25. 25. 25. 75. 25.]]\n", + "\n", + " [[100. 25. 25. 25. 25.]\n", + " [ 75. 25. 25. 25. 25.]\n", + " [ 25. 25. 25. 25. 75.]]]\n" + ] + } + ], + "source": [ + "print(\"\\nD array (original):\")\n", + "print(d)\n", + "print(\"\\nF is an array of the following numbers:\")\n", + "print(f)" ] }, { @@ -350,11 +647,59 @@ }, { "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]" + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 5. 3. 9. 10. 10.]\n", + " [10. 1. 7. 7. 9.]\n", + " [ 4. 4. 5. 9. 6.]]\n", + "\n", + " [[10. 4. 6. 5. 5.]\n", + " [ 9. 2. 5. 6. 6.]\n", + " [ 5. 3. 4. 3. 9.]]]\n", + "[[[ 25. 25. 75. 100. 100.]\n", + " [100. 0. 75. 75. 75.]\n", + " [ 25. 25. 25. 75. 25.]]\n", + "\n", + " [[100. 25. 25. 25. 25.]\n", + " [ 75. 25. 25. 25. 25.]\n", + " [ 25. 25. 25. 25. 75.]]]\n", + "[[['A' 'A' 'C' 'D' 'D']\n", + " ['D' '0.0' 'C' 'C' 'C']\n", + " ['A' 'A' 'A' 'C' 'A']]\n", + "\n", + " [['D' 'A' 'A' 'A' 'A']\n", + " ['C' 'A' 'A' 'A' 'A']\n", + " ['A' 'A' 'A' 'A' 'C']]]\n" + ] + } + ], + "source": [ + "fs = f.astype(str)\n", + "\n", + "for i in range(d.shape[0]):\n", + " for j in range(d.shape[1]):\n", + " for k in range(d.shape[2]):\n", + " # Get the current value\n", + " value = d[i, j, k]\n", + " \n", + " # Apply conditions and update f\n", + " if value > d_min and value < d_mean:\n", + " fs[i, j, k] = \"A\"\n", + " elif value > d_mean and value < d_max:\n", + " fs[i, j, k] = \"C\"\n", + " elif value == d_mean:\n", + " fs[i, j, k] = \"B\"\n", + " elif value == d_max:\n", + " fs[i, j, k] = \"D\"\n", + " \n", + "print(d)\n", + "print(f)\n", + "print(fs)" ] } ], @@ -374,7 +719,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.11.9" } }, "nbformat": 4,