diff --git a/your-code/LABS_Introduction_to_Numpy_Matheus_Freire.ipynb b/your-code/LABS_Introduction_to_Numpy_Matheus_Freire.ipynb new file mode 100644 index 0000000..2d70166 --- /dev/null +++ b/your-code/LABS_Introduction_to_Numpy_Matheus_Freire.ipynb @@ -0,0 +1,650 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Intrduction to NumPy\n", + "\n", + "\n", + "#### 1. Import NumPy under the name np." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Print your NumPy version." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1.21.5'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.version.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?" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.26347749, 0.07828423, 0.29862839, 0.15366829, 0.67417001],\n", + " [0.34767638, 0.0771898 , 0.74369082, 0.71208482, 0.07304296],\n", + " [0.80166138, 0.51339036, 0.27318251, 0.94149311, 0.2287255 ]],\n", + "\n", + " [[0.44365861, 0.68199016, 0.31989072, 0.080943 , 0.24966301],\n", + " [0.06577791, 0.62012891, 0.38803027, 0.36833548, 0.65197115],\n", + " [0.46933885, 0.5092976 , 0.91005934, 0.14236737, 0.32877812]]])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_array = np.random.random(size = (2,3,5))\n", + "new_array" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.8443671 , 0.37115011, 0.72160647, 0.84061845, 0.68822913],\n", + " [0.14657494, 0.17429454, 0.08657985, 0.76531292, 0.37811503],\n", + " [0.07561252, 0.55756172, 0.70778312, 0.07695481, 0.11313757]],\n", + "\n", + " [[0.61894859, 0.68908519, 0.42498373, 0.24688767, 0.55114211],\n", + " [0.28343154, 0.64322114, 0.60777954, 0.83844945, 0.10884731],\n", + " [0.65708967, 0.24029868, 0.41412779, 0.56291148, 0.74506422]]])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_array2 = np.random.sample(size = (2,3,5))\n", + "new_array2" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[-1.34436793e+00, 8.24952585e-01, -1.03283808e+00,\n", + " 2.79800777e-01, 2.25766087e-01],\n", + " [ 1.27668394e-01, -1.40425921e+00, 1.13214400e-01,\n", + " -1.45324159e+00, -5.78822754e-01],\n", + " [-1.54466005e+00, 8.58855420e-02, 8.07042019e-01,\n", + " 3.06952146e-04, -7.02806791e-01]],\n", + "\n", + " [[ 2.45751156e+00, -3.94415283e-01, 2.58248924e-01,\n", + " 9.10660843e-01, 8.32247982e-01],\n", + " [-1.21853552e-01, -4.17641844e-01, -1.35387833e+00,\n", + " -1.73496154e+00, 2.78136970e-01],\n", + " [-6.31571207e-01, 1.93843965e+00, 6.69909763e-01,\n", + " -9.45070802e-02, -1.52779907e+00]]])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_array3 = np.random.randn(2,3,5)\n", + "new_array3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print *a*.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.26347749 0.07828423 0.29862839 0.15366829 0.67417001]\n", + " [0.34767638 0.0771898 0.74369082 0.71208482 0.07304296]\n", + " [0.80166138 0.51339036 0.27318251 0.94149311 0.2287255 ]]\n", + "\n", + " [[0.44365861 0.68199016 0.31989072 0.080943 0.24966301]\n", + " [0.06577791 0.62012891 0.38803027 0.36833548 0.65197115]\n", + " [0.46933885 0.5092976 0.91005934 0.14236737 0.32877812]]]\n" + ] + } + ], + "source": [ + "a = new_array\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Create a 5x2x3 3-dimensional array with all values equaling 1. Assign the array to variable *b*." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "b = np.ones(shape = (5,2,3), dtype = int)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Print *b*.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "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": 29, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Matheus Freire\\AppData\\Local\\Temp\\ipykernel_7564\\160026346.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.\n", + " a == b\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 8. Are you able to add *a* and *b*? Why or why not?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_7564\\1216668022.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "a + b\n", + "\n", + "# You cannot add them 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 variable *c*." + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]]])" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = b.transpose()\n", + "c" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 10. Try to add *a* and *c*. Now it should work. Assign the sum to variable *d*. But why does it work now?" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.26347749, 1.07828423, 1.29862839, 1.15366829, 1.67417001],\n", + " [1.34767638, 1.0771898 , 1.74369082, 1.71208482, 1.07304296],\n", + " [1.80166138, 1.51339036, 1.27318251, 1.94149311, 1.2287255 ]],\n", + "\n", + " [[1.44365861, 1.68199016, 1.31989072, 1.080943 , 1.24966301],\n", + " [1.06577791, 1.62012891, 1.38803027, 1.36833548, 1.65197115],\n", + " [1.46933885, 1.5092976 , 1.91005934, 1.14236737, 1.32877812]]])" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c = c.reshape(2,3,5)\n", + "d = a + c\n", + "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": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.26347749 0.07828423 0.29862839 0.15366829 0.67417001]\n", + " [0.34767638 0.0771898 0.74369082 0.71208482 0.07304296]\n", + " [0.80166138 0.51339036 0.27318251 0.94149311 0.2287255 ]]\n", + "\n", + " [[0.44365861 0.68199016 0.31989072 0.080943 0.24966301]\n", + " [0.06577791 0.62012891 0.38803027 0.36833548 0.65197115]\n", + " [0.46933885 0.5092976 0.91005934 0.14236737 0.32877812]]]\n", + "[[[1.26347749 1.07828423 1.29862839 1.15366829 1.67417001]\n", + " [1.34767638 1.0771898 1.74369082 1.71208482 1.07304296]\n", + " [1.80166138 1.51339036 1.27318251 1.94149311 1.2287255 ]]\n", + "\n", + " [[1.44365861 1.68199016 1.31989072 1.080943 1.24966301]\n", + " [1.06577791 1.62012891 1.38803027 1.36833548 1.65197115]\n", + " [1.46933885 1.5092976 1.91005934 1.14236737 1.32877812]]]\n" + ] + } + ], + "source": [ + "print(a)\n", + "print(d)\n", + "\n", + "# they're different because we added variable c to d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 12. Multiply *a* and *c*. Assign the result to *e*." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.26347749, 0.07828423, 0.29862839, 0.15366829, 0.67417001],\n", + " [0.34767638, 0.0771898 , 0.74369082, 0.71208482, 0.07304296],\n", + " [0.80166138, 0.51339036, 0.27318251, 0.94149311, 0.2287255 ]],\n", + "\n", + " [[0.44365861, 0.68199016, 0.31989072, 0.080943 , 0.24966301],\n", + " [0.06577791, 0.62012891, 0.38803027, 0.36833548, 0.65197115],\n", + " [0.46933885, 0.5092976 , 0.91005934, 0.14236737, 0.32877812]]])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e = a * c\n", + "e" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 13. Does *e* equal to *a*? Why or why not?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "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": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e == a\n", + "\n", + "# because they have the same values" + ] + }, + { + "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": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.941493106239979\n", + "1.0657779061710917\n", + "1.4136865513101253\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean =d.mean()\n", + "\n", + "print(d_max)\n", + "print(d_min)\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`.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1.26347749, 1.07828423, 1.29862839, 1.15366829, 1.67417001],\n", + " [1.34767638, 1.0771898 , 1.74369082, 1.71208482, 1.07304296],\n", + " [1.80166138, 1.51339036, 1.27318251, 1.94149311, 1.2287255 ]],\n", + "\n", + " [[1.44365861, 1.68199016, 1.31989072, 1.080943 , 1.24966301],\n", + " [1.06577791, 1.62012891, 1.38803027, 1.36833548, 1.65197115],\n", + " [1.46933885, 1.5092976 , 1.91005934, 1.14236737, 1.32877812]]])" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = np.empty(shape = (2,3,5))\n", + "f" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 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*. If a value in *d* is larger than *d_mean* but smaller than *d_max*, assign 75 to the corresponding value in *f*. If a value equals to *d_mean*, assign 50 to the corresponding value in *f*. Assign 0 to the corresponding value(s) in *f* for *d_min* in *d*. Assign 100 to the corresponding value(s) in *f* for *d_max* in *d*. In the end, f should have only the following values: 0, 25, 50, 75, and 100.\n", + "\n", + "**Note**: you don't have to use Numpy in this question." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_7564\\1449678070.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[0md_min\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m25\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" + ] + } + ], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 17. Print *d* and *f*. Do you have your expected *f*?\n", + "For instance, if your *d* is:\n", + "```python\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", + "[[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", + "```python\n", + "[[[ 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": null, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), use string values (\"A\", \"B\", \"C\", \"D\", and \"E\") to label the array elements. For the example above, the expected result is:\n", + "\n", + "```python\n", + "[[[ '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", + "```\n", + "**Note**: you don't have to use Numpy in this question." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here" + ] + } + ], + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb deleted file mode 100644 index e66d6ce..0000000 --- a/your-code/main.ipynb +++ /dev/null @@ -1,367 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Intrduction to NumPy\n", - "\n", - "\n", - "#### 1. Import NumPy under the name np." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Print your NumPy version." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Method 3" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Print *a*.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Create a 5x2x3 3-dimensional array with all values equaling 1. Assign the array to variable *b*." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 6. Print *b*.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 8. Are you able to add *a* and *b*? Why or why not?\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your answer here" - ] - }, - { - "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 variable *c*." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 10. Try to add *a* and *c*. Now it should work. Assign the sum to variable *d*. But why does it work now?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 12. Multiply *a* and *c*. Assign the result to *e*." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 13. Does *e* equal to *a*? Why or why not?\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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`.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 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*. If a value in *d* is larger than *d_mean* but smaller than *d_max*, assign 75 to the corresponding value in *f*. If a value equals to *d_mean*, assign 50 to the corresponding value in *f*. Assign 0 to the corresponding value(s) in *f* for *d_min* in *d*. Assign 100 to the corresponding value(s) in *f* for *d_max* in *d*. In the end, f should have only the following values: 0, 25, 50, 75, and 100.\n", - "\n", - "**Note**: you don't have to use Numpy in this question." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 17. Print *d* and *f*. Do you have your expected *f*?\n", - "For instance, if your *d* is:\n", - "```python\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", - "[[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", - "```python\n", - "[[[ 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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 18. Bonus question: instead of using numbers (i.e. 0, 25, 50, 75, and 100), use string values (\"A\", \"B\", \"C\", \"D\", and \"E\") to label the array elements. For the example above, the expected result is:\n", - "\n", - "```python\n", - "[[[ '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", - "```\n", - "**Note**: you don't have to use Numpy in this question." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - } - ], - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}