When the data contains the observations and the line is fitted to the data, the error can be computed with the sum of squares formula
For example, when the data is and the line is (i.e., and ), the error is
Implement a class SquareSum
with the methods
add(x, y)
: add an observation to the datacalc(a, b)
: return the sum of squares error for the given line parameters
The time complexity of both methods should be .
In a file squaresum.py
, implement a class SquareSum
according to the following template:
class SquareSum: def __init__(self): # TODO def add(self, x, y): # TODO def calc(self, a, b): # TODO if __name__ == "__main__": s = SquareSum() s.add(1, 1) s.add(3, 2) s.add(5, 3) print(s.calc(1, 0)) # 5 print(s.calc(1, -1)) # 2 print(s.calc(0.5, 0.5)) # 0 s.add(4, 2) print(s.calc(0.5, 0.5)) # 0.25