From 88d0347968234df5fffac1788773c020b5e34b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Sim=C3=B5es?= Date: Mon, 13 Oct 2025 15:58:07 +0100 Subject: [PATCH] getting serious --- your-code/main.ipynb | 586 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 508 insertions(+), 78 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa14..a2527e7b 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" ] }, { @@ -34,11 +34,92 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "NumPy version: 2.1.3\n", + "Build Dependencies:\n", + " blas:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: C:/Users/diogo/anaconda3/Library/include\n", + " lib directory: C:/Users/diogo/anaconda3/Library/lib\n", + " name: mkl-sdl\n", + " openblas configuration: unknown\n", + " pc file directory: C:\\b\\abs_25aj347ekn\\croot\\numpy_and_numpy_base_1730835595898\\_h_env\\Library\\lib\\pkgconfig\n", + " version: '2023.1'\n", + " lapack:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: C:/Users/diogo/anaconda3/Library/include\n", + " lib directory: C:/Users/diogo/anaconda3/Library/lib\n", + " name: mkl-sdl\n", + " openblas configuration: unknown\n", + " pc file directory: C:\\b\\abs_25aj347ekn\\croot\\numpy_and_numpy_base_1730835595898\\_h_env\\Library\\lib\\pkgconfig\n", + " version: '2023.1'\n", + "Compilers:\n", + " c:\n", + " commands: cl.exe\n", + " linker: link\n", + " name: msvc\n", + " version: 19.29.30156\n", + " c++:\n", + " commands: cl.exe\n", + " linker: link\n", + " name: msvc\n", + " version: 19.29.30156\n", + " cython:\n", + " commands: cython\n", + " linker: cython\n", + " name: cython\n", + " version: 3.0.11\n", + "Machine Information:\n", + " build:\n", + " cpu: x86_64\n", + " endian: little\n", + " family: x86_64\n", + " system: windows\n", + " host:\n", + " cpu: x86_64\n", + " endian: little\n", + " family: x86_64\n", + " system: windows\n", + "Python Information:\n", + " path: C:\\b\\abs_25aj347ekn\\croot\\numpy_and_numpy_base_1730835595898\\_h_env\\python.exe\n", + " version: '3.13'\n", + "SIMD Extensions:\n", + " baseline:\n", + " - SSE\n", + " - SSE2\n", + " - SSE3\n", + " found:\n", + " - SSSE3\n", + " - SSE41\n", + " - POPCNT\n", + " - SSE42\n", + " - AVX\n", + " - F16C\n", + " - FMA3\n", + " - AVX2\n", + " not found:\n", + " - AVX512F\n", + " - AVX512CD\n", + " - AVX512_SKX\n", + " - AVX512_CLX\n", + " - AVX512_CNL\n", + " - AVX512_ICL\n", + "\n" + ] + } + ], + "source": [ + "print('NumPy version:', np.__version__)\n", + "np.show_config()\n", + "\n" ] }, { @@ -51,11 +132,87 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 27, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Method 1:\n", + " [[[0.01792416 0.10744072 0.61235602 0.1948446 0.68473376]\n", + " [0.67716679 0.47541603 0.34890427 0.71985755 0.86521324]\n", + " [0.25917746 0.48955677 0.36675396 0.77633289 0.27269339]]\n", + "\n", + " [[0.24587039 0.94012743 0.23087748 0.33480331 0.4024877 ]\n", + " [0.09351002 0.75441115 0.08714595 0.79517943 0.76014947]\n", + " [0.44727398 0.38153835 0.88686993 0.3729427 0.73539181]]] \n", + "\n", + "Method 2:\n", + " [[[0.36158027 0.25743374 0.95544866 0.63387979 0.40583448]\n", + " [0.27548314 0.6289854 0.96709595 0.4748191 0.22475062]\n", + " [0.54527662 0.13258013 0.69373671 0.17143409 0.59827616]]\n", + "\n", + " [[0.54795 0.15632178 0.66911973 0.70930353 0.50947677]\n", + " [0.28112672 0.70741466 0.11677014 0.04145532 0.0903432 ]\n", + " [0.68803589 0.92522299 0.36511706 0.50413059 0.13952582]]] \n", + "\n", + "Method 3:\n", + " [[[0.03188527 0.39842447 0.84541245 0.88214301 0.05380617]\n", + " [0.24628928 0.62438756 0.76306393 0.02018892 0.90341938]\n", + " [0.34432907 0.91625885 0.20401988 0.47083947 0.49552325]]\n", + "\n", + " [[0.30823345 0.09871532 0.35710606 0.84362463 0.00228068]\n", + " [0.53688747 0.68289644 0.07606144 0.10343173 0.45085907]\n", + " [0.88185658 0.71957681 0.20129248 0.81523656 0.54916614]]] \n", + "\n", + "Method 4 (normal distribution):\n", + " [[[-1.20702896 -1.27622312 -0.46442126 0.31801946 0.63008908]\n", + " [ 2.80598044 1.42331023 -0.4306625 -0.15277169 -0.25988648]\n", + " [-0.43824928 0.73422512 1.06842207 -1.03841339 0.63183934]]\n", + "\n", + " [[ 2.15624551 0.14641795 0.00371314 0.37596302 -0.93252511]\n", + " [-0.9869887 -1.37369928 -1.185623 1.27775371 0.37448658]\n", + " [ 2.08205561 0.1917957 0.06879726 -0.39213107 -1.8496658 ]]] \n", + "\n", + "Method 5 (uniform distribution):\n", + " [[[0.95391054 0.61042087 0.82488606 0.76620786 0.2924061 ]\n", + " [0.2237894 0.46686076 0.43180073 0.17843292 0.601803 ]\n", + " [0.70865691 0.16312427 0.95856183 0.20796966 0.99614889]]\n", + "\n", + " [[0.31370194 0.75167526 0.13774242 0.20948561 0.5941441 ]\n", + " [0.17794591 0.22237377 0.88373207 0.54019427 0.42509807]\n", + " [0.23778663 0.1538222 0.55126572 0.48488214 0.21313911]]] \n", + "\n", + "Chosen 'a' shape: (2, 3, 5)\n" + ] + } + ], + "source": [ + "# Method 1: Using np.random.random()\n", + "a = np.random.random((2, 3, 5))\n", + "print(\"Method 1:\\n\", a, \"\\n\")\n", + "\n", + "# Method 2: Using np.random.rand()\n", + "a2 = np.random.rand(2, 3, 5)\n", + "print(\"Method 2:\\n\", a2, \"\\n\")\n", + "\n", + "# Method 3: Using the new Generator API \n", + "rng = np.random.default_rng()\n", + "a3 = rng.random((2, 3, 5))\n", + "print(\"Method 3:\\n\", a3, \"\\n\")\n", + "\n", + "# Method 4: Using np.random.randn() (values from normal distribution)\n", + "a4 = np.random.randn(2, 3, 5)\n", + "print(\"Method 4 (normal distribution):\\n\", a4, \"\\n\")\n", + "\n", + "# Method 5: Using np.random.uniform() (explicitly setting range)\n", + "a5 = np.random.uniform(0, 1, (2, 3, 5))\n", + "print(\"Method 5 (uniform distribution):\\n\", a5, \"\\n\")\n", + "\n", + "# Assign one of them as the main variable \"a\" for later use\n", + "a = a3 # clean and reproducible version\n", + "print(\"Chosen 'a' shape:\", a.shape)\n" ] }, { @@ -68,11 +225,23 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 4)\n", + "(2, 5)\n", + "(4, 4)\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a_zeros.shape)\n", + "print(a_ones.shape)\n", + "print(a_eye.shape)" ] }, { @@ -85,11 +254,13 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "\n", + "b = np.ones((5, 2, 3))\n", + "\n" ] }, { @@ -102,11 +273,34 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 9, + "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", + "Shape: (5, 2, 3)\n" + ] + } + ], + "source": [ + "print(b)\n", + "print(\"Shape:\", b.shape)" ] }, { @@ -119,11 +313,27 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Size of a: 30\n", + "Size of b: 30\n", + "Do they have the same size? True\n" + ] + } + ], + "source": [ + "print(\"Size of a:\", a.size)\n", + "print(\"Size of b:\", b.size)\n", + "\n", + "#Check if they have the same number of elements\n", + "same_size = a.size == b.size\n", + "print(\"Do they have the same size?\", same_size)\n", + "\n" ] }, { @@ -136,11 +346,31 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape of a: (2, 3, 5)\n", + "Shape of b: (5, 2, 3)\n", + "Addition failed: operands could not be broadcast together with shapes (2,3,5) (5,2,3) \n" + ] + } + ], + "source": [ + "print(\"Shape of a:\", a.shape)\n", + "print(\"Shape of b:\", b.shape)\n", + "\n", + "try:\n", + " c = a + b\n", + " print(\"Addition succeeded!\")\n", + " print(\"Result shape:\", c.shape)\n", + "except ValueError as e:\n", + " print(\"Addition failed:\", e)\n", + "\n", + "#You cannot add a and b because their dimensions are not aligned and cannot be broadcasted" ] }, { @@ -154,11 +384,29 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original shape of b: (5, 2, 3)\n", + "Target shape like a: (2, 3, 5)\n", + "New shape of c: (2, 3, 5)\n", + "Does c match a's shape? True\n" + ] + } + ], + "source": [ + "print(\"Original shape of b:\", b.shape)\n", + "print(\"Target shape like a:\", a.shape)\n", + "\n", + "#Transpose b: rearrange axes to match a's (2, 3, 5)\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "print(\"New shape of c:\", c.shape)\n", + "print(\"Does c match a's shape?\", c.shape == a.shape)\n" ] }, { @@ -171,11 +419,33 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape of a: (2, 3, 5)\n", + "Shape of c: (2, 3, 5)\n", + "✅ Addition successful!\n", + "Shape of result (d): (2, 3, 5)\n", + "Do a and c have identical shapes? True\n" + ] + } + ], + "source": [ + "print(\"Shape of a:\", a.shape)\n", + "print(\"Shape of c:\", c.shape)\n", + "\n", + "d = a + c\n", + "\n", + "print(\"Addition successful!\")\n", + "print(\"Shape of result (d):\", d.shape)\n", + "print(\"Do a and c have identical shapes?\", a.shape == c.shape)\n", + "\n", + "#Because they now share identical dimensions, NumPy can perform element-wise addition directly\n", + "#Each element in a is added to the element in c at the same position\n" ] }, { @@ -188,12 +458,46 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array a:\n", + " [[[0.9992854 0.19184592 0.66361488 0.27741862 0.1834368 ]\n", + " [0.37999693 0.51662213 0.239003 0.80325279 0.15746359]\n", + " [0.18941655 0.20980381 0.72945172 0.55647951 0.68895234]]\n", + "\n", + " [[0.00942516 0.66689542 0.61330581 0.46424554 0.06025703]\n", + " [0.5212287 0.08853098 0.96204601 0.7978705 0.84823591]\n", + " [0.53794762 0.30049065 0.40669706 0.23478838 0.68410828]]]\n", + "\n", + "Array d (a + c):\n", + " [[[1.9992854 1.19184592 1.66361488 1.27741862 1.1834368 ]\n", + " [1.37999693 1.51662213 1.239003 1.80325279 1.15746359]\n", + " [1.18941655 1.20980381 1.72945172 1.55647951 1.68895234]]\n", + "\n", + " [[1.00942516 1.66689542 1.61330581 1.46424554 1.06025703]\n", + " [1.5212287 1.08853098 1.96204601 1.7978705 1.84823591]\n", + " [1.53794762 1.30049065 1.40669706 1.23478838 1.68410828]]]\n", + "\n", + "Check if d is exactly twice a (since c = b transposed was all ones):\n", + "False\n" + ] + } + ], + "source": [ + "print(\"Array a:\\n\", a)\n", + "print(\"\\nArray d (a + c):\\n\", d)\n", + "\n", + "# Check how d relates to a\n", + "print(\"\\nCheck if d is exactly twice a (since c = b transposed was all ones):\")\n", + "print(np.allclose(d, a * 2))\n", + "\n", + "#c was created by transposing b, and b was filled entirely with ones.\n", + "#Therefore, when we computed d = a + c, every element of a was increased by 1 (because 1 was added from c)" ] }, { @@ -206,11 +510,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "e = a * c\n" ] }, { @@ -224,12 +528,26 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Check if e and a are equal: True\n", + "Approximately equal? True\n" + ] + } + ], + "source": [ + "print(\"Check if e and a are equal:\", np.array_equal(e, a))\n", + "\n", + "print(\"Approximately equal?\", np.allclose(e, a))\n", + "\n", + "\n", + "#Yes, e equals a because multiplying any array by an array of ones leaves all values unchanged.\n", + "#Therefore, e and a contain exactly the same values (except for tiny floating-point rounding errors, which np.allclose() ignores safely)." ] }, { @@ -243,11 +561,13 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n", + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)\n", "\n" ] }, @@ -261,11 +581,31 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.9992854 , 1.19184592, 1.66361488, 1.27741862, 1.1834368 ],\n", + " [1.37999693, 1.51662213, 1.239003 , 1.80325279, 1.15746359],\n", + " [1.18941655, 1.20980381, 1.72945172, 1.55647951, 1.68895234]],\n", + "\n", + " [[1.00942516, 1.66689542, 1.61330581, 1.46424554, 1.06025703],\n", + " [1.5212287 , 1.08853098, 1.96204601, 1.7978705 , 1.84823591],\n", + " [1.53794762, 1.30049065, 1.40669706, 1.23478838, 1.68410828]]])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = np.empty_like(d)\n", + "d\n", + "\n", + "\n" ] }, { @@ -287,11 +627,35 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0. 25. 75. 100.]\n" + ] + } + ], + "source": [ + "for i in range(d.shape[0]):\n", + " for j in range(d.shape[1]):\n", + " for k in range(d.shape[2]):\n", + " value = d[i, j, k]\n", + " if value == d_min:\n", + " f[i, j, k] = 0\n", + " elif value == d_max:\n", + " f[i, j, k] = 100\n", + " elif value == d_mean:\n", + " f[i, j, k] = 50\n", + " elif d_min < value < d_mean:\n", + " f[i, j, k] = 25\n", + " elif d_mean < value < d_max:\n", + " f[i, j, k] = 75\n", + "\n", + "\n", + "\n" ] }, { @@ -325,11 +689,39 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.9992854 1.19184592 1.66361488 1.27741862 1.1834368 ]\n", + " [1.37999693 1.51662213 1.239003 1.80325279 1.15746359]\n", + " [1.18941655 1.20980381 1.72945172 1.55647951 1.68895234]]\n", + "\n", + " [[1.00942516 1.66689542 1.61330581 1.46424554 1.06025703]\n", + " [1.5212287 1.08853098 1.96204601 1.7978705 1.84823591]\n", + " [1.53794762 1.30049065 1.40669706 1.23478838 1.68410828]]]\n", + "[[[100. 25. 75. 25. 25.]\n", + " [ 25. 75. 25. 75. 25.]\n", + " [ 25. 25. 75. 75. 75.]]\n", + "\n", + " [[ 0. 75. 75. 25. 25.]\n", + " [ 75. 25. 75. 75. 75.]\n", + " [ 75. 25. 25. 25. 75.]]]\n", + "1.4660705677494847\n", + "1.9992854029712817\n", + "1.0094251612707836\n" + ] + } + ], + "source": [ + "print(d)\n", + "print(f)\n", + "print(d_mean)\n", + "print(d_max)\n", + "print(d_min)" ] }, { @@ -350,17 +742,55 @@ }, { "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]" + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['E' 'B' 'D' 'B' 'B']\n", + " ['B' 'D' 'B' 'D' 'B']\n", + " ['B' 'B' 'D' 'D' 'D']]\n", + "\n", + " [['A' 'D' 'D' 'B' 'B']\n", + " ['D' 'B' 'D' 'D' 'D']\n", + " ['D' 'B' 'B' 'B' 'D']]]\n" + ] + } + ], + "source": [ + "# 18. Bonus question — label array elements with strings instead of numbers\n", + "# A (lowest) → d_min\n", + "# B → between d_min and d_mean\n", + "# C → exactly d_mean\n", + "# D → between d_mean and d_max\n", + "# E (highest) → d_max\n", + "\n", + "f = np.empty(d.shape, dtype=object) # array of Python objects to hold strings\n", + "\n", + "for i in range(d.shape[0]):\n", + " for j in range(d.shape[1]):\n", + " for k in range(d.shape[2]):\n", + " value = d[i, j, k]\n", + " if value == d_min:\n", + " f[i, j, k] = \"A\"\n", + " elif value == d_max:\n", + " f[i, j, k] = \"E\"\n", + " elif value == d_mean:\n", + " f[i, j, k] = \"C\"\n", + " elif d_min < value < d_mean:\n", + " f[i, j, k] = \"B\"\n", + " elif d_mean < value < d_max:\n", + " f[i, j, k] = \"D\"\n", + "\n", + "print(f)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -374,7 +804,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.5" } }, "nbformat": 4,