2017-02-26 2 views
1

Ich mache jetzt eine lineare Regressionsanalyse. Die Eingabevariable ist Größe. Die Ausgabevariable ist Preis. Ich speichere den Datensatz im 2D-Array. Ich weiß, dass die Verwendung von NumPy eine einfach durchzuführende Analyse ist, aber meine Professoren sagten mir, ich solle die for-Schleife nur für Iterationen verwenden. Formula of interation is shown as the picture in the hyperlink. Deshalb habe ich beschlossen, den folgenden Code die Berechnung auszuführen:Einfache lineare Regression mit For-Schleife nur mit Python

#Structure of array (Stored in float), with structure like this [Room, Price] 
array = [[4.0, 399.9], [5.0, 329.9], [6.0, 369.0]] 

#Set initial value 
theta_price = 0 
theta_room = 0 
stepsize = 0.01 
item = 3 

#Perform iterations 
for looping in range(0, 50): #Loop 50 times 
    for j in array[0]: #Call the value stored in array[0] 
     for k in array[1]: #Call the value stored in array[1] 
      theta_price_1 = theta_price - stepsize * (1/item) * (sum((theta_price + theta_room * int(j) - int(k)))#Perform iterations of theta 0 
      theta_room_1 = theta_room - stepsize * (1/item) * (sum((theta_price + t + theta_room * int(j) - int(k))*int(j)))#Perform iterations of theta 1 
      #Bring the new theta value to the next loop 
      theta_price = theta_price_1 
      theta_room = theta_room_1 
      print(theta_price,theta_room)#Print the result for every loop 

Der obige Code wurde nicht mit Fehlermeldung in Zeile Funktion 10, dass:

'int' object is not iterable 

Aber wenn ich die Summenfunktion zu entfernen, es funktioniert mit falschen Berechnungsergebnissen. Daher weiß ich, dass es einige Probleme mit der Summenfunktion und dem Array gibt, aber ich weiß nicht, wie ich es lösen soll.

+0

Die 'sum' sollte in jeder Iteration über alle' x' und 'y',' array [0] 'und' arrary [1] 'angewendet werden, aber nicht eine davon. – zsrkmyn

Antwort

0

Wie ich im Kommentar erwähnt habe, sollte sum auf alle Elemente in jeder Iteration angewendet werden, das ist, was Batch Gradient Descent tut. So sollte der Code sein:

theta_price = 0 
theta_room = 0 
stepsize = 0.1 
item = 5 
#Perform iterations 
array = [ 
      [0,1,2,3,4], 
      [5,6,7,8,9], 
     ] 

for looping in range(0, 500): #Loop 50 times 
     theta_price = theta_price - stepsize * (1/item) * (sum([theta_price + theta_room * int(j) - int(k) for j, k in zip(array[0], array[1])]))#Perform iterations of theta 0 
     theta_room = theta_room - stepsize * (1/item) * (sum([(theta_price + theta_room * int(j) - int(k)) * int(j) for j, k in zip(array[0], array[1])]))#Perform iterations of theta 1 
     print(theta_price,theta_room)#Print the result for every loop 

nach 500 Iterationen mit den 5 Testdaten, ich das Ergebnis bekommen:

4.999999614653767 1.0000001313279816 

, die zu erwarten sind.

+0

Vielen Dank für Ihre Antwort, aber wenn das Array 2D-Array ist, ist die Antwort falsch. Ich habe gerade das Format des Arrays als Referenz hinzugefügt. Ich habe versucht, die Antwort zu verbessern durch: für j [0], k [0] in Array Der Grund für diese Änderung ist, dass es funktioniert, wenn dieses Format verwendet, um mit "für ein in Array" zu finden. Die Fehlermeldung wird jedoch mit einem eingerückten Block erwartet. Wie ändere ich die Antwort auf meine Situation? –

+0

Dann können Sie 'numpy.array' für Bequemlichkeit verwenden. – zsrkmyn

+0

Wie bereits erwähnt, möchte ich NumPy nicht so oft wie möglich verwenden und möchte die for-Schleife ändern, um sie für mein Array zu übernehmen. –

Verwandte Themen