Dada una array numpy 2d, la tarea es aplanar una array numpy 2d en una array 1d. A continuación se presentan algunos métodos para resolver la tarea.
Método #1: Usar np.flatten()
# Python code to demonstrate # flattening a 2d numpy array # into 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]]) # printing initial arrays print("initial array", str(ini_array1)) # Multiplying arrays result = ini_array1.flatten() # printing result print("New resulting array: ", result)
Producción:
initial array [[1 2 3] [2 4 5] [1 2 3]] New resulting array: [1 2 3 2 4 5 1 2 3]
Método #2: Usar np.ravel()
# Python code to demonstrate # flattening a 2d numpy array # into 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]]) # printing initial arrays print("initial array", str(ini_array1)) # Multiplying arrays result = ini_array1.ravel() # printing result print("New resulting array: ", result)
Producción:
initial array [[1 2 3] [2 4 5] [1 2 3]] New resulting array: [1 2 3 2 4 5 1 2 3]
Método #3: Usar np.reshape()
# Python code to demonstrate # flattening a 2d numpy array # into 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]]) # printing initial arrays print("initial array", str(ini_array1)) # Multiplying arrays result = ini_array1.reshape([1, 9]) # printing result print("New resulting array: ", result)
Producción:
initial array [[1 2 3] [2 4 5] [1 2 3]] New resulting array: [[1 2 3 2 4 5 1 2 3]]
Publicación traducida automáticamente
Artículo escrito por garg_ak0109 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA