# This interactive cell allows the user to repeatedly guess parameters for a line that seek to minimize the Total Squared Error
# between the resulting line and the provided data points. This is included in the book text.
var('x')
@interact
def _(Points = input_box([(-2,4),(3,1),(1,3),(4,1)]),
m = slider(-4,4,0.05,1),b = slider(-2,4,0.05,1)):
G = points(Points,size=20)
xpt = []
ypt = []
f = m*x + b
TSE = 0
for k in range(len(Points)):
x0 = Points[k][0]
xpt.append(x0)
y0 = Points[k][1]
ypt.append(y0)
TSE += (f(x=x0) - y0)^2
G += line([(x0,f(x=x0)),(x0,y0)],color='orange')
G += plot(f,x,min(xpt),max(xpt),color='gray')
T = 'Total Squared Error = $%s$'%str(TSE)
G.show(title = T)
|
Click to the left again to hide and once more to show the dynamic interactive window
|