El método Python stats.stdev() se “utiliza para calcular la desviación estándar de una muestra de datos determinada”.
Sintaxis
stdev([data-set], xbar)
Parámetros
- [data]: Un iterable con números de valor real.
- xbar (opcional) : toma la media real del conjunto de datos como valor.
Valor de retorno
El método stats.stdev() devuelve un valor flotante que representa la desviación estándar de un dato determinado.
Ejemplo 1: ¿Cómo funciona el método stats.stdev()?
import statistics
dataset = [1, 2, 3, 4, 5]
print("Standard Deviation of a dataset is % s "
% (statistics.stdev(dataset)))
Salida
Standard Deviation of a dataset is 1.5811388300841898
Ejemplo 2: ¿Cómo utilizar el método stdev()?
import statistics
dataset = [11, 21, 18, 19, 46]
print("Standard Deviation of dataset is % s "
% (statistics.stdev(dataset)))
Producción
Standard Deviation of dataset is 13.397761006974262
Ejemplo 3: Usando la función stdev() con la función mean()
import statistics
dataset = [11, 21, 18, 19, 46]
meanValue = statistics.mean(dataset)
print("Standard Deviation of the dataset is % s "
% (statistics.stdev(dataset, xbar=meanValue)))
Producción
Standard Deviation of the dataset is 13.397761006974262
Ejemplo 4: stats.StatisticsError: Variance requiere al menos dos puntos de datos
Si solo pasamos un punto de datos, generará StatisticsError porque la función stdev() requiere un mínimo de dos puntos de datos.
import statistics
dataset = [11]
print("Standard Deviation of the dataset is % s "
% (statistics.stdev(dataset)))
Producción
statistics.StatisticsError: variance requires at least two data points
Ejemplo 5: Demostrar la diferencia entre los resultados de variance() y stdev()
import statistics
sample = [11, 21, 18, 19, 46]
print("Standard Deviation of the sample is % s "
% (statistics.stdev(sample)))
print("Variance of the sample is % s"
% (statistics.variance(sample)))
Producción
Standard Deviation of the sample is 13.397761006974262
Variance of the sample is 179.5
Ejemplo 6: Demostrar el uso del parámetro xbar
import statistics
sample = [11, 21, 18, 19, 46]
m = statistics.mean(sample)
print("Standard Deviation of Sample set is % s"
% (statistics.stdev(sample, xbar=m)))
Producción
Standard Deviation of Sample set is 13.397761006974262
Recuerda seguirme y estar atento a las nuevas publicaciones de Python.