Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 8-13-LOB.py
- from graphics import *
- def listMulti(list1,list2):
- tempAcc = 0
- for i in range(len(list1)):
- tempAcc += list1[i] * list2[i]
- print tempAcc
- return tempAcc
- def squareList(iterable):
- itSum = 0
- for i in iterable:
- itSum += i**2
- return itSum
- def listMean(iterable):
- return sum(iterable)/len(iterable)
- def regression(xList,yList,win):
- xBar = listMean(xList)
- yBar = listMean(yList)
- xListSq = squareList(xList)
- xListXyList = listMulti(xList,yList)
- m = ((xListXyList) - ((len(xList)*xBar*yBar)))/\
- ((xListSq) - (len(xList)* (xBar**2)))
- y1 = yBar + m*(-50.0 - xBar)
- y2 = yBar + m*(50.0 - xBar)
- Line(Point(-50.0,y1),Point(50.0,y2)).draw(win)
- return "ybar: %f\txBar: %f\tm: %f\ty1: %f\ty2: %f" %(yBar,xBar,m,y1,y2)
- def windraw():
- win = GraphWin("Line of Best Fit",500,500)
- win.setCoords(-50.0,-50.0,50.0,50.0)
- doneBox = Rectangle(Point(-50,-50),Point(-40,-45))
- doneBox.setWidth(3)
- doneBoxTxt = Text(Point(-45,-47.5),"DONE")
- doneBox.draw(win)
- doneBoxTxt.draw(win)
- return win
- def pointBuild(xList,yList,win):
- tempPoint = Point(25,25) # prime tempPoint for sentinel loop
- # tests if given point is past rectangle created for doneBox
- while (tempPoint.getX() - (Point(-40,-45)).getX() == abs(tempPoint.getX() - (Point(-40,-45)).getX())) or\
- (tempPoint.getY() - (Point(-40,-45)).getY() == abs(tempPoint.getY() - (Point(-40,-45)).getY())):
- tempPoint = win.getMouse()
- tempPoint.draw(win)
- xList.append(tempPoint.getX()); yList.append(tempPoint.getY())
- def main():
- xList,yList = [],[]
- win = windraw()
- pointBuild(xList,yList,win)
- print regression(xList,yList,win)
- # Test out coordinate lists accumulation from pointBuild
- for i in range(len(xList)-1):
- print "Point(%2.2f,%2.2f)" % (xList[i],yList[i])
- win.getMouse()
- win.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement