Le numpy.append() ajoute des valeurs le long de l'axe mentionné à la fin du tableau Syntaxe :
numpy.append(array, values, axis = None)>
Paramètres :
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.>
Retour :
An copy of array with values being appended at the end as per the mentioned object along a given axis.>
Code 1 : ajout de tableaux
Python
j e s t
# 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>(>'
1D arr2 : '>, arr2)> print>(>'Shape : '>, arr2.shape)> > > # appending the arrays> arr3>=> geek.append(arr1, arr2)> print>(>'
Appended arr3 : '>, arr3)> |
>
>
Sortir :
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]>
Le complexité temporelle de la fonction numpy.append() est O(n) où n est le nombre d'éléments ajoutés. Cela signifie que le temps nécessaire pour ajouter des éléments augmente linéairement avec le nombre d'éléments ajoutés.
Le complexité de l'espace de la fonction numpy.append() est également O(n) où n est le nombre d'éléments ajoutés. Cela signifie que la quantité d'espace nécessaire pour ajouter des éléments augmente linéairement avec le nombre d'éléments ajoutés.
Code 2 : Jouer avec l'axe
Python
# Python Program illustrating> # numpy.append()> > import> numpy as geek> > #Working on 1D> arr1>=> geek.arange(>8>).reshape(>2>,>4>)> print>(>'2D arr1 :
'>, arr1)> print>(>'Shape : '>, arr1.shape)> > > arr2>=> geek.arange(>8>,>16>).reshape(>2>,>4>)> print>(>'
2D arr2 :
'>, arr2)> print>(>'Shape : '>, arr2.shape)> > > # appending the arrays> arr3>=> geek.append(arr1, arr2)> print>(>'
Appended arr3 by flattened : '>, arr3)> > # appending the arrays with axis = 0> arr3>=> geek.append(arr1, arr2, axis>=> 0>)> print>(>'
Appended arr3 with axis 0 :
'>, arr3)> > # appending the arrays with axis = 1> arr3>=> geek.append(arr1, arr2, axis>=> 1>)> print>(>'
Appended arr3 with axis 1 :
'>, arr3)> |
chaîne séparée en Java
>
>
Sortir :
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]]>