From ac70308d1fa603e8228b70f91790d4366d5c8f02 Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Fri, 21 Apr 2023 00:02:50 +0100 Subject: [PATCH] msg --- your-code/main.ipynb | 572 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 518 insertions(+), 54 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..adc7ed7 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "import numpy as np" ] }, { @@ -28,13 +28,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.23.5\n" + ] + } + ], "source": [ - "# your code here" + "print(np.__version__)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -43,13 +58,65 @@ "**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": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.27037873, 0.70309754, 0.82337817],\n", + " [0.20766475, 0.05413314, 0.8032404 ],\n", + " [0.38223483, 0.5359152 , 0.67083381],\n", + " [0.28344007, 0.79039299, 0.63706239],\n", + " [0.95277429, 0.35026345, 0.40956157]],\n", + "\n", + " [[0.48340665, 0.91636314, 0.1425986 ],\n", + " [0.02143037, 0.19644788, 0.83660673],\n", + " [0.00513804, 0.34560732, 0.39811757],\n", + " [0.74611856, 0.17200909, 0.80826464],\n", + " [0.67436406, 0.82727513, 0.58980246]]])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method 1\n", + "a = np.random.random(size = (2,5,3))\n", + "a" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n", + " 27, 28, 29])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Method 1" + "# Method 2\n", + "np.arange(start = 10, stop = 30)\n" ] }, { @@ -57,8 +124,53 @@ "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", + " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Method 2" + "# Method 3\n", + "np.arange(30)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([18. , 20.13793103, 22.27586207, 24.4137931 , 26.55172414,\n", + " 28.68965517, 30.82758621, 32.96551724, 35.10344828, 37.24137931,\n", + " 39.37931034, 41.51724138, 43.65517241, 45.79310345, 47.93103448,\n", + " 50.06896552, 52.20689655, 54.34482759, 56.48275862, 58.62068966,\n", + " 60.75862069, 62.89655172, 65.03448276, 67.17241379, 69.31034483,\n", + " 71.44827586, 73.5862069 , 75.72413793, 77.86206897, 80. ])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.linspace(start = 18, stop = 80, num = 30)\n" ] }, { @@ -66,9 +178,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# Method 3" - ] + "source": [] }, { "cell_type": "markdown", @@ -79,11 +189,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.89097919 0.05595845 0.97682385]\n", + " [0.49302744 0.95289283 0.34244638]\n", + " [0.02879384 0.52265663 0.85916947]\n", + " [0.08807397 0.40824695 0.35273691]\n", + " [0.83319282 0.60011868 0.90759892]]\n", + "\n", + " [[0.82002392 0.14040795 0.07741416]\n", + " [0.19218605 0.42625625 0.02376496]\n", + " [0.28585249 0.820609 0.48523099]\n", + " [0.74063318 0.37669379 0.1529996 ]\n", + " [0.15708925 0.527143 0.42613889]]]\n" + ] + } + ], + "source": [ + "# your code here\n", + "print (a)" ] }, { @@ -93,14 +222,47 @@ "#### 5. Create a 5x2x3 3-dimensional array with all values equaling 1. Assign the array to variable *b*." ] }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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]]])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "b = np.ones(shape = (5,2,3), dtype=int)\n", + "b" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] }, { "cell_type": "markdown", @@ -111,11 +273,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 22, + "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)" ] }, { @@ -125,14 +309,56 @@ "#### 7. Do *a* and *b* have the same size? How do you prove that in Python code?" ] }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 5, 3)\n", + "(5, 2, 3)\n" + ] + } + ], + "source": [ + "# your code here\n", + "\n", + "#No, their size is different\n", + "\n", + "print (a.shape)\n", + "print (b.shape)" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] }, { "cell_type": "markdown", @@ -147,7 +373,9 @@ "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "\n", + "##No, because their structure is different" ] }, { @@ -159,11 +387,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1, 1, 1],\n", + " [1, 1, 1],\n", + " [1, 1, 1],\n", + " [1, 1, 1],\n", + " [1, 1, 1]],\n", + "\n", + " [[1, 1, 1],\n", + " [1, 1, 1],\n", + " [1, 1, 1],\n", + " [1, 1, 1],\n", + " [1, 1, 1]]])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "c = b.reshape (2,5,3)\n", + "c" ] }, { @@ -175,11 +426,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.27037873, 1.70309754, 1.82337817],\n", + " [1.20766475, 1.05413314, 1.8032404 ],\n", + " [1.38223483, 1.5359152 , 1.67083381],\n", + " [1.28344007, 1.79039299, 1.63706239],\n", + " [1.95277429, 1.35026345, 1.40956157]],\n", + "\n", + " [[1.48340665, 1.91636314, 1.1425986 ],\n", + " [1.02143037, 1.19644788, 1.83660673],\n", + " [1.00513804, 1.34560732, 1.39811757],\n", + " [1.74611856, 1.17200909, 1.80826464],\n", + " [1.67436406, 1.82727513, 1.58980246]]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code/answer here\n", + "d = a + c\n", + "d" ] }, { @@ -189,14 +463,53 @@ "#### 11. Print *a* and *d*. Notice the difference and relation of the two array in terms of the values? Explain." ] }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.27037873 0.70309754 0.82337817]\n", + " [0.20766475 0.05413314 0.8032404 ]\n", + " [0.38223483 0.5359152 0.67083381]\n", + " [0.28344007 0.79039299 0.63706239]\n", + " [0.95277429 0.35026345 0.40956157]]\n", + "\n", + " [[0.48340665 0.91636314 0.1425986 ]\n", + " [0.02143037 0.19644788 0.83660673]\n", + " [0.00513804 0.34560732 0.39811757]\n", + " [0.74611856 0.17200909 0.80826464]\n", + " [0.67436406 0.82727513 0.58980246]]]\n", + "[[[1.27037873 1.70309754 1.82337817]\n", + " [1.20766475 1.05413314 1.8032404 ]\n", + " [1.38223483 1.5359152 1.67083381]\n", + " [1.28344007 1.79039299 1.63706239]\n", + " [1.95277429 1.35026345 1.40956157]]\n", + "\n", + " [[1.48340665 1.91636314 1.1425986 ]\n", + " [1.02143037 1.19644788 1.83660673]\n", + " [1.00513804 1.34560732 1.39811757]\n", + " [1.74611856 1.17200909 1.80826464]\n", + " [1.67436406 1.82727513 1.58980246]]]\n" + ] + } + ], + "source": [ + "print (a)\n", + "print (d)\n", + "\n", + "#Yes, they are different because the second one is the values of the first plus 1" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code/answer here" - ] + "source": [] }, { "cell_type": "markdown", @@ -205,36 +518,166 @@ "#### 12. Multiply *a* and *c*. Assign the result to *e*." ] }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.27037873, 0.70309754, 0.82337817],\n", + " [0.20766475, 0.05413314, 0.8032404 ],\n", + " [0.38223483, 0.5359152 , 0.67083381],\n", + " [0.28344007, 0.79039299, 0.63706239],\n", + " [0.95277429, 0.35026345, 0.40956157]],\n", + "\n", + " [[0.48340665, 0.91636314, 0.1425986 ],\n", + " [0.02143037, 0.19644788, 0.83660673],\n", + " [0.00513804, 0.34560732, 0.39811757],\n", + " [0.74611856, 0.17200909, 0.80826464],\n", + " [0.67436406, 0.82727513, 0.58980246]]])" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e = a * c\n", + "e" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, "source": [ - "# your code here" + "#### 13. Does *e* equal to *a*? Why or why not?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.27037873 0.70309754 0.82337817]\n", + " [0.20766475 0.05413314 0.8032404 ]\n", + " [0.38223483 0.5359152 0.67083381]\n", + " [0.28344007 0.79039299 0.63706239]\n", + " [0.95277429 0.35026345 0.40956157]]\n", + "\n", + " [[0.48340665 0.91636314 0.1425986 ]\n", + " [0.02143037 0.19644788 0.83660673]\n", + " [0.00513804 0.34560732 0.39811757]\n", + " [0.74611856 0.17200909 0.80826464]\n", + " [0.67436406 0.82727513 0.58980246]]]\n", + "[[[0.27037873 0.70309754 0.82337817]\n", + " [0.20766475 0.05413314 0.8032404 ]\n", + " [0.38223483 0.5359152 0.67083381]\n", + " [0.28344007 0.79039299 0.63706239]\n", + " [0.95277429 0.35026345 0.40956157]]\n", + "\n", + " [[0.48340665 0.91636314 0.1425986 ]\n", + " [0.02143037 0.19644788 0.83660673]\n", + " [0.00513804 0.34560732 0.39811757]\n", + " [0.74611856 0.17200909 0.80826464]\n", + " [0.67436406 0.82727513 0.58980246]]]\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "\n", + "print (a)\n", + "print (e)\n", + "\n", + "#Yes it does, because the second array is the values of the first times 1 and as we know a number times 1 equals\n", + "#the number" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### 13. Does *e* equal to *a*? Why or why not?\n" + "#### 14. 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": null, + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.9527742895851388" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "\n", + "d_max = d.max()\n", + "d_max\n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.005138035128263" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code/answer here" + "d_min = d.min()\n", + "d_min\n", + "\n" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 54, "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.501264052400867" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#### 14. Identify the max, min, and mean values in *d*. Assign those values to variables *d_max*, *d_min* and *d_mean*." + "d_mean = d.mean()\n", + "d_mean" ] }, { @@ -242,9 +685,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] }, { "cell_type": "markdown", @@ -255,13 +696,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "f = np.empty(shape = (2,3,5))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -345,7 +809,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -359,7 +823,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.9" } }, "nbformat": 4,