numpy.swapaxes() function - Python Last Updated : 22 Apr, 2025 Comments Improve Suggest changes Like Article Like Report numpy.swapaxes() function allow us to interchange two axes of a multi-dimensional NumPy array. It focuses on swapping only two specified axes while leaving the rest unchanged. It is used to rearrange the structure of an array without altering its actual data. The syntax of numpy.swapaxes() is:numpy.swapaxes(array, axis1, axis2)where:array : is the input array whose axes are to be swapped.axis1 : The first axis to be swapped.axis2 : The second axis to be swapped with axis1.1. Swapping Axes in a 2D ArrayFor a 2D array, swapping axes is basically transposing the array. Python import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.swapaxes(arr, axis1=0, axis2=1) print("Original array:\n", arr) print("Array after swapping axes:\n", result) Output:Swapping Axis in 2D Array2. Swapping Axes in a 3D ArraySuppose you have a 3D array with shape (2, 3, 4) and you want to swap the first axis (axis 0) with the last axis (axis 2). Python import numpy as np arr = np.random.rand(2, 3, 4) result = np.swapaxes(arr, axis1=0, axis2=2) print("Original shape:", arr.shape) print("New shape:", result.shape) Output : Swapping Axis in 3D ArrayIn this example the axis at position 0 (size 2) has been swapped with the axis at position 2 (size 4) resulting in a new shape of (4, 3, 2).Comparison with Other FunctionsWhile numpy.swapaxes() is ideal for swapping two axes there are other functions in NumPy that perform similar tasks:1. numpy.transpose() :Reorders all axes based on a given sequence using matrix transpose.Useful for complete transpose but less flexible than swapaxes() for specified axis swapping.2. numpy.moveaxis() :Moves one or more axes to new positions.More versatile than swapaxes() but requires specifying source and destination axes.For scenarios requiring simple interchange of two axes numpy.swapaxes() is the most efficient method. Comment More info S sanjoy_62 Follow Improve Article Tags : Machine Learning Python-numpy Python numpy-arrayManipulation python Explore Machine Learning BasicsIntroduction to Machine Learning8 min readTypes of Machine Learning13 min readWhat is Machine Learning Pipeline?7 min readApplications of Machine Learning3 min readPython for Machine LearningMachine Learning with Python Tutorial5 min readNumPy Tutorial - Python Library3 min readPandas Tutorial6 min readData Preprocessing in Python3 min readEDA - Exploratory Data Analysis in Python6 min readFeature EngineeringWhat is Feature Engineering?5 min readIntroduction to Dimensionality Reduction4 min readFeature Selection Techniques in Machine Learning6 min readSupervised LearningSupervised Machine Learning7 min readLinear Regression in Machine learning15+ min readLogistic Regression in Machine Learning11 min readDecision Tree in Machine Learning9 min readRandom Forest Algorithm in Machine Learning5 min readK-Nearest Neighbor(KNN) Algorithm8 min readSupport Vector Machine (SVM) Algorithm9 min readNaive Bayes Classifiers7 min readUnsupervised LearningWhat is Unsupervised Learning5 min readK means Clustering â Introduction6 min readHierarchical Clustering in Machine Learning6 min readDBSCAN Clustering in ML - Density based clustering6 min readApriori Algorithm6 min readFrequent Pattern Growth Algorithm5 min readECLAT Algorithm - ML3 min readPrincipal Component Analysis(PCA)7 min readModel Evaluation and TuningEvaluation Metrics in Machine Learning9 min readRegularization in Machine Learning7 min readCross Validation in Machine Learning7 min readHyperparameter Tuning7 min readML | Underfitting and Overfitting5 min readBias and Variance in Machine Learning10 min readAdvanced TechniquesReinforcement Learning8 min readSemi-Supervised Learning in ML5 min readSelf-Supervised Learning (SSL)6 min readEnsemble Learning8 min readMachine Learning PracticeTop 50+ Machine Learning Interview Questions and Answers15+ min read100+ Machine Learning Projects with Source Code [2025]6 min read Like