statistiques.py

Created by numworks

Created on April 11, 2018

499 Bytes

Ce script contient plusieurs fonctions pour calculer des grandeurs statistiques sur une série à une variable. A partir de la liste L de vos données (par exemple [1,2,3,4]), vous pouvez calculer la moyenne (moyenne(L)), la médiane (mediane(L)), les premier et troisième quartiles (quartiles(L)), la variance (variance(L)) et l’écart-type (ecart_type(L)).


from math import *
def mediane(L):
  L=sorted(L)
  n=len(L)
  if n%2==1:
    return L[n//2]
  else:
    return (L[n//2]+L[n//2-1])/2
def moyenne(L):
  return sum(L)/len(L)
def quartiles(L):
  L=sorted(L)
  n=len(L)
  if n%4==0:
    return L[n//4-1],L[3*n//4-1]
  else:
    return L[n//4],L[3*n//4]
def variance(L):
  m=moyenne(L)
  L_squares=[]
  for i in range(len(L)):
    L_squares.append(L[i]**2)
  return moyenne(L_squares)-m**2
def ecart_type(L):
  return sqrt(variance(L))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.