2016-10-13 4 views
-2

나는 물론 "어떻게 컴퓨터 과학자처럼 생각하는"일하고을 사용하여 파이썬에서 산포도를 개발하고,이 문제에 끼 었어 :거북이

각 줄에 포함하도록 데이터 파일 labdata.txt 해석 a x, y 좌표 쌍.

Y = Y + m (X-X)

이 파일의 데이터를 판독하고, 그 포인트는 다음 식에 따른 최적 라인 플롯 거북 사용 plotRegression라는 함수를 작성

m = Σxiyi-nx¯yx2i-nx¯2

프로그램은 점을 분석하고 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

귀하의 거북이 그래픽은 부차적 인 문제입니다. 데이터를 올바르게 읽지 못하는 것 같습니다. 마지막 x, y 쌍을 제외한 모든 것을 던지고 있습니다. 그리고 map()plotRegression() 안에 결과를 색인하려는 경우 여기에 친구가 아닙니다. 또한 형식 매개 변수 data 및 기타 세부 정보 대신 함수에서 직접 plot_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() 
: 당신이 더 나은 방향으로 향하고 얻는 경우에

이 여기에 코드를 내 재 작업의 참조