diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa14..4ab17dd1 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "import numpy as np\n" ] }, { @@ -34,11 +34,37 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "NumPy version:\n" + ] + }, + { + "data": { + "text/plain": [ + "'2.1.3'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "\n", + "\n", + "\n", + "print(\"NumPy version:\")\n", + "\n", + "\n", + "np.__version__\n", + "\n", + "\n" ] }, { @@ -51,11 +77,32 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "a:\n", + "[[[1 0 0 0 0]\n", + " [1 2 1 1 1]\n", + " [2 0 0 0 0]]\n", + "\n", + " [[2 0 1 2 1]\n", + " [0 0 2 0 0]\n", + " [0 2 2 2 1]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "\n", + "a = np.random.randint(3, size=(2, 3, 5)) # (block, rows, columns)\n", + "np.random.random((2,3,5))\n", + "# Print the generated vector\n", + "print(\"\\na:\")\n", + "print(a)" ] }, { @@ -68,11 +115,28 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/na:\n", + "[[[2 2 1 2 0]\n", + " [2 0 1 1 2]\n", + " [2 2 0 0 2]]\n", + "\n", + " [[2 2 2 0 2]\n", + " [1 1 0 0 1]\n", + " [1 1 1 2 0]]]\n" + ] + } + ], + "source": [ + "a = np.random.randint(3, size=(2, 3, 5))\n", + "print(\"/na:\")\n", + "print(a)" ] }, { @@ -85,11 +149,36 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 14, + "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": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b =np.ones((5,2, 3))\n", + "np.ones((5, 2, 3))\n" ] }, { @@ -102,11 +191,35 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/nb:\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", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]]\n" + ] + } + ], + "source": [ + "b =np.ones((5,2, 3))\n", + "print(\"/nb:\")\n", + "print(b)\n" ] }, { @@ -119,11 +232,22 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 20, "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "30\n" + ] + } + ], + "source": [ + "print(a.size)\n", + "print(b.size)\n", + "\n" ] }, { @@ -136,11 +260,22 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "30\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a.size)\n", + "print(b.size)\n", + "#The Tensors are not compatible because shape is different. \n" ] }, { @@ -154,11 +289,25 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape of c: (2, 3, 5)\n" + ] + } + ], + "source": [ + "# Transpose b to match the shape of a\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "print(\"Shape of c:\", c.shape)\n", + "\n", + "\n", + "\n" ] }, { @@ -171,11 +320,32 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[2. 1. 1. 1. 1.]\n", + " [2. 3. 2. 2. 2.]\n", + " [3. 1. 1. 1. 1.]]\n", + "\n", + " [[3. 1. 2. 3. 2.]\n", + " [1. 1. 3. 1. 1.]\n", + " [1. 3. 3. 3. 2.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "\n", + "d = a + c\n", + "print(d)\n", + "\n", + "\n", + "#it works because a and c have the same shape now. \n", + "\n", + "\n" ] }, { @@ -188,11 +358,37 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a:\n", + "[[[1 0 0 0 0]\n", + " [1 2 1 1 1]\n", + " [2 0 0 0 0]]\n", + "\n", + " [[2 0 1 2 1]\n", + " [0 0 2 0 0]\n", + " [0 2 2 2 1]]]\n", + "d:\n", + "[[[1.85836099 1.67064465 1.62576044 1.40243961 1.88454931]\n", + " [1.75354326 1.69403643 1.36729252 1.61415071 1.12104981]\n", + " [1.72201435 1.1862918 1.87078449 1.7726778 1.88180042]]\n", + "\n", + " [[1.44747908 1.31673383 1.02000951 1.52218947 1.97066381]\n", + " [1.79129243 1.74983003 1.96028037 1.85166831 1.65450881]\n", + " [1.18068344 1.9587381 1.00656599 1.93402165 1.73514584]]]\n" + ] + } + ], + "source": [ + "print(\"a:\")\n", + "print(a)\n", + "print(\"d:\")\n", + "print(d)\n", "\n" ] }, @@ -206,11 +402,12 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 95, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "e = a * c\n" ] }, { @@ -224,11 +421,22 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(np.all(e == a)) \n", + "#every number multiplied by 1 is 1\n", + "\n", + "\n", "\n" ] }, @@ -243,14 +451,61 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.97066381\n" + ] + } + ], "source": [ "### [your code here]\n", + "d_max = d.max() \n", + "print(d_max)\n", + "\n", "\n" ] }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.00656599\n" + ] + } + ], + "source": [ + "d_min = d.min() \n", + "print(d_min)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.6175069086666667\n" + ] + } + ], + "source": [ + "d_mean = d.mean() \n", + "print(d_mean)\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -261,11 +516,47 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 108, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75. 75. 75. 25. 75.]\n", + " [ 75. 75. 25. 25. 25.]\n", + " [ 75. 25. 75. 75. 75.]]\n", + "\n", + " [[ 25. 25. 25. 25. 100.]\n", + " [ 75. 75. 75. 75. 75.]\n", + " [ 25. 75. 0. 75. 75.]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "f = np.empty(d.shape)\n", + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "d.shape" ] }, { @@ -287,11 +578,38 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75 75 75 25 75]\n", + " [ 75 75 25 25 25]\n", + " [ 75 25 75 75 75]]\n", + "\n", + " [[ 25 25 25 25 100]\n", + " [ 75 75 75 75 75]\n", + " [ 25 75 0 75 75]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "def populate_f(x):\n", + " if x == d_min:\n", + " return 0\n", + " elif x == d_max:\n", + " return 100\n", + " elif x == d_mean:\n", + " return 50\n", + " elif d_min < x < d_mean:\n", + " return 25\n", + " else:\n", + " return 75\n", + "f = np.vectorize(populate_f)(d)\n", + "print(f)\n" ] }, { @@ -325,12 +643,96 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array d:\n", + " [[[1.85836099 1.67064465 1.62576044 1.40243961 1.88454931]\n", + " [1.75354326 1.69403643 1.36729252 1.61415071 1.12104981]\n", + " [1.72201435 1.1862918 1.87078449 1.7726778 1.88180042]]\n", + "\n", + " [[1.44747908 1.31673383 1.02000951 1.52218947 1.97066381]\n", + " [1.79129243 1.74983003 1.96028037 1.85166831 1.65450881]\n", + " [1.18068344 1.9587381 1.00656599 1.93402165 1.73514584]]]\n", + "\n", + "Array f:\n", + " [[[ 75. 75. 75. 25. 75.]\n", + " [ 75. 75. 25. 25. 25.]\n", + " [ 75. 25. 75. 75. 75.]]\n", + "\n", + " [[ 25. 25. 25. 25. 100.]\n", + " [ 75. 75. 75. 75. 75.]\n", + " [ 25. 75. 0. 75. 75.]]]\n" + ] + } + ], + "source": [ + "print(\"Array d:\\n\", d)\n", + "print(\"\\nArray f:\\n\", f)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d:\n", + "[[[1.85836099 1.67064465 1.62576044 1.40243961 1.88454931]\n", + " [1.75354326 1.69403643 1.36729252 1.61415071 1.12104981]\n", + " [1.72201435 1.1862918 1.87078449 1.7726778 1.88180042]]\n", + "\n", + " [[1.44747908 1.31673383 1.02000951 1.52218947 1.97066381]\n", + " [1.79129243 1.74983003 1.96028037 1.85166831 1.65450881]\n", + " [1.18068344 1.9587381 1.00656599 1.93402165 1.73514584]]]\n", + "f:\n", + "[[[ 75. 75. 75. 25. 75.]\n", + " [ 75. 75. 25. 25. 25.]\n", + " [ 75. 25. 75. 75. 75.]]\n", + "\n", + " [[ 25. 25. 25. 25. 100.]\n", + " [ 75. 75. 75. 75. 75.]\n", + " [ 25. 75. 0. 75. 75.]]]\n" + ] + } + ], + "source": [ + "d = np.array ([[[1.85836099, 1.67064465, 1.62576044, 1.40243961, 1.88454931],\n", + " [1.75354326, 1.69403643, 1.36729252, 1.61415071, 1.12104981],\n", + " [1.72201435, 1.1862918 , 1.87078449, 1.7726778 , 1.88180042]],\n", + "\n", + " [[1.44747908, 1.31673383, 1.02000951, 1.52218947, 1.97066381], \n", + " [1.79129243, 1.74983003, 1.96028037, 1.85166831, 1.65450881],\n", + " [1.18068344, 1.9587381 , 1.00656599, 1.93402165, 1.73514584]]])\n", + "\n", + "print(\"d:\")\n", + "print(d)\n", + "\n", + "\n", + "f = np.array ([[[ 75., 75., 75., 25., 75.],\n", + " [ 75., 75., 25., 25., 25.],\n", + " [ 75., 25., 75., 75., 75.]],\n", + "\n", + " [[ 25., 25., 25., 25., 100.],\n", + " [ 75., 75., 75., 75., 75.],\n", + " [ 25., 75., 0., 75., 75.]]])\n", + "print(\"f:\")\n", + "print(f)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "### [your code here]\n" - ] + "source": [] }, { "cell_type": "markdown", @@ -360,7 +762,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -374,7 +776,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.5" } }, "nbformat": 4,