diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 0000000..32928f6 --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,454 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 906, + "metadata": {}, + "outputs": [], + "source": [ + "# 1. Import the NUMPY package under the name np.\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 907, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "version: 1.24.1\n", + "--\n", + "openblas64__info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['openblas\\\\lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]\n", + " runtime_library_dirs = ['openblas\\\\lib']\n", + "blas_ilp64_opt_info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['openblas\\\\lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]\n", + " runtime_library_dirs = ['openblas\\\\lib']\n", + "openblas64__lapack_info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['openblas\\\\lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]\n", + " runtime_library_dirs = ['openblas\\\\lib']\n", + "lapack_ilp64_opt_info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['openblas\\\\lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]\n", + " runtime_library_dirs = ['openblas\\\\lib']\n", + "Supported SIMD extensions in this NumPy install:\n", + " baseline = SSE,SSE2,SSE3\n", + " found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2,AVX512F,AVX512CD,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL\n", + " not found = \n" + ] + } + ], + "source": [ + "# 2. Print the NUMPY version and the configuration.\n", + "print(\"version:\", np.version.version)\n", + "print(\"--\")\n", + "np. show_config()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 908, + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable \"a\"\n", + "# Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find?\n", + "\n", + "a = np.random.random((2, 3, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 909, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.57485532, 0.88090983, 0.55910758, 0.42921281, 0.3631501 ],\n", + " [0.6674474 , 0.65087378, 0.24494241, 0.41568877, 0.5675643 ],\n", + " [0.5197136 , 0.43954487, 0.77769719, 0.31542534, 0.36488613]],\n", + "\n", + " [[0.04350651, 0.32742911, 0.04886006, 0.75453739, 0.74074782],\n", + " [0.09702833, 0.90102115, 0.43271407, 0.51671031, 0.23660598],\n", + " [0.95918773, 0.36345007, 0.20740672, 0.93400986, 0.43963275]]])" + ] + }, + "execution_count": 909, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 4. Print a.\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 910, + "metadata": {}, + "outputs": [], + "source": [ + "# 5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n", + "#Assign the array to variable \"b\"\n", + "\n", + "b = np.ones(((5, 2, 3)), dtype=int)" + ] + }, + { + "cell_type": "code", + "execution_count": 911, + "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": 911, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 6. Print b.\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 912, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 912, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 7. Do a and b have the same size? How do you prove that in Python code?\n", + "a.size == b.size" + ] + }, + { + "cell_type": "code", + "execution_count": 913, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You can't because although having the same sizes, the shapes of each array are not the same.\n" + ] + } + ], + "source": [ + "# 8. Are you able to add a and b? Why or why not?\n", + "try:\n", + " a + b\n", + "except ValueError:\n", + " print(\"You can't because although having the same sizes, the shapes of each array are not the same.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 914, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n", + "(5, 2, 3)\n" + ] + } + ], + "source": [ + "# 9. Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). Assign the transposed array to varialbe \"c\".\n", + "print(a.shape)\n", + "print(b.shape)\n", + "\n", + "c = b.transpose(1, 2, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 915, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.57485532, 1.88090983, 1.55910758, 1.42921281, 1.3631501 ],\n", + " [1.6674474 , 1.65087378, 1.24494241, 1.41568877, 1.5675643 ],\n", + " [1.5197136 , 1.43954487, 1.77769719, 1.31542534, 1.36488613]],\n", + "\n", + " [[1.04350651, 1.32742911, 1.04886006, 1.75453739, 1.74074782],\n", + " [1.09702833, 1.90102115, 1.43271407, 1.51671031, 1.23660598],\n", + " [1.95918773, 1.36345007, 1.20740672, 1.93400986, 1.43963275]]])" + ] + }, + "execution_count": 915, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 10. Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". But why does it work now?\n", + "# Now it can't be printed because the shapes of both array are the same and therefore it can be done.\n", + "d = a + c\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": 916, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.57485532 0.88090983 0.55910758 0.42921281 0.3631501 ]\n", + " [0.6674474 0.65087378 0.24494241 0.41568877 0.5675643 ]\n", + " [0.5197136 0.43954487 0.77769719 0.31542534 0.36488613]]\n", + "\n", + " [[0.04350651 0.32742911 0.04886006 0.75453739 0.74074782]\n", + " [0.09702833 0.90102115 0.43271407 0.51671031 0.23660598]\n", + " [0.95918773 0.36345007 0.20740672 0.93400986 0.43963275]]]\n", + "--\n", + "[[[1.57485532 1.88090983 1.55910758 1.42921281 1.3631501 ]\n", + " [1.6674474 1.65087378 1.24494241 1.41568877 1.5675643 ]\n", + " [1.5197136 1.43954487 1.77769719 1.31542534 1.36488613]]\n", + "\n", + " [[1.04350651 1.32742911 1.04886006 1.75453739 1.74074782]\n", + " [1.09702833 1.90102115 1.43271407 1.51671031 1.23660598]\n", + " [1.95918773 1.36345007 1.20740672 1.93400986 1.43963275]]]\n" + ] + } + ], + "source": [ + "# 11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.\n", + "# Array d's values are array a's values plus 1, which are the values from the b/c array that we were working on before.\n", + "print(a)\n", + "print(\"--\")\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 917, + "metadata": {}, + "outputs": [], + "source": [ + "# 12. Multiply a and c. Assign the result to e.\n", + "e = a * c" + ] + }, + { + "cell_type": "code", + "execution_count": 918, + "metadata": {}, + "outputs": [], + "source": [ + "# 13. Does e equal to a? Why or why not?\n", + "# e does equal a because we just multiplied all values of array a by 1 (all values of array c)." + ] + }, + { + "cell_type": "code", + "execution_count": 919, + "metadata": {}, + "outputs": [], + "source": [ + "#14. Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\".\n", + "d_max = np.amax(d)\n", + "d_min = np.amin(d)\n", + "d_mean = np.mean(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 920, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.5271177 , 0.95455118, 0.20294793, 0.40347215, 0.08269049],\n", + " [0.10321868, 0.005264 , 0.19727896, 0.45559368, 0.81141968],\n", + " [0.33009072, 0.99484183, 0.0238141 , 0.79810465, 0.16384766]],\n", + "\n", + " [[0.8117101 , 0.11536968, 0.86147976, 0.73294066, 0.80734859],\n", + " [0.99662125, 0.83537524, 0.35172852, 0.41894545, 0.23487772],\n", + " [0.72032889, 0.58746752, 0.08823937, 0.98245729, 0.18600065]]])" + ] + }, + "execution_count": 920, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#15. Now we want to label the values in d. First create an empty array \"f\" with the same shape (i.e. 2x3x5) as d using `np.empty`.\n", + "f = np.empty((2,3,5))\n", + "f" + ] + }, + { + "cell_type": "code", + "execution_count": 921, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\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", + "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", + "\n", + "for i in range(len(d)):\n", + " for j in range(len(d[i])):\n", + " for k in range(len(d[i][j])):\n", + " if d[i][j][k] > d_min and d[i][j][k] < d_mean:\n", + " f[i][j][k] = 25\n", + " elif d[i][j][k] > d_mean and d[i][j][k] < d_max:\n", + " f[i][j][k] = 75\n", + " elif d[i][j][k] == d_mean:\n", + " f[i][j][k] = 50\n", + " elif d[i][j][k] == d_min:\n", + " f[i][j][k] = 0\n", + " elif d[i][j][k] == d_max:\n", + " f[i][j][k] = 100" + ] + }, + { + "cell_type": "code", + "execution_count": 922, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.57485532 1.88090983 1.55910758 1.42921281 1.3631501 ]\n", + " [1.6674474 1.65087378 1.24494241 1.41568877 1.5675643 ]\n", + " [1.5197136 1.43954487 1.77769719 1.31542534 1.36488613]]\n", + "\n", + " [[1.04350651 1.32742911 1.04886006 1.75453739 1.74074782]\n", + " [1.09702833 1.90102115 1.43271407 1.51671031 1.23660598]\n", + " [1.95918773 1.36345007 1.20740672 1.93400986 1.43963275]]]\n", + "--\n", + "[[[ 75. 75. 75. 25. 25.]\n", + " [ 75. 75. 25. 25. 75.]\n", + " [ 75. 25. 75. 25. 25.]]\n", + "\n", + " [[ 0. 25. 25. 75. 75.]\n", + " [ 25. 75. 25. 75. 25.]\n", + " [100. 25. 25. 75. 25.]]]\n" + ] + } + ], + "source": [ + "\"\"\"\n", + "# 17. Print d and f.\n", + "\"\"\"\n", + "\n", + "print(d)\n", + "print(\"--\")\n", + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 924, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "#18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), how to use string values \n", + "(\"A\", \"B\", \"C\", \"D\", and \"E\") to label the array elements?\n", + "\"\"\"\n", + "\n", + "x = [[[0, 0, 0, 0, 0],[0, 0, 0, 0, 0],[0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]]\n", + "y = np.zeros((2,3,5))\n", + "\n", + "for i in range(len(d)):\n", + " for j in range(len(d[i])):\n", + " for k in range(len(d[i][j])):\n", + " if d[i][j][k] > d_min and d[i][j][k] < d_mean:\n", + " x[i][j][k] = \"B\"\n", + " elif d[i][j][k] > d_mean and d[i][j][k] < d_max:\n", + " x[i][j][k] = \"D\"\n", + " elif d[i][j][k] == d_mean:\n", + " x[i][j][k] = \"C\"\n", + " elif d[i][j][k] == d_min:\n", + " x[i][j][k] = \"A\"\n", + " elif d[i][j][k] == d_max:\n", + " x[i][j][k] = \"E\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}