From cc2d8be5c2499894fe99f706d39a872fe24bae15 Mon Sep 17 00:00:00 2001 From: leticiademarchiferreira <127995157+leticiademarchiferreira@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:32:55 +0200 Subject: [PATCH] C:\Users\Leticia Demarchi\Documents\Ironhack Lisbon April\Labs\1 Week\lab-numpy --- your-code/main.ipynb | 474 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 390 insertions(+), 84 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..91b05bf 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "import numpy as np" ] }, { @@ -28,11 +28,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'1.24.2'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "np.version.version" ] }, { @@ -45,29 +56,76 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 1" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.22994732 0.85402183 0.06920446 0.12713647 0.54553723]\n", + " [0.19333446 0.77719632 0.04050665 0.34264043 0.42888035]\n", + " [0.70225931 0.36629179 0.37707756 0.98780883 0.29479798]]\n", + "\n", + " [[0.12754452 0.76775833 0.44568838 0.93092892 0.49618978]\n", + " [0.24180846 0.11185735 0.67726857 0.95342851 0.81956733]\n", + " [0.08979878 0.68055134 0.39471544 0.92338688 0.03363388]]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "a = np.random.rand(2, 3, 5)\n", + "print(a)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.6392745 0.71473923 0.97765037 0.83331926 0.89510351]\n", + " [0.48030721 0.9675124 0.82132377 0.71452134 0.49720732]\n", + " [0.73294762 0.55329364 0.99652628 0.14991591 0.687638 ]]\n", + "\n", + " [[0.49061146 0.61152264 0.21450827 0.32045128 0.72902605]\n", + " [0.28096238 0.1672366 0.61700693 0.22539691 0.28688449]\n", + " [0.99528979 0.70439625 0.90060238 0.8062965 0.82237096]]]\n" + ] + } + ], "source": [ - "# Method 2" + "b = np.random.sample(size = (2,3,5))\n", + "print (b)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.88489881 0.40434761 0.43798143 0.17691545 0.7069395 ]\n", + " [0.05894542 0.04949018 0.12233392 0.44478056 0.49289665]\n", + " [0.46723047 0.61230726 0.75699837 0.78097504 0.39062554]]\n", + "\n", + " [[0.1579316 0.4783604 0.75666945 0.51036347 0.50104765]\n", + " [0.1059208 0.10630203 0.23485837 0.17837837 0.89807836]\n", + " [0.07171612 0.79254671 0.09423846 0.50079385 0.74610346]]]\n" + ] + } + ], "source": [ - "# Method 3" + "c = np.random.random(size = (2,3,5))\n", + "print (c)" ] }, { @@ -79,11 +137,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.22994732 0.85402183 0.06920446 0.12713647 0.54553723]\n", + " [0.19333446 0.77719632 0.04050665 0.34264043 0.42888035]\n", + " [0.70225931 0.36629179 0.37707756 0.98780883 0.29479798]]\n", + "\n", + " [[0.12754452 0.76775833 0.44568838 0.93092892 0.49618978]\n", + " [0.24180846 0.11185735 0.67726857 0.95342851 0.81956733]\n", + " [0.08979878 0.68055134 0.39471544 0.92338688 0.03363388]]]\n" + ] + } + ], "source": [ - "# your code here" + "print (a)" ] }, { @@ -95,11 +167,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "b = np.ones(shape = (5,2,3), dtype = int)" ] }, { @@ -111,11 +183,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 13, + "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)" ] }, { @@ -127,11 +220,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Leticia Demarchi\\AppData\\Local\\Temp\\ipykernel_27480\\160026346.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.\n", + " a == b\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == b" ] }, { @@ -143,11 +255,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your answer here" + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[15], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m a \u001b[39m+\u001b[39;49m b\n\u001b[0;32m 3\u001b[0m \u001b[39m# the different size\u001b[39;00m\n", + "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "a + b\n", + "\n", + "# the different size" ] }, { @@ -159,11 +285,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]]])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = b.transpose()\n", + "c" ] }, { @@ -175,11 +320,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.22994732 1.85402183 1.06920446 1.12713647 1.54553723]\n", + " [1.19333446 1.77719632 1.04050665 1.34264043 1.42888035]\n", + " [1.70225931 1.36629179 1.37707756 1.98780883 1.29479798]]\n", + "\n", + " [[1.12754452 1.76775833 1.44568838 1.93092892 1.49618978]\n", + " [1.24180846 1.11185735 1.67726857 1.95342851 1.81956733]\n", + " [1.08979878 1.68055134 1.39471544 1.92338688 1.03363388]]]\n" + ] + } + ], + "source": [ + "c = c.reshape(2,3,5)\n", + "d = a + c\n", + "\n", + "print (d)\n", + "\n" ] }, { @@ -191,11 +354,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.22994732 0.85402183 0.06920446 0.12713647 0.54553723]\n", + " [0.19333446 0.77719632 0.04050665 0.34264043 0.42888035]\n", + " [0.70225931 0.36629179 0.37707756 0.98780883 0.29479798]]\n", + "\n", + " [[0.12754452 0.76775833 0.44568838 0.93092892 0.49618978]\n", + " [0.24180846 0.11185735 0.67726857 0.95342851 0.81956733]\n", + " [0.08979878 0.68055134 0.39471544 0.92338688 0.03363388]]]\n", + "[[[1.22994732 1.85402183 1.06920446 1.12713647 1.54553723]\n", + " [1.19333446 1.77719632 1.04050665 1.34264043 1.42888035]\n", + " [1.70225931 1.36629179 1.37707756 1.98780883 1.29479798]]\n", + "\n", + " [[1.12754452 1.76775833 1.44568838 1.93092892 1.49618978]\n", + " [1.24180846 1.11185735 1.67726857 1.95342851 1.81956733]\n", + " [1.08979878 1.68055134 1.39471544 1.92338688 1.03363388]]]\n" + ] + } + ], + "source": [ + "print(a)\n", + "print(d)\n", + "\n", + "# Different because i added variable c to d" ] }, { @@ -207,11 +394,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.22994732 0.85402183 0.06920446 0.12713647 0.54553723]\n", + " [0.19333446 0.77719632 0.04050665 0.34264043 0.42888035]\n", + " [0.70225931 0.36629179 0.37707756 0.98780883 0.29479798]]\n", + "\n", + " [[0.12754452 0.76775833 0.44568838 0.93092892 0.49618978]\n", + " [0.24180846 0.11185735 0.67726857 0.95342851 0.81956733]\n", + " [0.08979878 0.68055134 0.39471544 0.92338688 0.03363388]]]\n" + ] + } + ], "source": [ - "# your code here" + "e = a * c\n", + "print (e)" ] }, { @@ -223,11 +425,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 23, + "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": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e == a\n", + "\n", + "# Its true because they have same value." ] }, { @@ -239,11 +460,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.9878088273725407\n", + "1.0336338785069095\n", + "1.4676922484288266\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean =d.mean()\n", + "\n", + "print(d_max)\n", + "print(d_min)\n", + "print(d_mean)" ] }, { @@ -255,11 +492,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.22994732 0.85402183 0.06920446 0.12713647 0.54553723]\n", + " [0.19333446 0.77719632 0.04050665 0.34264043 0.42888035]\n", + " [0.70225931 0.36629179 0.37707756 0.98780883 0.29479798]]\n", + "\n", + " [[0.12754452 0.76775833 0.44568838 0.93092892 0.49618978]\n", + " [0.24180846 0.11185735 0.67726857 0.95342851 0.81956733]\n", + " [0.08979878 0.68055134 0.39471544 0.92338688 0.03363388]]]\n" + ] + } + ], "source": [ - "# your code here" + "f = np.empty(shape = (2,3,5))\n", + "print (f)" ] }, { @@ -275,11 +527,37 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "expected ':' (2206072419.py, line 17)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[52], line 17\u001b[1;36m\u001b[0m\n\u001b[1;33m else d[i] == d_max:\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m expected ':'\n" + ] + } + ], + "source": [ + "d_min = d.min()\n", + "d_max = d.max()\n", + "d_mean = d.mean()\n", + "\n", + "\n", + "f_populate = [0] * len(d) \n", + "\n", + "for i in range(len(d)):\n", + " if d[i] == d_min:\n", + " f_populate[i] = 0\n", + " if d_min < d[i] < d_mean:\n", + " f_populate[i] <= 25\n", + " if d[i] == d_mean:\n", + " f_populate[i] <= 50\n", + " elif d_mean < d[i] < d_max:\n", + " f_populate[i] <= 75\n", + " else d[i] == d_max:\n", + " f_populate[i] >= 100" ] }, { @@ -309,11 +587,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d = [[[1.22994732 1.85402183 1.06920446 1.12713647 1.54553723]\n", + " [1.19333446 1.77719632 1.04050665 1.34264043 1.42888035]\n", + " [1.70225931 1.36629179 1.37707756 1.98780883 1.29479798]]\n", + "\n", + " [[1.12754452 1.76775833 1.44568838 1.93092892 1.49618978]\n", + " [1.24180846 1.11185735 1.67726857 1.95342851 1.81956733]\n", + " [1.08979878 1.68055134 1.39471544 1.92338688 1.03363388]]]\n", + "f = [[[25, 75, 25, 25, 75], [25, 75, 25, 25, 25], [75, 25, 25, 100, 25]], [[25, 75, 25, 75, 75], [25, 25, 75, 75, 75], [25, 75, 25, 75, 0]]]\n" + ] + } + ], + "source": [ + "print(\"d =\", d)\n", + "\n", + "\n", + "print(\"f =\", f)" ] }, { @@ -335,12 +631,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "could not convert string to float: 'B'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[55], line 11\u001b[0m\n\u001b[0;32m 9\u001b[0m f_labeled[i][j][k] \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mE\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[39melif\u001b[39;00m val \u001b[39m<\u001b[39m d_mean:\n\u001b[1;32m---> 11\u001b[0m f_labeled[i][j][k] \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mB\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 12\u001b[0m \u001b[39melif\u001b[39;00m val \u001b[39m>\u001b[39m d_mean:\n\u001b[0;32m 13\u001b[0m f_labeled[i][j][k] \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mD\u001b[39m\u001b[39m\"\u001b[39m\n", + "\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'B'" + ] + } + ], + "source": [] } ], "metadata": { @@ -359,7 +665,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.11.2" } }, "nbformat": 4,