Commits

Miki Tebeka  committed acabc1b

Single variable solved

  • Participants
  • Parent commits 1139b63

Comments (0)

Files changed (1)

     return diff2.sum() / (2 * len(ys))
 
 
+def diff(i, j, theta, features, ys):
+    return ((features[i] * theta.T)[0, 0] - ys[i, 0]) * features[i, j]
+
+
+def step(j, theta, alpha, features, ys):
+    m = len(features)
+    mul = alpha/m
+
+    sumdiffs = sum(diff(i, j, theta, features, ys) for i in range(m))
+    return theta[0, j] - (mul * sumdiffs)
+
+
 def linreg(features, ys):
     alpha = 0.01
     niter = 1500
 
+    theta = np.matrix([0, 0])
+    for round in xrange(niter):
+        t0 = step(0, theta, alpha, features, ys)
+        t1 = step(1, theta, alpha, features, ys)
+        theta = np.matrix([t0, t1])
 
-def plot():
+    return theta
+
+
+def plot(theta):
     data = list(load('ex1/ex1data1.txt'))
     xs = [p.item(0) for p in data]
     ys = [p.item(1) for p in data]
 
+    t0 = theta[0,0]
+    t1 = theta[0,1]
+
+    tys = [(t0 + t1*x) for x in xs]
+
     fig = plt.figure()
     ax = fig.add_subplot(111)
     ax.plot(xs, ys, 'x', color='red')
+    ax.plot(xs, tys, color='blue')
     ax.set_xlabel('Population of City in 10,000s')
     ax.set_ylabel('Profit in $10,000s')
     plt.show()