{ "cells": [ { "cell_type": "markdown", "id": "a5d89bd6-3171-429d-80f9-674fecc7d840", "metadata": {}, "source": [ "Introducción a NumPy --- 14:24 min\n", "===\n", "\n", "* 14:24 min | Última modificación: Octubre 12, 2021 " ] }, { "cell_type": "code", "execution_count": 1, "id": "908e0c3e-dfe1-42cd-bd78-13a65be84219", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Importación de la libreria y creación de un\n", "# array\n", "# ===============================================\n", "#\n", "import numpy as np\n", "\n", "some_list = [1, 2, 3, 4, 5, 6]\n", "type(some_list)" ] }, { "cell_type": "code", "execution_count": 2, "id": "cade02d9-126e-4f02-86b7-80583de804fd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(6,)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Creación de un array a partir de uan lista\n", "# ===============================================\n", "#\n", "some_array = np.array(some_list)\n", "\n", "display(\n", " type(some_array),\n", " some_array,\n", " some_array.shape,\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "id": "234e5f0d-c1a9-45a6-ab50-3628451445d8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 1., 1., 1., 1.])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función ones()\n", "# ===============================================\n", "#\n", "ones_array = np.ones(5)\n", "ones_array" ] }, { "cell_type": "code", "execution_count": 4, "id": "605715d5-b350-4465-8464-78be8701316a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones((3, 4))" ] }, { "cell_type": "code", "execution_count": 5, "id": "6c00bacd-10cc-4165-a04b-1d1a60be55ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función arange()\n", "# ===============================================\n", "#\n", "range_array = np.arange(10)\n", "range_array" ] }, { "cell_type": "code", "execution_count": 6, "id": "dd97ee36-7aa5-43e6-8fa9-0acfe8d92e1c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1. , 1.21052632, 1.42105263, 1.63157895, 1.84210526,\n", " 2.05263158, 2.26315789, 2.47368421, 2.68421053, 2.89473684,\n", " 3.10526316, 3.31578947, 3.52631579, 3.73684211, 3.94736842,\n", " 4.15789474, 4.36842105, 4.57894737, 4.78947368, 5. ])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función linspace()\n", "# ===============================================\n", "#\n", "linspace_array = np.linspace(\n", " start=1,\n", " stop=5,\n", " num=20,\n", " endpoint=True,\n", ")\n", "\n", "linspace_array" ] }, { "cell_type": "code", "execution_count": 7, "id": "e0ea5860-ab98-4b02-85c3-032ee6f668e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.7841071 , 0.08279329, 0.85304076, 0.31961308],\n", " [0.14432893, 0.44994109, 0.04584197, 0.47972086],\n", " [0.12858044, 0.59402962, 0.44033422, 0.3265233 ]])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "''" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([[1.80534142, 1.76071563, 1.47248309, 1.57283928],\n", " [1.85959762, 1.45114157, 1.45125685, 1.67641946],\n", " [1.37704914, 1.28532614, 1.38548571, 1.54240142]])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "''" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([[-0.61405818, -0.92727512, 1.38932549, -0.10729285],\n", " [ 2.99013011, -1.0328996 , -0.60144859, 0.62906101],\n", " [ 1.49205984, 2.11208538, 1.27538646, -2.01684845]])" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Números Aleatorios\n", "# ===============================================\n", "#\n", "display(\n", " np.random.random(\n", " size=(3, 4),\n", " ),\n", " \"\",\n", " np.random.uniform(\n", " low=1,\n", " high=2,\n", " size=(3, 4),\n", " ),\n", " \"\",\n", " np.random.normal(\n", " loc=1,\n", " scale=2,\n", " size=(3, 4),\n", " ),\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "id": "248fae41-f803-4d2a-8914-a01d10145a46", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "9" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([2, 3, 4, 5, 6])" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Selección de elementos\n", "# ===============================================\n", "#\n", "some_array = np.arange(10)\n", "\n", "display(\n", " some_array[0],\n", " some_array[-1],\n", " some_array[2:7],\n", ")" ] }, { "cell_type": "code", "execution_count": 24, "id": "ef56d436-6fee-4918-a67b-fde1917d4a9d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 4, 7])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Selección de la primera columna\n", "#\n", "some_matrix = np.array(\n", " [\n", " [1, 2, 3],\n", " [4, 5, 6],\n", " [7, 8, 9],\n", " ]\n", ")\n", "some_matrix[:, 0]" ] }, { "cell_type": "code", "execution_count": 10, "id": "087ee183-5902-40c6-95da-15268afa34b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 6])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Segunda fila\n", "#\n", "some_matrix[1, :]" ] }, { "cell_type": "code", "execution_count": 11, "id": "09c5ece3-76de-49f8-a674-1e866594c31b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 5, 8])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Segunda columna\n", "#\n", "some_matrix[:, 1]" ] }, { "cell_type": "code", "execution_count": 12, "id": "fdd5bce6-0ce7-4933-b970-08c250a78b0f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [5, 6, 7, 8, 9]])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'---'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([[0, 1],\n", " [2, 3],\n", " [4, 5],\n", " [6, 7],\n", " [8, 9]])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'---'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(5, 2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Método reshape()\n", "# ===============================================\n", "#\n", "display(\n", " range_array.reshape(2, 5),\n", " \"---\",\n", " range_array.reshape(5, 2),\n", " \"---\",\n", " range_array.reshape(5, 2).shape,\n", ")" ] }, { "cell_type": "code", "execution_count": 13, "id": "a60108ff-f052-4d0f-a7bf-adb0671b329d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'---'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([[0],\n", " [1],\n", " [2],\n", " [3],\n", " [4],\n", " [5],\n", " [6],\n", " [7],\n", " [8],\n", " [9]])" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Método reshape()\n", "# ===============================================\n", "#\n", "display(\n", " range_array,\n", " \"---\",\n", " range_array.reshape(-1, 1),\n", ")" ] }, { "cell_type": "code", "execution_count": 14, "id": "d3994156-731a-47c6-b3af-c45239476502", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [2, 3],\n", " [3, 4]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Función column_stack()\n", "# ===============================================\n", "#\n", "a = np.array((1, 2, 3))\n", "b = np.array((2, 3, 4))\n", "ab = np.column_stack((a, b))\n", "ab" ] }, { "cell_type": "code", "execution_count": 15, "id": "5deb2e33-6372-42f9-a6ed-79810c919664", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.5" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "4.5" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "8.25" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "2.8722813232690143" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Funciones estadísticas\n", "# ===============================================\n", "#\n", "display(\n", " np.mean(range_array),\n", " np.median(range_array),\n", " np.var(range_array),\n", " np.std(range_array),\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "id": "3de3d245-503c-40a1-84e8-0f374eecea2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.92214097, 0.92085579, 0.32797461, 0.47011169])" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "array([3.79065793e-01, 6.25521253e+03, 3.73954472e+01, 3.69668464e-01])" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#\n", "# Operaadores aritméticos\n", "# ===============================================\n", "#\n", "first_array = np.random.random(4)\n", "second_array = np.random.random(4)\n", "\n", "display(\n", " first_array + second_array,\n", " first_array / second_array ** 2,\n", ")" ] }, { "cell_type": "code", "execution_count": 17, "id": "f9bf0c8e-a84f-40a1-ad75-8c78e7c2fd91", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11.63139833, 12.01795548, 12.65783554],\n", " [12.26671018, 12.68652686, 11.44699822],\n", " [12.45564177, 12.40830808, 11.7421942 ]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Operaciones matriciales\n", "# ===============================================\n", "#\n", "first_matrix = np.random.uniform(\n", " low=1,\n", " high=2,\n", " size=(3, 3),\n", ")\n", "\n", "second_matrix = np.random.uniform(\n", " low=10,\n", " high=11,\n", " size=(3, 3),\n", ")\n", "\n", "first_matrix + second_matrix" ] }, { "cell_type": "code", "execution_count": 18, "id": "555fb820-8ac9-4082-9485-57f156c82ed3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, False, False, True, True, True,\n", " True])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Operadores lógicos\n", "# ===============================================\n", "#\n", "np.arange(10) > 5" ] }, { "cell_type": "code", "execution_count": 19, "id": "31697db8-2f9c-4396-b5d7-53a5ed0216db", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([6, 7, 8, 9])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Selección lógica\n", "# ===============================================\n", "#\n", "some_array = np.arange(10)\n", "bool_array = some_array > 5\n", "some_array[bool_array]" ] }, { "cell_type": "code", "execution_count": 20, "id": "84808536-0a6b-4eba-88cd-68f940d6d5e1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([False, False, False, False, True, True, True, True, False,\n", " False])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_gt_3 = some_array > 3\n", "array_lt_8 = some_array < 8\n", "array_gt_3_and_lt_8 = np.logical_and(array_gt_3, array_lt_8)\n", "array_gt_3_and_lt_8" ] }, { "cell_type": "code", "execution_count": 21, "id": "67046c12-8879-4157-88c7-4c0b07afe2f1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ True, True, True, True, False, False, False, False, True,\n", " True])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_not_gt_3_or_not_lt_8 = np.logical_or(\n", " np.logical_not(array_gt_3), np.logical_not(array_lt_8)\n", ")\n", "array_not_gt_3_or_not_lt_8" ] }, { "cell_type": "code", "execution_count": 22, "id": "2e59d8ff-6ff2-434d-9088-5d3d186ab84d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "#\n", "# Iteración sobre arrays\n", "# ===============================================\n", "#\n", "for value in range_array:\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 23, "id": "5a608802-47ab-45ae-8cf0-60b477dbc64f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.51209191, 1.37778694, 1.74292753],\n", " [1.47442418, 1.77997925, 1.38107561],\n", " [1.46678555, 1.45803285, 1.530237 ]])" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "[1.51209191 1.37778694 1.74292753]\n", "[1.47442418 1.77997925 1.38107561]\n", "[1.46678555 1.45803285 1.530237 ]\n", "\n", "1.5120919101970627\n", "1.3777869434537025\n", "1.7429275260775094\n", "1.4744241820936903\n", "1.779979249861668\n", "1.381075613619974\n", "1.4667855496143267\n", "1.4580328518115055\n", "1.5302369970895575\n" ] } ], "source": [ "#\n", "# Iteración sobre una matriz\n", "# ===============================================\n", "#\n", "\n", "display(first_matrix)\n", "\n", "print(\"\")\n", "\n", "for value in first_matrix:\n", " print(value)\n", "\n", "print(\"\")\n", "\n", "for value in np.nditer(first_matrix):\n", " print(value)" ] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 5 }