numpy.append() en Python

El numpy.append() agrega valores a lo largo del eje mencionado al final de la
sintaxis de la array:

numpy.append(array, values, axis = None)

Parámetros:

array   : [array_like]Input array. 
values  : [array_like]values to be added in the arr. Values should be 
     shaped so that arr[...,obj,...] = values. If the axis is defined values can be of any
     shape as it will be flattened before use.
axis    : Axis along which we want to insert the values. By default, array
     is flattened.    

Devolver :

An copy of array with values being appended at the end as per the mentioned object
along a given axis. 

Código 1: Agregar arrays

# Python Program illustrating
# numpy.append()
  
import numpy as geek
  
#Working on 1D
arr1 = geek.arange(5)
print("1D arr1 : ", arr1)
print("Shape : ", arr1.shape)
  
  
arr2 = geek.arange(8, 12)
print("\n1D arr2 : ", arr2)
print("Shape : ", arr2.shape)
  
  
# appending the arrays
arr3 = geek.append(arr1, arr2)
print("\nAppended arr3 : ", arr3)

Producción :

1D arr1 :  [0 1 2 3 4]
Shape :  (5,)

1D arr2 :  [ 8  9 10 11]
Shape :  (4,)

Appended arr3 :  [ 0  1  2  3  4  8  9 10 11]

Código 2: Jugando con el eje

# Python Program illustrating
# numpy.append()
  
import numpy as geek
  
#Working on 1D
arr1 = geek.arange(8).reshape(2, 4)
print("2D arr1 : \n", arr1)
print("Shape : ", arr1.shape)
  
  
arr2 = geek.arange(8, 16).reshape(2, 4)
print("\n2D arr2 : \n", arr2)
print("Shape : ", arr2.shape)
  
  
# appending the arrays
arr3 = geek.append(arr1, arr2)
print("\nAppended arr3 by flattened : ", arr3)
  
# appending the arrays with axis = 0
arr3 = geek.append(arr1, arr2, axis = 0)
print("\nAppended arr3 with axis 0 : \n", arr3)
  
# appending the arrays with axis = 1
arr3 = geek.append(arr1, arr2, axis = 1)
print("\nAppended arr3 with axis 1 : \n", arr3)

Producción :

2D arr1 : 
 [[0 1 2 3]
 [4 5 6 7]]
Shape :  (2, 4)

2D arr2 : 
 [[ 8  9 10 11]
 [12 13 14 15]]
Shape :  (2, 4)

Appended arr3 by flattened :  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Appended arr3 with axis 0 : 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Appended arr3 with axis 1 : 
 [[ 0  1  2  3  8  9 10 11]
 [ 4  5  6  7 12 13 14 15]]

References :
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.append.html#numpy.append
.
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

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