From ecc26ffd0c7ed65a33871390b20701c63ef5de39 Mon Sep 17 00:00:00 2001 From: foscanit Date: Tue, 17 Oct 2023 18:21:10 +0200 Subject: [PATCH] =?UTF-8?q?[Cl=C3=A0udia=20Pintos]=20lab-numpy=20in=20an?= =?UTF-8?q?=20ipnyb=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- your-code/main.ipynb | 842 +++++++++++++++++++++++++++++++++++++++++++ your-code/main.py | 6 +- 2 files changed, 846 insertions(+), 2 deletions(-) create mode 100644 your-code/main.ipynb diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 0000000..7e6fb0e --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,842 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. Import the NUMPY package under the name np." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: numpy in /Users/usuari/miniconda3/envs/ironhack/lib/python3.11/site-packages (1.26.1)\r\n" + ] + } + ], + "source": [ + "!pip install numpy\n", + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. Print the NUMPY version and the configuration." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "numpy version: 1.26.1\n", + "numpy configuration: \n", + "Build Dependencies:\n", + " blas:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: /usr/local/include\n", + " lib directory: /usr/local/lib\n", + " name: openblas64\n", + " openblas configuration: USE_64BITINT=1 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=\n", + " NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= SANDYBRIDGE MAX_THREADS=3\n", + " pc file directory: /usr/local/lib/pkgconfig\n", + " version: 0.3.23.dev\n", + " lapack:\n", + " detection method: internal\n", + " found: true\n", + " include directory: unknown\n", + " lib directory: unknown\n", + " name: dep4378818576\n", + " openblas configuration: unknown\n", + " pc file directory: unknown\n", + " version: 1.26.1\n", + "Compilers:\n", + " c:\n", + " commands: clang\n", + " linker: ld64\n", + " name: clang\n", + " version: 14.0.0\n", + " c++:\n", + " commands: clang++\n", + " linker: ld64\n", + " name: clang\n", + " version: 14.0.0\n", + " cython:\n", + " commands: cython\n", + " linker: cython\n", + " name: cython\n", + " version: 3.0.3\n", + "Machine Information:\n", + " build:\n", + " cpu: x86_64\n", + " endian: little\n", + " family: x86_64\n", + " system: darwin\n", + " host:\n", + " cpu: x86_64\n", + " endian: little\n", + " family: x86_64\n", + " system: darwin\n", + "Python Information:\n", + " path: /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/cibw-run-6zvpqdla/cp311-macosx_x86_64/build/venv/bin/python\n", + " version: '3.11'\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_KNL\n", + " - AVX512_SKX\n", + " - AVX512_CLX\n", + " - AVX512_CNL\n", + " - AVX512_ICL\n", + "\n" + ] + } + ], + "source": [ + "print(\"numpy version: \", np.__version__)\n", + "print(\"numpy configuration: \")\n", + "np.show_config()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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?" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# first way\n", + "a = np.random.randint(101, size = (2, 3, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. Print a." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 89 46 27 58 29]\n", + " [ 41 14 95 0 67]\n", + " [ 82 13 46 44 23]]\n", + "\n", + " [[ 47 25 29 28 100]\n", + " [ 67 94 82 26 82]\n", + " [ 32 48 46 25 18]]]\n" + ] + } + ], + "source": [ + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5. Create a 5x2x3 3-dimensional array with all values equaling 1.
\n", + "ssign the array to variable \"b\"" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [], + "source": [ + "b = np.random.randint(1, 2, size = (5, 2, 3))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6. Print b." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "7. Do a and b have the same size? How do you prove that in Python code?" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "30\n" + ] + } + ], + "source": [ + "# option 1\n", + "\n", + "print(a.size)\n", + "print(b.size)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# option 2\n", + "\n", + "a.size == b.size" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8. Are you able to add a and b? Why or why not?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "No, because they don't have the same shape." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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\"." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "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", + "[[[ 89 46 27 58 29]\n", + " [ 41 14 95 0 67]\n", + " [ 82 13 46 44 23]]\n", + "\n", + " [[ 47 25 29 28 100]\n", + " [ 67 94 82 26 82]\n", + " [ 32 48 46 25 18]]]\n" + ] + } + ], + "source": [ + "# I look at a, and put the positions of b.\n", + "\n", + "c = b.transpose(1, 2, 0)\n", + "print(c)\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "10. Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". But why does it work now?" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 90 47 28 59 30]\n", + " [ 42 15 96 1 68]\n", + " [ 83 14 47 45 24]]\n", + "\n", + " [[ 48 26 30 29 101]\n", + " [ 68 95 83 27 83]\n", + " [ 33 49 47 26 19]]]\n" + ] + } + ], + "source": [ + "d = a + c\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 89 46 27 58 29]\n", + " [ 41 14 95 0 67]\n", + " [ 82 13 46 44 23]]\n", + "\n", + " [[ 47 25 29 28 100]\n", + " [ 67 94 82 26 82]\n", + " [ 32 48 46 25 18]]]\n" + ] + } + ], + "source": [ + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 90 47 28 59 30]\n", + " [ 42 15 96 1 68]\n", + " [ 83 14 47 45 24]]\n", + "\n", + " [[ 48 26 30 29 101]\n", + " [ 68 95 83 27 83]\n", + " [ 33 49 47 26 19]]]\n" + ] + } + ], + "source": [ + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "12. Multiply a and c. Assign the result to e." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 89 46 27 58 29]\n", + " [ 41 14 95 0 67]\n", + " [ 82 13 46 44 23]]\n", + "\n", + " [[ 47 25 29 28 100]\n", + " [ 67 94 82 26 82]\n", + " [ 32 48 46 25 18]]]\n" + ] + } + ], + "source": [ + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 89 46 27 58 29]\n", + " [ 41 14 95 0 67]\n", + " [ 82 13 46 44 23]]\n", + "\n", + " [[ 47 25 29 28 100]\n", + " [ 67 94 82 26 82]\n", + " [ 32 48 46 25 18]]]\n" + ] + } + ], + "source": [ + "e = a * c\n", + "print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "13. Does e equal to a? Why or why not?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Yes, because al the elements of c are 1. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "14. Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\"" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "101\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "print(d_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "d_min = d.min()\n", + "print(d_min)" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "48.43333333333333\n" + ] + } + ], + "source": [ + "d_mean = d.mean()\n", + "print(d_mean)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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`." + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "f = np.empty((2, 3, 5), dtype=\"int\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "16.\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", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [], + "source": [ + "# solution to question 16\n", + "#option 1\n", + "\n", + "for i in range(d.shape[0]):\n", + " for x in range(d.shape[1]):\n", + " for z in range(d.shape[2]):\n", + " val = d[i][x][z]\n", + " if val > d_min and val < d_mean:\n", + " f[i][x][z] = 25\n", + " elif val > d_mean and val < d_max:\n", + " f[i][x][z] = 75\n", + " elif val == d_mean:\n", + " f[i][x][z] = 50\n", + " elif val == d_min:\n", + " f[i][x][z] = 0\n", + " elif val == d_max:\n", + " f[i][x][z] = 100" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75 25 25 75 25]\n", + " [ 25 25 75 0 75]\n", + " [ 75 25 25 25 25]]\n", + "\n", + " [[ 25 25 25 25 100]\n", + " [ 75 75 75 25 75]\n", + " [ 25 75 25 25 25]]]\n" + ] + } + ], + "source": [ + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [], + "source": [ + "# solution to question 16\n", + "#option 2\n", + "\n", + "for i in range(2):\n", + " for x in range(3):\n", + " for z in range(5):\n", + " val = d[i][x][z]\n", + " if val > d_min and val < d_mean:\n", + " f[i][x][z] = 25\n", + " elif val > d_mean and val < d_max:\n", + " f[i][x][z] = 75\n", + " elif val == d_mean:\n", + " f[i][x][z] = 50\n", + " elif val == d_min:\n", + " f[i][x][z] = 0\n", + " elif val == d_max:\n", + " f[i][x][z] = 100" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "17.\n", + "
\n", + "#17. Print d and f. Do you have your expected f?
\n", + "For instance, if your d is:
\n", + "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", + " [[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", + "Your f should be:
\n", + "array([[[ 75., 75., 75., 25., 75.],
\n", + " [ 75., 75., 25., 25., 25.],
\n", + " [ 75., 25., 75., 75., 75.]],
\n", + " [[ 25., 25., 25., 25., 100.],
\n", + " [ 75., 75., 75., 75., 75.],
\n", + " [ 25., 75., 0., 75., 75.]]])
\n" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 90 47 28 59 30]\n", + " [ 42 15 96 1 68]\n", + " [ 83 14 47 45 24]]\n", + "\n", + " [[ 48 26 30 29 101]\n", + " [ 68 95 83 27 83]\n", + " [ 33 49 47 26 19]]]\n" + ] + } + ], + "source": [ + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 75 25 25 75 25]\n", + " [ 25 25 75 0 75]\n", + " [ 75 25 25 25 25]]\n", + "\n", + " [[ 25 25 25 25 100]\n", + " [ 75 75 75 25 75]\n", + " [ 25 75 25 25 25]]]\n" + ] + } + ], + "source": [ + "print(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "18.\n", + "
\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? You are expecting the result to be:
\n", + "array([[[ 'D', 'D', 'D', 'B', 'D'],
\n", + " [ 'D', 'D', 'B', 'B', 'B'],
\n", + " [ 'D', 'B', 'D', 'D', 'D']],
\n", + " [[ 'B', 'B', 'B', 'B', 'E'],
\n", + " [ 'D', 'D', 'D', 'D', 'D'],
\n", + " [ 'B', 'D', 'A', 'D', 'D']]])
\n", + "Again, you don't need Numpy in this question.
\n" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [], + "source": [ + "e = np.empty((2, 3, 5), dtype=str)" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [], + "source": [ + "em_dict = {0:\"A\", 25:\"B\", 50:\"C\", 75:\"D\", 100:\"E\"}" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(e.shape[0]):\n", + " for x in range(e.shape[1]):\n", + " for z in range(e.shape[2]):\n", + " val = d[i][x][z]\n", + " if val > d_min and val < d_mean:\n", + " e[i][x][z] = em_dict[25]\n", + " elif val > d_mean and val < d_max:\n", + " e[i][x][z] = em_dict[75]\n", + " elif val == d_mean:\n", + " e[i][x][z] = em_dict[50]\n", + " elif val == d_min:\n", + " e[i][x][z] = em_dict[0]\n", + " elif val == d_max:\n", + " e[i][x][z] = em_dict[100] " + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['D' 'B' 'B' 'D' 'B']\n", + " ['B' 'B' 'D' 'A' 'D']\n", + " ['D' 'B' 'B' 'B' 'B']]\n", + "\n", + " [['B' 'B' 'B' 'B' 'E']\n", + " ['D' 'D' 'D' 'B' 'D']\n", + " ['B' 'D' 'B' 'B' 'B']]]\n" + ] + } + ], + "source": [ + "print(e)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.py b/your-code/main.py index 78c792b..f43b0db 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,9 +1,11 @@ #1. Import the NUMPY package under the name np. - +import numpy as np #2. Print the NUMPY version and the configuration. - +print("numpy version: ", np.__version__) +print("numpy configuration: ") +np.show_config() #3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable "a"