Advertisement
Tranvick

linreg_map

May 5th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. def linreg_map(X, t, alpha, beta, max_iter=100, tol=1e-4, display=False):
  2.     if display:
  3.         print('%9s %15s' % ('iteration', 'f'))
  4.    
  5.     n, d = X.shape
  6.     F = lambda w, xi: (alpha * np.sum(xi + (X.dot(w) - t) ** 2 / xi) + n * beta * w.dot(w)) / 2.
  7.     delta = 1e-8
  8.    
  9.     w = np.ones(d)
  10.     xi = np.ones(n)
  11.     f = F(w, xi)
  12.     if display:
  13.         print('%9d %15.11f' % (0, f))
  14.    
  15.     for iteration in xrange(1, max_iter + 1):
  16.         A = alpha * ((X).T / xi).dot(X) + n * beta * np.identity(d)
  17.         b = alpha * np.sum(X.T * t / xi, axis=1)
  18.         w = solve(A, b)
  19.        
  20.         xi = np.abs(X.dot(w) - t)
  21.         xi[xi < delta] = delta
  22.         f_ = f
  23.         f = F(w, xi)
  24.        
  25.         if display:
  26.             print('%9d %15.11f' % (iteration, f))
  27.         if (np.abs(f_ - f) < tol):
  28.             break
  29.     return w.reshape((-1, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement