numpy.moveaxis() function | Python Last Updated : 09 Apr, 2025 Comments Improve Suggest changes Like Article Like Report numpy.moveaxis() function allows you to rearrange axes of an array. It is used when you need to shift dimensions of an array to different positions without altering the actual data. The syntax for the numpy.moveaxis() function is as follows:numpy.moveaxis(array, source, destination)Parameters:array: input array whose axes are to be moved.source: The original position(s) of the axis (or axes) that you want to move. This can be a single integer or a list/tuple of integers.destination: The new position(s) where the specified axes should be placed. Like source, this can also be a single integer or a list/tuple of integers.How numpy.moveaxis() Works?1. Moving a Single AxisIf you want to move a single axis of an array, specify the axis to move (source) and where to move it (destination). Python import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print("Original Array:") print(arr) print("Shape of Original Array: ", arr.shape) # Move axis 0 to position 2 moved_arr = np.moveaxis(arr, 0, 2) print("\nArray after moveaxis:") print(moved_arr) print("Shape of Array after moveaxis: ", moved_arr) Output : We have moved axis 0 (first axis) to position 2 (last position). result is an array where the original first dimension becomes the third dimension. 2. Moving Multiple AxesYou can also move multiple axes at once by passing sequences to both the source and destination parameters. Python import numpy as np arr = np.random.rand(2, 3, 4) result = np.moveaxis(arr, source=[0, 1], destination=[1, 0]) print("Original shape:", arr.shape) print("New shape:", result.shape) Output : Original shape: (2, 3, 4) New shape: (3, 2, 4)Why Use numpy.moveaxis()?Compared to transpose() or swapaxes(), moveaxis() is simple to use and rearrange the axes without complex permutations. moveaxis() allows easy rearrangement of one or multiple axes without modifying the data. The function does not create a new copy of the array but rather rearranges the axes. This makes it efficient when working with large datasets.By understanding the use of moveaxis(), you can streamline data manipulation tasks. Comment More info S sanjoy_62 Follow Improve Article Tags : Machine Learning AI-ML-DS Python-numpy Python numpy-arrayManipulation python AI-ML-DS With Python +2 More 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 Python4 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 Learning5 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