numpy.squeeze() en Python

numpy.squeeze()La función se usa cuando queremos eliminar entradas unidimensionales de la forma de una array.

Sintaxis: numpy.squeeze(arr, eje=Ninguno)

Parámetros:
arr: [array_like] Array de entrada.
eje : [Ninguno o entero o tupla de enteros, opcional] Selecciona un subconjunto de las entradas unidimensionales en la forma. Si se selecciona un eje con una entrada de forma mayor que uno, se genera un error.

Retorno:
exprimido [ndarray] La array de entrada, pero con todas o un subconjunto de las dimensiones de longitud 1 eliminadas. Esto es siempre un sí mismo o una vista en arr.

Código #1:

# Python program explaining
# numpy.squeeze function
  
import numpy as geek
  
in_arr = geek.array([[[2, 2, 2], [2, 2, 2]]])
   
print ("Input array : ", in_arr) 
print("Shape of input array : ", in_arr.shape)  
  
out_arr = geek.squeeze(in_arr) 
  
print ("output squeezed array : ", out_arr)
print("Shape of output array : ", out_arr.shape) 

Producción :

Input array :  [[[2 2 2]
  [2 2 2]]]
Shape of input array :  (1, 2, 3)
output squeezed array :  [[2 2 2]
 [2 2 2]]
Shape of output array :  (2, 3)

 
Código #2:

# Python program explaining
# numpy.squeeze function
import numpy as geek
in_arr = geek.arange(9).reshape(1, 3, 3) 
  
print ("Input array : ", in_arr)  
out_arr = geek.squeeze(in_arr, axis = 0) 
  
print ("output array : ", out_arr)  
print("The shapes of Input and Output array : ") 
  
print(in_arr.shape, out_arr.shape)

Producción :

Input array :  [[[0 1 2]
  [3 4 5]
  [6 7 8]]]
output array :  [[0 1 2]
 [3 4 5]
 [6 7 8]]
The shapes of Input and Output array : 
(1, 3, 3) (3, 3)

Nota :

ValueError :
If axis is not None, and an axis being squeezed is not of length 1.

 
Código #3:

# Python program explaining
# numpy.squeeze function
# when value error occurs
import numpy as geek
  
in_arr = geek.arange(9).reshape(1, 3, 3) 
  
print ("Input array : ", in_arr)  
out_arr = geek.squeeze(in_arr, axis = 1) 
  
print ("output array : ", out_arr)  
print("The shapes of Input and Output array : ")
   
print(in_arr.shape, out_arr.shape)

Producción :

ValueError                                Traceback (most recent call last)
 in ()
      5 
      6 print ("Input array : ", in_arr)
----> 7 out_arr = geek.squeeze(in_arr, axis=1)
      8 print ("output array : ", out_arr)
      9 print("The shapes of Input and Output array : ")

~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in squeeze(a, axis)
   1196     try:
   1197         # First try to use the new axis= parameter
-> 1198         return squeeze(axis=axis)
   1199     except TypeError:
   1200         # For backwards compatibility

ValueError: cannot select an axis to squeeze out which has size not equal to one

Publicación traducida automáticamente

Artículo escrito por jana_sayantan 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 *