【发布时间】:2015-08-10 23:13:06
【问题描述】:
我正在使用 Zelle 的 graphics.py 程序 (http://mcsp.wartburg.edu/zelle/python/graphics.py) 创建一个简单的回归图表。我检查了我的函数,它们返回了它们应该返回的值,所以它一定是我的回归计算本身出现了一些错误。我使用这个公式进行了一些小的调整来解释我的窗口大小:
这是我的代码:
"""
This program takes points from the user, and then plots a regression line
with the 'best fit' from the collection of points.
"""
import math
from graphics import *
def listMean(list1):
mean = sum(list1)/len(list1)
print('Sum of list is', sum(list1), 'Number of point is', len(list1))
return(mean)
def squareList(list1):
squaredList = [x**2 for x in list1]
print('List 1 is', list1, 'SquaredList is', squaredList)
listSumSquared = sum(squaredList)
print('The sum of the x-values squared is {}.'.format(listSumSquared))
return listSumSquared
def xAndY(list1, list2):
xAndYCombined = map(lambda x,y: x*y, list1, list2)
print('List 1 and 2: ', list1, list2, 'Combined: ', xAndYCombined)
xAndYSum = sum(xAndYCombined)
print(xAndYSum)
return xAndYSum
def main():
win = GraphWin('Regression Line', 500, 500)
win.setCoords(-10, -10, 10, 10)
win.setBackground('black')
# create done button
done_button = Rectangle(Point(-9, -9), Point(-7, -8))
done_button.setFill('White')
done_button.draw(win)
done = Text(Point(-8, -8.5), 'Done')
done.setFill('black')
done.draw(win)
# create lists to hold y and x values
y_vals = []
x_vals = []
while True:
usr_click = win.getMouse()
usr_click.setFill('white')
usr_click.draw(win)
x_val = usr_click.getX()
y_val = usr_click.getY()
if (x_val >= -9 and x_val <= -7) and (y_val >= -9 and y_val <= -8):
break
else:
x_vals.append(x_val)
y_vals.append(y_val)
xBar = listMean(x_vals)
yBar = listMean(y_vals)
x2Sum = squareList(x_vals)
xySum = xAndY(x_vals, y_vals)
slope = ((xySum - (len(x_vals) * xBar * yBar)) / (x2Sum - (len(x_vals) * x2Sum)))
y1 = yBar + (slope * (-10 - xBar))
y2 = yBar + (slope * (10 - xBar))
reg_line = Line(Point(-10, y1), Point(10, y2))
reg_line.setFill('white')
reg_line.draw(win)
win.getMouse()
win.close()
if __name__ == '__main__':
main()
【问题讨论】:
-
你也可以使用
sklearn,三个函数调用也一样 -
同意,本书的大部分内容是教授语法和语言概念,而不是教授特定的分析概念。
标签: python python-3.x regression scatter-plot