Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.var() in Python
Next article icon

numpy.roll() in Python

Last Updated : 08 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The numpy.roll() function rolls array elements along the specified axis. Basically what happens is that elements of the input array are being shifted. If an element is being rolled first to the last position, it is rolled back to the first position. 
 

Syntax : 

numpy.roll(array, shift, axis = None)

Parameters : 

array : [array_like][array_like]Input array, whose elements we want to roll  shift : [int or int_tuple]No. of times we need to shift array elements.          If a tuple, then axis must be a tuple of the same size, and each of the given           axes is shifted          by the corresponding number.           If an int while axis is a tuple of ints, then the same value is used for all given axes.  axis  :  [array_like]Plane, along which we wish to roll array or shift it's elements.

Return : 

Output rolled array, with the same shape as a.

 

Python




# Python Program illustrating
# numpy.roll() method
   
import numpy as geek
   
array = geek.arange(12).reshape(3, 4)
print("Original array : \n", array)
   
# Rolling array; Shifting one place
print("\nRolling with 1 shift : \n", geek.roll(array, 1))
  
# Rolling array; Shifting five places
print("\nRolling with 5 shift : \n", geek.roll(array, 5))
  
# Rolling array; Shifting five places with 0th axis
print("\nRolling with 2 shift with 0 axis : \n", geek.roll(array, 2, axis = 0))
 
 

Output : 
 

Original array :    [[ 0  1  2  3]   [ 4  5  6  7]   [ 8  9 10 11]]    Rolling with 1 shift :    [[11  0  1  2]   [ 3  4  5  6]   [ 7  8  9 10]]    Rolling with 5 shift :    [[ 7  8  9 10]   [11  0  1  2]   [ 3  4  5  6]]    Rolling with 2 shift with 0 axis :    [[ 4  5  6  7]   [ 8  9 10 11]   [ 0  1  2  3]]

These codes won’t run on online IDE’s. So please, run them on your systems to explore the working.

 



Next Article
numpy.var() in Python
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Python
  • Python numpy-arrayManipulation
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • numpy.rot90() in Python
    The numpy.rot90() method performs rotation of an array by 90 degrees in the plane specified by axis(0 or 1). Syntax: numpy.rot90(array, k = 1, axes = (0, 1)) Parameters : array : [array_like]i.e. array having two or more dimensions. k : [optional , int]No. of times we wish to rotate array by 90 degr
    2 min read
  • numpy.round_() in Python
    The round_() function in NumPy rounds the elements of an array to a specified number of decimal places. This function is extremely useful when working with floating-point numbers and when precision is important in scientific computing or data analysis. Syntax: numpy.round_(arr, decimals=0, out=None)
    3 min read
  • numpy.var() in Python
    numpy.var(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any). Example : x = 1 1 1 1 1 Standard Deviation = 0 . Variance = 0 y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of distribution 4 = 7 Step 2 : Summat
    3 min read
  • numpy.sum() in Python
    This function returns the sum of array elements over the specified axis. Syntax: numpy.sum(arr, axis, dtype, out): Parameters: arr: Input array. axis: The axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along
    3 min read
  • numpy.std() in Python
    numpy.std() is a function provided by the NumPy library that calculates the standard deviation of an array or a set of values. Standard deviation is a measure of the amount of variation or dispersion of a set of values. [Tex]\text{Standard Deviation} = \sqrt{\text{mean} \left( (x - x.\text{mean}())^
    4 min read
  • numpy.repeat() in Python
    The numpy.repeat() function repeats elements of the array - arr. Syntax : numpy.repeat(arr, repetitions, axis = None) Parameters : array : [array_like]Input array. repetitions : No. of repetitions of each array elements along the given axis. axis : Axis along which we want to repeat values. By defau
    2 min read
  • numpy.rollaxis() function | Python
    numpy.rollaxis() function roll the specified axis backwards, until it lies in a given position. Syntax : numpy.rollaxis(arr, axis, start=0) Parameters : arr : [ndarray] Input array. axis : [int] The axis to roll backwards. The positions of the other axes do not change relative to one another. start
    1 min read
  • numpy.take() in Python
    The numpy.take() function returns elements from array along the mentioned axis and indices. Syntax: numpy.take(array, indices, axis = None, out = None, mode ='raise') Parameters : array : array_like, input array indices : index of the values to be fetched axis : [int, optional] axis over which we ne
    2 min read
  • numpy.reshape() in Python
    In Python, numpy.reshape() function is used to give a new shape to an existing NumPy array without changing its data. It is important for manipulating array structures in Python. Let's understand with an example: [GFGTABS] Python import numpy as np # Creating a 1D NumPy array arr = np.array([1, 2, 3
    3 min read
  • numpy.stack() in Python
    NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack(). Important points:stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.to join 2 arrays, they must
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences