{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "uYJgXVaXMXjG" }, "source": [ "# NumPy [2] Funciones sobre matrices" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "91raueFPMXjL", "outputId": "878d6806-279e-4f22-c5d2-cc0b32f8b6e4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [ 5, 6, 7, 8, 9],\n", " [10, 11, 12, 13, 14]])" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "import numpy as np # importamos la librería NumPy como np\n", "\n", "m = np.arange(15).reshape(3, 5) # creamos una matriz 2D\n", "m" ] }, { "cell_type": "markdown", "metadata": { "id": "t9fN-hMTMXjR" }, "source": [ "### shape\n", "Proporciona el número de filas × columnas." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "GGCKB4EmMXjU", "outputId": "d5525f20-d6cf-432b-d208-65c36d1fe2ed", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(3, 5)" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "m.shape" ] }, { "cell_type": "markdown", "metadata": { "id": "UzdUmnb-MXjW" }, "source": [ "### ndim\n", "Proporciona la dimensión de la matriz (número de ejes)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "2YOjl3MlMXjX", "outputId": "5fdc2365-2f03-4d1f-983d-6b989fa0114c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "m.ndim" ] }, { "cell_type": "markdown", "metadata": { "id": "DstX0v37MXjZ" }, "source": [ "### size\n", "Número de elementos que incluye una matriz." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "AThoab-pMXja", "outputId": "adc1cfc0-fb12-4786-82c9-a448d598c731", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "15" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "m.size" ] }, { "cell_type": "markdown", "metadata": { "id": "GoXZxHkYMXjd" }, "source": [ "### zeros\n", "Crea una matriz de ceros." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "Vqr9T1C3MXjg", "outputId": "a7911c4d-b04e-4f16-b8f5-8d247a2e3ce7", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0.],\n", " [0., 0., 0., 0., 0.]])" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "matriz = np.zeros((3, 5))\n", "matriz" ] }, { "cell_type": "markdown", "metadata": { "id": "Ytvg_RsTMXjj" }, "source": [ "### linspace\n", "Genera una matriz con números secuenciales entre uno inicial y uno final, indicando la cantidad de números deseada. \n", "* Inicio en 10\n", "* Final en 14\n", "* Número de elementos 5" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "WVqipr2BMXjl", "outputId": "d9503f2b-635e-41a0-e0c1-5428950c86eb", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([10., 11., 12., 13., 14.])" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "m=np.linspace(10, 14, 5)\n", "m" ] }, { "cell_type": "markdown", "metadata": { "id": "ygnZKWj8MXjm" }, "source": [ "* Inicio en 90\n", "* Final en 20\n", "* Número de elementos 5" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "KslWN_IPMXjn", "outputId": "56eddecc-0058-4543-82db-990cf841c703", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([90. , 72.5, 55. , 37.5, 20. ])" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "m=np.linspace(90, 20, 5)\n", "m" ] }, { "cell_type": "markdown", "metadata": { "id": "URmJLVanMXjp" }, "source": [ "## Matriz 3D\n", "Una matriz de tres dimensiones requiere: hojas × filas × columnas. A la tercera dimensión se la llama habitualmente hojas o páginas." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "RoQUpbNSMXjq", "outputId": "9c54f638-2d1d-44a6-a3aa-2f09ab508ee8", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]],\n", "\n", " [[12, 13, 14, 15],\n", " [16, 17, 18, 19],\n", " [20, 21, 22, 23]]])" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "m3d = np.arange(24).reshape(2, 3, 4)\n", "m3d" ] }, { "cell_type": "markdown", "metadata": { "id": "edxry61UMXjq" }, "source": [ "## Operar con arrays" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "Wf0KPjVRMXjt", "outputId": "4d8a87fe-0340-45e3-9f0a-866da5758313", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "array_pares=np.arange(0, 20, 2)\n", "array_pares" ] }, { "cell_type": "markdown", "metadata": { "id": "NQcYfetkMXju" }, "source": [ "### Operaciones aritméticas" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "Hq4c7H6CMXjv", "outputId": "17ef7e8c-fd47-496d-e932-ffdd5e1329d4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "array_impares=array_pares + 1 # suma 1 a todos los elementos y lo convierte en un array de impares\n", "array_impares" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "TB17uB6LMXjw", "outputId": "093ecb24-9f94-4357-ed5e-d0a82553874a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 10, 30, 50, 70, 90, 110, 130, 150, 170, 190])" ] }, "metadata": {}, "execution_count": 11 } ], "source": [ "array_impares * 10 # multiplicar" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "0DylGrKkMXjw", "outputId": "896815e4-5609-4f1e-e7f0-f0eb77e3bb20", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "array_impares - array_pares # restar arrays" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "OW2Sd5W6MXjx" }, "outputs": [], "source": [ "#array_impares / array_pares # dividir arrays da error ya que dividen entre cero" ] }, { "cell_type": "markdown", "metadata": { "id": "1swsX1AWMXjy" }, "source": [ "### Estadística" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "NzpnUr9EMXj0", "outputId": "9f9fd6d9-c077-4af7-aac4-b06cd0c259de", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29])" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "numeros_primos = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\n", "array_primos = np.array(numeros_primos)\n", "array_primos" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "id": "lZUKOgxiMXj0", "outputId": "5901ccf2-8230-4502-910e-4cb5a3d32a2e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "129" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "array_primos.sum() # suma" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "mdasxcrSMXj1", "outputId": "467dbd5b-e335-4d4c-8f19-c5eb218121c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "12.9" ] }, "metadata": {}, "execution_count": 16 } ], "source": [ "array_primos.mean() # media" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "id": "Xwm6QLz5MXj2", "outputId": "78a14f96-1b52-4b90-8b5e-c8d64ef6240e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "73.28999999999999" ] }, "metadata": {}, "execution_count": 17 } ], "source": [ "array_primos.var() # varianza" ] }, { "cell_type": "markdown", "metadata": { "id": "1UfzQQGBMXj2" }, "source": [ "### Ordenación de arrays" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "id": "HqxmmUFtMXj3", "outputId": "e1e20c71-88b5-46d6-d17a-24a9cfd71ba9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]\n" ] } ], "source": [ "# Creamos la serie de Fibonacci\n", "fibo = [0, 1]\n", "for i in range (11):\n", " fibo.append(fibo[-1] + fibo[-2])\n", "print(fibo)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "UJmbzjHpMXj4", "outputId": "63590cf3-c85c-4d87-c9de-d47b3cf2f499", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144])" ] }, "metadata": {}, "execution_count": 19 } ], "source": [ "# Convertimos la lista en un ndarray\n", "fibonacci = np.array(fibo)\n", "fibonacci" ] }, { "cell_type": "markdown", "metadata": { "id": "2BAFlAKYMXj4" }, "source": [ "#### Desordenamos" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "wTgJfzxeMXj5", "outputId": "0c62efc0-1bc0-4b0f-b7b5-e7417ba63173", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 1, 144, 3, 34, 89, 13, 21, 55, 1, 8, 0, 2, 5])" ] }, "metadata": {}, "execution_count": 20 } ], "source": [ "# desordenamos el array\n", "np.random.shuffle(fibonacci) # permite desordenar un array\n", "fibonacci" ] }, { "cell_type": "markdown", "metadata": { "id": "3ZMDUS4PMXj6" }, "source": [ "#### Ordenamos" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "id": "2OCcn3A8MXj7", "outputId": "32236e1c-f9de-420e-bf0b-9d588b03e0d1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144])" ] }, "metadata": {}, "execution_count": 21 } ], "source": [ "# ordenar un array\n", "np.sort(fibonacci) # ordena el vector, pero no queda asignado al array fibonacci" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "id": "0H9naCVPMXj8", "outputId": "c99bae84-f321-471e-d830-8612736f825a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 1, 144, 3, 34, 89, 13, 21, 55, 1, 8, 0, 2, 5])" ] }, "metadata": {}, "execution_count": 22 } ], "source": [ "fibonacci # comprobamos que no queda asignado el vector ordenado a la variable fibonacci" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "id": "SWLXyGMOMXj9", "outputId": "af1fdcfa-611b-40ca-f3d3-993a8e895547", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144])" ] }, "metadata": {}, "execution_count": 23 } ], "source": [ "ordenado = np.sort(fibonacci) # asignamos el array a la variable ordenado\n", "ordenado" ] }, { "cell_type": "markdown", "metadata": { "id": "qB3eot_DMXj-" }, "source": [ "#### Reverso" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "id": "uzDSwSrAMXj_", "outputId": "8d71ca95-5017-45bf-95fc-7d0d19f2ee35", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 1, 0])" ] }, "metadata": {}, "execution_count": 24 } ], "source": [ "inverso=ordenado[::-1]\n", "inverso" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "id": "wg1DOpeIMXkA", "outputId": "e6ebf00c-c075-4067-cdb1-041e76069111", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144])" ] }, "metadata": {}, "execution_count": 25 } ], "source": [ "inverso.sort() # si ordenamos el array inverso quedará ordenado de menor a mayor\n", "inverso" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "id": "XcSma2oCMXkA", "outputId": "188ad7af-7087-4b15-924b-6bd0accacf7e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ True, True, True, True, True, True, True, True, True,\n", " True, True, True, True])" ] }, "metadata": {}, "execution_count": 26 } ], "source": [ "inverso == np.array(fibo) # una comprobación" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.11" }, "colab": { "name": "0040_numpy2_matrices.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 0 }