numpy.ndarray.view() in Python (original) (raw)
Last Updated : 01 Mar, 2024
**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
import
numpy as geek
a
=
geek.arange(
10
, dtype
=
'int16'
)
print
(
"a is: \n"
, a)
v
=
a.view(
'int32'
)
print
(
"\n After using view() with dtype = 'int32' a is : \n"
, a)
v
+
=
1
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
import
numpy as geek
a
=
geek.arange(
10
, dtype
=
'int16'
)
print
(
"a is:"
, a)
v
=
a.view(
'int16'
)
print
(
"\n After using view() with dtype = 'int16' a is :\n"
, a)
v
+
=
1
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
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]
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 itera 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