diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa14..52ff36ab 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,12 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n" ] }, { @@ -34,11 +35,31 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "NumPy version:\n" + ] + }, + { + "data": { + "text/plain": [ + "'2.3.3'" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(\"NumPy version:\")\n", + "np.__version__" ] }, { @@ -51,11 +72,24 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "a:\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "a = np.random.random((2, 3, 5)) # (rows, columns)\n", + "\n", + "# Print the generated vector\n", + "print(\"\\na:\")\n" ] }, { @@ -68,11 +102,26 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.94891185 0.69934772 0.95392324 0.06408272 0.66659779]\n", + " [0.68748058 0.43671501 0.80566322 0.04406075 0.33400431]\n", + " [0.74132223 0.15778053 0.27293981 0.52482532 0.77690994]]\n", + "\n", + " [[0.50470868 0.91071293 0.19173581 0.09140001 0.26888879]\n", + " [0.92337024 0.1057429 0.3201242 0.63731464 0.01616369]\n", + " [0.60349597 0.33638534 0.16213903 0.31423468 0.53367144]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(a)" ] }, { @@ -85,11 +134,37 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], + "execution_count": null, + "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": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "b = np.ones((5, 2, 3)) # (rows, columns)\n", + "np.ones((5, 2, 3))" ] }, { @@ -102,11 +177,39 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "b:\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": [ - "### [your code here]\n" + "### [your code here]\n", + "b = np.ones((5, 2, 3)) # (rows, columns)\n", + "\n", + "# Print the generated vector\n", + "print(\"\\nb:\")\n", + "print(b)" ] }, { @@ -119,11 +222,22 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "30\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(a.size)\n", + "print(b.size)\n" ] }, { @@ -136,11 +250,51 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n", + "(5, 2, 3)\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print(a.shape)\n", + "print(b.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "#The 2 Tensor are not compatible because shape is different" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(5, 2, 3)" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.shape" ] }, { @@ -154,11 +308,27 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1. 1. 1. 1. 1.]\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.]\n", + " [1. 1. 1. 1. 1.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "c = np.transpose(b, (1, 2, 0))\n", + "print(c)" ] }, { @@ -171,11 +341,27 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], + "execution_count": 138, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.94891185 1.69934772 1.95392324 1.06408272 1.66659779]\n", + " [1.68748058 1.43671501 1.80566322 1.04406075 1.33400431]\n", + " [1.74132223 1.15778053 1.27293981 1.52482532 1.77690994]]\n", + "\n", + " [[1.50470868 1.91071293 1.19173581 1.09140001 1.26888879]\n", + " [1.92337024 1.1057429 1.3201242 1.63731464 1.01616369]\n", + " [1.60349597 1.33638534 1.16213903 1.31423468 1.53367144]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "d = a+c\n", + "print(d)" ] }, { @@ -188,12 +374,47 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a:\n", + "[[[0.94891185 0.69934772 0.95392324 0.06408272 0.66659779]\n", + " [0.68748058 0.43671501 0.80566322 0.04406075 0.33400431]\n", + " [0.74132223 0.15778053 0.27293981 0.52482532 0.77690994]]\n", + "\n", + " [[0.50470868 0.91071293 0.19173581 0.09140001 0.26888879]\n", + " [0.92337024 0.1057429 0.3201242 0.63731464 0.01616369]\n", + " [0.60349597 0.33638534 0.16213903 0.31423468 0.53367144]]]\n", + "d:\n", + "[[[1.94891185 1.69934772 1.95392324 1.06408272 1.66659779]\n", + " [1.68748058 1.43671501 1.80566322 1.04406075 1.33400431]\n", + " [1.74132223 1.15778053 1.27293981 1.52482532 1.77690994]]\n", + "\n", + " [[1.50470868 1.91071293 1.19173581 1.09140001 1.26888879]\n", + " [1.92337024 1.1057429 1.3201242 1.63731464 1.01616369]\n", + " [1.60349597 1.33638534 1.16213903 1.31423468 1.53367144]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print(\"a:\")\n", + "print(a)\n", + "print(\"d:\")\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n", - "\n" + "The values of tensor d are the result of the sum a+b" ] }, { @@ -206,11 +427,29 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.94891185, 0.69934772, 0.95392324, 0.06408272, 0.66659779],\n", + " [0.68748058, 0.43671501, 0.80566322, 0.04406075, 0.33400431],\n", + " [0.74132223, 0.15778053, 0.27293981, 0.52482532, 0.77690994]],\n", + "\n", + " [[0.50470868, 0.91071293, 0.19173581, 0.09140001, 0.26888879],\n", + " [0.92337024, 0.1057429 , 0.3201242 , 0.63731464, 0.01616369],\n", + " [0.60349597, 0.33638534, 0.16213903, 0.31423468, 0.53367144]]])" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "e = a * c" ] }, { @@ -224,12 +463,13 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### [your code here]\n", - "\n" + "\n", + "e is equal to a, because the Tensor b has only 1 numbers" ] }, { @@ -243,12 +483,81 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], + "execution_count": 139, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.94891185 1.69934772 1.95392324 1.06408272 1.66659779]\n", + " [1.68748058 1.43671501 1.80566322 1.04406075 1.33400431]\n", + " [1.74132223 1.15778053 1.27293981 1.52482532 1.77690994]]\n", + "\n", + " [[1.50470868 1.91071293 1.19173581 1.09140001 1.26888879]\n", + " [1.92337024 1.1057429 1.3201242 1.63731464 1.01616369]\n", + " [1.60349597 1.33638534 1.16213903 1.31423468 1.53367144]]]\n" + ] + } + ], "source": [ "### [your code here]\n", - "\n" + "\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 140, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.953923235618411\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "print(d_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.016163692153468\n" + ] + } + ], + "source": [ + "d_min = d.min()\n", + "print(d_min)" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.4678217792962374\n" + ] + } + ], + "source": [ + "d_mean = d.mean()\n", + "print(d_mean)" ] }, { @@ -261,19 +570,33 @@ }, { "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [], + "execution_count": 143, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.94891185 1.69934772 1.95392324 1.06408272 1.66659779]\n", + " [1.68748058 1.43671501 1.80566322 1.04406075 1.33400431]\n", + " [1.74132223 1.15778053 1.27293981 1.52482532 1.77690994]]\n", + "\n", + " [[1.50470868 1.91071293 1.19173581 1.09140001 1.26888879]\n", + " [1.92337024 1.1057429 1.3201242 1.63731464 1.01616369]\n", + " [1.60349597 1.33638534 1.16213903 1.31423468 1.53367144]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "f = np.empty(d.shape)\n", + "print(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "\n", - "\n", "\n", "### 16. Populate the values in f. For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.\n", "If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.\n", @@ -287,20 +610,121 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 145, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75 75 100 25 75]\n", + " [ 75 25 75 25 25]\n", + " [ 75 25 25 75 75]]\n", + "\n", + " [[ 75 75 25 25 25]\n", + " [ 75 25 25 75 0]\n", + " [ 75 25 25 25 75]]]\n" + ] + } + ], + "source": [ + "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)" + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[False, False, False, False, False],\n", + " [False, False, False, False, False],\n", + " [False, False, False, False, False]],\n", + "\n", + " [[False, False, False, False, False],\n", + " [False, False, False, False, False],\n", + " [False, False, False, False, False]]])" + ] + }, + "execution_count": 146, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d == d_mean" + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[False, False, False, False, False],\n", + " [False, False, False, False, False],\n", + " [False, False, False, False, False]],\n", + "\n", + " [[False, False, False, False, False],\n", + " [False, False, False, False, True],\n", + " [False, False, False, False, False]]])" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d == d_min " + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[False, False, True, False, False],\n", + " [False, False, False, False, False],\n", + " [False, False, False, False, False]],\n", + "\n", + " [[False, False, False, False, False],\n", + " [False, False, False, False, False],\n", + " [False, False, False, False, False]]])" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d == d_max" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "\n", - "\n", - "\n", "\n", "### 17. Print d and f. Do you have your expected f?\n", "For instance, if your d is:\n", @@ -325,11 +749,129 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 149, + "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" + ] + } + ], + "source": [ + "### [your code here]\n", + "\n", + "e = 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", + "d = e\n", + "print(\"d:\")\n", + "print(d)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 153, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.6175069086666667\n" + ] + } + ], + "source": [ + "\n", + "d_mean = d.mean()\n", + "print(d_mean)" + ] + }, + { + "cell_type": "code", + "execution_count": 154, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.00656599\n" + ] + } + ], + "source": [ + "\n", + "d_min = d.min()\n", + "print(d_min)" + ] + }, + { + "cell_type": "code", + "execution_count": 156, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.97066381\n" + ] + } + ], "source": [ - "### [your code here]\n" + "d_max = d.max()\n", + "print(d_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 158, + "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": [ + "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)" ] }, { @@ -374,7 +916,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.8" } }, "nbformat": 4,