diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..6845ba1 Binary files /dev/null and b/.DS_Store differ diff --git a/your-code/lab.ipynb b/your-code/lab.ipynb new file mode 100644 index 0000000..95a25ed --- /dev/null +++ b/your-code/lab.ipynb @@ -0,0 +1,429 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as numpy\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.11897008, 0.29613734, 0.47278353, 0.44220668, 0.52731243],\n", + " [0.33351895, 0.07813552, 0.01167319, 0.09694292, 0.19686695],\n", + " [0.94762343, 0.98095862, 0.45305536, 0.91703525, 0.80370209]],\n", + "\n", + " [[0.77801335, 0.18977725, 0.53745079, 0.71971178, 0.16821655],\n", + " [0.12755851, 0.22525974, 0.73825694, 0.65819364, 0.00332843],\n", + " [0.9678279 , 0.0709996 , 0.79483683, 0.27051068, 0.46819624]]])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = numpy.random.random((2,3,5))\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 3, + "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": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n", + "#Assign the array to variable \"b\"\n", + "\n", + "b = numpy.ones((5,2,3))\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numpy.size(a)\n", + "numpy.size(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'a' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\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 3\u001b[0m \u001b[0;31m#NO las matrices son de tamaño y dimensiones diferentes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\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[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined" + ] + } + ], + "source": [ + "#8. Are you able to add a and b? Why or why not?\n", + "\n", + "#NO las matrices son de tamaño y dimensiones diferentes\n", + "\n", + "print(a + b)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[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.]]])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#9. Transpose b so that it has the same structure of a (i.e. become a 2x3x5 array). \n", + "#Assign the transposed array to varialbe \"c\".\n", + "\n", + "c = numpy.reshape(b, (2,3,5))\n", + "c\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.11897008 1.29613734 1.47278353 1.44220668 1.52731243]\n", + " [1.33351895 1.07813552 1.01167319 1.09694292 1.19686695]\n", + " [1.94762343 1.98095862 1.45305536 1.91703525 1.80370209]]\n", + "\n", + " [[1.77801335 1.18977725 1.53745079 1.71971178 1.16821655]\n", + " [1.12755851 1.22525974 1.73825694 1.65819364 1.00332843]\n", + " [1.9678279 1.0709996 1.79483683 1.27051068 1.46819624]]]\n" + ] + } + ], + "source": [ + "#10. Try to add a and c. Now it should work. Assign the sum to varialbe \"d\". \n", + "#But why does it work now?\n", + "\n", + "d = numpy.add(a, c)\n", + "\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.11897008 0.29613734 0.47278353 0.44220668 0.52731243]\n", + " [0.33351895 0.07813552 0.01167319 0.09694292 0.19686695]\n", + " [0.94762343 0.98095862 0.45305536 0.91703525 0.80370209]]\n", + "\n", + " [[0.77801335 0.18977725 0.53745079 0.71971178 0.16821655]\n", + " [0.12755851 0.22525974 0.73825694 0.65819364 0.00332843]\n", + " [0.9678279 0.0709996 0.79483683 0.27051068 0.46819624]]]\n", + "[[[1.11897008 1.29613734 1.47278353 1.44220668 1.52731243]\n", + " [1.33351895 1.07813552 1.01167319 1.09694292 1.19686695]\n", + " [1.94762343 1.98095862 1.45305536 1.91703525 1.80370209]]\n", + "\n", + " [[1.77801335 1.18977725 1.53745079 1.71971178 1.16821655]\n", + " [1.12755851 1.22525974 1.73825694 1.65819364 1.00332843]\n", + " [1.9678279 1.0709996 1.79483683 1.27051068 1.46819624]]]\n" + ] + } + ], + "source": [ + "#11. Print a and d. Notice the difference and relation of the two array in terms \n", + "#of the values? Explain.\n", + "\n", + "#C es matriz de unos y a no. \n", + "\n", + "print(a)\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.11897008, 0.29613734, 0.47278353, 0.44220668, 0.52731243],\n", + " [0.33351895, 0.07813552, 0.01167319, 0.09694292, 0.19686695],\n", + " [0.94762343, 0.98095862, 0.45305536, 0.91703525, 0.80370209]],\n", + "\n", + " [[0.77801335, 0.18977725, 0.53745079, 0.71971178, 0.16821655],\n", + " [0.12755851, 0.22525974, 0.73825694, 0.65819364, 0.00332843],\n", + " [0.9678279 , 0.0709996 , 0.79483683, 0.27051068, 0.46819624]]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e = numpy.multiply(a,c)\n", + "e\n", + "\n", + "#13. Does e equal to a? Why or why not?\n", + "\n", + "#Sí, porque multiplicamos cada valor x 1" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.9809586150690985\n", + "1.003328432752701\n", + "1.4465020175663816\n" + ] + } + ], + "source": [ + "#14. Identify the max, min, and mean values in d. \n", + "#Assign those values to variables \"d_max\", \"d_min\", and \"d_mean\"\n", + "\n", + "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": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[ 25. 25. 75. 25. 75.]\n", + " [ 25. 25. 25. 25. 25.]\n", + " [ 75. 100. 75. 75. 75.]]\n", + "\n", + " [[ 75. 25. 75. 75. 25.]\n", + " [ 25. 25. 75. 75. 0.]\n", + " [ 75. 25. 75. 25. 75.]]]\n" + ] + } + ], + "source": [ + "#15. Now we want to label the values in d. \n", + "#First create an empty array \"f\" with the same shape (i.e. 2x3x5) as d using `np.empty`.\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 \n", + "#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", + "\n", + "f = numpy.empty([2,3,5])\n", + "\n", + "# el primer elemento es la posición y e segundo es el numero o valor\n", + "\n", + "for i in range(len(d)):\n", + " for j in range(len(d[0])):\n", + " for k in range(len(d[0,0])):\n", + " #print(d[i][j][k], d_min, d_max, d_mean)\n", + " if d[i][j][k] > d_min and d[i][j][k] < d_mean: f[i][j][k] = 25\n", + " elif d[i][j][k] > d_mean and d[i][j][k] < d_max: f[i][j][k] = 75\n", + " elif d[i][j][k] == d_mean: f[i][j][k] = 50\n", + " elif d[i][j][k] == d_min: f[i][j][k] = 0\n", + " elif d[i][j][k] == d_max: f[i][j][k] = 100\n", + "print(f) " + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.15741396 1.57718496 1.55794585 1.75429087 1.18295349]\n", + " [1.38785103 1.36467957 1.68662754 1.59351609 1.34470664]\n", + " [1.09105556 1.89683448 1.75053437 1.356337 1.1626572 ]]\n", + "\n", + " [[1.88741025 1.47293087 1.36953125 1.75441738 1.32975527]\n", + " [1.91122312 1.04106238 1.88871489 1.15340316 1.08850475]\n", + " [1.97455596 1.88699055 1.95089172 1.18386821 1.91114982]]]\n" + ] + } + ], + "source": [ + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['B' 'B' 'D' 'B' 'D']\n", + " ['B' 'B' 'B' 'B' 'B']\n", + " ['D' 'E' 'D' 'D' 'D']]\n", + "\n", + " [['D' 'B' 'D' 'D' 'B']\n", + " ['B' 'B' 'D' 'D' 'A']\n", + " ['D' 'B' 'D' 'B' 'D']]]\n" + ] + } + ], + "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.\n", + "\n", + "ff = numpy.empty([2,3,5], dtype=\"object\")\n", + "\n", + "for i in range(len(d)):\n", + " for j in range(len(d[0])):\n", + " for k in range(len(d[0,0])):\n", + " #print(d[i][j][k], d_min, d_max, d_mean)\n", + " if d[i][j][k] > d_min and d[i][j][k] < d_mean: ff[i][j][k] = 'B'\n", + " elif d[i][j][k] > d_mean and d[i][j][k] < d_max: ff[i][j][k] = 'D'\n", + " elif d[i][j][k] == d_mean: ff[i][j][k] = 'C'\n", + " elif d[i][j][k] == d_min: ff[i][j][k] = 'A'\n", + " elif d[i][j][k] == d_max: ff[i][j][k] = 'E'\n", + "print(ff) \n", + "\n", + "\n", + "\n" + ] + }, + { + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}