Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def compute_w_diff(x, y, w, b, C=0.0):
- prod_xy = np.array(y).reshape((y.size, 1)) * x
- exp = np.exp(-prod_xy.dot(w) + b)
- big_sum = np.sum(prod_xy * (1 - 1/(1 + exp)), axis=0)
- return big_sum.reshape(w.shape)/y.size - C * w
- def compute_b_diff(x, y, w, b):
- prod_xy = np.array(y).reshape((y.size, 1)) * x
- return np.sum(np.array(y).reshape((y.size, 1)) * (1 - 1/(1 + np.exp(-prod_xy.dot(w) + b))))/y.size
- class LinearClassifier(BaseEstimator, LinearClassifierMixin):
- def __init__(self, step, stop_function=lambda iters, dif : iters < 10000, rand_init=False, C=0.0):
- self.step = step
- self.stop_function = stop_function
- self.rand_init = rand_init
- self.C = C
- def fit(self, X, y):
- if self.rand_init:
- w = (np.random.rand(x.shape[1]).reshape((x.shape[1], 1)) - 0.5) / x.shape[0]
- else:
- w = np.zeros((x.shape[1], 1))
- b = 0
- current_iters = 0
- while True:
- w_dif = self.step * compute_w_diff(X, y, w, b, self.C)
- b_dif = self.step * compute_b_diff(X, y, w, b)
- w += w_dif
- b += b_dif
- if self.stop_function(current_iters, w_dif):
- self.coef_ = w
- self.intercept_ = b
- return self
- current_iters += 1
- def predict_proba(self, X):
- return super()._predict_proba_lr(X)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement