我正在研究“如何像计算机科学家一样思考”课程,并坚持这个问题:
解释数据文件labdata.txt,使每行包含一个x,y坐标对。编写一个名为plotRegression的函数,该函数从该文件中读取数据并使用乌龟根据以下公式绘制这些点和最佳拟合线:
Y = Y + M(X-X)
M =Σxiyi-nx¯y¯Σx2i-NX2
您的程序应使用setworldcoordinates分析点并正确缩放窗口,以便可以绘制每个点。然后你应该通过这些点以不同的颜色绘制最佳拟合线。
这是我到目前为止所做的,但我一直得到'int'不支持索引错误。我一直在这里使用各种在线资源和一些解决方案,但似乎无法使其正常工作。
有谁可以帮我弄清楚要纠正什么?
import turtle
def plotRegression(data):
win = turtle.Screen()
win.bgcolor('pink')
t = turtle.Turtle()
t.shape('circle')
# t.turtlesize(0.2)
x_list, y_list = [int(i[0]) for i in plot_data], [int(i[1]) for i in plot_data]
x_list, y_list = [float(i) for i in x_list], [float(i) for i in y_list]
x_sum, y_sum = sum(x_list), sum(y_list)
x_bar, y_bar = x_sum / len(x_list), y_sum / len(y_list)
x_list_square = [i ** 2 for i in x_list]
x_list_square_sum = sum(x_list_square)
xy_list = [x_list[i] * y_list[i] for i in range(len(x_list))]
xy_list_sum = sum(xy_list)
m = (xy_list_sum - len(x_list) * x_bar * y_bar) / (x_list_square_sum - len(x_list) * x_bar ** 2)
# best y
y_best = [(y_bar + m * (x_list[i] - x_bar)) for i in range(len(x_list))]
# plot points
max_x = max(x_list)
max_y = max(y_list)
win.setworldcoordinates(0, 0, max_x, max_y)
for i in range(len(x_list)):
t.penup()
t.setposition(x_list[i], y_list[i])
t.stamp()
# plot best y
t.penup()
t.setposition(0, 0)
t.color('blue')
for i in range(len(x_list)):
t.setposition(x_list[i], y_best[i])
t.pendown()
win.exitonclick()
f = open("labdata.txt", "r")
for aline in f:
plot_data = map(int, aline.split())
plotRegression(plot_data)
答案 0 :(得分:0)
我认为您的乌龟图形是次要问题 - 您无法正确读取数据。除了最后一对x,y对之外,你还在扔掉所有东西。而map()
不是您的朋友,因为您希望将结果编入索引plotRegression()
。您也可以直接在函数中访问plot_data
,而不是正式参数data
和其他详细信息。
这是我对您的代码进行的返工,看看它是否会让您朝着更好的方向前进:
from turtle import Turtle, Screen
def plotRegression(data):
x_list, y_list = [int(i[0]) for i in data], [int(i[1]) for i in data]
x_list, y_list = [float(i) for i in x_list], [float(i) for i in y_list]
x_sum, y_sum = sum(x_list), sum(y_list)
x_bar, y_bar = x_sum / len(x_list), y_sum / len(y_list)
x_list_square = [i ** 2 for i in x_list]
x_list_square_sum = sum(x_list_square)
xy_list = [x_list[i] * y_list[i] for i in range(len(x_list))]
xy_list_sum = sum(xy_list)
m = (xy_list_sum - len(x_list) * x_bar * y_bar) / (x_list_square_sum - len(x_list) * x_bar ** 2)
# best y
y_best = [(y_bar + m * (x_list[i] - x_bar)) for i in range(len(x_list))]
# plot points
turtle = Turtle(shape = 'circle')
for i in range(len(x_list)):
turtle.penup()
turtle.setposition(x_list[i], y_list[i])
turtle.stamp()
# plot best y
turtle.penup()
turtle.setposition(0, 0)
turtle.color('blue')
for i in range(len(x_list)):
turtle.setposition(x_list[i], y_best[i])
turtle.pendown()
return (min(x_list), min(y_list), max(x_list), max(y_list))
screen = Screen()
screen.bgcolor('pink')
f = open("labdata.txt", "r")
plot_data = []
for aline in f:
x, y = aline.split()
plot_data.append((x, y))
# This next line should be something like:
# screen.setworldcoordinates(*plotRegression(plot_data))
# but setworldcoordinates() is so tricky to work with
# that I'm going to leave it at:
print(*plotRegression(plot_data))
# and suggest you trace a rectangle with the return
# values to get an idea what's going to happen to
# your coordinate system
screen.exitonclick()