{ "cells": [ { "cell_type": "markdown", "id": "436f9cba", "metadata": {}, "source": [ "# Transpose arrays and swap axes\n", "\n", "Transpose is a special form of reshaping that also provides a view of the underlying data without copying anything. Arrays have the [Transpose](https://numpy.org/doc/stable/reference/generated/numpy.transpose.html) method and also the special [T](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.T.html) attribute:" ] }, { "cell_type": "code", "execution_count": 1, "id": "8f1170da", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "864383e9", "metadata": {}, "outputs": [], "source": [ "data = np.arange(16)" ] }, { "cell_type": "code", "execution_count": 3, "id": "1252c797", "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": "0184cebd", "metadata": {}, "outputs": [], "source": [ "reshaped_data = data.reshape((4, 4))" ] }, { "cell_type": "code", "execution_count": 5, "id": "3045e448", "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": "fce7877b", "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": "a056b80b", "metadata": {}, "source": [ "[numpy.dot](https://numpy.org/doc/stable/reference/generated/numpy.dot.html) returns the scalar product of two arrays, for example:" ] }, { "cell_type": "code", "execution_count": 7, "id": "a315f3cb", "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": "9748f100", "metadata": {}, "source": [ "The `@` infix operator is another way to perform matrix multiplication. It implements the semantics of the `@` operator introduced in Python 3.5 with [PEP 465](https://peps.python.org/pep-0465/) and is an abbreviation of [np.matmul](https://numpy.org/doc/stable/reference/generated/numpy.matmul.html)." ] }, { "cell_type": "code", "execution_count": 8, "id": "452cc86c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.int64(1240)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.T @ data" ] }, { "cell_type": "markdown", "id": "9ed92d44", "metadata": {}, "source": [ "For higher dimensional arrays, [transpose](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.transpose.html) accepts a tuple of axis numbers to swap the axes:" ] }, { "cell_type": "code", "execution_count": 9, "id": "eabf771b", "metadata": {}, "outputs": [], "source": [ "array_3d = np.arange(16).reshape((2, 2, 4))" ] }, { "cell_type": "code", "execution_count": 10, "id": "81d17b52", "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": "edff77ed", "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": "87fa49b0", "metadata": {}, "source": [ "Here the axes have been reordered with the second axis in first place, the first axis in second place and the last axis unchanged." ] }, { "cell_type": "markdown", "id": "1c0b351e", "metadata": {}, "source": [ "`ndarray` also has a [swapaxes](https://numpy.org/doc/stable/reference/generated/numpy.swapaxes.html) method that takes a pair of axis numbers and swaps the specified axes to rearrange the data:" ] }, { "cell_type": "code", "execution_count": 12, "id": "4d1abfc5", "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 }