diff --git "a/.ipynb_checkpoints/Introducci\303\263n a Numpy-checkpoint.ipynb" "b/.ipynb_checkpoints/Introducci\303\263n a Numpy-checkpoint.ipynb" new file mode 100644 index 0000000..7d2ff7b --- /dev/null +++ "b/.ipynb_checkpoints/Introducci\303\263n a Numpy-checkpoint.ipynb" @@ -0,0 +1,507 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introducción a Numpy\n", + "\n", + "Alan Badillo Salas (badillo.soft@hotmail.com)\n", + "\n", + "## Introducción\n", + "\n", + "Numpy es una librería de python especializada en el manejo de arreglos n-dimensionales con potencia cercana a c/c++ gracias a su precompilación y tipado de datos.\n", + "\n", + "El componente principal de numpy es el arreglo n-dimensional, dónde 1 dimensión significa un vector en matemáticas o un arreglo en computación. La dimensión significa una matriz o tabla la cuál contiene dos ejes de crecimiento, las filas y columnas o el eje primario y el secundario. Una imagen por ejemplo puede ser codificada en 3 dimensiones, un ancho, un largo y una profundidad de 3 canles de color. Dimensiones superiores permitirán manejar información más compleja.\n", + "\n", + "## Arreglos de 1 dimensión (Vectores)\n", + "\n", + "Podemos crear vectores o arreglos uni-dimensionales en mumpy a tráves de las funciones `range`, `array` y `random`." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "# Crear un arreglo a partir de una lista de python\n", + "vec1 = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17])\n", + "\n", + "print(vec1)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "print(type(vec1))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El tamaño del vector es: 9\n" + ] + } + ], + "source": [ + "# Obtener el tamaño del arreglo\n", + "n = len(vec1)\n", + "\n", + "print(\"El tamaño del vector es: {}\".format(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "La dimensión de un arreglo `n-dimensional` está dada por `shape`, la cual contiene una `n-tupla` con los tamaños en cada dimensión, por ejemplo, un vector con `9` elementos tiene una dimensión, es decir, tiene un `shape` que es una `1-tupla` (`(*,)`), en la `1-tupla` podemos encontrar el tamaño de los elementos del arreglo para el eje. En otro ejemplo, si tenemos un arreglo `2-dimensional` (una matriz), su `shape` será una `2-tupla` (`(*, *)`), dónde el primer valor en la tupla representa el tamaño del primer eje (filas) y el segundo valor en la tupla representa el número de elementos en el segundo eje (columnas)." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La dimensión del vector es: (9,)\n" + ] + } + ], + "source": [ + "dim = vec1.shape\n", + "\n", + "print(\"La dimensión del vector es: {}\".format(dim))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Operaciones entre vectores\n", + "\n", + "Podemos realizar algunas operaciones importantes entre vectores según la documentación oficial de numpy que se encuentra disponible en https://docs.scipy.org/doc/." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Suma: [6 6 6 6 6]\n", + "Resta: [-4 -2 0 2 4]\n" + ] + } + ], + "source": [ + "v1 = np.array([1, 2, 3, 4, 5])\n", + "v2 = np.array([5, 4, 3, 2, 1])\n", + "\n", + "print(\"Suma: {}\".format(v1 + v2))\n", + "print(\"Resta: {}\".format(v1 - v2))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 * v1 = [ 2 4 6 8 10]\n", + "v1 * 10 = [10 20 30 40 50]\n" + ] + } + ], + "source": [ + "# vector * escalar / escalar * vector\n", + "v3 = 2 * v1\n", + "v4 = v1 * 10\n", + "\n", + "print(\"2 * v1 = {}\".format(v3))\n", + "print(\"v1 * 10 = {}\".format(v4))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "v1^2 = [ 1 4 9 16 25]\n", + "v1^(1/2) = [1. 1.41421356 1.73205081 2. 2.23606798]\n" + ] + } + ], + "source": [ + "v5 = v1 ** 2\n", + "v6 = v1 ** 0.5\n", + "\n", + "print(\"v1^2 = {}\".format(v5))\n", + "print(\"v1^(1/2) = {}\".format(v6))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sin(v1) = [ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427]\n", + "cos(v1) = [ 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219]\n" + ] + } + ], + "source": [ + "v7 = np.sin(v1)\n", + "v8 = np.cos(v1)\n", + "\n", + "print(\"sin(v1) = {}\".format(v7))\n", + "print(\"cos(v1) = {}\".format(v8))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sin(v1º) = [0.01745241 0.0348995 0.05233596 0.06975647 0.08715574]\n", + "cos(v1º) = [0.9998477 0.99939083 0.99862953 0.99756405 0.9961947 ]\n" + ] + } + ], + "source": [ + "v9 = np.sin(v1 * np.pi / 180.)\n", + "v10 = np.cos(v1 * np.pi / 180.)\n", + "\n", + "print(\"sin(v1º) = {}\".format(v9))\n", + "print(\"cos(v1º) = {}\".format(v10))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Operaciones entre matrices\n", + "\n", + "Las matrices son arreglos de 2 dimensiones, las cuales pueden ayudarnos a retener datos multidimensionales, resolver ecuaciones lineales, modelas sistemas dinámicos lineales, hacer optimización, entre otras cosas." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n" + ] + } + ], + "source": [ + "# Crear una matriz de 3x3\n", + "mat1 = np.array([\n", + " [1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]\n", + "])\n", + "\n", + "print(mat1)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dimensión: (3, 3)\n" + ] + } + ], + "source": [ + "dim = mat1.shape\n", + "\n", + "print(\"Dimensión: {}\".format(dim))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Un sistema de ecuaciones simultáneas se define a partir de ecuaciones lineales que poseen una o más incognitas. Dichos sistemas se pueden resolver a partir de una representación matricial.\n", + "\n", + "Por ejemplo, si tenemos 3 ecuaciones que describen las equivalencias entre 3 incognitas, podemos encontrar el vector de incognitas como se muestra a continuación.\n", + "\n", + "Un `x`, más dos `y` es igual a `32`.\n", + "Dos `x`, menos un `y`, más cuatro `z` es igual `4`.\n", + "Un `x`, menos seis `z` es igual a `10`.\n", + "\n", + "> 1 * x + 2 * y = 32\n", + "\n", + "> 2 * x - 1 * y + 4 * z = 12\n", + "\n", + "> 1 * x - 6 * z = 10\n", + "\n", + "| 1 2 0 | | x | | 32 |\n", + "\n", + "| 2 -1 4 | | y | = | 28 |\n", + "\n", + "| 1 0 -6 | | z | | 10 |" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[16. 8. 1.]\n" + ] + } + ], + "source": [ + "A = np.array([\n", + " [1, 2, 0],\n", + " [2, -1, 4],\n", + " [1, 0, -6]\n", + "])\n", + "\n", + "b = np.array([32, 28, 10])\n", + "\n", + "x = np.dot(np.linalg.inv(A), b)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[16. 8. 1.]\n" + ] + } + ], + "source": [ + "x = np.linalg.solve(A, b)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Generar arreglos n-dimensionales\n", + "\n", + "Tenemos varios métodos útiles para generar arreglos aleatorios, ordenados y basados en distribuciones." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\n", + " 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36\n", + " 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54\n", + " 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72\n", + " 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90\n", + " 91 92 93 94 95 96 97 98 99 100]\n" + ] + } + ], + "source": [ + "# Crear un arreglo con 100 elementos del 1 al 100\n", + "x = np.arange(1, 101)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0.]\n", + "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1.]\n" + ] + } + ], + "source": [ + "# Crear un arreglo con 100 elementos 0's\n", + "x = np.zeros(100)\n", + "# Crear un arreglo con 100 elementos 1's\n", + "y = np.ones(100)\n", + "\n", + "print(x)\n", + "print(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Algo muy importante y útil es crear arreglos a partir de números aleatorios para poder utilizarlos como perturbadores de modelos, como generadores de datos aleatorios, entre otros. La documentación se encuentra en https://docs.scipy.org/doc/numpy-1.15.4/reference/routines.random.html." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.5797281 0.35265298 0.5405803 0.34510746 0.51952005 0.17807994\n", + " 0.16365099 0.78942373 0.94562964 0.22360458 0.44281828 0.45608266\n", + " 0.32919176 0.27009892 0.37868061 0.39521384 0.95393806 0.80225394\n", + " 0.54895089 0.91753921 0.25479783 0.82614327 0.39444858 0.53911876\n", + " 0.57765646 0.92895817 0.26291368 0.51516767 0.89273388 0.44459308\n", + " 0.66775034 0.75110455 0.66596155 0.54461959 0.00643209 0.67108138\n", + " 0.53357365 0.79344639 0.89397719 0.54796575 0.2941559 0.66644502\n", + " 0.76031806 0.37788733 0.1740883 0.08943032 0.23428461 0.48861957\n", + " 0.55198128 0.98495224 0.61567146 0.84083723 0.49946292 0.11303123\n", + " 0.90102917 0.55271946 0.2946729 0.37214732 0.95393456 0.90191595\n", + " 0.64380768 0.30532433 0.95443809 0.53181639 0.35842961 0.87936518\n", + " 0.80785665 0.78247915 0.8558269 0.83600379 0.46891404 0.1755544\n", + " 0.41997631 0.83716052 0.90246884 0.0867858 0.34138837 0.49048341\n", + " 0.28744089 0.77506001 0.79434923 0.68563508 0.40168108 0.92230756\n", + " 0.98257357 0.21044662 0.93602452 0.63853754 0.12631281 0.05668229\n", + " 0.29129462 0.87691474 0.47759211 0.56998526 0.67500797 0.81782706\n", + " 0.6841755 0.96618746 0.09651078 0.4846972 ]\n" + ] + } + ], + "source": [ + "# Crear un arreglo de aleatorios entre [0, 1)\n", + "x = np.random.random(100)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 4 5 1 4 1 5 4 1 1 4 4 4 4 1 4 6 1 5 4 1 1 1 4 1 4 5 1 4 1 5 6 1 4 1 1 4\n", + " 6 6 1 5 6 1 4 1 1 1 5 5 6 4 6 4 5 5 5 6 6 1 6 1 6 4 1 6 6 5 5 4 6 4 1 5 6\n", + " 1 1 4 6 1 5 4 6 1 4 5 6 4 4 1 5 1 5 5 4 4 5 1 5 5 6]\n" + ] + } + ], + "source": [ + "# Crear un arreglo de aleatorios en una lista\n", + "a = [1, 4, 5, 6]\n", + "\n", + "x = np.random.choice(a, 100)\n", + "\n", + "print(x)" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git "a/Introducci\303\263n a Numpy.ipynb" "b/Introducci\303\263n a Numpy.ipynb" new file mode 100644 index 0000000..7d2ff7b --- /dev/null +++ "b/Introducci\303\263n a Numpy.ipynb" @@ -0,0 +1,507 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Introducción a Numpy\n", + "\n", + "Alan Badillo Salas (badillo.soft@hotmail.com)\n", + "\n", + "## Introducción\n", + "\n", + "Numpy es una librería de python especializada en el manejo de arreglos n-dimensionales con potencia cercana a c/c++ gracias a su precompilación y tipado de datos.\n", + "\n", + "El componente principal de numpy es el arreglo n-dimensional, dónde 1 dimensión significa un vector en matemáticas o un arreglo en computación. La dimensión significa una matriz o tabla la cuál contiene dos ejes de crecimiento, las filas y columnas o el eje primario y el secundario. Una imagen por ejemplo puede ser codificada en 3 dimensiones, un ancho, un largo y una profundidad de 3 canles de color. Dimensiones superiores permitirán manejar información más compleja.\n", + "\n", + "## Arreglos de 1 dimensión (Vectores)\n", + "\n", + "Podemos crear vectores o arreglos uni-dimensionales en mumpy a tráves de las funciones `range`, `array` y `random`." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 3 5 7 9 11 13 15 17]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "\n", + "# Crear un arreglo a partir de una lista de python\n", + "vec1 = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17])\n", + "\n", + "print(vec1)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "print(type(vec1))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El tamaño del vector es: 9\n" + ] + } + ], + "source": [ + "# Obtener el tamaño del arreglo\n", + "n = len(vec1)\n", + "\n", + "print(\"El tamaño del vector es: {}\".format(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "La dimensión de un arreglo `n-dimensional` está dada por `shape`, la cual contiene una `n-tupla` con los tamaños en cada dimensión, por ejemplo, un vector con `9` elementos tiene una dimensión, es decir, tiene un `shape` que es una `1-tupla` (`(*,)`), en la `1-tupla` podemos encontrar el tamaño de los elementos del arreglo para el eje. En otro ejemplo, si tenemos un arreglo `2-dimensional` (una matriz), su `shape` será una `2-tupla` (`(*, *)`), dónde el primer valor en la tupla representa el tamaño del primer eje (filas) y el segundo valor en la tupla representa el número de elementos en el segundo eje (columnas)." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "La dimensión del vector es: (9,)\n" + ] + } + ], + "source": [ + "dim = vec1.shape\n", + "\n", + "print(\"La dimensión del vector es: {}\".format(dim))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Operaciones entre vectores\n", + "\n", + "Podemos realizar algunas operaciones importantes entre vectores según la documentación oficial de numpy que se encuentra disponible en https://docs.scipy.org/doc/." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Suma: [6 6 6 6 6]\n", + "Resta: [-4 -2 0 2 4]\n" + ] + } + ], + "source": [ + "v1 = np.array([1, 2, 3, 4, 5])\n", + "v2 = np.array([5, 4, 3, 2, 1])\n", + "\n", + "print(\"Suma: {}\".format(v1 + v2))\n", + "print(\"Resta: {}\".format(v1 - v2))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 * v1 = [ 2 4 6 8 10]\n", + "v1 * 10 = [10 20 30 40 50]\n" + ] + } + ], + "source": [ + "# vector * escalar / escalar * vector\n", + "v3 = 2 * v1\n", + "v4 = v1 * 10\n", + "\n", + "print(\"2 * v1 = {}\".format(v3))\n", + "print(\"v1 * 10 = {}\".format(v4))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "v1^2 = [ 1 4 9 16 25]\n", + "v1^(1/2) = [1. 1.41421356 1.73205081 2. 2.23606798]\n" + ] + } + ], + "source": [ + "v5 = v1 ** 2\n", + "v6 = v1 ** 0.5\n", + "\n", + "print(\"v1^2 = {}\".format(v5))\n", + "print(\"v1^(1/2) = {}\".format(v6))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sin(v1) = [ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427]\n", + "cos(v1) = [ 0.54030231 -0.41614684 -0.9899925 -0.65364362 0.28366219]\n" + ] + } + ], + "source": [ + "v7 = np.sin(v1)\n", + "v8 = np.cos(v1)\n", + "\n", + "print(\"sin(v1) = {}\".format(v7))\n", + "print(\"cos(v1) = {}\".format(v8))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sin(v1º) = [0.01745241 0.0348995 0.05233596 0.06975647 0.08715574]\n", + "cos(v1º) = [0.9998477 0.99939083 0.99862953 0.99756405 0.9961947 ]\n" + ] + } + ], + "source": [ + "v9 = np.sin(v1 * np.pi / 180.)\n", + "v10 = np.cos(v1 * np.pi / 180.)\n", + "\n", + "print(\"sin(v1º) = {}\".format(v9))\n", + "print(\"cos(v1º) = {}\".format(v10))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Operaciones entre matrices\n", + "\n", + "Las matrices son arreglos de 2 dimensiones, las cuales pueden ayudarnos a retener datos multidimensionales, resolver ecuaciones lineales, modelas sistemas dinámicos lineales, hacer optimización, entre otras cosas." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n" + ] + } + ], + "source": [ + "# Crear una matriz de 3x3\n", + "mat1 = np.array([\n", + " [1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]\n", + "])\n", + "\n", + "print(mat1)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dimensión: (3, 3)\n" + ] + } + ], + "source": [ + "dim = mat1.shape\n", + "\n", + "print(\"Dimensión: {}\".format(dim))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Un sistema de ecuaciones simultáneas se define a partir de ecuaciones lineales que poseen una o más incognitas. Dichos sistemas se pueden resolver a partir de una representación matricial.\n", + "\n", + "Por ejemplo, si tenemos 3 ecuaciones que describen las equivalencias entre 3 incognitas, podemos encontrar el vector de incognitas como se muestra a continuación.\n", + "\n", + "Un `x`, más dos `y` es igual a `32`.\n", + "Dos `x`, menos un `y`, más cuatro `z` es igual `4`.\n", + "Un `x`, menos seis `z` es igual a `10`.\n", + "\n", + "> 1 * x + 2 * y = 32\n", + "\n", + "> 2 * x - 1 * y + 4 * z = 12\n", + "\n", + "> 1 * x - 6 * z = 10\n", + "\n", + "| 1 2 0 | | x | | 32 |\n", + "\n", + "| 2 -1 4 | | y | = | 28 |\n", + "\n", + "| 1 0 -6 | | z | | 10 |" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[16. 8. 1.]\n" + ] + } + ], + "source": [ + "A = np.array([\n", + " [1, 2, 0],\n", + " [2, -1, 4],\n", + " [1, 0, -6]\n", + "])\n", + "\n", + "b = np.array([32, 28, 10])\n", + "\n", + "x = np.dot(np.linalg.inv(A), b)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[16. 8. 1.]\n" + ] + } + ], + "source": [ + "x = np.linalg.solve(A, b)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Generar arreglos n-dimensionales\n", + "\n", + "Tenemos varios métodos útiles para generar arreglos aleatorios, ordenados y basados en distribuciones." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18\n", + " 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36\n", + " 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54\n", + " 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72\n", + " 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90\n", + " 91 92 93 94 95 96 97 98 99 100]\n" + ] + } + ], + "source": [ + "# Crear un arreglo con 100 elementos del 1 al 100\n", + "x = np.arange(1, 101)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", + " 0. 0. 0. 0.]\n", + "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", + " 1. 1. 1. 1.]\n" + ] + } + ], + "source": [ + "# Crear un arreglo con 100 elementos 0's\n", + "x = np.zeros(100)\n", + "# Crear un arreglo con 100 elementos 1's\n", + "y = np.ones(100)\n", + "\n", + "print(x)\n", + "print(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Algo muy importante y útil es crear arreglos a partir de números aleatorios para poder utilizarlos como perturbadores de modelos, como generadores de datos aleatorios, entre otros. La documentación se encuentra en https://docs.scipy.org/doc/numpy-1.15.4/reference/routines.random.html." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.5797281 0.35265298 0.5405803 0.34510746 0.51952005 0.17807994\n", + " 0.16365099 0.78942373 0.94562964 0.22360458 0.44281828 0.45608266\n", + " 0.32919176 0.27009892 0.37868061 0.39521384 0.95393806 0.80225394\n", + " 0.54895089 0.91753921 0.25479783 0.82614327 0.39444858 0.53911876\n", + " 0.57765646 0.92895817 0.26291368 0.51516767 0.89273388 0.44459308\n", + " 0.66775034 0.75110455 0.66596155 0.54461959 0.00643209 0.67108138\n", + " 0.53357365 0.79344639 0.89397719 0.54796575 0.2941559 0.66644502\n", + " 0.76031806 0.37788733 0.1740883 0.08943032 0.23428461 0.48861957\n", + " 0.55198128 0.98495224 0.61567146 0.84083723 0.49946292 0.11303123\n", + " 0.90102917 0.55271946 0.2946729 0.37214732 0.95393456 0.90191595\n", + " 0.64380768 0.30532433 0.95443809 0.53181639 0.35842961 0.87936518\n", + " 0.80785665 0.78247915 0.8558269 0.83600379 0.46891404 0.1755544\n", + " 0.41997631 0.83716052 0.90246884 0.0867858 0.34138837 0.49048341\n", + " 0.28744089 0.77506001 0.79434923 0.68563508 0.40168108 0.92230756\n", + " 0.98257357 0.21044662 0.93602452 0.63853754 0.12631281 0.05668229\n", + " 0.29129462 0.87691474 0.47759211 0.56998526 0.67500797 0.81782706\n", + " 0.6841755 0.96618746 0.09651078 0.4846972 ]\n" + ] + } + ], + "source": [ + "# Crear un arreglo de aleatorios entre [0, 1)\n", + "x = np.random.random(100)\n", + "\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 4 5 1 4 1 5 4 1 1 4 4 4 4 1 4 6 1 5 4 1 1 1 4 1 4 5 1 4 1 5 6 1 4 1 1 4\n", + " 6 6 1 5 6 1 4 1 1 1 5 5 6 4 6 4 5 5 5 6 6 1 6 1 6 4 1 6 6 5 5 4 6 4 1 5 6\n", + " 1 1 4 6 1 5 4 6 1 4 5 6 4 4 1 5 1 5 5 4 4 5 1 5 5 6]\n" + ] + } + ], + "source": [ + "# Crear un arreglo de aleatorios en una lista\n", + "a = [1, 4, 5, 6]\n", + "\n", + "x = np.random.choice(a, 100)\n", + "\n", + "print(x)" + ] + } + ], + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}