diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 0000000..67c41bc --- /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":15,"metadata":{},"outputs":[],"source":["import numpy as np\n","import random"]},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[],"source":["def numpy_info (array):\n"," print(f\"The type is {type(array)}\")\n"," print(f\"The n.dim is {array.ndim}\")\n"," print(f\"The shape is {array.shape}\")\n"," print(f\"The size is {array.size}\")"]},{"cell_type":"markdown","metadata":{},"source":[". Print the NUMPY version and the configuration."]},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["1.26.1\n","\n"]}],"source":["print(np.__version__)\n","print(np.show_config)"]},{"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":18,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The type is \n","The n.dim is 3\n","The shape is (2, 3, 5)\n","The size is 30\n","The type is \n","The n.dim is 3\n","The shape is (2, 3, 5)\n","The size is 30\n","The type is \n","The n.dim is 3\n","The shape is (2, 3, 5)\n","The size is 30\n"]}],"source":["a = np.random.randint(100, size = (2, 3, 5))\n","numpy_info(a)\n","a_1 = np.random.rand(2, 3, 5)\n","numpy_info(a_1)\n","a_2 = np.random.choice(100, size= (2, 3, 5))\n","numpy_info(a_2)"]},{"cell_type":"markdown","metadata":{},"source":[". Print a."]},{"cell_type":"code","execution_count":19,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[0.19054229 0.08811853 0.9689958 0.45814016 0.83376166]\n"," [0.39145568 0.81129293 0.00854977 0.646514 0.63518629]\n"," [0.53433367 0.6294305 0.13253987 0.07989989 0.05865877]]\n","\n"," [[0.5533395 0.72889715 0.09546565 0.60474081 0.34365328]\n"," [0.00190017 0.40331366 0.41965764 0.64858559 0.44769148]\n"," [0.27414328 0.11926717 0.98149418 0.08919737 0.60571278]]]\n","[[[ 9 87 83 98 83]\n"," [35 28 28 34 81]\n"," [69 19 42 99 57]]\n","\n"," [[93 81 36 26 52]\n"," [68 0 35 84 2]\n"," [34 30 78 94 76]]]\n","[[[ 9 87 83 98 83]\n"," [35 28 28 34 81]\n"," [69 19 42 99 57]]\n","\n"," [[93 81 36 26 52]\n"," [68 0 35 84 2]\n"," [34 30 78 94 76]]]\n"]}],"source":["print(a_1)\n","print(a_2)\n","print(a_2)"]},{"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":20,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The type is \n","The n.dim is 3\n","The shape is (5, 2, 3)\n","The size is 30\n"]}],"source":["b = np.ones((5, 2, 3))\n","numpy_info(b)"]},{"cell_type":"markdown","metadata":{},"source":[". Print b."]},{"cell_type":"code","execution_count":21,"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":22,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["30\n","30\n"]}],"source":["a.size == b.size\n","print(a.size)\n","print(b.size)"]},{"cell_type":"markdown","metadata":{},"source":[". Are you able to add a and b? Why or why not?
\n","a + b -> ValueError
\n","e cannot add a + b because even though the size is the same, the shape is not so in order to be able to aperate between those arrays, we'd need to transpose."]},{"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":23,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["(2, 3, 5)\n","(5, 2, 3)\n"]}],"source":["print(a.shape)\n","print(b.shape)"]},{"cell_type":"code","execution_count":24,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["(2, 3, 5)\n"]}],"source":["c = b.transpose(1, 2, 0)\n","print(c.shape)"]},{"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":25,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[96. 86. 50. 71. 44.]\n"," [68. 78. 23. 60. 61.]\n"," [43. 91. 22. 70. 80.]]\n","\n"," [[98. 37. 18. 3. 22.]\n"," [75. 97. 84. 35. 32.]\n"," [96. 18. 74. 66. 69.]]]\n"]}],"source":["d = a + c\n","print(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":26,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[95 85 49 70 43]\n"," [67 77 22 59 60]\n"," [42 90 21 69 79]]\n","\n"," [[97 36 17 2 21]\n"," [74 96 83 34 31]\n"," [95 17 73 65 68]]]\n","[[[96. 86. 50. 71. 44.]\n"," [68. 78. 23. 60. 61.]\n"," [43. 91. 22. 70. 80.]]\n","\n"," [[98. 37. 18. 3. 22.]\n"," [75. 97. 84. 35. 32.]\n"," [96. 18. 74. 66. 69.]]]\n"]},{"data":{"text/plain":["'as c was a ones array, when we changed the shape, we could superpose the both of them and do the addition.\\nThis is why the values are the innitial ones +1. \\nBut we can also observe that the d array has a dot after each value, I think that happens because normally\\nan array of ones as an array of 0 are used to create masks aka a filter to an array with a condition to return True or false\\nand only show the True ones, that is when the condition is met'"]},"execution_count":26,"metadata":{},"output_type":"execute_result"}],"source":["print(a)\n","print(d)\n","'''as c was a ones array, when we changed the shape, we could superpose the both of them and do the addition.\n","This is why the values are the innitial ones +1. \n","But we can also observe that the d array has a dot after each value, I think that happens because normally\n","an array of ones as an array of 0 are used to create masks aka a filter to an array with a condition to return True or false\n","and only show the True ones, that is when the condition is met'''"]},{"cell_type":"markdown","metadata":{},"source":["2. Multiply a and c. Assign the result to e."]},{"cell_type":"code","execution_count":27,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[95. 85. 49. 70. 43.]\n"," [67. 77. 22. 59. 60.]\n"," [42. 90. 21. 69. 79.]]\n","\n"," [[97. 36. 17. 2. 21.]\n"," [74. 96. 83. 34. 31.]\n"," [95. 17. 73. 65. 68.]]]\n","[[[95 85 49 70 43]\n"," [67 77 22 59 60]\n"," [42 90 21 69 79]]\n","\n"," [[97 36 17 2 21]\n"," [74 96 83 34 31]\n"," [95 17 73 65 68]]]\n","The type is \n","The n.dim is 3\n","The shape is (2, 3, 5)\n","The size is 30\n","The type is \n","The n.dim is 3\n","The shape is (2, 3, 5)\n","The size is 30\n"]}],"source":["e = a * c\n","print(e)\n","print(a)\n","numpy_info(e)\n","numpy_info(a)"]},{"cell_type":"markdown","metadata":{},"source":["3. Does e equal to a? Why or why not?"]},{"cell_type":"code","execution_count":28,"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":28,"metadata":{},"output_type":"execute_result"}],"source":["a == e "]},{"cell_type":"markdown","metadata":{},"source":["\n"," Yes, because it e equals a, the first array multiplied with c, the ones mask, so basically you are passing a mask of always true.
\n","so c is a ones array, so it multiplies per one. I don't know if that means that e becomes a mask, because of the dots?\n"]},{"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":29,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["98.0\n"]}],"source":["d_max = np.max(d)\n","print(d_max)"]},{"cell_type":"code","execution_count":30,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["3.0\n"]}],"source":["d_min= np.min(d)\n","print(d_min)"]},{"cell_type":"code","execution_count":31,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["58.9\n"]}],"source":["d_mean = np.mean(d)\n","print(d_mean)"]},{"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":32,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[95. 85. 49. 70. 43.]\n"," [67. 77. 22. 59. 60.]\n"," [42. 90. 21. 69. 79.]]\n","\n"," [[97. 36. 17. 2. 21.]\n"," [74. 96. 83. 34. 31.]\n"," [95. 17. 73. 65. 68.]]]\n","The type is \n","The n.dim is 3\n","The shape is (2, 3, 5)\n","The size is 30\n"]}],"source":["f = np.empty((2, 3, 5))\n","print(f)\n","numpy_info(f)"]},{"cell_type":"markdown","metadata":{},"source":["he result say that f == e and f == d"]},{"cell_type":"code","execution_count":33,"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":33,"metadata":{},"output_type":"execute_result"}],"source":["f == e"]},{"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":"markdown","metadata":{},"source":["\n","f = np.where( d_min < d < d_mean, 25)
\n","f = np.where(d == d_mean, 50)
\n","f = np.where(d == d_min , 0)
\n","f = np.where(d == d_max, 100)
\n","f[d_min < d < d_mean] = 25
\n","all(d_min < d < d_mean, 25)\n"]},{"cell_type":"code","execution_count":34,"metadata":{},"outputs":[],"source":["f[(d_min < d) & ( d < d_mean) ] = 25\n","f[(d_mean < d) & ( d < d_max)] = 75\n","f[d == d_mean] = 50\n","f[d == d_min ] = 0\n","f[d == d_max] = 100"]},{"cell_type":"code","execution_count":37,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[ 75. 75. 25. 75. 25.]\n"," [ 75. 75. 25. 75. 75.]\n"," [ 25. 75. 25. 75. 75.]]\n","\n"," [[100. 25. 25. 0. 25.]\n"," [ 75. 75. 75. 25. 25.]\n"," [ 75. 25. 75. 75. 75.]]]\n"]}],"source":["print(f)"]},{"cell_type":"code","execution_count":38,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[96. 86. 50. 71. 44.]\n"," [68. 78. 23. 60. 61.]\n"," [43. 91. 22. 70. 80.]]\n","\n"," [[98. 37. 18. 3. 22.]\n"," [75. 97. 84. 35. 32.]\n"," [96. 18. 74. 66. 69.]]]\n"]}],"source":["print(d)"]},{"cell_type":"markdown","metadata":{},"source":["\n","
\n","f = list(f.flatten())
\n","print(f)
\n","for i in f:
\n"," i = int(i)
\n"," if d_min < d < d_mean:
\n"," i = 25
\n"," elif d == d_mean:
\n"," i = 50
\n"," elif d == d_min:
\n"," i = 0
\n"," else:
\n"," i = 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":"code","execution_count":36,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[[96. 86. 50. 71. 44.]\n"," [68. 78. 23. 60. 61.]\n"," [43. 91. 22. 70. 80.]]\n","\n"," [[98. 37. 18. 3. 22.]\n"," [75. 97. 84. 35. 32.]\n"," [96. 18. 74. 66. 69.]]]\n","[[[ 75. 75. 25. 75. 25.]\n"," [ 75. 75. 25. 75. 75.]\n"," [ 25. 75. 25. 75. 75.]]\n","\n"," [[100. 25. 25. 0. 25.]\n"," [ 75. 75. 75. 25. 25.]\n"," [ 75. 25. 75. 75. 75.]]]\n"]}],"source":["print(d)\n","print(f)"]},{"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"]},{"cell_type":"code","execution_count":42,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[['' '' '' '' '']\n"," ['' '' '' '' '']\n"," ['' '' '' '' '']]\n","\n"," [['' '' '' '' '']\n"," ['' '' '' '' '']\n"," ['' '' '' '' '']]]\n"]}],"source":["h = np.empty((2, 3, 5), dtype=str)\n","print(h)"]},{"cell_type":"code","execution_count":43,"metadata":{},"outputs":[],"source":["\n","h[(d_min < d) & ( d < d_mean) ] = \"B\"\n","h[(d_mean < d) & ( d < d_max)] = \"D\"\n","h[d == d_mean] = \"C\"\n","h[d == d_min ] = \"A\"\n","h[d == d_max] = \"E\""]},{"cell_type":"code","execution_count":44,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[[['D' 'D' 'B' 'D' 'B']\n"," ['D' 'D' 'B' 'D' 'D']\n"," ['B' 'D' 'B' 'D' 'D']]\n","\n"," [['E' 'B' 'B' 'A' 'B']\n"," ['D' 'D' 'D' 'B' 'B']\n"," ['D' 'B' 'D' 'D' 'D']]]\n"]}],"source":["print(h)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":[]}],"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.5"}},"nbformat":4,"nbformat_minor":2} diff --git a/your-code/main.py b/your-code/main.py index 78c792b..3d8b694 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,67 +1,127 @@ #1. Import the NUMPY package under the name np. +import numpy as np +import random +def numpy_info (array): + print(f"The type is {type(array)}") + print(f"The n.dim is {array.ndim}") + print(f"The shape is {array.shape}") + print(f"The size is {array.size}") #2. Print the NUMPY version and the configuration. +print(np.__version__) +print(np.show_config) #3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable "a" # Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find? - +a = np.random.randint(100, size = (2, 3, 5)) +numpy_info(a) +a_1 = np.random.rand(2, 3, 5) +numpy_info(a_1) +a_2 = np.random.choice(100, size= (2, 3, 5)) +numpy_info(a_2) #4. Print a. +print(a_1) +print(a_2) +print(a_2) #5. Create a 5x2x3 3-dimensional array with all values equaling 1. #Assign the array to variable "b" - +b = np.ones((5, 2, 3)) +numpy_info(b) #6. Print b. +print(b) + #7. Do a and b have the same size? How do you prove that in Python code? +a.size == b.size +print(a.size) +print(b.size) #8. Are you able to add a and b? Why or why not? +# a + b -> ValueError +#We cannot add a + b because even though the size is the same, the shape is not so in order to be able to aperate between those arrays, we'd need to transpose. #9. Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe "c". +print(a.shape) +print(b.shape) +c = b.transpose(1, 2, 0) +print(c.shape) -#10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +#10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +d = a + c +print(d) #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. - +print(a) +print(d) +'''as c was a ones array, when we changed the shape, we could superpose the both of them and do the addition. +This is why the values are the innitial ones +1. +But we can also observe that the d array has a dot after each value, I think that happens because normally +an array of ones as an array of 0 are used to create masks aka a filter to an array with a condition to return True or false +and only show the True ones, that is when the condition is met''' #12. Multiply a and c. Assign the result to e. +e = a * c +print(e) +print(a) +numpy_info(e) +numpy_info(a) #13. Does e equal to a? Why or why not? +a == e +''' Yes, because it e equals a, the first array multiplied with c, the ones mask, so basically you are passing a mask of always true. +Also c is a ones array, so it multiplies per one. I don't know if that means that e becomes a mask, because of the dots?''' #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_max = np.max(d) +print(d_max) + +d_min= np.min(d) +print(d_min) + +d_mean = np.mean(d) +print(d_mean) #15. 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`. +f = np.empty((2, 3, 5)) +print(f) +numpy_info(f) + +#The result say that f == e and f == d + +f == e @@ -74,6 +134,41 @@ In the end, f should have only the following values: 0, 25, 50, 75, and 100. Note: you don't have to use Numpy in this question. """ +'''f = np.where( d_min < d < d_mean, 25) +f = np.where(d == d_mean, 50) +f = np.where(d == d_min , 0) +f = np.where(d == d_max, 100) + +f[d_min < d < d_mean] = 25 +f.all(d_min < d < d_mean, 25)''' + +f[(d_min < d) & ( d < d_mean) ] = 25 +f[(d_mean < d) & ( d < d_max)] = 75 +f[d == d_mean] = 50 +f[d == d_min ] = 0 +f[d == d_max] = 100 + +print(f) + +''' + +f = list(f.flatten()) +print(f) + +for i in f: + i = int(i) + if d_min < d < d_mean: + i = 25 + elif d == d_mean: + i = 50 + elif d == d_min: + i = 0 + else: + i = 100 + +print(f) +''' + @@ -98,7 +193,8 @@ [ 75., 75., 75., 75., 75.], [ 25., 75., 0., 75., 75.]]]) """ - +print(d) +print(f) """ #18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values @@ -111,4 +207,15 @@ [ 'D', 'D', 'D', 'D', 'D'], [ 'B', 'D', 'A', 'D', 'D']]]) Again, you don't need Numpy in this question. -""" \ No newline at end of file +""" + +h = np.empty((2, 3, 5), dtype=str) +print(h) + +h[(d_min < d) & ( d < d_mean) ] = "B" +h[(d_mean < d) & ( d < d_max)] = "D" +h[d == d_mean] = "C" +h[d == d_min ] = "A" +h[d == d_max] = "E" + +print(h) \ No newline at end of file