From 2c9b3a38bc5523b1881997f3802010dec7e39516 Mon Sep 17 00:00:00 2001 From: Ekraj Pokhrel Date: Sun, 16 Apr 2023 14:21:37 +0200 Subject: [PATCH] Lab numpy solutins --- your-code/main.ipynb | 373 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 306 insertions(+), 67 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..15adbd5 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "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": 137, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'1.21.5'" + ] + }, + "execution_count": 137, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "np.version.version" ] }, { @@ -45,29 +58,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 138, "metadata": {}, "outputs": [], "source": [ - "# Method 1" + "# Method 1\n", + "a = np.random.random((2,3,5))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "metadata": {}, "outputs": [], "source": [ - "# Method 2" + "# Method 2\n", + "a = np.random.randn(2,3,5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 140, "metadata": {}, "outputs": [], "source": [ - "# Method 3" + "# Method 3\n", + "a = np.random.rand(2,3,5)" ] }, { @@ -79,11 +95,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 141, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.62685931 0.19298137 0.86105538 0.77472924 0.53917686]\n", + " [0.67607384 0.68442526 0.10560228 0.27942381 0.21414295]\n", + " [0.02992513 0.12405569 0.81730263 0.71386705 0.88806069]]\n", + "\n", + " [[0.15564406 0.0642394 0.92646944 0.7028728 0.91846473]\n", + " [0.70392543 0.67796244 0.84708366 0.8148024 0.28540609]\n", + " [0.07928522 0.36940072 0.93964675 0.56139738 0.41161123]]]\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(a)" ] }, { @@ -95,11 +126,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 142, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "b = np.ones(shape = (5,2,3), dtype = int)" ] }, { @@ -111,11 +143,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 143, + "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": 143, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "b" ] }, { @@ -127,11 +184,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 144, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 144, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "a.shape == b.shape\n", + "# ->> a and b are not of same size" ] }, { @@ -143,11 +213,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 145, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "# ->> a and b are not in same shape we can not add without reshaping one of the array" ] }, { @@ -159,11 +230,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 146, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "c = b.reshape(2,3,5)" ] }, { @@ -175,11 +247,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 147, "metadata": {}, "outputs": [], "source": [ - "# your code/answer here" + "# your code/answer here\n", + "d = a + c" ] }, { @@ -191,11 +264,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 148, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.62685931 0.19298137 0.86105538 0.77472924 0.53917686]\n", + " [0.67607384 0.68442526 0.10560228 0.27942381 0.21414295]\n", + " [0.02992513 0.12405569 0.81730263 0.71386705 0.88806069]]\n", + "\n", + " [[0.15564406 0.0642394 0.92646944 0.7028728 0.91846473]\n", + " [0.70392543 0.67796244 0.84708366 0.8148024 0.28540609]\n", + " [0.07928522 0.36940072 0.93964675 0.56139738 0.41161123]]]\n", + "[[[1.62685931 1.19298137 1.86105538 1.77472924 1.53917686]\n", + " [1.67607384 1.68442526 1.10560228 1.27942381 1.21414295]\n", + " [1.02992513 1.12405569 1.81730263 1.71386705 1.88806069]]\n", + "\n", + " [[1.15564406 1.0642394 1.92646944 1.7028728 1.91846473]\n", + " [1.70392543 1.67796244 1.84708366 1.8148024 1.28540609]\n", + " [1.07928522 1.36940072 1.93964675 1.56139738 1.41161123]]]\n" + ] + } + ], + "source": [ + "# your code/answer here\n", + "print(a) \n", + "print(d)\n", + "\n", + "# ->> array d is the sum between array a and c. Array c was transformed form of array b which contains \n", + "# value 1 for all the element. since array d is 1 more than the value of array a." ] }, { @@ -207,11 +306,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 149, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.62685931 0.19298137 0.86105538 0.77472924 0.53917686]\n", + " [0.67607384 0.68442526 0.10560228 0.27942381 0.21414295]\n", + " [0.02992513 0.12405569 0.81730263 0.71386705 0.88806069]]\n", + "\n", + " [[0.15564406 0.0642394 0.92646944 0.7028728 0.91846473]\n", + " [0.70392543 0.67796244 0.84708366 0.8148024 0.28540609]\n", + " [0.07928522 0.36940072 0.93964675 0.56139738 0.41161123]]]\n" + ] + } + ], + "source": [ + "# your code here\n", + "e = a * c\n", + "print(e)" ] }, { @@ -223,11 +338,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 150, + "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": 150, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code/answer here\n", + "e == a\n", + "# yes! the value of each element in array c is 1 so multiplying a with c will give same array as in a . " ] }, { @@ -239,11 +373,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 151, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.939646754914739\n", + "1.0299251334161892\n", + "1.5328631080671753\n" + ] + } + ], + "source": [ + "# your code here\n", + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)\n", + "print(d_max)\n", + "print(d_min)\n", + "print(d_mean)" ] }, { @@ -255,11 +405,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 152, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "f = np.empty([2,3,5])" ] }, { @@ -275,11 +426,39 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 153, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75. 25. 75. 75. 75.]\n", + " [ 75. 75. 25. 25. 25.]\n", + " [ 0. 25. 75. 75. 75.]]\n", + "\n", + " [[ 25. 25. 75. 75. 75.]\n", + " [ 75. 75. 75. 75. 25.]\n", + " [ 25. 25. 100. 75. 25.]]]\n" + ] + } + ], + "source": [ + "# your code here\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", + " if d_min < d[i,j,k]