diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa1..8821cdd 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,13 @@ }, { "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", + "\n" ] }, { @@ -34,11 +36,94 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.2.5\n", + "Build Dependencies:\n", + " blas:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: C:/Users/runneradmin/AppData/Local/Temp/cibw-run-065080rt/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/include\n", + " lib directory: C:/Users/runneradmin/AppData/Local/Temp/cibw-run-065080rt/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/lib\n", + " name: scipy-openblas\n", + " openblas configuration: OpenBLAS 0.3.28 USE64BITINT DYNAMIC_ARCH NO_AFFINITY\n", + " Haswell MAX_THREADS=24\n", + " pc file directory: D:/a/numpy/numpy/.openblas\n", + " version: 0.3.28\n", + " lapack:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: C:/Users/runneradmin/AppData/Local/Temp/cibw-run-065080rt/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/include\n", + " lib directory: C:/Users/runneradmin/AppData/Local/Temp/cibw-run-065080rt/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/lib\n", + " name: scipy-openblas\n", + " openblas configuration: OpenBLAS 0.3.28 USE64BITINT DYNAMIC_ARCH NO_AFFINITY\n", + " Haswell MAX_THREADS=24\n", + " pc file directory: D:/a/numpy/numpy/.openblas\n", + " version: 0.3.28\n", + "Compilers:\n", + " c:\n", + " commands: cl\n", + " linker: link\n", + " name: msvc\n", + " version: 19.29.30159\n", + " c++:\n", + " commands: cl\n", + " linker: link\n", + " name: msvc\n", + " version: 19.29.30159\n", + " cython:\n", + " commands: cython\n", + " linker: cython\n", + " name: cython\n", + " version: 3.0.12\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:\\Users\\runneradmin\\AppData\\Local\\Temp\\build-env-19lia66t\\Scripts\\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": [ - "### [your code here]\n" + "### [your code here]\n", + "print(np.__version__)\n", + "np.show_config()\n" ] }, { @@ -51,11 +136,49 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "random.randint: [[[0.17 0.22 0.25 0.94 0.46]\n", + " [0.67 0.81 0.86 0.57 0.38]\n", + " [0.44 0.53 0.82 0.02 0.13]]\n", + "\n", + " [[0.18 0.63 0.52 0.72 0.13]\n", + " [0.66 0.22 0.72 0.55 0.08]\n", + " [0.89 0.14 0.83 0.24 0.15]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "# Method 1: Using np.random.rand\n", + "a = np.random.rand(2, 3, 5)\n", + "#print(\"random.rand:\", a)\n", + "\n", + "# Method 2: Using np.random.random_sample\n", + "a = np.random.random_sample((2, 3, 5))\n", + "#print(\"random_sample:\", a)\n", + "\n", + "# Method 3: Using np.random.uniform\n", + "a = np.random.uniform(0, 1, (2, 3, 5))\n", + "#print(\"random.uniform:\", a)\n", + "\n", + "# Method 4: Using np.random.randn (standard normal distribution)\n", + "a = np.random.randn(2, 3, 5)\n", + "#print(\"random.randn:\", a)\n", + "\n", + "# Method 5: Using np.random.randint (random integers, scaled to floats)\n", + "a = np.random.randint(0, 100, (2, 3, 5)) / 100.0\n", + "print(\"random.randint:\", a)\n", + "\n", + "# Method 6: Using np.empty and filling it with random values\n", + "a = np.empty((2, 3, 5))\n", + "a[:] = np.random.rand(2, 3, 5)\n", + "#print(\"random.rand with empty variable:\", a)\n" ] }, { @@ -68,11 +191,26 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.21655809 0.06640551 0.84166532 0.99322523 0.97437033]\n", + " [0.73401661 0.77438803 0.41836324 0.53243085 0.2531631 ]\n", + " [0.18287475 0.09424083 0.83053862 0.37454524 0.82985794]]\n", + "\n", + " [[0.70794959 0.01956831 0.91897163 0.31193413 0.27576659]\n", + " [0.04376403 0.48390919 0.21771284 0.89421278 0.12088888]\n", + " [0.10569049 0.18105116 0.22735079 0.26585621 0.33871129]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(a)\n" ] }, { @@ -85,11 +223,12 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "b = np.ones((5, 2, 3))\n" ] }, { @@ -102,11 +241,33 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 20, "metadata": {}, - "outputs": [], + "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" + "### [your code here]\n", + "print(b)\n" ] }, { @@ -119,11 +280,27 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a and b have the same size\n", + "30 30\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "if a.size == b.size:\n", + " print(\"a and b have the same size\")\n", + " \n", + "else:\n", + " print(\"a and b have different sizes\")\n", + "\n", + "print(a.size, b.size)\n" ] }, { @@ -136,11 +313,36 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"No we are not able to add 'a' and 'b' because they have different shapes. \\nAs per the broadcasting rules in numpy, the shapes of the arrays must be compatible which means they should be same or one of them should be 1\\nFor our code:\\na.shape = (2, 3, 5)\\nb.shape = (5, 2, 3)\\n\\nThe dimensions don't align in a way that would allow broadcasting:\\n\\nThe first dimension of a is 2, while the first dimension of b is 5.\\nThe second dimension of a is 3, and the second dimension of b is 2.\\nThe third dimension of a is 5, and the third dimension of b is 3. \\n\\nAs per the broadcasting rules in numpy, the shapes of the arrays must be compatible which means they should be same or one of them should be 1\\nAs any of these conditions are not met, we canno add 'a and 'b'.\"" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "\"\"\"No we are not able to add 'a' and 'b' because they have different shapes. \n", + "As per the broadcasting rules in numpy, the shapes of the arrays must be compatible which means they should be same or one of them should be 1\n", + "For our code:\n", + "a.shape = (2, 3, 5)\n", + "b.shape = (5, 2, 3)\n", + "\n", + "The dimensions don't align in a way that would allow broadcasting:\n", + "\n", + "The first dimension of a is 2, while the first dimension of b is 5.\n", + "The second dimension of a is 3, and the second dimension of b is 2.\n", + "The third dimension of a is 5, and the third dimension of b is 3. \n", + "\n", + "As per the broadcasting rules in numpy, the shapes of the arrays must be compatible which means they should be same or one of them should be 1\n", + "As any of these conditions are not met, we canno add 'a and 'b'.\"\"\"\n" ] }, { @@ -154,11 +356,22 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "c = b.transpose((1, 2, 0))\n", + "print(c.shape)\n", + "\n" ] }, { @@ -171,11 +384,42 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n", + "[[[1.25981827 1.39519379 1.44234385 1.46874158 1.50662712]\n", + " [1.78968524 1.82254671 1.26932618 1.44304531 1.28013578]\n", + " [1.86482855 1.17317569 1.71204915 1.72204516 1.10928244]]\n", + "\n", + " [[1.05756323 1.32015538 1.05633544 1.99087328 1.68776029]\n", + " [1.39820198 1.02154498 1.32971737 1.04505438 1.04143463]\n", + " [1.85357474 1.57423069 1.96255241 1.84238581 1.41077028]]]\n" + ] + }, + { + "data": { + "text/plain": [ + "\"it worked because the shapes of 'a' and 'c' are now compatible for boradcasting\"" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "d = a + c\n", + "print(d.shape)\n", + "print (d)\n", + "\n", + "\"\"\"it worked because the shapes of 'a' and 'c' are now compatible for boradcasting\"\"\"\n", + "\n" ] }, { @@ -188,11 +432,46 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "array of a: [[[0.25981827 0.39519379 0.44234385 0.46874158 0.50662712]\n", + " [0.78968524 0.82254671 0.26932618 0.44304531 0.28013578]\n", + " [0.86482855 0.17317569 0.71204915 0.72204516 0.10928244]]\n", + "\n", + " [[0.05756323 0.32015538 0.05633544 0.99087328 0.68776029]\n", + " [0.39820198 0.02154498 0.32971737 0.04505438 0.04143463]\n", + " [0.85357474 0.57423069 0.96255241 0.84238581 0.41077028]]]\n", + "array of d: [[[1.25981827 1.39519379 1.44234385 1.46874158 1.50662712]\n", + " [1.78968524 1.82254671 1.26932618 1.44304531 1.28013578]\n", + " [1.86482855 1.17317569 1.71204915 1.72204516 1.10928244]]\n", + "\n", + " [[1.05756323 1.32015538 1.05633544 1.99087328 1.68776029]\n", + " [1.39820198 1.02154498 1.32971737 1.04505438 1.04143463]\n", + " [1.85357474 1.57423069 1.96255241 1.84238581 1.41077028]]]\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Both 'a' and 'd' are of same shape but the values are different because 'a' is a random array and 'd' is the sum of 'a' and transposed 'b'\"" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "### [your code here]\n", + "print(\"array of a:\", a)\n", + "print(\"array of d:\", d)\n", + "\n", + "\"\"\"Both 'a' and 'd' are of same shape but the values are different because 'a' is a random array and 'd' is the sum of 'a' and transposed 'b'\"\"\"\n", "\n" ] }, @@ -206,11 +485,29 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n", + "[[[0.25981827 0.39519379 0.44234385 0.46874158 0.50662712]\n", + " [0.78968524 0.82254671 0.26932618 0.44304531 0.28013578]\n", + " [0.86482855 0.17317569 0.71204915 0.72204516 0.10928244]]\n", + "\n", + " [[0.05756323 0.32015538 0.05633544 0.99087328 0.68776029]\n", + " [0.39820198 0.02154498 0.32971737 0.04505438 0.04143463]\n", + " [0.85357474 0.57423069 0.96255241 0.84238581 0.41077028]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "e = a*c\n", + "print(e.shape)\n", + "print (e)\n" ] }, { @@ -224,12 +521,25 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Both 'a' and 'e' are of same shape but the values are different because 'a' is a random array and 'e' is the multiple of 'a' and transposed 'b'\"" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "### [your code here]\n", - "\n" + "\n", + "\n", + "\"\"\"Both 'a' and 'e' are of same shape but the values are different because 'a' is a random array and 'e' is the multiple of 'a' and transposed 'b'\"\"\"" ] }, { @@ -243,11 +553,29 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MAX Value of d: 1.9908732764427755\n", + "MIN Value of d: 1.021544975378919\n", + "MEAN Value of d: 1.4616999904127739\n" + ] + } + ], "source": [ "### [your code here]\n", + "\n", + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean = d.mean()\n", + "print(\"MAX Value of d:\", d_max)\n", + "print(\"MIN Value of d:\", d_min)\n", + "print(\"MEAN Value of d:\", d_mean)\n", + "\n", "\n" ] }, @@ -261,11 +589,12 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "f = np.empty((2,3,5))\n" ] }, { @@ -287,11 +616,39 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 25. 25. 25. 75. 75.]\n", + " [ 75. 75. 25. 25. 25.]\n", + " [ 75. 25. 75. 75. 25.]]\n", + "\n", + " [[ 25. 25. 25. 100. 75.]\n", + " [ 25. 0. 25. 25. 25.]\n", + " [ 75. 75. 75. 75. 25.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\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[i][j][k] == d_max:\n", + " f[i][j][k] = 100\n", + " elif d[i][j][k] == d_min:\n", + " f[i][j][k] = 0\n", + " elif d[i][j][k] == d_mean:\n", + " f[i][j][k] = 50\n", + " elif (d[i][j][k] > d_mean) & (d[i][j][k] < d_max):\n", + " f[i][j][k] = 75\n", + " elif (d[i][j][k] > d_min) & (d[i][j][k] < d_mean):\n", + " f[i][j][k] = 25\n", + "print(f)\n" ] }, { @@ -325,11 +682,36 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d array: \n", + " [[[1.25981827 1.39519379 1.44234385 1.46874158 1.50662712]\n", + " [1.78968524 1.82254671 1.26932618 1.44304531 1.28013578]\n", + " [1.86482855 1.17317569 1.71204915 1.72204516 1.10928244]]\n", + "\n", + " [[1.05756323 1.32015538 1.05633544 1.99087328 1.68776029]\n", + " [1.39820198 1.02154498 1.32971737 1.04505438 1.04143463]\n", + " [1.85357474 1.57423069 1.96255241 1.84238581 1.41077028]]]\n", + "f array: \n", + " [[[ 25. 25. 25. 75. 75.]\n", + " [ 75. 75. 25. 25. 25.]\n", + " [ 75. 25. 75. 75. 25.]]\n", + "\n", + " [[ 25. 25. 25. 100. 75.]\n", + " [ 25. 0. 25. 25. 25.]\n", + " [ 75. 75. 75. 75. 25.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(\"d array:\",\"\\n\", d)\n", + "print(\"f array:\", \"\\n\", f)" ] }, { @@ -350,11 +732,64 @@ }, { "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [], + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original 3D list (d):\n", + "[[1.25981827 1.39519379 1.44234385 1.46874158 1.50662712]\n", + " [1.78968524 1.82254671 1.26932618 1.44304531 1.28013578]\n", + " [1.86482855 1.17317569 1.71204915 1.72204516 1.10928244]]\n", + "[[1.05756323 1.32015538 1.05633544 1.99087328 1.68776029]\n", + " [1.39820198 1.02154498 1.32971737 1.04505438 1.04143463]\n", + " [1.85357474 1.57423069 1.96255241 1.84238581 1.41077028]]\n", + "\n", + "Letter-coded 3D list (f):\n", + "[['E', 'E', 'E', 'D', 'D'], ['D', 'D', 'E', 'E', 'E'], ['D', 'E', 'D', 'D', 'E']]\n", + "[['E', 'E', 'E', 'A', 'D'], ['E', 'B', 'E', 'E', 'E'], ['D', 'D', 'D', 'D', 'E']]\n" + ] + } + ], "source": [ - "### [your code here]" + "\n", + "flat = [string_value for alphabet in d for row in alphabet for string_value in row]\n", + "d_min = min(flat)\n", + "d_max = max(flat)\n", + "d_mean = sum(flat) / len(flat)\n", + "\n", + "# Create a new 3D list f of the same shape\n", + "f = []\n", + "\n", + "for i in range(len(d)):\n", + " alphabet = []\n", + " for j in range(len(d[i])):\n", + " row = []\n", + " for k in range(len(d[i][j])):\n", + " val = d[i][j][k]\n", + " if val == d_max:\n", + " row.append('A')\n", + " elif val == d_min:\n", + " row.append('B')\n", + " elif val == d_mean:\n", + " row.append('C')\n", + " elif d_mean < val < d_max:\n", + " row.append('D')\n", + " elif d_min < val < d_mean:\n", + " row.append('E')\n", + " alphabet.append(row)\n", + " f.append(alphabet)\n", + "\n", + "# Output the result\n", + "print(\"Original 3D list (d):\")\n", + "for alphabet in d:\n", + " print(alphabet)\n", + "\n", + "print(\"\\nLetter-coded 3D list (f):\")\n", + "for alphabet in f:\n", + " print(alphabet)\n" ] } ], @@ -374,7 +809,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.3" } }, "nbformat": 4,