Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def linreg_map(X, t, alpha, beta, max_iter=100, tol=1e-4, display=False):
- if display:
- print('%9s %15s' % ('iteration', 'f'))
- n, d = X.shape
- F = lambda w, xi: (alpha * np.sum(xi + (X.dot(w) - t) ** 2 / xi) + n * beta * w.dot(w)) / 2.
- delta = 1e-8
- w = np.ones(d)
- xi = np.ones(n)
- f = F(w, xi)
- if display:
- print('%9d %15.11f' % (0, f))
- for iteration in xrange(1, max_iter + 1):
- A = alpha * ((X).T / xi).dot(X) + n * beta * np.identity(d)
- b = alpha * np.sum(X.T * t / xi, axis=1)
- w = solve(A, b)
- xi = np.abs(X.dot(w) - t)
- xi[xi < delta] = delta
- f_ = f
- f = F(w, xi)
- if display:
- print('%9d %15.11f' % (iteration, f))
- if (np.abs(f_ - f) < tol):
- break
- return w.reshape((-1, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement