From d1924196e7ca50a10edaf5df698f1ce6540dee0b Mon Sep 17 00:00:00 2001 From: MLK14 Date: Sun, 9 Mar 2025 11:54:25 +0300 Subject: [PATCH] labN1 --- your-code/main.ipynb | 751 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 701 insertions(+), 50 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa1..6ebd0e4 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,112 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Numpy version: 2.0.2\n", + "{\n", + " \"Compilers\": {\n", + " \"c\": {\n", + " \"name\": \"clang\",\n", + " \"linker\": \"ld64\",\n", + " \"version\": \"15.0.0\",\n", + " \"commands\": \"cc\"\n", + " },\n", + " \"cython\": {\n", + " \"name\": \"cython\",\n", + " \"linker\": \"cython\",\n", + " \"version\": \"3.0.11\",\n", + " \"commands\": \"cython\"\n", + " },\n", + " \"c++\": {\n", + " \"name\": \"clang\",\n", + " \"linker\": \"ld64\",\n", + " \"version\": \"15.0.0\",\n", + " \"commands\": \"c++\"\n", + " }\n", + " },\n", + " \"Machine Information\": {\n", + " \"host\": {\n", + " \"cpu\": \"aarch64\",\n", + " \"family\": \"aarch64\",\n", + " \"endian\": \"little\",\n", + " \"system\": \"darwin\"\n", + " },\n", + " \"build\": {\n", + " \"cpu\": \"aarch64\",\n", + " \"family\": \"aarch64\",\n", + " \"endian\": \"little\",\n", + " \"system\": \"darwin\"\n", + " }\n", + " },\n", + " \"Build Dependencies\": {\n", + " \"blas\": {\n", + " \"name\": \"accelerate\",\n", + " \"found\": true,\n", + " \"version\": \"unknown\",\n", + " \"detection method\": \"system\",\n", + " \"include directory\": \"unknown\",\n", + " \"lib directory\": \"unknown\",\n", + " \"openblas configuration\": \"unknown\",\n", + " \"pc file directory\": \"unknown\"\n", + " },\n", + " \"lapack\": {\n", + " \"name\": \"accelerate\",\n", + " \"found\": true,\n", + " \"version\": \"unknown\",\n", + " \"detection method\": \"system\",\n", + " \"include directory\": \"unknown\",\n", + " \"lib directory\": \"unknown\",\n", + " \"openblas configuration\": \"unknown\",\n", + " \"pc file directory\": \"unknown\"\n", + " }\n", + " },\n", + " \"Python Information\": {\n", + " \"path\": \"/private/var/folders/4d/0gnh84wj53j7wyk695q0tc_80000gn/T/build-env-kq4kuj35/bin/python\",\n", + " \"version\": \"3.9\"\n", + " },\n", + " \"SIMD Extensions\": {\n", + " \"baseline\": [\n", + " \"NEON\",\n", + " \"NEON_FP16\",\n", + " \"NEON_VFPV4\",\n", + " \"ASIMD\"\n", + " ],\n", + " \"found\": [\n", + " \"ASIMDHP\"\n", + " ],\n", + " \"not found\": [\n", + " \"ASIMDFHM\"\n", + " ]\n", + " }\n", + "}\n", + "Numpy configuration: None\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/amanialshaikh/Library/Python/3.9/lib/python/site-packages/numpy/__config__.py:155: UserWarning: Install `pyyaml` for better output\n", + " warnings.warn(\"Install `pyyaml` for better output\", stacklevel=1)\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Print numpy version\n", + "print(\"Numpy version:\", np.__version__)\n", + "\n", + "# Print numpy configuration\n", + "print(\"Numpy configuration:\", np.__config__.show())\n", + "\n" ] }, { @@ -51,11 +154,58 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using np.random.rand():\n", + " [[[0.86545489 0.52400869 0.37963522 0.06386432 0.09143375]\n", + " [0.80453156 0.8336031 0.45846409 0.76017007 0.90819691]\n", + " [0.88825281 0.84222222 0.94639923 0.11745611 0.24898267]]\n", + "\n", + " [[0.45672698 0.24600849 0.72907526 0.05706793 0.81309349]\n", + " [0.89689947 0.66489693 0.77417767 0.4307765 0.98733536]\n", + " [0.44959856 0.79298454 0.68295713 0.37539338 0.46444599]]]\n", + "\n", + "Using np.random.randn():\n", + " [[[ 0.86119598 -1.77055971 -0.29480462 1.57873045 -1.23212602]\n", + " [-0.89843078 0.43445164 0.43217569 0.1853388 1.30155093]\n", + " [ 0.37028785 -0.21670403 -0.74148755 0.15079187 0.78013884]]\n", + "\n", + " [[-0.40512675 1.63721737 -1.52663031 -2.09777952 1.04819613]\n", + " [-0.8511727 1.47371333 3.07614261 0.08253865 -0.53479535]\n", + " [-1.1628611 0.2851776 -0.90095851 0.71500803 1.91764325]]]\n", + "\n", + "Using np.random.random():\n", + " [[[0.56140425 0.80904657 0.39901576 0.77510853 0.01666849]\n", + " [0.21049186 0.81412672 0.34577175 0.52769718 0.03024245]\n", + " [0.62932187 0.74391874 0.50785871 0.46751773 0.94363672]]\n", + "\n", + " [[0.68031637 0.38849145 0.34057654 0.57626842 0.64231715]\n", + " [0.52762623 0.33271392 0.47996538 0.31483715 0.74330979]\n", + " [0.60924463 0.16650612 0.16231487 0.84308082 0.10326695]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Method 1: Using np.random.rand()\n", + "a_rand = np.random.rand(2, 3, 5)\n", + "print(\"Using np.random.rand():\\n\", a_rand)\n", + "\n", + "# Method 2: Using np.random.randn()\n", + "a_randn = np.random.randn(2, 3, 5)\n", + "print(\"\\nUsing np.random.randn():\\n\", a_randn)\n", + "\n", + "# Method 3: Using np.random.random()\n", + "a_random = np.random.random((2, 3, 5))\n", + "print(\"\\nUsing np.random.random():\\n\", a_random)\n", + "\n" ] }, { @@ -68,11 +218,39 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array a:\n", + " [[[5.46471528e-01 3.27163183e-01 1.10252585e-02 6.56417672e-01\n", + " 2.63367307e-01]\n", + " [9.55668755e-01 7.89896188e-01 6.21650937e-01 2.44914290e-01\n", + " 5.31642961e-01]\n", + " [2.53811692e-01 4.51139176e-01 2.04624176e-01 7.54521857e-02\n", + " 7.41547177e-01]]\n", + "\n", + " [[2.60943624e-01 3.86565674e-01 9.46578471e-01 2.01453647e-01\n", + " 7.93296608e-01]\n", + " [8.20094052e-01 7.01341828e-01 8.95348138e-01 5.33201449e-01\n", + " 2.53640262e-01]\n", + " [4.43378062e-01 7.60787086e-01 5.83360036e-01 1.92248114e-01\n", + " 6.12458933e-04]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Generating a 2x3x5 random array using np.random.rand()\n", + "a = np.random.rand(2, 3, 5)\n", + "\n", + "# Printing the array a\n", + "print(\"Array a:\\n\", a)\n" ] }, { @@ -85,11 +263,40 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array 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", + "import numpy as np\n", + "\n", + "# Creating a 5x2x3 array with all values equal to 1\n", + "b = np.ones((5, 2, 3))\n", + "\n", + "# Printing the array b\n", + "print(\"Array b:\\n\", b)\n" ] }, { @@ -102,11 +309,38 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array 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", + "import numpy as np\n", + "\n", + "# Create the 5x2x3 array with all values equal to 1 and print it\n", + "print(\"Array b:\\n\", np.ones((5, 2, 3)))\n", + "\n" ] }, { @@ -119,11 +353,47 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Do a and b have the same size? False\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Generate the 2x3x5 array for a and the 5x2x3 array for b\n", + "a = np.random.rand(2, 3, 5) # Example array a\n", + "b = np.ones((5, 2, 3)) # Array b\n", + "\n", + "# Check if a and b have the same size\n", + "print(\"Do a and b have the same size?\", a.shape == b.shape)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Shape of a: (2, 3, 5)\n", + "Shape of b: (5, 2, 3)\n" + ] + } + ], + "source": [ + "print(\"Shape of a:\", a.shape)\n", + "print(\"Shape of b:\", b.shape)\n" ] }, { @@ -136,11 +406,32 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: operands could not be broadcast together with shapes (2,3,5) (5,2,3) \n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Create array a and b\n", + "a = np.random.rand(2, 3, 5) # Shape (2, 3, 5)\n", + "b = np.ones((5, 2, 3)) # Shape (5, 2, 3)\n", + "\n", + "# Check if the arrays can be added\n", + "try:\n", + " result = a + b\n", + " print(\"Arrays a and b can be added.\")\n", + "except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n" ] }, { @@ -154,11 +445,38 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transposed array c with shape (2, 3, 5)\n", + "[[[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", + "import numpy as np\n", + "\n", + "# Create array b with shape (5, 2, 3)\n", + "b = np.ones((5, 2, 3))\n", + "\n", + "# Transpose b to match the shape of a (2, 3, 5)\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "# Print the transposed array c\n", + "print(\"Transposed array c with shape\", c.shape)\n", + "print(c)\n", + "\n" ] }, { @@ -171,11 +489,44 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array d (sum of a and c):\n", + "[[[1.52551894 1.08728389 1.54746002 1.41482029 1.51715502]\n", + " [1.97925244 1.31717203 1.38683503 1.24537308 1.29130933]\n", + " [1.69259125 1.76429593 1.56471189 1.92576953 1.29484073]]\n", + "\n", + " [[1.63375537 1.56899756 1.57027333 1.23472388 1.23900893]\n", + " [1.36097084 1.9004949 1.88054783 1.69828641 1.22033657]\n", + " [1.19714167 1.4821865 1.96022631 1.132951 1.11032192]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Create array a with shape (2, 3, 5)\n", + "a = np.random.rand(2, 3, 5)\n", + "\n", + "# Create array b with shape (5, 2, 3)\n", + "b = np.ones((5, 2, 3))\n", + "\n", + "# Transpose b to match the shape of a (2, 3, 5)\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "# Add a and c\n", + "d = a + c\n", + "\n", + "# Print the result\n", + "print(\"Array d (sum of a and c):\")\n", + "print(d)\n", + "\n" ] }, { @@ -188,11 +539,56 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array a:\n", + "[[[0.0518145 0.77763945 0.15895751 0.34511432 0.87029493]\n", + " [0.9671362 0.15291426 0.96352046 0.74291333 0.93025035]\n", + " [0.55454039 0.67018775 0.89099883 0.6705905 0.09268978]]\n", + "\n", + " [[0.51032892 0.11495435 0.13574917 0.37161889 0.90603955]\n", + " [0.48841893 0.46976114 0.73826919 0.47130551 0.79173891]\n", + " [0.86037662 0.32463623 0.18428464 0.69325645 0.05285976]]]\n", + "\n", + "Array d (sum of a and c):\n", + "[[[1.0518145 1.77763945 1.15895751 1.34511432 1.87029493]\n", + " [1.9671362 1.15291426 1.96352046 1.74291333 1.93025035]\n", + " [1.55454039 1.67018775 1.89099883 1.6705905 1.09268978]]\n", + "\n", + " [[1.51032892 1.11495435 1.13574917 1.37161889 1.90603955]\n", + " [1.48841893 1.46976114 1.73826919 1.47130551 1.79173891]\n", + " [1.86037662 1.32463623 1.18428464 1.69325645 1.05285976]]]\n" + ] + } + ], "source": [ "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Create array a with shape (2, 3, 5)\n", + "a = np.random.rand(2, 3, 5)\n", + "\n", + "# Create array b with shape (5, 2, 3)\n", + "b = np.ones((5, 2, 3))\n", + "\n", + "# Transpose b to match the shape of a (2, 3, 5)\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "# Add a and c to get d\n", + "d = a + c\n", + "\n", + "# Print a and d\n", + "print(\"Array a:\")\n", + "print(a)\n", + "\n", + "print(\"\\nArray d (sum of a and c):\")\n", + "print(d)\n", + "\n", "\n" ] }, @@ -206,11 +602,68 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array a:\n", + "[[[0.59326016 0.13608674 0.12551519 0.08901926 0.32216637]\n", + " [0.67652318 0.62190144 0.47246425 0.15042165 0.10683963]\n", + " [0.1060509 0.47946997 0.02265766 0.05338122 0.09492863]]\n", + "\n", + " [[0.20344244 0.18151441 0.22034244 0.55202456 0.70269839]\n", + " [0.37641246 0.14954011 0.43849995 0.59792229 0.29861257]\n", + " [0.55737894 0.96486411 0.52905842 0.26630666 0.36676463]]]\n", + "\n", + "Array c:\n", + "[[[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", + "\n", + "Array e (result of a * c):\n", + "[[[0.59326016 0.13608674 0.12551519 0.08901926 0.32216637]\n", + " [0.67652318 0.62190144 0.47246425 0.15042165 0.10683963]\n", + " [0.1060509 0.47946997 0.02265766 0.05338122 0.09492863]]\n", + "\n", + " [[0.20344244 0.18151441 0.22034244 0.55202456 0.70269839]\n", + " [0.37641246 0.14954011 0.43849995 0.59792229 0.29861257]\n", + " [0.55737894 0.96486411 0.52905842 0.26630666 0.36676463]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Create array a with shape (2, 3, 5)\n", + "a = np.random.rand(2, 3, 5)\n", + "\n", + "# Create array b with shape (5, 2, 3)\n", + "b = np.ones((5, 2, 3))\n", + "\n", + "# Transpose b to match the shape of a (2, 3, 5)\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "# Multiply a and c to get e\n", + "e = a * c\n", + "\n", + "# Print the result\n", + "print(\"Array a:\")\n", + "print(a)\n", + "\n", + "print(\"\\nArray c:\")\n", + "print(c)\n", + "\n", + "print(\"\\nArray e (result of a * c):\")\n", + "print(e)\n", + "\n" ] }, { @@ -224,11 +677,34 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Are a and e equal? True\n" + ] + } + ], "source": [ "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Create a random array a\n", + "a = np.random.rand(2, 3, 5)\n", + "\n", + "# Create array b with shape (5, 2, 3) and transpose it to match a's shape\n", + "b = np.ones((5, 2, 3))\n", + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "# Multiply a and c\n", + "e = a * c\n", + "\n", + "# Check if a and e are equal\n", + "print(\"Are a and e equal?\", np.array_equal(a, e)) # This will return True\n", + "\n", "\n" ] }, @@ -243,11 +719,35 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max value in d: 1.9671362043497493\n", + "Min value in d: 1.0518145047556822\n", + "Mean value in d: 1.5317720270520463\n" + ] + } + ], "source": [ "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Assuming d has already been defined from previous steps\n", + "# Example: d = a + c (where a and c are arrays you worked with before)\n", + "\n", + "# Calculate max, min, and mean values in d\n", + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "# Print the results\n", + "print(f\"Max value in d: {d_max}\")\n", + "print(f\"Min value in d: {d_min}\")\n", + "print(f\"Mean value in d: {d_mean}\")\n", "\n" ] }, @@ -261,11 +761,34 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[None None None None None]\n", + " [None None None None None]\n", + " [None None None None None]]\n", + "\n", + " [[None None None None None]\n", + " [None None None None None]\n", + " [None None None None None]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Assuming d has already been defined with shape (2, 3, 5)\n", + "# Create an empty array \"f\" with the same shape as \"d\"\n", + "f = np.empty_like(d, dtype=object) # Use dtype=object since we'll store labels (strings)\n", + "\n", + "# Print f to see the initial empty array\n", + "print(f)\n", + "\n" ] }, { @@ -287,11 +810,58 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array f:\n", + "[[[25 25 75 25 25]\n", + " [75 25 25 75 25]\n", + " [75 25 75 75 75]]\n", + "\n", + " [[25 25 75 75 75]\n", + " [75 75 75 25 25]\n", + " [75 100 25 75 0]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "import numpy as np\n", + "\n", + "# Example arrays a, d_min, d_max, and d_mean\n", + "a = np.random.rand(2, 3, 5)\n", + "d = np.random.rand(2, 3, 5) * 2 # Random values for d as an example\n", + "d_min = np.min(d)\n", + "d_max = np.max(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "# Create an empty array \"f\" with the same shape as \"d\"\n", + "f = np.empty_like(d, dtype=object) # Using dtype=object to store labels as strings\n", + "\n", + "# Loop through each element in d and apply the conditions\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_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_max:\n", + " f[i, j, k] = 100\n", + " elif d_min < d[i, j, k] < d_mean:\n", + " f[i, j, k] = 25\n", + " elif d_mean < d[i, j, k] < d_max:\n", + " f[i, j, k] = 75\n", + "\n", + "# Print the resulting f\n", + "print(\"Array f:\")\n", + "print(f)\n", + "\n", + " \n", + "\n" ] }, { @@ -325,11 +895,70 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array 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", + "\n", + "Array f:\n", + "[[[ 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": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np\n", + "\n", + "# Example d array\n", + "d = 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", + "\n", + "# Find min, max, and mean\n", + "d_min = np.min(d)\n", + "d_max = np.max(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "# Create an empty array f with the same shape as d\n", + "f = np.empty_like(d, dtype=float) # Using float for numeric values\n", + "\n", + "# Populate the values in f based on the conditions in one loop\n", + "f[(d == d_min)] = 0\n", + "f[(d == d_mean)] = 50\n", + "f[(d == d_max)] = 100\n", + "f[(d > d_min) & (d < d_mean)] = 25\n", + "f[(d > d_mean) & (d < d_max)] = 75\n", + "\n", + "# Print d and f\n", + "print(\"Array d:\")\n", + "print(d)\n", + "\n", + "print(\"\\nArray f:\")\n", + "print(f)\n", + "\n", + "\n", + "\n" ] }, { @@ -350,11 +979,33 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ - "### [your code here]" + "### [your code here]\n", + "# Example d array\n", + "d = 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", + "\n", + "# Find min, max, and mean\n", + "d_min = np.min(d)\n", + "d_max = np.max(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "# Create an empty array f with the same shape as d but with string values\n", + "f = np.empty_like(d, dtype=object) # Using object for string values\n", + "\n", + "# Populate the values in f based on the conditions using strings\n", + "f[(d == d_min)] = \"A\"\n", + "f[(d == d_mean)] = \"C\"\n", + "f[(d == d_max)] = \"E\"\n", + "f[(d > d_min) & (d < d_mean)] = \"B\"\n" ] } ], @@ -374,7 +1025,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.9.6" } }, "nbformat": 4,