API · NumPy
Python Numpy.std() - Fonction d'écart type
La fonction numpy std() vérifie et calcule l'écart type des données dans un tableau le long de l'axe spécifié.
Sur cette page
La fonction Numpy.std() calcule l’écart type du tableau donné le long de l’axe spécifié.
Syntaxe de numpy.std()
numpy.std(arr, axis=None, dtype=float64)
Paramètres
arr |
array_like tableau d’entrée pour calculer l’écart type |
axis |
None, int or tuple of int Axis along which the standard deviation is computed. axis=0 means standard deviation computed along the column, axis=1 means standard deviation along the row. It treats the multiple dimension array as a flattened list if axis is not given. |
dtype |
dtype or None Data type used during the calcuation of the standard deviation. |
Revenir
Il retourne l’écart type du tableau donné, ou un tableau avec l’écart type le long de l’axe spécifié.
Exemples de codes: numpy.std() avec un tableau 1-D
Lorsque le tableau Python 1-D est l’entrée, la fonction Numpy.std() calcule l’écart type de toutes les valeurs du tableau.
import numpy as np
arr = [10, 20, 30]
print("1-D array :", arr)
print("Standard Deviation of arr is ", np.std(arr))
Production:
1-D array : [10, 20, 30]
Standard deviation of arr is 8.16496580927726
Ici, le tableau 1-D a les éléments de 10, 20 et 30; par conséquent, la valeur dans le DataFrame renvoyé est l’écart-type sans affecter aucune information d’axe.
Exemples de codes: numpy.std() avec tableau 2D
import numpy as np
arr = [[10, 20, 30], [3, 50, 5], [70, 80, 90], [100, 110, 120]]
print("Two Dimension array :", arr)
print("SD of with no axis :", np.std(arr))
print("SD of with axis along column :", np.std(arr, axis=0))
print("SD of with axis aong row :", np.std(arr, axis=1))
Production:
Two Dimension array : [[10, 20, 30], [3, 50, 5], [70, 80, 90], [100, 110, 120]]
SD of with no axis : 41.21960159384798
SD of with axis along column : [40.73312534 33.54101966 45.87687326]
SD of with axis aong row : [ 8.16496581 21.6999744 8.16496581 8.16496581]
np.std(arr) traite le tableau d’entrée comme le tableau aplati et calcule l’écart type de ce tableau aplati 1D.
np.std(arr, axe = 0) calcule l’écart type le long de la colonne. Il retourne [40.73312534 33.54101966 45.87687326] comme écart-type de chaque colonne du tableau d’entrée.
np.std(arr, axe = 1) calcule l’écart type le long de la ligne. Il retourne [8.16496581 21.6999744 8.16496581 8.16496581] comme écart-type de chaque ligne du tableau d’entrée.
Exemples de codes: numpy.std() avec dtype spécifié
import numpy as np
arr = [10, 20, 30]
print("Single Dimension array :", arr)
print("SD of Single Dimension array :", np.std(arr))
print("SD value with float32 data :", np.std(arr, dtype=np.float32))
print("SD value with float64 data :", np.std(arr, dtype=np.float64))
Production:
Single Dimension array : [10, 20, 30]
SD of Single Dimension array : 8.16496580927726
SD value with float32 data : 8.164966
SD value with float64 data : 8.16496580927726
Si le paramètre dtype est donné dans la fonction numpy.std() , il utilise le type de données spécifié lors du calcul de l’écart-type.
Il est évident de remarquer que l’écart-type a une résolution inférieure si nous affectons dtype à float32 plutôt qu’à float64.
