diff --git a/main.ipynb b/main.ipynb new file mode 100644 index 00000000..545bd52a --- /dev/null +++ b/main.ipynb @@ -0,0 +1,592 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lab | Numpy Deep Dive" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. Import the NUMPY package under the name np.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here]\n", + "import numpy\n", + "import numpy as np\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "### 2. Print the NUMPY version and the configuration.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My NUMPY version is: 2.1.3\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print('My NUMPY version is: ', np.__version__)" + ] + }, + { + "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?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here]\n", + "a = np.random.random((2, 3, 5))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 4. Print a.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.78330145 0.43360272 0.04289967 0.99208505 0.81832997]\n", + " [0.37737294 0.77315309 0.62543038 0.44735036 0.72192398]\n", + " [0.50183891 0.98692273 0.34168931 0.01570024 0.89183956]]\n", + "\n", + " [[0.52640029 0.61408117 0.67116489 0.99644353 0.85757116]\n", + " [0.10153289 0.57986599 0.35577033 0.86191948 0.85912848]\n", + " [0.27807579 0.06993381 0.77577063 0.08677677 0.1115856 ]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n", + "### Assign the array to variable \"b\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here]\n", + "b = np.ones((5, 2, 3), dtype = int)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 6. Print b.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": [ + "### [your code here]\n", + "print(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 7. Do a and b have the same size? How do you prove that in Python code?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "### [your code here]\n", + "len(a) == len(b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 8. Are you able to add a and b? Why or why not?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here]\n", + "\n", + "### No, because they have different shapes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "### 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" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "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": [ + "### [your code here]\n", + "c = b.transpose(1, 2, 0)\n", + "print(c)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 10. Try to add a and c. Now it should work. Assign the sum to variable \"d\". But why does it work now?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here]\n", + "d = a + c\n", + "\n", + "### Now it works because a and c have the same shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.78330145 0.43360272 0.04289967 0.99208505 0.81832997]\n", + " [0.37737294 0.77315309 0.62543038 0.44735036 0.72192398]\n", + " [0.50183891 0.98692273 0.34168931 0.01570024 0.89183956]]\n", + "\n", + " [[0.52640029 0.61408117 0.67116489 0.99644353 0.85757116]\n", + " [0.10153289 0.57986599 0.35577033 0.86191948 0.85912848]\n", + " [0.27807579 0.06993381 0.77577063 0.08677677 0.1115856 ]]]\n", + "[[[1.78330145 1.43360272 1.04289967 1.99208505 1.81832997]\n", + " [1.37737294 1.77315309 1.62543038 1.44735036 1.72192398]\n", + " [1.50183891 1.98692273 1.34168931 1.01570024 1.89183956]]\n", + "\n", + " [[1.52640029 1.61408117 1.67116489 1.99644353 1.85757116]\n", + " [1.10153289 1.57986599 1.35577033 1.86191948 1.85912848]\n", + " [1.27807579 1.06993381 1.77577063 1.08677677 1.1115856 ]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print(a)\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 12. Multiply a and c. Assign the result to e.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.78330145 0.43360272 0.04289967 0.99208505 0.81832997]\n", + " [0.37737294 0.77315309 0.62543038 0.44735036 0.72192398]\n", + " [0.50183891 0.98692273 0.34168931 0.01570024 0.89183956]]\n", + "\n", + " [[0.52640029 0.61408117 0.67116489 0.99644353 0.85757116]\n", + " [0.10153289 0.57986599 0.35577033 0.86191948 0.85912848]\n", + " [0.27807579 0.06993381 0.77577063 0.08677677 0.1115856 ]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "e = a*c\n", + "print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "### 13. Does e equal to a? Why or why not?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "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": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "### [your code here]\n", + "e == a\n", + "\n", + "### This happens because 'e' is the multiplication of 'a' and 'c', where 'c' is an array that contains only the number 1. when you multiply anything by one, it result in the same number." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "### 14. Identify the max, min, and mean values in d. Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max value in d: 1.996443527621794\n", + "Min value in d: 1.0157002428450579\n", + "Mean value in d: 1.5499820388922698\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean = d.mean()\n", + "\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}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 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" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here\n", + "f = np.empty([2, 3, 5])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\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", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "### [your code here]\n", + "f[(d > d_min) & (d < d_mean)] = 25\n", + "f[(d > d_mean) & (d < d_max)] = 75\n", + "f[d == d_mean] = 50\n", + "f[d == d_min] = 0\n", + "f[d == d_max] = 100" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "\n", + "\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", + "\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", + "Your f should be:\n", + "array([[[ 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", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.78330145 1.43360272 1.04289967 1.99208505 1.81832997]\n", + " [1.37737294 1.77315309 1.62543038 1.44735036 1.72192398]\n", + " [1.50183891 1.98692273 1.34168931 1.01570024 1.89183956]]\n", + "\n", + " [[1.52640029 1.61408117 1.67116489 1.99644353 1.85757116]\n", + " [1.10153289 1.57986599 1.35577033 1.86191948 1.85912848]\n", + " [1.27807579 1.06993381 1.77577063 1.08677677 1.1115856 ]]]\n", + "[[[ 75. 25. 25. 75. 75.]\n", + " [ 25. 75. 75. 25. 75.]\n", + " [ 25. 75. 25. 0. 75.]]\n", + "\n", + " [[ 25. 75. 75. 100. 75.]\n", + " [ 25. 75. 25. 75. 75.]\n", + " [ 25. 25. 75. 25. 25.]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print(d)\n", + "print(f)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 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", + "\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." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['B' 'A' 'A' 'B' 'B']\n", + " ['A' 'B' 'B' 'A' 'B']\n", + " ['A' 'B' 'A' 'D' 'B']]\n", + "\n", + " [['A' 'B' 'B' 'E' 'B']\n", + " ['A' 'B' 'A' 'B' 'B']\n", + " ['A' 'A' 'B' 'A' 'A']]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "\n", + "f_str = np.empty([2, 3, 5], dtype = str)\n", + "\n", + "f_str[(d > d_min) & (d < d_mean)] = 'A'\n", + "f_str[(d > d_mean) & (d < d_max)] = 'B'\n", + "f_str[d == d_mean] = 'C'\n", + "f_str[d == d_min] = 'D'\n", + "f_str[d == d_max] = 'E'\n", + "\n", + "print(f_str)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}