{ "cells": [ { "cell_type": "markdown", "id": "a6ac8738", "metadata": {}, "source": [ "# Transponieren von Arrays und Vertauschen von Achsen\n", "\n", "Transponieren ist eine spezielle Form der Umformung, die ebenfalls eine Sicht auf die zugrunde liegenden Daten liefert, ohne etwas zu kopieren. Arrays haben die [Transpose](https://numpy.org/doc/stable/reference/generated/numpy.transpose.html)-Methode und auch das spezielle [T](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.T.html)-Attribut:" ] }, { "cell_type": "code", "execution_count": 1, "id": "c2aa7360", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "6d01c45c", "metadata": {}, "outputs": [], "source": [ "data = np.arange(16)" ] }, { "cell_type": "code", "execution_count": 3, "id": "a2254933", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 4, "id": "8414fe8e", "metadata": {}, "outputs": [], "source": [ "reshaped_data = data.reshape((4, 4))" ] }, { "cell_type": "code", "execution_count": 5, "id": "8e645294", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reshaped_data" ] }, { "cell_type": "code", "execution_count": 6, "id": "adab9773", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 4, 8, 12],\n", " [ 1, 5, 9, 13],\n", " [ 2, 6, 10, 14],\n", " [ 3, 7, 11, 15]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reshaped_data.T" ] }, { "cell_type": "markdown", "id": "9bac6da9", "metadata": {}, "source": [ "[numpy.dot](https://numpy.org/doc/stable/reference/generated/numpy.dot.html) gibt das Skalarprodukt zweier Arrays zurück, z.B.:" ] }, { "cell_type": "code", "execution_count": 7, "id": "991b6e00", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[224, 248, 272, 296],\n", " [248, 276, 304, 332],\n", " [272, 304, 336, 368],\n", " [296, 332, 368, 404]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(reshaped_data.T, reshaped_data)" ] }, { "cell_type": "markdown", "id": "05e2cf64", "metadata": {}, "source": [ "Der `@` Infix-Operator ist eine weitere Möglichkeit, eine Matrixmultiplikation durchzuführen. Er implementiert die Semantik des `@`-Operators, der mit [PEP 465](https://peps.python.org/pep-0465/) in Python 3.5 eingeführt wurde und ist eine Abkürzung von [np.matmul](https://numpy.org/doc/stable/reference/generated/numpy.matmul.html)." ] }, { "cell_type": "code", "execution_count": 8, "id": "3cf100a4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(1240)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.T @ data" ] }, { "cell_type": "markdown", "id": "dea9bca7", "metadata": {}, "source": [ "Bei höherdimensionalen Arrays akzeptiert [transpose](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.transpose.html) ein Tupel von Achsennummern, um die Achsen zu vertauschen:" ] }, { "cell_type": "code", "execution_count": 9, "id": "da25d773", "metadata": {}, "outputs": [], "source": [ "array_3d = np.arange(16).reshape((2, 2, 4))" ] }, { "cell_type": "code", "execution_count": 10, "id": "faa81bd8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7]],\n", "\n", " [[ 8, 9, 10, 11],\n", " [12, 13, 14, 15]]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_3d" ] }, { "cell_type": "code", "execution_count": 11, "id": "abbc8991", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 1, 2, 3],\n", " [ 8, 9, 10, 11]],\n", "\n", " [[ 4, 5, 6, 7],\n", " [12, 13, 14, 15]]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_3d.transpose((1, 0, 2))" ] }, { "cell_type": "markdown", "id": "0bfeeae0", "metadata": {}, "source": [ "Hier wurden die Achsen neu geordnet, wobei die zweite Achse an erster Stelle steht, die erste Achse an zweiter Stelle und die letzte Achse unverändert bleibt." ] }, { "cell_type": "markdown", "id": "31ef0d22", "metadata": {}, "source": [ "`ndarray` verfügt auch über eine Methode [swapaxes](https://numpy.org/doc/stable/reference/generated/numpy.swapaxes.html), die ein Paar von Achsennummern nimmt und die angegebenen Achsen vertauscht, um die Daten neu anzuordnen:" ] }, { "cell_type": "code", "execution_count": 12, "id": "e69e3ca2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 0, 4],\n", " [ 1, 5],\n", " [ 2, 6],\n", " [ 3, 7]],\n", "\n", " [[ 8, 12],\n", " [ 9, 13],\n", " [10, 14],\n", " [11, 15]]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_3d.swapaxes(1, 2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }