¿Cómo usar el método de Python stats.stdev()?

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

  1. [data]: Un iterable con números de valor real.
  2. 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.

Artículos Relacionados
Desarrollador de software vs Ingeniero de software, ¿Cuál es mejor?

Software engineers and Software developers are not the same tech professionals. In this article, we have highlighted the major differences between the two professionals.

Hoja de trucos Completa de React JS

React es una biblioteca de JavaScript para crear interfaces de usuario. Esta chuleta está dirigida a las versiones de React 15 y 16. Componentes import React from 'react' import ReactDOM from 'react-dom' class Hello extends React.Component { render () { return <div className='message-box'> Hello ¡SEGUIR LEYENDO!

Spotify cierra la librería libspotify

La biblioteca de C, libspotify que los desarrolladores usaban para interactuar con Spotify y transmitir audio se deshabilitará definitivamente el 16 de mayo de 2022. Si todavía tienes un software que dependa de libspotify, debes tomar las medidas oportunas para adaptar su aplicación, cerrarlo ¡SEGUIR LEYENDO!