regression.py

Created by numworks

Created on March 31, 2020

507 Bytes

Lancer reg(L1,L2) pour tracer la régression linéaire à partir des données contenues dans les listes L1 et L2. Pour faire fonctionner l’exemple, entrer reg().


from matplotlib.pyplot import *

x=[56,42,72,36,63,47,55,49,38,42,68,60]
y=[136,132,136,130,138,132,136,130,142,134,136,140]

def moy(L):
  return sum(L)/len(L)

def cov(L1,L2):
  res=0
  for i in range(len(L1)):
    res=res+(L1[i]-moy(L1))*(L2[i]-moy(L2))
  return res/len(L1)

def reg(L1=x,L2=y):
  a=cov(L1,L2)/cov(L1,L1)
  b=moy(L2)-a*moy(L1)
  X=[t for t in range(100)]
  Y=[a*t+b for t in X]
  plot(X,Y)
  scatter(L1,L2)
  grid()
  axis((min(L1),max(L1),min(L2),max(L2)))
  show()