diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa14..50c6e11e 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "import numpy as np\n" ] }, { @@ -34,11 +34,19 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.5\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(np.version.version)" ] }, { @@ -51,11 +59,66 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.00981632 0.10487195 0.75172023 0.24717833 0.1757527 ]\n", + " [0.28466374 0.11573415 0.02898033 0.19788668 0.55923292]\n", + " [0.3909844 0.2197333 0.93232603 0.58334253 0.51506052]]\n", + "\n", + " [[0.77208879 0.1081357 0.42335421 0.41416298 0.02054184]\n", + " [0.97612701 0.68891587 0.46166182 0.87802931 0.1344402 ]\n", + " [0.12471473 0.971848 0.16558616 0.59028948 0.17461716]]]\n", + "Way 1 result.\n", + "[[[0.10815675 0.20357791 0.04645035 0.74097967 0.50536846]\n", + " [0.87056147 0.05694009 0.56844548 0.48487816 0.8391152 ]\n", + " [0.11785172 0.79252638 0.84433558 0.37761322 0.35078954]]\n", + "\n", + " [[0.58563343 0.45460649 0.60805108 0.86105639 0.35747511]\n", + " [0.08480654 0.66506703 0.47416759 0.48257034 0.41581101]\n", + " [0.3278739 0.01095876 0.42482839 0.51821374 0.17643662]]]\n", + "Way 2 result.\n", + "[[[9.57002042 6.27715117 5.84649964 5.75971897 5.29035103]\n", + " [8.15113081 6.43689239 9.15706219 9.94508772 5.74917366]\n", + " [6.0035414 5.28826462 9.54471226 8.92331808 5.92352057]]\n", + "\n", + " [[7.69727074 7.1177014 6.6091268 9.87589836 9.11025159]\n", + " [9.39751299 8.31129871 6.46875273 7.34732826 8.91197435]\n", + " [7.93392557 8.24437613 6.2450613 9.06761935 7.42428969]]]\n", + "Way 3 result.\n" + ] + } + ], + "source": [ + "# Way 1 --> Using a tuple (2, 3, 5) as an argument\n", + "a = np.random.random((2, 3, 5))\n", + "\n", + "print(a)\n", + "\n", + "print(\"Way 1 result.\")\n", + "\n", + "# Way 2 --> Using dimensions directly instead of a tuple to define the array. \n", + "a = np.random.rand(2, 3, 5)\n", + "\n", + "print(a)\n", + "\n", + "print(\"Way 2 result.\")\n", + "\n", + "# Way 3 --> This is similar to way 1, but the first two places of the argument are to set the min and maax value of the range from which we should generate the random values. \n", + "a = np.random.uniform(5, 10, (2, 3, 5))\n", + "\n", + "print(a)\n", + "\n", + "print(\"Way 3 result.\")\n", + "\n", + "\n", + "#For the next exercise, let's use the first iteration of a:\n", + "\n", + "a = np.random.random((2, 3, 5))" ] }, { @@ -68,11 +131,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.04351799 0.71964368 0.98239922 0.80435276 0.6917323 ]\n", + " [0.8062494 0.10107773 0.10673246 0.04102788 0.47390678]\n", + " [0.00869559 0.17332658 0.81141575 0.76646175 0.82315202]]\n", + "\n", + " [[0.25904348 0.80230673 0.37430907 0.86166923 0.33292779]\n", + " [0.80761588 0.04442697 0.72960752 0.22502103 0.01414849]\n", + " [0.36082474 0.82508008 0.19013151 0.81064563 0.48328153]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a)" ] }, { @@ -85,11 +162,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "b = np.ones((5, 2, 3))" ] }, { @@ -102,11 +179,32 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "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)" ] }, { @@ -119,11 +217,22 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "a.size == b.size\n" ] }, { @@ -136,11 +245,28 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 24, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We cannot add them, as they don't have the same shape. \n" + ] + } + ], + "source": [ + "def add_arrays(a, b):\n", + " try:\n", + " result = np.add(a, b)\n", + "\n", + " return result\n", + " \n", + " except ValueError:\n", + " print(\"We cannot add them, as they don't have the same shape. \")\n", + "\n", + "add_arrays(a, b)\n" ] }, { @@ -154,11 +280,29 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = b.ravel()\n", + "\n", + "b = b.reshape(5, 3, 2)\n", + "\n", + "c = np.transpose(b)\n", + "\n", + "c.shape\n", + "\n" ] }, { @@ -171,11 +315,13 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "d = np.add(a, c)\n", + "\n", + "#Now it works, because after reshaping and transposing b, the shape is 2x3x5, which matches with the one from a. \n" ] }, { @@ -188,12 +334,43 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.04351799 0.71964368 0.98239922 0.80435276 0.6917323 ]\n", + " [0.8062494 0.10107773 0.10673246 0.04102788 0.47390678]\n", + " [0.00869559 0.17332658 0.81141575 0.76646175 0.82315202]]\n", + "\n", + " [[0.25904348 0.80230673 0.37430907 0.86166923 0.33292779]\n", + " [0.80761588 0.04442697 0.72960752 0.22502103 0.01414849]\n", + " [0.36082474 0.82508008 0.19013151 0.81064563 0.48328153]]]\n", + "End of a\n", + "[[[1.04351799 1.71964368 1.98239922 1.80435276 1.6917323 ]\n", + " [1.8062494 1.10107773 1.10673246 1.04102788 1.47390678]\n", + " [1.00869559 1.17332658 1.81141575 1.76646175 1.82315202]]\n", + "\n", + " [[1.25904348 1.80230673 1.37430907 1.86166923 1.33292779]\n", + " [1.80761588 1.04442697 1.72960752 1.22502103 1.01414849]\n", + " [1.36082474 1.82508008 1.19013151 1.81064563 1.48328153]]]\n", + "End of d\n" + ] + } + ], "source": [ - "### [your code here]\n", - "\n" + "\n", + "print(a)\n", + "\n", + "print(\"End of a\")\n", + "\n", + "print(d)\n", + "\n", + "print(\"End of d\")\n", + "\n", + "#The relation between the values of a and d is that a values are exactly the same as d values, but substracting 1. This makes sense, as c was a \"ones\" array, so the sum will add 1 to all the values from array a. " ] }, { @@ -206,11 +383,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 65, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "e = np.multiply(a, c)" ] }, { @@ -224,12 +401,24 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array_equal(e, a)\n", + "\n", + "# e equals a, as when mulyipying a by c, as all the values in c are 1, the result of the multiplication will return the exact same values that were originally in array a. That is why, all the values from e (resulting from a * c multiplication) will be the same as the values from a.\n" ] }, { @@ -243,12 +432,31 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.9823992239262962\n", + "1.0086955911402782\n", + "1.4824910524208432\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "\n", + "print(d_max)\n", + "\n", + "d_min = d.min()\n", + "\n", + "print(d_min)\n", + "\n", + "d_mean = d.mean()\n", + "\n", + "print(d_mean)\n" ] }, { @@ -261,11 +469,30 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.04351799, 1.71964368, 1.98239922, 1.80435276, 1.6917323 ],\n", + " [1.8062494 , 1.10107773, 1.10673246, 1.04102788, 1.47390678],\n", + " [1.00869559, 1.17332658, 1.81141575, 1.76646175, 1.82315202]],\n", + "\n", + " [[1.25904348, 1.80230673, 1.37430907, 1.86166923, 1.33292779],\n", + " [1.80761588, 1.04442697, 1.72960752, 1.22502103, 1.01414849],\n", + " [1.36082474, 1.82508008, 1.19013151, 1.81064563, 1.48328153]]])" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = np.empty((2, 3, 5))\n", + "\n", + "f" ] }, { @@ -287,11 +514,57 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ 25, 75, 100, 75, 75],\n", + " [ 75, 25, 25, 25, 25],\n", + " [ 0, 25, 75, 75, 75]],\n", + "\n", + " [[ 25, 75, 25, 75, 25],\n", + " [ 75, 25, 75, 25, 25],\n", + " [ 25, 75, 25, 75, 75]]])" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = d.ravel()\n", + "\n", + "f = f.ravel()\n", + "\n", + "i = 0\n", + "\n", + "for element in d:\n", + " if d[i] == d_max:\n", + " f[i] = 100\n", + " i += 1\n", + " elif d_mean < d[i] < d_max:\n", + " f[i] = 75\n", + " i += 1\n", + " elif d[i] == d_mean:\n", + " f[i] = 50\n", + " i += 1\n", + " elif d_mean > d[i] > d_min:\n", + " f[i] = 25\n", + " i += 1\n", + " elif d[i] == d_min:\n", + " f[i] = 0\n", + " i += 1\n", + "\n", + "d = d.reshape(2, 3, 5)\n", + "\n", + "f = f.reshape(2, 3, 5)\n", + "\n", + "f = f.astype(int)\n", + "\n", + "f" ] }, { @@ -325,11 +598,40 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.04351799 1.71964368 1.98239922 1.80435276 1.6917323 ]\n", + " [1.8062494 1.10107773 1.10673246 1.04102788 1.47390678]\n", + " [1.00869559 1.17332658 1.81141575 1.76646175 1.82315202]]\n", + "\n", + " [[1.25904348 1.80230673 1.37430907 1.86166923 1.33292779]\n", + " [1.80761588 1.04442697 1.72960752 1.22502103 1.01414849]\n", + " [1.36082474 1.82508008 1.19013151 1.81064563 1.48328153]]]\n", + "End of array d.\n", + "[[[ 25 75 100 75 75]\n", + " [ 75 25 25 25 25]\n", + " [ 0 25 75 75 75]]\n", + "\n", + " [[ 25 75 25 75 25]\n", + " [ 75 25 75 25 25]\n", + " [ 25 75 25 75 75]]]\n", + "End of array f.\n" + ] + } + ], + "source": [ + "print(d)\n", + "\n", + "print(\"End of array d.\")\n", + "\n", + "print(f)\n", + "\n", + "print(\"End of array f.\")\n" ] }, { @@ -350,11 +652,57 @@ }, { "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]" + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[['B', 'D', 'E', 'D', 'D'],\n", + " ['D', 'B', 'B', 'B', 'B'],\n", + " ['A', 'B', 'D', 'D', 'D']],\n", + "\n", + " [['B', 'D', 'B', 'D', 'B'],\n", + " ['D', 'B', 'D', 'B', 'B'],\n", + " ['B', 'D', 'B', 'D', 'D']]], dtype=' d[i] > d_min:\n", + " f[i] = \"B\"\n", + " i += 1\n", + " elif d[i] == d_min:\n", + " f[i] = \"A\"\n", + " i += 1\n", + "\n", + "d = d.reshape(2, 3, 5)\n", + "\n", + "f = f.reshape(2, 3, 5)\n", + "\n", + "f" ] } ], @@ -374,7 +722,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.3" } }, "nbformat": 4,