python代码实现
ltx=[20,22,24,26,28,30] lty=[24.3,26.5,28.7,30.5,31.7,32.9] lenx=len(ltx) leny=len(lty) sumx=0 sumy=0 for i in range(0,lenx): sumx+=ltx[i] sumy+=lty[i] avgx=sumx/lenx avgy=sumy/leny print("x平均值=",avgx,"\ty的平均值=",avgy) Sxx=0 Sxy=0 for i in range(0,lenx): Sxx+=(ltx[i]-avgx)*(ltx[i]-avgx) Sxy+=(ltx[i]-avgx)*(lty[i]-avgy) print("Sxx=",Sxx,"\tSxy=",Sxy) B1=Sxy/Sxx B0=avgy-avgx*B1 print("所得回归方程为:y=",B0,"+",B1,"x")