From 90b5fb26eceb07aab680c96e75b85484bcc0bc23 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sun, 20 Apr 2014 21:41:51 +0200 Subject: [PATCH] calculate the sum of squared residuals more pythonic --- least-squares.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/least-squares.py b/least-squares.py index 58fefd6..d0a14b7 100755 --- a/least-squares.py +++ b/least-squares.py @@ -16,9 +16,7 @@ yn = [6, 5, 7, 10, 11, 12, 14] # # For each data point the vertical error/residual is x*b1 + b2 - y. We want to # minimize the sum of the squared residuals (least squares). -S = Symbol('0') -for i in range(0, len(xn)): - S += (xn[i] * b1 + b2 - yn[i]) ** 2 +S = sum((xn[i] * b1 + b2 - yn[i]) ** 2 for i in range(0, len(xn))) print(S) # Minimize S by setting its partial derivatives to zero.