numpy.ndarray.view() en Python

numpy.ndarray.view() ayuda a obtener una nueva vista de la array con los mismos datos.
 

Sintaxis: ndarray.view(dtype=None, type=None)
Parámetros:  
dtype : Descriptor de tipo de datos de la vista devuelta, por ejemplo, float32 o int16. El valor predeterminado, Ninguno, hace que la vista tenga el mismo tipo de datos que a. 
type : tipo Python, opcional
Devoluciones : ndarray o matrix.

Código #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)
Producción: 

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]

 

  
Código #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)
Producción: 

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]

 

  
Código #3: 
 

Python3

# Python program explaining 
# numpy.ndarray.view() function
 
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)

Publicación traducida automáticamente

Artículo escrito por ArkadipGhosh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *