 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Roll the specified axis backwards until it lies in a given position in Numpy
To roll the specified axis backwards, until it lies in a given position, use the numpy.moveaxis() method in Python Numpy. Here,
- The 1st parameter is the Input array
- The 2nd parameter is the axis to be rolled. The positions of the other axes do not change relative to one another.
- The 3rd parameter is the start i.e. when start <= axis, the axis is rolled back until it lies in this position.
When start > axis, the axis is rolled until it lies before this position.
Steps
At first, import the required library −
import numpy as np
Create an array with ones −
arr = np.ones((2, 3, 4, 5))
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
To roll the specified axis backwards, until it lies in a given position, use the numpy.moveaxis() method −
print("
Result
",np.rollaxis(arr, 3, 1).shape)
print("
Result
",np.rollaxis(arr, 2).shape)
print("
Result
",np.rollaxis(arr, 1).shape)
print("
Result
",np.rollaxis(arr, 1, 4).shape)
Example
import numpy as np
# Create an array with ones
arr = np.ones((2, 3, 4, 5))
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# To roll the specified axis backwards, until it lies in a given position, use the numpy.moveaxis() method in Python Numpy
# Here, the 1st parameter is the Input array
# The 2nd parameter is the axis to be rolled. The positions of the other axes do not change relative to one another.
# The 3rd parameter is the start i.e. when start <= axis, the axis is rolled back until it lies in this position.
# When start > axis, the axis is rolled until it lies before this position.
print("
Result
",np.rollaxis(arr, 3, 1).shape)
print("
Result
",np.rollaxis(arr, 2).shape)
print("
Result
",np.rollaxis(arr, 1).shape)
print("
Result
",np.rollaxis(arr, 1, 4).shape)
Output
Array... [[[[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]] [[[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]]] Array datatype... float64 Array Dimensions... 4 Our Array Shape... (2, 3, 4, 5) Result (2, 5, 3, 4) Result (4, 2, 3, 5) Result (3, 2, 4, 5) Result (2, 4, 5, 3)
Advertisements
                    