From 6d644a36bac4b4d6d17699ac6eebd96215b8d03e Mon Sep 17 00:00:00 2001 From: ElsaRobles <54522438+ElsaRobles@users.noreply.github.com> Date: Sat, 26 Oct 2019 17:00:52 -0500 Subject: [PATCH] Laboratorio resuelto --- your-code/Lab_Numpy.ipynb | 353 ++++++++++++++++++++++++++++++++++++++ your-code/main.py | 139 +++++++++++++-- 2 files changed, 478 insertions(+), 14 deletions(-) create mode 100644 your-code/Lab_Numpy.ipynb diff --git a/your-code/Lab_Numpy.ipynb b/your-code/Lab_Numpy.ipynb new file mode 100644 index 0000000..847057a --- /dev/null +++ b/your-code/Lab_Numpy.ipynb @@ -0,0 +1,353 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.17.2\n" + ] + } + ], + "source": [ + "print(np.version.version)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "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", + "a = np.random.random((2,3,5))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n", + " [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n", + " [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n", + "\n", + " [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n", + " [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n", + " [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n" + ] + } + ], + "source": [ + "#4. Print a.\n", + "\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "#5. Create a 5x2x3 3-dimensional array with all values equaling 1.\n", + "#Assign the array to variable \"b\"\n", + "\n", + "b = np.ones((5,2,3))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "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", + "\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "30\n", + "30\n" + ] + } + ], + "source": [ + "#7. Do a and b have the same size? How do you prove that in Python code?\n", + "\n", + "print(a.size)\n", + "print(b.size)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "#8. Are you able to add a and b? Why or why not?\n", + "\n", + "#No se pueden agregar porque no tienen la misma forma \n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "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": [ + "#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", + "\n", + "c = np.reshape(b,(2,3,5))\n", + "print(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.92598104 1.40197817 1.42482632 1.66371613 1.93045929]\n", + " [1.13760188 1.03166089 1.0704651 1.57791757 1.4598114 ]\n", + " [1.1690768 1.13553113 1.90339829 1.38074646 1.19877052]]\n", + "\n", + " [[1.49544784 1.60575526 1.809597 1.38395514 1.55734081]\n", + " [1.34173021 1.80337932 1.15776063 1.02389911 1.4974305 ]\n", + " [1.26252848 1.69824184 1.87914224 1.63275067 1.58913698]]]\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", + "\n", + "d = np.add(a,c)\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n", + " [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n", + " [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n", + "\n", + " [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n", + " [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n", + " [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n", + "[[[1.92598104 1.40197817 1.42482632 1.66371613 1.93045929]\n", + " [1.13760188 1.03166089 1.0704651 1.57791757 1.4598114 ]\n", + " [1.1690768 1.13553113 1.90339829 1.38074646 1.19877052]]\n", + "\n", + " [[1.49544784 1.60575526 1.809597 1.38395514 1.55734081]\n", + " [1.34173021 1.80337932 1.15776063 1.02389911 1.4974305 ]\n", + " [1.26252848 1.69824184 1.87914224 1.63275067 1.58913698]]]\n" + ] + } + ], + "source": [ + "#11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain.\n", + "\n", + "#Los dos arreglos son iguales porque tienen la misma forma es un arreglo de 3 dimensiones con 30 elementos. \n", + "print(a)\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n", + " [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n", + " [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n", + "\n", + " [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n", + " [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n", + " [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n" + ] + } + ], + "source": [ + "#12. Multiply a and c. Assign the result to e.\n", + "\n", + "e = np.multiply(a, c) \n", + "print(e)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n", + " [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n", + " [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n", + "\n", + " [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n", + " [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n", + " [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n", + "[[[0.92598104 0.40197817 0.42482632 0.66371613 0.93045929]\n", + " [0.13760188 0.03166089 0.0704651 0.57791757 0.4598114 ]\n", + " [0.1690768 0.13553113 0.90339829 0.38074646 0.19877052]]\n", + "\n", + " [[0.49544784 0.60575526 0.809597 0.38395514 0.55734081]\n", + " [0.34173021 0.80337932 0.15776063 0.02389911 0.4974305 ]\n", + " [0.26252848 0.69824184 0.87914224 0.63275067 0.58913698]]]\n" + ] + } + ], + "source": [ + "#13. Does e equal to a? Why or why not?\n", + "print(a)\n", + "print(e)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.9304592859190688\n", + "1.0238991148469805\n", + "1.4716678997998724\n" + ] + } + ], + "source": [ + "d_max = d.max()\n", + "d_min = d.min()\n", + "d_mean = d.mean()\n", + "\n", + "print(d_max)\n", + "\n", + "print(d_min)\n", + "\n", + "print(d_mean)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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", + "\n", + "f = np.empty([2,3,5])\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 +} diff --git a/your-code/main.py b/your-code/main.py index 78c792b..3237580 100644 --- a/your-code/main.py +++ b/your-code/main.py @@ -1,80 +1,191 @@ #1. Import the NUMPY package under the name np. - +import numpy as np #2. Print the NUMPY version and the configuration. - +print(np.version.version) +1.17.2 #3. Generate a 2x3x5 3-dimensional array with random values. Assign the array to variable "a" # Challenge: there are at least three easy ways that use numpy to generate random arrays. How many ways can you find? +a = np.random.random((2,3,5)) - +#Segunda manera de generar random arrays +np.array((2,3,5)) #4. Print a. +print(a) +[[[0.87518155 0.97160302 0.74320166 0.52388431 0.23212673] + [0.49494077 0.0479125 0.93681344 0.1634494 0.83706916] + [0.21275378 0.11567146 0.85040991 0.89222066 0.22173655]] + + [[0.66351454 0.39961527 0.74214677 0.09326884 0.98651048] + [0.34882669 0.88676666 0.80927674 0.69553686 0.43548238] + [0.90895205 0.92070078 0.15513116 0.09261683 0.75755615]]] #5. Create a 5x2x3 3-dimensional array with all values equaling 1. #Assign the array to variable "b" +b = np.ones((5,2,3)) #6. Print b. +print(b) +[[[1. 1. 1.] + [1. 1. 1.]] -#7. Do a and b have the same size? How do you prove that in Python code? + [[1. 1. 1.] + [1. 1. 1.]] + [[1. 1. 1.] + [1. 1. 1.]] + [[1. 1. 1.] + [1. 1. 1.]] + [[1. 1. 1.] + [1. 1. 1.]]] +#7. Do a and b have the same size? How do you prove that in Python code? -#8. Are you able to add a and b? Why or why not? +print(a.size) +print(b.size) +30 +30 +#8. Are you able to add a and b? Why or why not? + +#No se pueden agregar porque no tienen la misma forma #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". +c = np.reshape(b,(2,3,5)) +print(c) + +[[[1. 1. 1. 1. 1.] + [1. 1. 1. 1. 1.] + [1. 1. 1. 1. 1.]] + + [[1. 1. 1. 1. 1.] + [1. 1. 1. 1. 1.] + [1. 1. 1. 1. 1.]]] #10. Try to add a and c. Now it should work. Assign the sum to varialbe "d". But why does it work now? +d = np.add(a,c) +print(d) + +[[[1.87518155 1.97160302 1.74320166 1.52388431 1.23212673] + [1.49494077 1.0479125 1.93681344 1.1634494 1.83706916] + [1.21275378 1.11567146 1.85040991 1.89222066 1.22173655]] + [[1.66351454 1.39961527 1.74214677 1.09326884 1.98651048] + [1.34882669 1.88676666 1.80927674 1.69553686 1.43548238] + [1.90895205 1.92070078 1.15513116 1.09261683 1.75755615]]] #11. Print a and d. Notice the difference and relation of the two array in terms of the values? Explain. +#Los dos arreglos son iguales porque tienen la misma forma es un arreglo de 3 dimensiones con 30 elementos. +print(a) +print(d) + +[[[0.87518155 0.97160302 0.74320166 0.52388431 0.23212673] + [0.49494077 0.0479125 0.93681344 0.1634494 0.83706916] + [0.21275378 0.11567146 0.85040991 0.89222066 0.22173655]] + [[0.66351454 0.39961527 0.74214677 0.09326884 0.98651048] + [0.34882669 0.88676666 0.80927674 0.69553686 0.43548238] + [0.90895205 0.92070078 0.15513116 0.09261683 0.75755615]]] +[[[1.87518155 1.97160302 1.74320166 1.52388431 1.23212673] + [1.49494077 1.0479125 1.93681344 1.1634494 1.83706916] + [1.21275378 1.11567146 1.85040991 1.89222066 1.22173655]] + [[1.66351454 1.39961527 1.74214677 1.09326884 1.98651048] + [1.34882669 1.88676666 1.80927674 1.69553686 1.43548238] + [1.90895205 1.92070078 1.15513116 1.09261683 1.75755615]]] #12. Multiply a and c. Assign the result to e. +e = np.multiply(a, c) +print(e) +[[[0.87518155 0.97160302 0.74320166 0.52388431 0.23212673] + [0.49494077 0.0479125 0.93681344 0.1634494 0.83706916] + [0.21275378 0.11567146 0.85040991 0.89222066 0.22173655]] + [[0.66351454 0.39961527 0.74214677 0.09326884 0.98651048] + [0.34882669 0.88676666 0.80927674 0.69553686 0.43548238] + [0.90895205 0.92070078 0.15513116 0.09261683 0.75755615]]] #13. Does e equal to a? Why or why not? +print(a) +print(e) +[[[0.87518155 0.97160302 0.74320166 0.52388431 0.23212673] + [0.49494077 0.0479125 0.93681344 0.1634494 0.83706916] + [0.21275378 0.11567146 0.85040991 0.89222066 0.22173655]] + [[0.66351454 0.39961527 0.74214677 0.09326884 0.98651048] + [0.34882669 0.88676666 0.80927674 0.69553686 0.43548238] + [0.90895205 0.92070078 0.15513116 0.09261683 0.75755615]]] +[[[0.87518155 0.97160302 0.74320166 0.52388431 0.23212673] + [0.49494077 0.0479125 0.93681344 0.1634494 0.83706916] + [0.21275378 0.11567146 0.85040991 0.89222066 0.22173655]] + + [[0.66351454 0.39961527 0.74214677 0.09326884 0.98651048] + [0.34882669 0.88676666 0.80927674 0.69553686 0.43548238] + [0.90895205 0.92070078 0.15513116 0.09261683 0.75755615]]] #14. Identify the max, min, and mean values in d. Assign those values to variables "d_max", "d_min", and "d_mean" +d_max = d.max() +d_min = d.min() +d_mean = d.mean() + +print(d_max) +print(d_min) +print(d_mean) + +1.9865104765777961 +1.0479125005961147 +1.5671625702060197 #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`. +f = np.empty([2,3,5]) +for i, a in enumerate(d): + for j, b in enumerate(a): + for k, c in enumerate(b): + if c == d_min: + f[i][j][k] = 0 + elif c > d_min and c < d_mean: + f[i][j][k] = 25 + elif c == d_mean: + f[i][j][k] = 50 + elif c > d_mean and c < d_max: + f[i][j][k] = 75 + elif c == d_max: + f[i][j][k] =100 -""" -#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. -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. -Note: you don't have to use Numpy in this question. -""" +print(f) + +[[[ 75. 75. 75. 25. 25.] + [ 25. 0. 75. 25. 75.] + [ 25. 25. 75. 75. 25.]] + [[ 75. 25. 75. 25. 100.] + [ 25. 75. 75. 75. 25.] + [ 75. 75. 25. 25. 75.]]]