diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa14..2e69e1a1 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,11 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 92, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "import numpy as np\n" ] }, { @@ -34,11 +34,90 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Build Dependencies:\n", + " blas:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: C:/Users/black/anaconda3/Library/include\n", + " lib directory: C:/Users/black/anaconda3/Library/lib\n", + " name: mkl-sdl\n", + " openblas configuration: unknown\n", + " pc file directory: C:\\b\\abs_c1ywpu18ar\\croot\\numpy_and_numpy_base_1708638681471\\_h_env\\Library\\lib\\pkgconfig\n", + " version: '2023.1'\n", + " lapack:\n", + " detection method: internal\n", + " found: true\n", + " include directory: unknown\n", + " lib directory: unknown\n", + " name: dep1583119523984\n", + " openblas configuration: unknown\n", + " pc file directory: unknown\n", + " version: 1.26.4\n", + "Compilers:\n", + " c:\n", + " commands: cl.exe\n", + " linker: link\n", + " name: msvc\n", + " version: 19.29.30154\n", + " c++:\n", + " commands: cl.exe\n", + " linker: link\n", + " name: msvc\n", + " version: 19.29.30154\n", + " cython:\n", + " commands: cython\n", + " linker: cython\n", + " name: cython\n", + " version: 3.0.8\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:\\b\\abs_c1ywpu18ar\\croot\\numpy_and_numpy_base_1708638681471\\_h_env\\python.exe\n", + " version: '3.12'\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", + "1.26.4 None\n" + ] + } + ], + "source": [ + "print(np.__version__, np.show_config())\n" ] }, { @@ -51,11 +130,77 @@ }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0 2 6 2 9]\n", + " [8 4 0 5 2]\n", + " [1 5 7 1 9]]\n", + "\n", + " [[5 3 9 8 0]\n", + " [0 2 9 3 4]\n", + " [9 9 0 5 4]]]\n" + ] + } + ], + "source": [ + "a = np.random.randint(10, size=(2,3,5))\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[-0.65594566 0.61110811 -0.8747299 0.26462819 -1.22039634]\n", + " [-0.57931643 -0.27990516 0.11231808 2.08666773 1.1816648 ]\n", + " [-1.70864461 0.65431402 0.20303076 0.47044393 -0.22062924]]\n", + "\n", + " [[ 0.22390359 -2.359598 0.6001545 -0.61251093 0.44866037]\n", + " [-0.99946652 -0.08874024 0.30731537 0.01789873 0.27977288]\n", + " [-0.53600122 -0.1402848 0.52123248 0.3990963 1.21883166]]]\n" + ] + } + ], + "source": [ + "a = np.random.randn(2, 3, 5)\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.87959623 0.96562183 0.16376637 0.01670297 0.68482418]\n", + " [0.35335704 0.67209734 0.66468047 0.12338724 0.32832676]\n", + " [0.66163619 0.90027189 0.83893079 0.61432874 0.81872485]]\n", + "\n", + " [[0.9013533 0.39357404 0.09042541 0.2406817 0.06953732]\n", + " [0.17750684 0.35635412 0.87496882 0.62505625 0.01872674]\n", + " [0.92180136 0.07202439 0.6725766 0.71102093 0.9752847 ]]]\n" + ] + } + ], + "source": [ + "a = np.random.random((2, 3, 5))\n", + "\n", + "print(a)\n" ] }, { @@ -68,11 +213,25 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 104, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.87959623 0.96562183 0.16376637 0.01670297 0.68482418]\n", + " [0.35335704 0.67209734 0.66468047 0.12338724 0.32832676]\n", + " [0.66163619 0.90027189 0.83893079 0.61432874 0.81872485]]\n", + "\n", + " [[0.9013533 0.39357404 0.09042541 0.2406817 0.06953732]\n", + " [0.17750684 0.35635412 0.87496882 0.62505625 0.01872674]\n", + " [0.92180136 0.07202439 0.6725766 0.71102093 0.9752847 ]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "print(a)\n" ] }, { @@ -85,11 +244,34 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 105, + "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": [ + "b = np.random.randint(1, 2, size=(5,2,3))\n", + "\n", + "print(b)\n" ] }, { @@ -102,11 +284,32 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 106, + "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 +322,21 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(a.size == b.size)\n", + "\n", + "# checks, if there is the same total of elements (2*3*5 = 30; numbers interchangable, as \"*\")\n" ] }, { @@ -136,11 +349,25 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 110, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[110]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43madd\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3\u001b[39m \u001b[38;5;66;03m# it is not possible to combine the arrays a & b, as they have to have the same axis\u001b[39;00m\n", + "\u001b[31mValueError\u001b[39m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "np.add(a, b)\n", + "\n", + "# it is not possible to combine the arrays a & b, as they have to have the same axis\n" ] }, { @@ -154,11 +381,28 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[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": [ + "c = np.transpose(b, (1, 2, 0))\n", + "\n", + "print(c)\n", + "\n" ] }, { @@ -171,11 +415,29 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.87959623 1.96562183 1.16376637 1.01670297 1.68482418]\n", + " [1.35335704 1.67209734 1.66468047 1.12338724 1.32832676]\n", + " [1.66163619 1.90027189 1.83893079 1.61432874 1.81872485]]\n", + "\n", + " [[1.9013533 1.39357404 1.09042541 1.2406817 1.06953732]\n", + " [1.17750684 1.35635412 1.87496882 1.62505625 1.01872674]\n", + " [1.92180136 1.07202439 1.6725766 1.71102093 1.9752847 ]]]\n" + ] + } + ], + "source": [ + "d = np.add(a, c)\n", + "\n", + "print(d)\n", + "\n", + "# as the shapes are after transposing c the same of both arrays the same, we can use simple multiplication" ] }, { @@ -188,12 +450,38 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", - "\n" + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Array a:\n", + " [[[0.87959623 0.96562183 0.16376637 0.01670297 0.68482418]\n", + " [0.35335704 0.67209734 0.66468047 0.12338724 0.32832676]\n", + " [0.66163619 0.90027189 0.83893079 0.61432874 0.81872485]]\n", + "\n", + " [[0.9013533 0.39357404 0.09042541 0.2406817 0.06953732]\n", + " [0.17750684 0.35635412 0.87496882 0.62505625 0.01872674]\n", + " [0.92180136 0.07202439 0.6725766 0.71102093 0.9752847 ]]] \n", + "\n", + "Array d:\n", + " [[[1.87959623 1.96562183 1.16376637 1.01670297 1.68482418]\n", + " [1.35335704 1.67209734 1.66468047 1.12338724 1.32832676]\n", + " [1.66163619 1.90027189 1.83893079 1.61432874 1.81872485]]\n", + "\n", + " [[1.9013533 1.39357404 1.09042541 1.2406817 1.06953732]\n", + " [1.17750684 1.35635412 1.87496882 1.62505625 1.01872674]\n", + " [1.92180136 1.07202439 1.6725766 1.71102093 1.9752847 ]]]\n" + ] + } + ], + "source": [ + "print(\"\\nArray a:\\n\", a, \"\\n\\nArray d:\\n\", d)\n", + "\n", + "# array a is the originially defined array and d is the array of a with each element being added by the value 1 (from c)\n" ] }, { @@ -206,11 +494,51 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "a:\n", + " [[[0.87959623 0.96562183 0.16376637 0.01670297 0.68482418]\n", + " [0.35335704 0.67209734 0.66468047 0.12338724 0.32832676]\n", + " [0.66163619 0.90027189 0.83893079 0.61432874 0.81872485]]\n", + "\n", + " [[0.9013533 0.39357404 0.09042541 0.2406817 0.06953732]\n", + " [0.17750684 0.35635412 0.87496882 0.62505625 0.01872674]\n", + " [0.92180136 0.07202439 0.6725766 0.71102093 0.9752847 ]]]\n", + "\n", + "b:\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", + "e:\n", + " [[[0.87959623 0.96562183 0.16376637 0.01670297 0.68482418]\n", + " [0.35335704 0.67209734 0.66468047 0.12338724 0.32832676]\n", + " [0.66163619 0.90027189 0.83893079 0.61432874 0.81872485]]\n", + "\n", + " [[0.9013533 0.39357404 0.09042541 0.2406817 0.06953732]\n", + " [0.17750684 0.35635412 0.87496882 0.62505625 0.01872674]\n", + " [0.92180136 0.07202439 0.6725766 0.71102093 0.9752847 ]]]\n" + ] + } + ], + "source": [ + "print(\"\\na:\\n\", a)\n", + "print(\"\\nb:\\n\", c)\n", + "\n", + "\n", + "e = np.multiply(a, c)\n", + "\n", + "print(\"\\ne:\\n\", e)\n" ] }, { @@ -224,11 +552,30 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", + "execution_count": 115, + "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": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == e\n", + "\n", + "# yes, e equals to a, as we multiplied each value of a with each value of e and all elements of e are set to 1\n", "\n" ] }, @@ -243,11 +590,23 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n", + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "max value of d: 1.9752847008233076\n", + "min value of d: 1.0167029659821756\n", + "mean value of d: 1.5262381806595848\n" + ] + } + ], + "source": [ + "d_max = np.max(d); print(\"max value of d:\", d_max)\n", + "d_min = np.min(d); print(\"min value of d:\", d_min)\n", + "d_mean = np.mean(d); print(\"mean value of d:\", d_mean)\n", "\n" ] }, @@ -261,11 +620,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 117, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "f = np.empty((2, 3, 5))\n" ] }, { @@ -275,11 +634,14 @@ "\n", "\n", "\n", - "### 16. Populate the values in f. For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.\n", - "If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.\n", - "If a value equals to d_mean, assign 50 to the corresponding value in f.\n", - "Assign 0 to the corresponding value(s) in f for d_min in d.\n", - "Assign 100 to the corresponding value(s) in f for d_max in d.\n", + "### 16. Populate the values in f. \n", + "\n", + "- For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.\n", + "- If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.\n", + "- If a value equals to d_mean, assign 50 to the corresponding value in f.\n", + "- Assign 0 to the corresponding value(s) in f for d_min in d.\n", + "- Assign 100 to the corresponding value(s) in f for d_max in d.\n", + "\n", "In the end, f should have only the following values: 0, 25, 50, 75, and 100.\n", "Note: you don't have to use Numpy in this question.\n", "\n" @@ -287,11 +649,34 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": 120, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n", + "(2, 3, 5)\n" + ] + } + ], + "source": [ + "print(d.shape)\n", + "print(f.shape)\n", + "\n", + "for index in np.ndindex(d.shape):\n", + " element = d[index] \n", + " if d_mean > element > d_min:\n", + " f[index] = 25\n", + " elif d_mean < element < d_max:\n", + " f[index] = 75\n", + " elif element == d_mean:\n", + " f[index] = 50\n", + " elif element == d_min:\n", + " f[index] = 0\n", + " elif element == d_max:\n", + " f[index] = 100" ] }, { @@ -325,11 +710,44 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "### [your code here]\n" + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.5262381806595848\n", + "1.9752847008233076\n", + "1.0167029659821756\n", + "[[[1.87959623 1.96562183 1.16376637 1.01670297 1.68482418]\n", + " [1.35335704 1.67209734 1.66468047 1.12338724 1.32832676]\n", + " [1.66163619 1.90027189 1.83893079 1.61432874 1.81872485]]\n", + "\n", + " [[1.9013533 1.39357404 1.09042541 1.2406817 1.06953732]\n", + " [1.17750684 1.35635412 1.87496882 1.62505625 1.01872674]\n", + " [1.92180136 1.07202439 1.6725766 1.71102093 1.9752847 ]]]\n", + "[[[ 75. 75. 25. 0. 75.]\n", + " [ 25. 75. 75. 25. 25.]\n", + " [ 75. 75. 75. 75. 75.]]\n", + "\n", + " [[ 75. 25. 25. 25. 25.]\n", + " [ 25. 25. 75. 75. 25.]\n", + " [ 75. 25. 75. 75. 100.]]]\n" + ] + } + ], + "source": [ + "print(d_mean)\n", + "print(d_max)\n", + "print(d_min)\n", + "\n", + "\n", + "print(d)\n", + "\n", + "print(f)\n", + "\n", + "#yes, comparing to the values of d: mean, min, max, the expected new values are correctly populated in f\n" ] }, { @@ -350,19 +768,60 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 132, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['D' 'D' 'B' 'A' 'D']\n", + " ['B' 'D' 'D' 'B' 'B']\n", + " ['D' 'D' 'D' 'D' 'D']]\n", + "\n", + " [['D' 'B' 'B' 'B' 'B']\n", + " ['B' 'B' 'D' 'D' 'B']\n", + " ['D' 'B' 'D' 'D' 'E']]]\n" + ] + } + ], + "source": [ + "#d_str = d.astype(str)\n", + "#d_mean_str = d_mean.astype(str)\n", + "#d_min_str = d_min.astype(str)\n", + "#d_max_str = d_max.astype(str)\n", + "\n", + "f = np.empty(d.shape, dtype=str)\n", + "\n", + "for index in np.ndindex(d.shape):\n", + " element = d[index] \n", + " if element == d_min:\n", + " f[index] = \"A\"\n", + " elif element == d_max:\n", + " f[index] = \"E\"\n", + " elif element == d_mean:\n", + " f[index] = \"C\"\n", + " elif d_min < element < d_mean:\n", + " f[index] = \"B\"\n", + " elif d_mean < element < d_max:\n", + " f[index] = \"D\"\n", + "\n", + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "### [your code here]" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python (Ironhack)", "language": "python", - "name": "python3" + "name": "ironhack" }, "language_info": { "codemirror_mode": { @@ -374,7 +833,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.12.7" } }, "nbformat": 4,