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:
Python | Numpy ndarray.item()
Next article icon

numpy.ndarray.view() in Python

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

numpy.ndarray.view() helps to get a new view of array with the same data.
 

Syntax: ndarray.view(dtype=None, type=None)
Parameters: 
dtype : Data-type descriptor of the returned view, e.g., float32 or int16. The default, None, results in the view having the same data-type as a. 
type : Python type, optional
Returns : ndarray or matrix.


Code #1: 
 

Python3
# Python program explaining   # numpy.ndarray.view() function   import numpy as geek  a = geek.arange(10, dtype ='int16')  print("a is: \n", a)  # using view() method v = a.view('int32') print("\n After using view() with dtype = 'int32' a is : \n", a)  v += 1  # addition of 1 to each element of v print("\n After using view() with dtype = 'int32' and adding 1 a is : \n", a) 

Output
a is:   [0 1 2 3 4 5 6 7 8 9]   After using view() with dtype = 'int32' a is :   [0 1 2 3 4 5 6 7 8 9]   After using view() with dtype = 'int32' and adding 1 a is :   [1 1 3 3 5 5 7 7 9 9]

  
Code #2: 
 

Python3
# Python program explaining   # numpy.ndarray.view() function   import numpy as geek  a = geek.arange(10, dtype ='int16') print("a is:", a)  # Using view() method v = a.view('int16') print("\n After using view() with dtype = 'int16' a is :\n", a)  v += 1 # addition of 1 to each element of v print("\n After using view() with dtype = 'int16' and adding 1 a is : \n", a) 

Output
a is: [0 1 2 3 4 5 6 7 8 9]   After using view() with dtype = 'int16' a is :  [0 1 2 3 4 5 6 7 8 9]   After using view() with dtype = 'int16' and adding 1 a is :   [ 1  2  3  4  5  6  7  8  9 10]

  
Code #3: 

Python3
import numpy as geek  a = geek.arange(10, dtype ='int16') print("a is: \n", a)  v = a.view('int8') print("\n After using view() with dtype = 'int8' a is : \n", a)  v += 1 # addition of 1 to each element of v print("\n After using view() with dtype = 'int8' and adding 1 a is : \n", a) 

Output:

a is: 
[0 1 2 3 4 5 6 7 8 9] After using view() with dtype = 'int8' a is :
[0 1 2 3 4 5 6 7 8 9] After using view() with dtype = 'int8' and adding 1 a is :
[257 258 259 260 261 262 263 264 265 266]

Next Article
Python | Numpy ndarray.item()

A

ArkadipGhosh
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-ndarray
Practice Tags :
  • python

Similar Reads

  • Python | Numpy ndarray.__iand__()
    With the help of Numpy ndarray.__iand__() method, we can get the elements that is anded by the value that is provided as a parameter in numpy.ndarray.__iand__() method. Syntax: ndarray.__iand__($self, value, /) Return: self&=value Example #1 : In this example we can see that every element is and
    1 min read
  • Python | Numpy ndarray.__ior__()
    With the help of Numpy ndarray.__ior__() method, we can get the elements that is OR by the value that is provided as a parameter in numpy.ndarray.__ior__() method. Syntax: ndarray.__ior__($self, value, /) Return: self|=value Example #1 : In this example we can see that every element is or by the val
    1 min read
  • Python | Numpy ndarray.item()
    With the help of numpy.ndarray.item() method, we can fetch the data elements that is found at the given index on numpy array. Remember we can give index as one dimensional parameter or can be two dimensional. Parameters: *args : Arguments (variable number and type) -> none: This argument only works
    1 min read
  • Python | Numpy ndarray.__imod__()
    With the help of Numpy ndarray.__imod__(), every element in an array is operated on binary operator i.e mod(%). Remember we can use any type of values in an array and value for mod is applied as the parameter in ndarray.__imod__(). Syntax: ndarray.__imod__($self, value, /) Return: self%=value Exampl
    1 min read
  • Python | Numpy ndarray.__ipow__()
    With the help of Numpy ndarray.__ipow__() method, we will get all the elements powered with the value that is provided as a parameter in numpy.ndarray.__ipow__() method. Syntax: ndarray.__ipow__($self, value, /) Return: self**=value Example #1 : In this example we can see that every element get powe
    1 min read
  • Python | Numpy ndarray.__ixor__()
    With the help of Numpy ndarray.__ixor__() method, we can get the elements that is XOR by the value that is provided as a parameter in numpy.ndarray.__ixor__() method. Syntax: ndarray.__ixor__($self, value, /) Return: self^=value Example #1 : In this example we can see that every element is xor by th
    1 min read
  • numpy.ndarray.flat() in Python
    The numpy.ndarray.flat() function is used as a 1_D iterator over N-dimensional arrays. It is not a subclass of, Python’s built-in iterator object, otherwise it a numpy.flatiter instance. Syntax :  numpy.ndarray.flat() Parameters :  index : [tuple(int)] index of the values to iterate Return :   1-D i
    3 min read
  • numpy.ndarray.fill() in Python
    numpy.ndarray.fill() method is used to fill the numpy array with a scalar value. If we have to initialize a numpy array with an identical value then we use numpy.ndarray.fill(). Suppose we have to create a NumPy array a of length n, each element of which is v. Then we use this function as a.fill(v).
    2 min read
  • numpy.ndarray.resize() function - Python
    numpy.ndarray.resize() function change shape and size of array in-place. Syntax : numpy.ndarray.resize(new_shape, refcheck = True) Parameters : new_shape :[tuple of ints, or n ints] Shape of resized array. refcheck :[bool, optional] If False, reference count will not be checked. Default is True. Ret
    1 min read
  • Numpy ndarray.setfield() function | Python
    numpy.ndarray.setfield() function Put a value into a specified place in a field defined by a data-type. Place val into a’s field defined by dtype and beginning offset bytes into the field. Syntax : numpy.ndarray.setfield(val, dtype, offset=0) Parameters : val : [object] Value to be placed in field.
    1 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