regressao.py

Created by numworks-pt

Created on April 28, 2020

503 Bytes

Executar reg(L1,L2) para traçar a regressão linear a partir dos dados das listas L1 e L2. Para que o exemplo funcione, introduza 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 cookies policy.