Könnt ihr mir bitte sagen wie ich den folgenden Code mehr pythonisch machen kann?Wie machst du diesen Code pythonischer?
Der Code ist korrekt. Vollständige Offenlegung - es ist das Problem 1b in Handout # 4 von this maschinellen Lernkurs. Ich sollte Newton-Algorithmus auf die beiden Datensätze für die Anpassung einer logistischen Hypothese verwenden. Aber sie verwenden Matlab & Ich benutze scipy
ZB eine Frage, die ich habe, ist die Matrizen gerundet auf ganze Zahlen, bis ich einen Wert auf 0.0 initialisiert. Gibt es einen besseren Weg?
Dank
import os.path
import math
from numpy import matrix
from scipy.linalg import inv #, det, eig
x = matrix('0.0;0;1' )
y = 11
grad = matrix('0.0;0;0' )
hess = matrix('0.0,0,0;0,0,0;0,0,0')
theta = matrix('0.0;0;0' )
# run until convergence=6or7
for i in range(1, 6):
#reset
grad = matrix('0.0;0;0' )
hess = matrix('0.0,0,0;0,0,0;0,0,0')
xfile = open("q1x.dat", "r")
yfile = open("q1y.dat", "r")
#over whole set=99 items
for i in range(1, 100):
xline = xfile.readline()
s= xline.split(" ")
x[0] = float(s[1])
x[1] = float(s[2])
y = float(yfile.readline())
hypoth = 1/ (1+ math.exp(-(theta.transpose() * x)))
for j in range(0,3):
grad[j] = grad[j] + (y-hypoth)* x[j]
for k in range(0,3):
hess[j,k] = hess[j,k] - (hypoth *(1-hypoth)*x[j]*x[k])
theta = theta - inv(hess)*grad #update theta after construction
xfile.close()
yfile.close()
print "done"
print theta
was macht die Zeile y = 11? – SilentGhost
das setzt das coole Bit. – Geo
+1 für die Verwendung des Wortes Pythonic in einem Satz. – samoz