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()

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>.