from cs1graphics import * #from file import * # this module/function reads the information from a file # and stores it in a list, where every element is a tuple # of integer values def readData(): while True: filename=raw_input('Please, input the filename:') try: fname=file(filename) break except IOError: print 'Cannot open the file. Try again, please' listLines=fname.readlines() print 'read from file:', listLines fname.close() print 'File is closed' n=len(listLines) data=[] for i in range(n): pair=listLines[i].split() print pair data.append((int(pair[0]),int(pair[1]))) print data return data # this module/function draws the rectangular coordinate system on the screen # and displays all the points provided by data variable def display(data,paper): n=len(data) # getting number of points xAxis=Path(Point(0,300),Point(700,300)) # creating x-axis yAxis=Path(Point(350,0),Point(350,600)) # creating y-axis paper.add(xAxis) # drawing x-axis paper.add(yAxis) # drawing y-axis for i in range(n): print data[i][0]+350 print (-1)*data[i][1]+300 point=Point(data[i][0]+350,(-1)*data[i][1]+300) p=Circle(1,point) paper.add(p) def findRegression(data): n=len(data) sumxy, sumx, sumy, sumx2 = 0,0,0,0 for i in range(n): sumxy += data[i][0]*data[i][1] sumx += data[i][0] sumx2 += data[i][0]**2 sumy += data[i][1] print 'sum_x=%d, sum_y=%d, sum_xy=%d, sum_x^2=%d'%(sumx,sumy,sumxy,sumx2) # calculate the slope,m and the y-intercept,b m = (n*sumxy-sumx*sumy)/float(n*sumx2 - sumx**2) b=(sumy-m*sumx)/float(n) # round off b to two decimal places b=round(b,2) print 'b=',b # round off m to two decimal places m=round(m,2) print 'm=',m return m,b def draw_reg(m,b,paper): x1=-100 y1=x1*m+b point=Point(x1+350,(-1)*y1+300) print 'x1=%f, y1=%f'%(x1,y1) print 'x1_=%f, y1_=%f'%(x1+350,(-1)*y1+300) x2=100 y2=x2*m+b point2=Point(x2+350,(-1)*y2+300) print 'x2=%f, y2=%f'%(x2,y2) print 'x2_=%f, y2_=%f'%(x2+350,(-1)*y2+300) regline=Path(point,point2) paper.add(regline) def dispEq(m,b,paper): text='y='+str(m)+'x + ' + str(b) t=Text(text,12) t.move(100,550) paper.add(t) def main(): # read data from the input file data=readData() paper=Canvas(700,600,'light yellow','Regression Line') # display data on the screen display(data,paper) # find regression m,b=findRegression(data) # draw regression line draw_reg(m,b,paper) # display the equation of the regression line dispEq(m,b,paper) main()