2016-10-13 7 views
-2

Ich arbeite an dem Kurs „Wie wie ein Informatiker denken“, und ist in dieser Frage fest:Entwicklung ein Streudiagramm in Python Schildkröte mit

die Datendatei labdata.txt Interpretieren, so dass jede Zeile enthält ein x, y-Koordinatenpaar. Schreibe eine Funktion aufgerufen plotRegression, die die Daten aus dieser Datei liest und verwendet eine Schildkröte jene Punkte zu zeichnen und eine am besten passende Linie entsprechend den folgenden Formeln:

y = Y + m (x-x)

m = Σxiyi-nx¯y¯Σx2i-nx¯2

Ihr Programm sollte die Punkte analysieren und das Fenster mit setworldcoordinates korrekt skalieren, so dass jeder Punkt geplottet werden kann. Dann sollten Sie die beste Fit-Linie, in einer anderen Farbe, durch die Punkte ziehen.

Hier ist, was ich bisher habe, aber ich bekomme immer eine 'int' unterstützt keine Indexierungsfehler. Ich habe verschiedene Online-Ressourcen und ein paar Lösungen hier verwendet, aber es scheint nicht zu funktionieren.

Kann mir jemand helfen, herauszufinden, was zu korrigieren ist?

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) 

Antwort

0

Ich denke, Ihre Schildkröte Grafiken ist zweitrangig - Sie Ihre Daten in nicht richtig lesen. Du wirfst alle bis auf das letzte x, y-Paar. Und map() ist nicht Ihr Freund hier, wie Sie das Ergebnis innerhalb plotRegression() indizieren möchten. Sie greifen auch auf plot_data direkt in der Funktion anstelle des formalen Parameters data und anderen Details zu.

Hier ist meine Nacharbeiten des Codes, ob es Sie in eine bessere Richtung Überschrift bekommt:

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()