diff --git a/your-code/respuestasLabNumpy.ipynb b/your-code/respuestasLabNumpy.ipynb new file mode 100644 index 0000000..c49cd75 --- /dev/null +++ b/your-code/respuestasLabNumpy.ipynb @@ -0,0 +1,439 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#1. Import the NUMPY package under the name np.\n", + "import numpy as np\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.17.2\n" + ] + } + ], + "source": [ + "#2. Print the NUMPY version and the configuration.\n", + "print(np.version.version)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.04627415 0.47444118 0.6395084 0.86466358 0.45634285]\n", + " [0.25246135 0.16866061 0.16910388 0.21642111 0.20692802]\n", + " [0.75100689 0.27087636 0.76838914 0.1822334 0.44020446]]\n", + "\n", + " [[0.20229396 0.0619404 0.79097339 0.87882754 0.43231351]\n", + " [0.19318823 0.4933619 0.74401371 0.44647355 0.05924548]\n", + " [0.66350711 0.68710259 0.43905992 0.05947903 0.20349207]]]\n" + ] + } + ], + "source": [ + "#4. Print a.\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n", + "#Assign the array to variable \"b\"\n", + "b = np.ones((5,2,3))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "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": [ + "#6. Print b.\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#7. Do a and b have the same size? How do you prove that in Python code?\n", + "np.array_equal(a,b)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#8. Are you able to add a and b? Why or why not?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "#8. Are you able to add a and b? Why or why not?\n", + "print(np.add(a, b))\n", + "\n", + "print(\"No se pueden sumar porque no tienen la misma estructura.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "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", + "c = np.reshape(b,(2,3,5))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.04627415 1.47444118 1.6395084 1.86466358 1.45634285]\n", + " [1.25246135 1.16866061 1.16910388 1.21642111 1.20692802]\n", + " [1.75100689 1.27087636 1.76838914 1.1822334 1.44020446]]\n", + "\n", + " [[1.20229396 1.0619404 1.79097339 1.87882754 1.43231351]\n", + " [1.19318823 1.4933619 1.74401371 1.44647355 1.05924548]\n", + " [1.66350711 1.68710259 1.43905992 1.05947903 1.20349207]]]\n" + ] + } + ], + "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", + "d = np.add(a, c)\n", + "print(\"Funciona porque ya tienen la misma estructura.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.04627415 0.47444118 0.6395084 0.86466358 0.45634285]\n", + " [0.25246135 0.16866061 0.16910388 0.21642111 0.20692802]\n", + " [0.75100689 0.27087636 0.76838914 0.1822334 0.44020446]]\n", + "\n", + " [[0.20229396 0.0619404 0.79097339 0.87882754 0.43231351]\n", + " [0.19318823 0.4933619 0.74401371 0.44647355 0.05924548]\n", + " [0.66350711 0.68710259 0.43905992 0.05947903 0.20349207]]]\n", + "[[[1.04627415 1.47444118 1.6395084 1.86466358 1.45634285]\n", + " [1.25246135 1.16866061 1.16910388 1.21642111 1.20692802]\n", + " [1.75100689 1.27087636 1.76838914 1.1822334 1.44020446]]\n", + "\n", + " [[1.20229396 1.0619404 1.79097339 1.87882754 1.43231351]\n", + " [1.19318823 1.4933619 1.74401371 1.44647355 1.05924548]\n", + " [1.66350711 1.68710259 1.43905992 1.05947903 1.20349207]]]\n" + ] + } + ], + "source": [ + "#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.\n", + "print(a)\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "#12. Multiply a and c. Assign the result to e.\n", + "e = np.multiply(a,c)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#13. Does e equal to a? Why or why not?\n", + "np.array_equal(a,e)\n", + "print(\"Sí, porque tienen la misma estructura.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "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": 17, + "metadata": {}, + "outputs": [], + "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])" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + "#16. Populate the values in f. \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", + "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(2):\n", + " for j in range(3):\n", + " for k in range(5):\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": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.04627415 1.47444118 1.6395084 1.86466358 1.45634285]\n", + " [1.25246135 1.16866061 1.16910388 1.21642111 1.20692802]\n", + " [1.75100689 1.27087636 1.76838914 1.1822334 1.44020446]]\n", + "\n", + " [[1.20229396 1.0619404 1.79097339 1.87882754 1.43231351]\n", + " [1.19318823 1.4933619 1.74401371 1.44647355 1.05924548]\n", + " [1.66350711 1.68710259 1.43905992 1.05947903 1.20349207]]]\n", + "[[[ 0. 75. 75. 75. 75.]\n", + " [ 25. 25. 25. 25. 25.]\n", + " [ 75. 25. 75. 25. 75.]]\n", + "\n", + " [[ 25. 25. 75. 100. 75.]\n", + " [ 25. 75. 75. 75. 25.]\n", + " [ 75. 75. 75. 25. 25.]]]\n" + ] + } + ], + "source": [ + "\"\"\"\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", + "\n", + "print(d)\n", + "print(f)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['D' 'B' 'B' 'B' 'B']\n", + " ['A' 'A' 'A' 'A' 'A']\n", + " ['B' 'A' 'B' 'A' 'B']]\n", + "\n", + " [['A' 'A' 'B' 'E' 'B']\n", + " ['A' 'B' 'B' 'B' 'A']\n", + " ['B' 'B' 'B' 'A' 'A']]]\n" + ] + } + ], + "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? 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.\n", + "\"\"\"\n", + "g = np.empty([2,3,5], str)\n", + "\n", + "for i in range(2):\n", + " for j in range(3):\n", + " for k in range(5):\n", + " if d[i][j][k] > d_min and d[i][j][k] < d_mean:\n", + " g[i][j][k] = \"A\"\n", + " elif d[i][j][k] > d_mean and d[i][j][k] < d_max:\n", + " g[i][j][k] = \"B\"\n", + " elif d[i][j][k] == d_mean:\n", + " g[i][j][k] = \"C\"\n", + " elif d[i][j][k] == d_min:\n", + " g[i][j][k] = \"D\" \n", + " elif d[i][j][k] == d_max:\n", + " g[i][j][k]= \"E\"\n", + "print(g)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}