From 250fa4ae6931d0a13658abdad28728c42805aea3 Mon Sep 17 00:00:00 2001 From: Fernando Carvalho Date: Tue, 18 Apr 2023 13:27:16 +0100 Subject: [PATCH] done --- your-code/main.ipynb | 437 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 365 insertions(+), 72 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..eab6c70 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 339, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "import numpy as np" ] }, { @@ -28,11 +29,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 318, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.21.5\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(np.__version__)" ] }, { @@ -45,25 +55,54 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 1" + "execution_count": 319, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.95631584 0.41141796 0.38880668 0.37988925 0.49437349]\n", + " [0.37461065 0.45949193 0.33533647 0.87096876 0.70867683]\n", + " [0.12239341 0.59354259 0.86123391 0.16841005 0.39688409]]\n", + "\n", + " [[0.79676545 0.25978891 0.53047298 0.86352398 0.44918973]\n", + " [0.15581548 0.55711616 0.07735143 0.72414889 0.37969157]\n", + " [0.22150798 0.46880091 0.76339816 0.64187858 0.8128744 ]]]\n" + ] + } + ], + "source": [ + "# Method 1\n", + "a = np.random.random((2,3,5))\n", + "print(a)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 320, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3 4 5]\n", + " [4 5 6 5 1]\n", + " [7 8 9 5 3]]\n" + ] + } + ], "source": [ - "# Method 2" + "# Method 2\n", + "lst_lst = [[1,2,3,4,5],[4,5,6,5,1],[7,8,9,5,3]]\n", + "d = np.array(lst_lst)\n", + "print(d)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 321, "metadata": {}, "outputs": [], "source": [ @@ -79,11 +118,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 322, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.95631584 0.41141796 0.38880668 0.37988925 0.49437349]\n", + " [0.37461065 0.45949193 0.33533647 0.87096876 0.70867683]\n", + " [0.12239341 0.59354259 0.86123391 0.16841005 0.39688409]]\n", + "\n", + " [[0.79676545 0.25978891 0.53047298 0.86352398 0.44918973]\n", + " [0.15581548 0.55711616 0.07735143 0.72414889 0.37969157]\n", + " [0.22150798 0.46880091 0.76339816 0.64187858 0.8128744 ]]]\n" + ] + } + ], "source": [ - "# your code here" + "print(a)" ] }, { @@ -95,11 +148,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 323, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "b = np.ones((5,2,3))\n", + "#b.shape" ] }, { @@ -111,11 +167,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 324, + "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)" ] }, { @@ -127,11 +205,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 325, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "a: (2, 3, 5)\n", + "b: (5, 2, 3)\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(np.array_equal(a,b))\n", + "print(\"a:\",a.shape) \n", + "print(\"b:\",b.shape) " ] }, { @@ -143,11 +234,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 326, "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[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_5460\\1745588759.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# your answer here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;31m#The number os 2D Arrays declared in both must have the same shape\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], "source": [ - "# your answer here" + "# your answer here\n", + "np.add(a,b)\n", + "#The number os 2D Arrays declared in both must have the same shape" ] }, { @@ -159,11 +264,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 327, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "#b 5x2x3\n", + "# 0 1 2 \n", + "c = np.transpose(b,(1,2,0))\n", + "print(c.shape)" ] }, { @@ -175,11 +292,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 328, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A: (2, 3, 5)\n", + "C: (2, 3, 5)\n", + "D: (2, 3, 5)\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "print(\"A:\",a.shape)\n", + "print(\"C:\",c.shape)\n", + "d = np.add(a,c)\n", + "print(\"D:\",d.shape)\n", + "#print(d)\n", + "#The number os 2D Arrays declared in both must have the same shape" ] }, { @@ -191,11 +324,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 329, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A: [[[0.95631584 0.41141796 0.38880668 0.37988925 0.49437349]\n", + " [0.37461065 0.45949193 0.33533647 0.87096876 0.70867683]\n", + " [0.12239341 0.59354259 0.86123391 0.16841005 0.39688409]]\n", + "\n", + " [[0.79676545 0.25978891 0.53047298 0.86352398 0.44918973]\n", + " [0.15581548 0.55711616 0.07735143 0.72414889 0.37969157]\n", + " [0.22150798 0.46880091 0.76339816 0.64187858 0.8128744 ]]]\n", + "-- \n", + "D: [[[1.95631584 1.41141796 1.38880668 1.37988925 1.49437349]\n", + " [1.37461065 1.45949193 1.33533647 1.87096876 1.70867683]\n", + " [1.12239341 1.59354259 1.86123391 1.16841005 1.39688409]]\n", + "\n", + " [[1.79676545 1.25978891 1.53047298 1.86352398 1.44918973]\n", + " [1.15581548 1.55711616 1.07735143 1.72414889 1.37969157]\n", + " [1.22150798 1.46880091 1.76339816 1.64187858 1.8128744 ]]]\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "print(\"A:\",a)\n", + "print(\"-- \")\n", + "print(\"D:\",d)\n", + "#It sum's de content of array A with the B's 'ones' array." ] }, { @@ -207,11 +366,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 330, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "e = a * c" ] }, { @@ -223,11 +383,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 331, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "print(np.array_equal(a,e))\n", + "\n", + "# It is because It multiplied the content of A with the content of B wich are only 'ones'" ] }, { @@ -239,11 +410,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 332, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max: 1.956315842580949\n", + "Min: 1.0773514327283324\n", + "Mean: 1.5074892172936971\n" + ] + } + ], + "source": [ + "# your code here\n", + "#print(d)\n", + "d_max = np.amax(d)\n", + "d_min = np.amin(d)\n", + "d_mean = np.mean(d)\n", + "print(\"Max:\",d_max)\n", + "print(\"Min:\",d_min)\n", + "print(\"Mean:\",d_mean)\n", + "\n" ] }, { @@ -255,15 +444,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 333, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.]],\n", + "\n", + " [[0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.],\n", + " [0., 0., 0., 0., 0.]]])" + ] + }, + "execution_count": 333, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "\n", + "shape = (2,3,5)\n", + "f = np.zeros(shape) \n", + "f" ] }, { - "cell_type": "markdown", + "cell_type": "raw", "metadata": {}, "source": [ "#### 16. Populate the values in *f*. \n", @@ -275,11 +485,42 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 334, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean: 1.5074892172936971\n", + "MIN: 1.0773514327283324\n" + ] + } + ], + "source": [ + "# your code here\n", + "print(\"Mean:\",d_mean)\n", + "print(\"MIN:\",d_min)\n", + "\n", + "#z = np.empty(shape) * 0\n", + "\n", + "for b in range(0,2):\n", + " for l in range(0,3):\n", + " for r in range(0,5):\n", + " if d[b][l][r] > d_min and d[b][l][r] < d_mean:\n", + " f[b][l][r]=25\n", + " \n", + " elif d[b][l][r] > d_mean and d[b][l][r] < d_max:\n", + " f[b][l][r]=75\n", + " \n", + " elif d[b][l][r] == d_mean:\n", + " f[b][l][r]=50\n", + " \n", + " elif d[b][l][r] == d_min:\n", + " f[b][l][r]=0\n", + " \n", + " elif d[b][l][r] == d_max:\n", + " f[b][l][r]=100 \n" ] }, { @@ -309,11 +550,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 335, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[100. 25. 25. 25. 25.]\n", + " [ 25. 25. 25. 75. 75.]\n", + " [ 25. 75. 75. 25. 25.]]\n", + "\n", + " [[ 75. 25. 75. 75. 25.]\n", + " [ 25. 75. 0. 75. 25.]\n", + " [ 25. 25. 75. 75. 75.]]]\n" + ] + } + ], "source": [ - "# your code here" + "print(f)" ] }, { @@ -335,17 +590,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 336, "metadata": {}, "outputs": [], "source": [ "# your code here" ] + }, + { + "cell_type": "code", + "execution_count": 337, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['1' '2' '3' '4' '5']\n", + " ['4' '5' '6' '5' '1']\n", + " ['7' '8' 'A' '8' '3']\n", + " ['7' '10' '9' '5' '3']\n", + " ['7' '10' '9' '5' '3']\n", + " ['7' '10' '9' '5' '3']]\n" + ] + } + ], + "source": [ + "lst_lst = [1,2,3,4,5],[4,5,6,5,1],[7,8,'A',8,3],[7,10,9,5,3],[7,10,9,5,3],[7,10,9,5,3]\n", + "ar = np.array(lst_lst)\n", + "print(ar)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -359,7 +652,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.13" } }, "nbformat": 4,