From 28b1bc2affa80a9c86cc722378d37d0b31afd509 Mon Sep 17 00:00:00 2001 From: Julia Parnis Date: Mon, 13 Oct 2025 18:12:59 +0200 Subject: [PATCH] Week 2 day 1 lab commit --- your-code/main.ipynb | 443 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 382 insertions(+), 61 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa14..6c0e04fa 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "import numpy as np\n" ] }, { @@ -34,11 +34,115 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.3.3\n", + "{\n", + " \"Compilers\": {\n", + " \"c\": {\n", + " \"name\": \"msvc\",\n", + " \"linker\": \"link\",\n", + " \"version\": \"19.44.35215\",\n", + " \"commands\": \"cl\"\n", + " },\n", + " \"cython\": {\n", + " \"name\": \"cython\",\n", + " \"linker\": \"cython\",\n", + " \"version\": \"3.1.3\",\n", + " \"commands\": \"cython\"\n", + " },\n", + " \"c++\": {\n", + " \"name\": \"msvc\",\n", + " \"linker\": \"link\",\n", + " \"version\": \"19.44.35215\",\n", + " \"commands\": \"cl\"\n", + " }\n", + " },\n", + " \"Machine Information\": {\n", + " \"host\": {\n", + " \"cpu\": \"x86_64\",\n", + " \"family\": \"x86_64\",\n", + " \"endian\": \"little\",\n", + " \"system\": \"windows\"\n", + " },\n", + " \"build\": {\n", + " \"cpu\": \"x86_64\",\n", + " \"family\": \"x86_64\",\n", + " \"endian\": \"little\",\n", + " \"system\": \"windows\"\n", + " }\n", + " },\n", + " \"Build Dependencies\": {\n", + " \"blas\": {\n", + " \"name\": \"scipy-openblas\",\n", + " \"found\": true,\n", + " \"version\": \"0.3.30\",\n", + " \"detection method\": \"pkgconfig\",\n", + " \"include directory\": \"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-5t0hy_xh/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/include\",\n", + " \"lib directory\": \"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-5t0hy_xh/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/lib\",\n", + " \"openblas configuration\": \"OpenBLAS 0.3.30 USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=24\",\n", + " \"pc file directory\": \"D:/a/numpy-release/numpy-release/.openblas\"\n", + " },\n", + " \"lapack\": {\n", + " \"name\": \"scipy-openblas\",\n", + " \"found\": true,\n", + " \"version\": \"0.3.30\",\n", + " \"detection method\": \"pkgconfig\",\n", + " \"include directory\": \"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-5t0hy_xh/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/include\",\n", + " \"lib directory\": \"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-5t0hy_xh/cp313-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/lib\",\n", + " \"openblas configuration\": \"OpenBLAS 0.3.30 USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell MAX_THREADS=24\",\n", + " \"pc file directory\": \"D:/a/numpy-release/numpy-release/.openblas\"\n", + " }\n", + " },\n", + " \"Python Information\": {\n", + " \"path\": \"C:\\\\Users\\\\runneradmin\\\\AppData\\\\Local\\\\Temp\\\\build-env-q3zde7n4\\\\Scripts\\\\python.exe\",\n", + " \"version\": \"3.13\"\n", + " },\n", + " \"SIMD Extensions\": {\n", + " \"baseline\": [\n", + " \"SSE\",\n", + " \"SSE2\",\n", + " \"SSE3\"\n", + " ],\n", + " \"found\": [\n", + " \"SSSE3\",\n", + " \"SSE41\",\n", + " \"POPCNT\",\n", + " \"SSE42\",\n", + " \"AVX\",\n", + " \"F16C\",\n", + " \"FMA3\",\n", + " \"AVX2\"\n", + " ],\n", + " \"not found\": [\n", + " \"AVX512F\",\n", + " \"AVX512CD\",\n", + " \"AVX512_SKX\",\n", + " \"AVX512_CLX\",\n", + " \"AVX512_CNL\",\n", + " \"AVX512_ICL\"\n", + " ]\n", + " }\n", + "}\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\Users\\julia\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\numpy\\__config__.py:155: UserWarning: Install `pyyaml` for better output\n", + " warnings.warn(\"Install `pyyaml` for better output\", stacklevel=1)\n" + ] + } + ], + "source": [ + "print(np.__version__) # e.g. '1.26.4'\n", + "np.show_config() # prints build/config info\n" ] }, { @@ -51,11 +155,13 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "a=np.random.random((2, 3, 5))\n", + "a=np.random.randint(2, 10, size=(2, 3, 5))\n", + "a = np.random.randn(2, 3, 5) # generates a 3D array with mean=0 and stdev=1\n" ] }, { @@ -68,11 +174,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[-0.25201286 -0.3505856 1.21983551 -0.27376946 1.95929956]\n", + " [-0.83055925 -0.8578651 0.42667638 0.61014526 0.19618826]\n", + " [-0.0617004 -0.88251079 -0.59693443 0.1458109 0.18409469]]\n", + "\n", + " [[ 0.50080915 -0.40850651 -0.9871803 0.55756916 0.10894486]\n", + " [ 0.36666563 1.43648706 -1.09085687 -1.48658453 0.44172771]\n", + " [ 0.64354599 -0.9740823 -0.05544771 -0.838554 -1.90123862]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a)\n" ] }, { @@ -85,11 +205,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "b = np.ones ((5, 2, 3))\n" ] }, { @@ -102,11 +222,32 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 13, + "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": [ + "print(b)\n" ] }, { @@ -119,11 +260,22 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "a.size==b.size\n" ] }, { @@ -136,11 +288,12 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "# Answer: No.\n", + "# Reason: Although a and b have an equal size (number of elements), they have a different shape.\n" ] }, { @@ -154,11 +307,23 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "### [your code here]\n" + "c=np.transpose(b, (1, 2, 0)) # transposes from 5x2x3 array to 2x3x5\n", + "c.shape\n" ] }, { @@ -171,11 +336,12 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 65, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "d=a+c\n", + "# 2 arrays have the same shape => can be added\n" ] }, { @@ -188,12 +354,36 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[-0.25201286 -0.3505856 1.21983551 -0.27376946 1.95929956]\n", + " [-0.83055925 -0.8578651 0.42667638 0.61014526 0.19618826]\n", + " [-0.0617004 -0.88251079 -0.59693443 0.1458109 0.18409469]]\n", + "\n", + " [[ 0.50080915 -0.40850651 -0.9871803 0.55756916 0.10894486]\n", + " [ 0.36666563 1.43648706 -1.09085687 -1.48658453 0.44172771]\n", + " [ 0.64354599 -0.9740823 -0.05544771 -0.838554 -1.90123862]]]\n", + "[[[ 0.74798714 0.6494144 2.21983551 0.72623054 2.95929956]\n", + " [ 0.16944075 0.1421349 1.42667638 1.61014526 1.19618826]\n", + " [ 0.9382996 0.11748921 0.40306557 1.1458109 1.18409469]]\n", + "\n", + " [[ 1.50080915 0.59149349 0.0128197 1.55756916 1.10894486]\n", + " [ 1.36666563 2.43648706 -0.09085687 -0.48658453 1.44172771]\n", + " [ 1.64354599 0.0259177 0.94455229 0.161446 -0.90123862]]]\n" + ] + } + ], + "source": [ + "print (a)\n", + "print(d)\n", + "\n", + "# Array d is a sum of an array a and transposed array b, consisting only of 1. \n", + "# Therefore, each number in d larger than in a by 1.\n" ] }, { @@ -206,11 +396,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 67, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "e=a*c\n" ] }, { @@ -224,11 +414,58 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 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]]]\n", + "float64\n", + "float64\n", + "float64\n", + "[[[0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0.]]\n", + "\n", + " [[0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0.]\n", + " [0. 0. 0. 0. 0.]]]\n", + "[[[-0.25201286 -0.3505856 1.21983551 -0.27376946 1.95929956]\n", + " [-0.83055925 -0.8578651 0.42667638 0.61014526 0.19618826]\n", + " [-0.0617004 -0.88251079 -0.59693443 0.1458109 0.18409469]]\n", + "\n", + " [[ 0.50080915 -0.40850651 -0.9871803 0.55756916 0.10894486]\n", + " [ 0.36666563 1.43648706 -1.09085687 -1.48658453 0.44172771]\n", + " [ 0.64354599 -0.9740823 -0.05544771 -0.838554 -1.90123862]]]\n", + "[[[-0.25201286 -0.3505856 1.21983551 -0.27376946 1.95929956]\n", + " [-0.83055925 -0.8578651 0.42667638 0.61014526 0.19618826]\n", + " [-0.0617004 -0.88251079 -0.59693443 0.1458109 0.18409469]]\n", + "\n", + " [[ 0.50080915 -0.40850651 -0.9871803 0.55756916 0.10894486]\n", + " [ 0.36666563 1.43648706 -1.09085687 -1.48658453 0.44172771]\n", + " [ 0.64354599 -0.9740823 -0.05544771 -0.838554 -1.90123862]]]\n" + ] + } + ], + "source": [ + "print(e==a)\n", + "\n", + "print(a.dtype)\n", + "print(c.dtype)\n", + "print(e.dtype)\n", + "\n", + "print(e-a)\n", + "print(a)\n", + "print(e)\n", + "\n", "\n" ] }, @@ -243,11 +480,13 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n", + "d_max=np.max(d)\n", + "d_min=np.min(d)\n", + "d_mean=np.mean(d)\n", "\n" ] }, @@ -261,11 +500,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "f=np.empty((2, 3, 5))\n" ] }, { @@ -287,11 +526,33 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 25. 25. 75. 25. 100.]\n", + " [ 25. 25. 75. 75. 75.]\n", + " [ 75. 25. 25. 75. 75.]]\n", + "\n", + " [[ 75. 25. 25. 75. 75.]\n", + " [ 75. 75. 25. 25. 75.]\n", + " [ 75. 25. 75. 25. 0.]]]\n" + ] + } + ], + "source": [ + "inter=d.copy() # for an array inter=d, assigns d as inter as well\n", + "inter[(d>d_mean) & (dd_min)] = 25\n", + "inter[d==d_mean] = 50\n", + "inter[d==d_min] = 0\n", + "inter[d==d_max] = 100\n", + "\n", + "f=inter.copy()\n", + "print(f)\n" ] }, { @@ -325,11 +586,33 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 0.74798714 0.6494144 2.21983551 0.72623054 2.95929956]\n", + " [ 0.16944075 0.1421349 1.42667638 1.61014526 1.19618826]\n", + " [ 0.9382996 0.11748921 0.40306557 1.1458109 1.18409469]]\n", + "\n", + " [[ 1.50080915 0.59149349 0.0128197 1.55756916 1.10894486]\n", + " [ 1.36666563 2.43648706 -0.09085687 -0.48658453 1.44172771]\n", + " [ 1.64354599 0.0259177 0.94455229 0.161446 -0.90123862]]]\n", + "[[[ 25. 25. 75. 25. 100.]\n", + " [ 25. 25. 75. 75. 75.]\n", + " [ 75. 25. 25. 75. 75.]]\n", + "\n", + " [[ 75. 25. 25. 75. 75.]\n", + " [ 75. 75. 25. 25. 75.]\n", + " [ 75. 25. 75. 25. 0.]]]\n" + ] + } + ], + "source": [ + "print(d)\n", + "print(f)\n" ] }, { @@ -350,12 +633,50 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['B' 'B' 'D' 'B' 'E']\n", + " ['B' 'B' 'D' 'D' 'D']\n", + " ['D' 'B' 'B' 'D' 'D']]\n", + "\n", + " [['D' 'B' 'B' 'D' 'D']\n", + " ['D' 'D' 'B' 'B' 'D']\n", + " ['D' 'B' 'D' 'B' 'A']]]\n" + ] + } + ], + "source": [ + "g=np.empty((2,3,5), dtype=str)\n", + "\n", + "for block in range (d.shape[0]):\n", + " for row in range (d.shape[1]):\n", + " for column in range (d.shape[2]):\n", + " value=d[block, row, column]\n", + " if value==d_mean:\n", + " g[block, row, column]=\"C\"\n", + " elif (valued_min):\n", + " g[block, row, column]=\"B\"\n", + " elif value==d_min:\n", + " g[block, row, column]=\"A\"\n", + " elif value ==d_max:\n", + " g[block, row, column]=\"E\"\n", + " else:\n", + " g[block, row, column]=\"D\"\n", + "\n", + "print(g)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "### [your code here]" - ] + "source": [] } ], "metadata": { @@ -374,7 +695,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.5" } }, "nbformat": 4,