From 3680a1f9c81b196af019e5f41e0b33f55f22900d Mon Sep 17 00:00:00 2001 From: Amir Golafshan Date: Tue, 17 Oct 2023 19:41:10 +0200 Subject: [PATCH] This one was a harsh one... --- your-code/main.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 your-code/main.ipynb diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 0000000..6a20e33 --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1 @@ +{"cells":[{"cell_type":"markdown","metadata":{},"source":[". Import the NUMPY package under the name np."]},{"cell_type":"code","execution_count":103,"metadata":{},"outputs":[],"source":["import numpy as np\n","import random"]},{"cell_type":"markdown","metadata":{},"source":[". Print the NUMPY version and the configuration."]},{"cell_type":"code","execution_count":104,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["1.26.0\n"]}],"source":["print(np.__version__)"]},{"cell_type":"markdown","metadata":{},"source":[". Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\"
\n","Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find?"]},{"cell_type":"code","execution_count":105,"metadata":{},"outputs":[],"source":["a = np.random.randint(101, size =(2, 3, 5))\n"]},{"cell_type":"markdown","metadata":{},"source":[". Print a."]},{"cell_type":"code","execution_count":106,"metadata":{},"outputs":[{"data":{"text/plain":["array([[[ 71, 86, 82, 86, 88],\n"," [ 31, 9, 77, 68, 87],\n"," [ 7, 51, 79, 37, 43]],\n","\n"," [[ 21, 34, 19, 12, 20],\n"," [ 99, 84, 82, 22, 43],\n"," [100, 26, 100, 15, 25]]])"]},"execution_count":106,"metadata":{},"output_type":"execute_result"}],"source":["a"]},{"cell_type":"markdown","metadata":{},"source":[". Create a 5x2x3 3-dimensional array with all values equaling 1.
\n","ssign the array to variable \"b\""]},{"cell_type":"code","execution_count":107,"metadata":{},"outputs":[],"source":["b = np.ones((5, 2, 3))"]},{"cell_type":"markdown","metadata":{},"source":[". Print b."]},{"cell_type":"code","execution_count":108,"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)"]},{"cell_type":"markdown","metadata":{},"source":[". Do a and b have the same size? How do you prove that in Python code?"]},{"cell_type":"code","execution_count":109,"metadata":{},"outputs":[{"data":{"text/plain":["True"]},"execution_count":109,"metadata":{},"output_type":"execute_result"}],"source":["a.size == b.size"]},{"cell_type":"markdown","metadata":{},"source":[". Are you able to add a and b? Why or why not?"]},{"cell_type":"code","execution_count":110,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["You can't, because the shape is not the same which makes them not homogeneous and not summable\n"]}],"source":["try:\n"," print(a + b)\n","except ValueError:\n"," print(\"You can't, because the shape is not the same which makes them not homogeneous and not summable\")\n"," \n","\n"]},{"cell_type":"markdown","metadata":{},"source":[". Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe \"c\"."]},{"cell_type":"code","execution_count":111,"metadata":{},"outputs":[],"source":["c = b.transpose(1, 2, 0)"]},{"cell_type":"markdown","metadata":{},"source":["0. Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". But why does it work now?"]},{"cell_type":"code","execution_count":112,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["It works now because the Arrays are homogeneous\n"]},{"data":{"text/plain":["array([[[ 72., 87., 83., 87., 89.],\n"," [ 32., 10., 78., 69., 88.],\n"," [ 8., 52., 80., 38., 44.]],\n","\n"," [[ 22., 35., 20., 13., 21.],\n"," [100., 85., 83., 23., 44.],\n"," [101., 27., 101., 16., 26.]]])"]},"execution_count":112,"metadata":{},"output_type":"execute_result"}],"source":["d = a + c\n","print(\"It works now because the Arrays are homogeneous\")\n","d"]},{"cell_type":"markdown","metadata":{},"source":["1. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain."]},{"cell_type":"code","execution_count":113,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Array A\n"," [[[ 71 86 82 86 88]\n"," [ 31 9 77 68 87]\n"," [ 7 51 79 37 43]]\n","\n"," [[ 21 34 19 12 20]\n"," [ 99 84 82 22 43]\n"," [100 26 100 15 25]]] \n"," Array D \n"," [[[ 72. 87. 83. 87. 89.]\n"," [ 32. 10. 78. 69. 88.]\n"," [ 8. 52. 80. 38. 44.]]\n","\n"," [[ 22. 35. 20. 13. 21.]\n"," [100. 85. 83. 23. 44.]\n"," [101. 27. 101. 16. 26.]]]\n","Each value of the array was summed by 1, as the other array had only ones as a number saved\n"]}],"source":["print(\"Array A\\n\", a,\"\\n\",\"Array D \\n\", d)\n","print(\"Each value of the array was summed by 1, as the other array had only ones as a number saved\")"]},{"cell_type":"markdown","metadata":{},"source":["2. Multiply a and c. Assign the result to e."]},{"cell_type":"code","execution_count":114,"metadata":{},"outputs":[],"source":["e = a * c"]},{"cell_type":"markdown","metadata":{},"source":["3. Does e equal to a? Why or why not?"]},{"cell_type":"code","execution_count":115,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Because any number multiplied by the 'one array' will equal to itself as you are multiplying by one.\n"]}],"source":["e == a\n","print(\"Because any number multiplied by the 'one array' will equal to itself as you are multiplying by one.\")"]},{"cell_type":"markdown","metadata":{},"source":["4. Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\""]},{"cell_type":"code","execution_count":116,"metadata":{},"outputs":[],"source":["d_max = np.max(d)\n","d_min = np.min(d)\n","d_mean = np.mean(d)"]},{"cell_type":"markdown","metadata":{},"source":["5. Now we want to label the values in d. First create an empty array \"f\" with the same shape (i.e. 2x3x5) as d using `np.empty`."]},{"cell_type":"code","execution_count":117,"metadata":{},"outputs":[{"data":{"text/plain":["array([[[ 5., 92., 29., 61., 51.],\n"," [ 12., 55., 7., 4., 48.],\n"," [ 10., 100., 42., 24., 76.]],\n","\n"," [[ 27., 19., 87., 33., 72.],\n"," [ 20., 42., 13., 38., 31.],\n"," [ 65., 75., 57., 49., 68.]]])"]},"execution_count":117,"metadata":{},"output_type":"execute_result"}],"source":["f = np.empty((2, 3, 5))\n","f"]},{"cell_type":"markdown","metadata":{},"source":["\n","
\n","#16. Populate the values in f. For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.
\n","If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.
\n","If a value equals to d_mean, assign 50 to the corresponding value in f.
\n","Assign 0 to the corresponding value(s) in f for d_min in d.
\n","Assign 100 to the corresponding value(s) in f for d_max in d.
\n","In the end, f should have only the following values: 0, 25, 50, 75, and 100.
\n","Note: you don't have to use Numpy in this question.
\n"]},{"cell_type":"code","execution_count":118,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[ 75. 75. 75. 75. 75.]\n"," [ 25. 25. 75. 75. 75.]\n"," [ 0. 25. 75. 25. 25.]]\n","\n"," [[ 25. 25. 25. 25. 25.]\n"," [ 75. 75. 75. 25. 25.]\n"," [100. 25. 100. 25. 25.]]]\n"]}],"source":["# f is an empty Array\n","for i in range(2):\n"," for x in range(3):\n"," for z in range(5):\n"," val = d[i][x][z]\n"," \n"," if val > d_min and val < d_mean:\n"," f[i][x][z] = 25\n"," elif val > d_mean and val < d_max:\n"," f[i][x][z] = 75\n"," elif val == d_mean:\n"," f[i][x][z] = 50\n"," elif val == d_min:\n"," f[i][x][z] = 0\n"," elif val == d_max:\n"," f[i][x][z] = 100\n","print(f)\n"]},{"cell_type":"markdown","metadata":{},"source":["\n","
\n","#17. Print d and f. Do you have your expected f?
\n","For instance, if your d is:
\n","array([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931],
\n"," [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981],
\n"," [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]],
\n"," [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381],
\n"," [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881],
\n"," [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]])
\n","Your f should be:
\n","array([[[ 75., 75., 75., 25., 75.],
\n"," [ 75., 75., 25., 25., 25.],
\n"," [ 75., 25., 75., 75., 75.]],
\n"," [[ 25., 25., 25., 25., 100.],
\n"," [ 75., 75., 75., 75., 75.],
\n"," [ 25., 75., 0., 75., 75.]]])
\n"]},{"cell_type":"markdown","metadata":{},"source":["\n","
\n","#18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values
\n","(\"A\", \"B\", \"C\", \"D\", and \"E\") to label the array elements? You are expecting the result to be:
\n","array([[[ 'D', 'D', 'D', 'B', 'D'],
\n"," [ 'D', 'D', 'B', 'B', 'B'],
\n"," [ 'D', 'B', 'D', 'D', 'D']],
\n"," [[ 'B', 'B', 'B', 'B', 'E'],
\n"," [ 'D', 'D', 'D', 'D', 'D'],
\n"," [ 'B', 'D', 'A', 'D', 'D']]])
\n","Again, you don't need Numpy in this question.
\n"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.6"}},"nbformat":4,"nbformat_minor":2}