diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..fa2fb2c 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "import numpy as np" ] }, { @@ -28,11 +29,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'1.20.3'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "np.version.version" ] }, { @@ -45,29 +58,90 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 1" + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.11043939, 0.79934979, 0.31421338, 0.0946472 , 0.59387817],\n", + " [0.51287202, 0.02973354, 0.25205093, 0.41981816, 0.82130412],\n", + " [0.03894818, 0.37226438, 0.34494796, 0.89015818, 0.03068489]],\n", + "\n", + " [[0.49279487, 0.65500585, 0.14510117, 0.60207455, 0.82475285],\n", + " [0.31472158, 0.98959695, 0.93598016, 0.6049807 , 0.81625693],\n", + " [0.66724052, 0.73576872, 0.79813036, 0.28676034, 0.74784372]]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method 1\n", + "np.random.random(size = (2,3,5))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 2" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[8, 9, 2, 5, 3],\n", + " [4, 5, 5, 8, 3],\n", + " [3, 3, 5, 3, 8]],\n", + "\n", + " [[4, 3, 8, 3, 5],\n", + " [5, 3, 7, 7, 9],\n", + " [2, 3, 5, 8, 4]]])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method 2\n", + "a = np.random.randint(10, size = (2,3,5))\n", + "a" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 3" + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[-1.01716107, 0.70262909, 1.13334813, 1.88269214,\n", + " -0.22288816],\n", + " [ 0.59491518, -0.06627478, 0.85918733, -0.86862553,\n", + " -0.38297162],\n", + " [-0.09267598, -0.03391567, -1.5846985 , -1.51025496,\n", + " -2.02750719]],\n", + "\n", + " [[ 1.05323929, -0.30209153, 0.06557889, -0.02208535,\n", + " -0.34429993],\n", + " [ 0.34628556, -0.27149046, -0.45557256, -1.15669257,\n", + " -1.21654231],\n", + " [-0.65629648, -1.28993484, -0.36702137, -1.99261163,\n", + " -0.06425927]]])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Method 3 randn()\n", + "np.random.randn(2,3,5)" ] }, { @@ -79,11 +153,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[8 9 2 5 3]\n", + " [4 5 5 8 3]\n", + " [3 3 5 3 8]]\n", + "\n", + " [[4 3 8 3 5]\n", + " [5 3 7 7 9]\n", + " [2 3 5 8 4]]]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(a)" ] }, { @@ -95,11 +184,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "b = np.ones(shape=(5,2,3))" ] }, { @@ -111,11 +201,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 8, + "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 +239,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "a.shape == b.shape" ] }, { @@ -143,11 +267,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your answer here" + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/_0/46b7d7lx5gxfy45sbjf5py3h0000gn/T/ipykernel_79559/434330040.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# your answer here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m#cannot add because the shape of each array is different\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "# your answer here\n", + "\n", + "a + b\n", + "\n", + "#cannot add because the shape of each array is different" ] }, { @@ -159,11 +299,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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.]]])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "c = np.transpose(b , (1,2,0))\n", + "c" ] }, { @@ -175,11 +334,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ - "# your code/answer here" + "# your code/answer here\n", + "d = a + c\n", + "# we can add now because the shape of both matrix are the same" ] }, { @@ -191,11 +352,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[8 9 2 5 3]\n", + " [4 5 5 8 3]\n", + " [3 3 5 3 8]]\n", + "\n", + " [[4 3 8 3 5]\n", + " [5 3 7 7 9]\n", + " [2 3 5 8 4]]]\n", + "[[[ 9. 10. 3. 6. 4.]\n", + " [ 5. 6. 6. 9. 4.]\n", + " [ 4. 4. 6. 4. 9.]]\n", + "\n", + " [[ 5. 4. 9. 4. 6.]\n", + " [ 6. 4. 8. 8. 10.]\n", + " [ 3. 4. 6. 9. 5.]]]\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "print(a)\n", + "print(d)" ] }, { @@ -207,11 +391,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[8., 9., 2., 5., 3.],\n", + " [4., 5., 5., 8., 3.],\n", + " [3., 3., 5., 3., 8.]],\n", + "\n", + " [[4., 3., 8., 3., 5.],\n", + " [5., 3., 7., 7., 9.],\n", + " [2., 3., 5., 8., 4.]]])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "e = a*c\n", + "e" ] }, { @@ -223,11 +426,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]],\n", + "\n", + " [[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]]])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code/answer here\n", + "a == e\n", + "# yes because c is a matrix with only ones and multiplying a number by one will give the same number" ] }, { @@ -239,11 +461,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean = d.mean()" ] }, { @@ -255,11 +480,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[8., 9., 2., 5., 3.],\n", + " [4., 5., 5., 8., 3.],\n", + " [3., 3., 5., 3., 8.]],\n", + "\n", + " [[4., 3., 8., 3., 5.],\n", + " [5., 3., 7., 7., 9.],\n", + " [2., 3., 5., 8., 4.]]])" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "f = np.empty((2,3,5))\n", + "f" ] }, { @@ -275,11 +519,13 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "f = 0 * (d == d_min) + 25 * (d > d_min) * (d < d_mean) + 50 * (d == d_mean) + 75 * (d > d_mean) * (d < d_max) + 100 * (d == d_max)" ] }, { @@ -309,11 +555,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 9. 10. 3. 6. 4.]\n", + " [ 5. 6. 6. 9. 4.]\n", + " [ 4. 4. 6. 4. 9.]]\n", + "\n", + " [[ 5. 4. 9. 4. 6.]\n", + " [ 6. 4. 8. 8. 10.]\n", + " [ 3. 4. 6. 9. 5.]]]\n", + "[[[ 75 100 0 50 25]\n", + " [ 25 50 50 75 25]\n", + " [ 25 25 50 25 75]]\n", + "\n", + " [[ 25 25 75 25 50]\n", + " [ 50 25 75 75 100]\n", + " [ 0 25 50 75 25]]]\n" + ] + } + ], + "source": [ + "# your code here\n", + "print(d)\n", + "print(f)" ] }, { @@ -335,17 +604,92 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "range(0, 5)" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here \n", + "d\n", + "range(len(d[0][0]))" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 9. 10. 3. 6. 4.]\n", + " [ 5. 6. 6. 9. 4.]\n", + " [ 4. 4. 6. 4. 9.]]\n", + "\n", + " [[ 5. 4. 9. 4. 6.]\n", + " [ 6. 4. 8. 8. 10.]\n", + " [ 3. 4. 6. 9. 5.]]]\n", + "[[['D' 'E' 'A' 'C' 'B']\n", + " ['B' 'C' 'C' 'D' 'B']\n", + " ['B' 'B' 'C' 'B' 'D']]\n", + "\n", + " [['B' 'B' 'D' 'B' 'C']\n", + " ['C' 'B' 'D' 'D' 'E']\n", + " ['A' 'B' 'C' 'D' 'B']]]\n" + ] + } + ], + "source": [ + "f1= np.array(f, dtype=str)\n", + "for i in range(len(d)):\n", + " for k in range(len(d[i])):\n", + " for l in range(len(d[i][k])):\n", + " \n", + " if d[i][k][l] == d_min:\n", + " f1[i][k][l] = 'A'\n", + " \n", + " \n", + " elif d[i][k][l] > d_min and d[i][k][l] < d_mean:\n", + " f1[i][k][l] = 'B'\n", + " \n", + " \n", + " elif d[i][k][l] == d_mean:\n", + " f1[i][k][l] = 'C'\n", + " \n", + " \n", + " elif d[i][k][l] > d_mean and d[i][k][l] < d_max:\n", + " f1[i][k][l] = 'D'\n", + " \n", + " \n", + " elif d[i][k][l] == d_max:\n", + " f1[i][k][l] = 'E'\n", + "\n", + "print(d)\n", + "print(f1)" ] + }, + { + "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 +703,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.7" } }, "nbformat": 4,