[Python] Swapping rows and columns in Numpy data

Premise

Suppose you are doing the following in all

import numpy as np

Creating a 3x3 matrix

arr = np.arange(9).reshape((3,3))

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

Transpose of matrix

arr.T

array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])
Transpose rows and columns
arr.transpose()

array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])
arr.transpose((1,0))

array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])
Sapaxes to swap axes

Used when swapping two axes

arr.swapaxes(1,0)

array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])

arr.swapaxes(0,1)

array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])

Matrix multiplication

np.dot(arr.T,arr)

array([[45, 54, 63],
       [54, 66, 78],
       [63, 78, 93]])

3D matrix replacement

Three 2x2 matrices are stacked

arr3d = np.arange(12).reshape((3,2,2)) 
arr3d

array([[[ 0,  1],
        [ 2,  3]],

       [[ 4,  5],
        [ 6,  7]],

       [[ 8,  9],
        [10, 11]]])

arr3d[0]

array([[0, 1],
       [2, 3]])
Try using transpose on a 3D matrix

1st argument: 0 → Do nothing for 3 2x2 matrices Second argument: Transpose 2 → 2x2 matrix Third argument: 1 → Transpose 2x2 matrix

arr3d.transpose((0,2,1))

array([[[ 0,  2],
        [ 1,  3]],

       [[ 4,  6],
        [ 5,  7]],

       [[ 8, 10],
        [ 9, 11]]])

Recommended Posts

[Python] Swapping rows and columns in Numpy data
Hashing data in R and Python
Easily graph data in shell and Python
Python and numpy tips
Python variables and data types learned in chemoinformatics
Receive and display HTML form data in Python
Swapping values in Python
[Python] Sorting Numpy data
Add totals to rows and columns in pandas
Full-width and half-width processing of CSV data in Python
numpy memorandum 2 / transpose doesn't just swap rows and columns
Handle Ambient data in Python
Matrix multiplication in python numpy
Stack and Queue in Python
Unittest and CI in Python
Graph time series data in Python using pandas and matplotlib
[Python] How to delete rows and columns in a table (list of drop method options)
Get Leap Motion data in Python.
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Read Protocol Buffers data in Python3
Sorting algorithm and implementation in Python
Put python, numpy, opencv3 in ubuntu14
Manipulate files and folders in Python
About dtypes in Python and Cython
Handle NetCDF format data in Python
Self-organizing map in Python NumPy version
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
processing to use notMNIST data in Python (and tried to classify it)
[Python] How to add rows and columns to a table (pandas DataFrame)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Overlapping regular expressions in Python and Java
Get additional data in LDAP with python
Data pipeline construction with Python and Luigi
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Data input / output in Python (CSV, JSON)
Differences between Ruby and Python in scope
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Try working with binary data in Python
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python