{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "dcc3965b", "metadata": { "id": "dcc3965b" }, "source": [ "# NumPy [6] Ejes\n", "Para matrices 2D:\n", "* axis 0 para las columnas\n", "* axis 1 para las filas\n", "\n", "Para matrices en una dimensión se trabaja con un vector fila:\n", "* En este caso el eje siempre será axis 0" ] }, { "cell_type": "markdown", "id": "50573a39", "metadata": { "id": "50573a39" }, "source": [ "## Trabajando con matrices 2D" ] }, { "cell_type": "markdown", "id": "343b8eca", "metadata": { "id": "343b8eca" }, "source": [ "### Suma en un eje" ] }, { "cell_type": "code", "execution_count": null, "id": "ce88184e", "metadata": { "id": "ce88184e", "outputId": "58cf460b-5466-4913-db4b-515f103ffd01" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "m = np.array([[1,2,3], [4,5,6]])\n", "print(m)" ] }, { "cell_type": "code", "execution_count": null, "id": "81b6729d", "metadata": { "id": "81b6729d", "outputId": "1983cee6-4fd1-4ac4-9116-e82b3debf7a3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5 7 9]\n" ] } ], "source": [ "print(np.sum(m, axis=0)) # sumando columnas" ] }, { "cell_type": "code", "execution_count": null, "id": "1ba3bc6f", "metadata": { "id": "1ba3bc6f", "outputId": "2a3cf240-c374-4c4e-894f-3f423fda0c59" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 6 15]\n" ] } ], "source": [ "print(np.sum(m, axis=1)) # sumando filas" ] }, { "cell_type": "markdown", "id": "79f72f70", "metadata": { "id": "79f72f70" }, "source": [ "### Concatenar por ejes\n", "Antes de concatenar preparamos las matrices m1 y m2." ] }, { "cell_type": "code", "execution_count": null, "id": "f29106d1", "metadata": { "id": "f29106d1", "outputId": "fc77540e-935e-4075-8f90-da9d0b624be4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]]\n", "\n", "[[10 20 30]\n", " [40 50 60]]\n" ] } ], "source": [ "m1 = np.array([[1,2,3], [4,5,6]])\n", "m2 = np.array([[10,20,30], [40,50,60]])\n", "print(m1) # mostramos m1\n", "print('')\n", "print(m2) # mostramos m2" ] }, { "cell_type": "markdown", "id": "57cefda0", "metadata": { "id": "57cefda0" }, "source": [ "#### Concatenamos por columnas" ] }, { "cell_type": "code", "execution_count": null, "id": "3d025e55", "metadata": { "id": "3d025e55", "outputId": "56dae580-d76d-44cb-b4b8-b22d9405d814" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3]\n", " [ 4 5 6]\n", " [10 20 30]\n", " [40 50 60]]\n" ] } ], "source": [ "print(np.concatenate([m1, m2], axis=0)) # concatenamos por columnas" ] }, { "cell_type": "markdown", "id": "61f4642b", "metadata": { "id": "61f4642b" }, "source": [ "#### Concatenamos por filas" ] }, { "cell_type": "code", "execution_count": null, "id": "a376fd30", "metadata": { "id": "a376fd30", "outputId": "7f1b9cd3-b5ce-45b8-8e5f-007847d319df" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 10 20 30]\n", " [ 4 5 6 40 50 60]]\n" ] } ], "source": [ "print(np.concatenate([m1, m2], axis=1)) # concatenamos por filas" ] }, { "cell_type": "markdown", "id": "5940f6ea", "metadata": { "id": "5940f6ea" }, "source": [ "## Trabajando con un vector\n", "Trabajando con una matriz de una dimensión o vector fila. El único eje que existe es el axis 0." ] }, { "cell_type": "markdown", "id": "0363877e", "metadata": { "id": "0363877e" }, "source": [ "### Concatenar vectores\n", "Los vectores siempre son de tipo fila. \n", "Se trata de concatenar dos vectores fila." ] }, { "cell_type": "code", "execution_count": null, "id": "946c108c", "metadata": { "id": "946c108c", "outputId": "89ce1bdd-bfbf-4c11-cf51-ce729444b158" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "m1 = np.array([1, 2, 3])\n", "m2 = np.array([4, 5, 6])\n", "print(np.concatenate([m1, m2])) # sin poner lo del eje" ] }, { "cell_type": "code", "execution_count": null, "id": "15914d4e", "metadata": { "id": "15914d4e", "outputId": "3f0e86f3-7ba4-4931-ad1c-ada968effb42" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]\n" ] } ], "source": [ "print(np.concatenate([m1, m2], axis=0)) # con el axis 0" ] }, { "cell_type": "code", "execution_count": null, "id": "3e87969a", "metadata": { "id": "3e87969a" }, "outputs": [], "source": [ "#print(np.concatenate([m1,m2], axis=1)) # ERROR: con el axis 1" ] } ], "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": "0080_numpy6_ejes.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }