Last active
August 5, 2018 15:26
-
-
Save emilemathieu/c8873e7a3f1aca7d123bea7bc9305627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class binary_classification(object): | |
def __init__(self, kernel, C=1.0, max_iter=1000, tol=0.001): | |
self.kernel = kernel # K(x_i, x_j) = <phi(x_i), phi(x_j)> | |
self.C = C # penalty coefficient | |
self.max_iter = max_iter # maximum number of iterations for solver | |
self.tol = tol # tolerance for the solver | |
def fit(self, X, y): | |
# Compute coefficients of the dual problem | |
lagrange_multipliers, intercept = self._compute_weights(X, y) | |
self.intercept_ = intercept # b | |
support_vector_indices = lagrange_multipliers > self.support_vector_tol | |
self.dual_coef_ = lagrange_multipliers[support_vector_indices] * y[support_vector_indices] # alpha_i | |
self.support_vectors_ = X[support_vector_indices] | |
def _compute_weights(self, X, y): | |
# Solver to compute the intercept and lagrange multipliers | |
raise NotImplementedError() | |
def _compute_kernel_support_vectors(self, X): | |
res = np.zeros((X.shape[0], self.support_vectors_.shape[0])) | |
for i,x_i in enumerate(X): | |
for j,x_j in enumerate(self.support_vectors_): | |
res[i, j] = self.kernel(x_i, x_j) | |
return res | |
def predict(self, X): | |
# Given a new datapoint, predict its label | |
kernel_support_vectors = self._compute_kernel_support_vectors(X) | |
prediction = self.intercept_ + np.sum(np.multiply(kernel_support_vectors, self.dual_coef_),1) | |
return np.sign(prediction) | |
def score(self, X, y): | |
# Compute proportion of correct classifications given true labels | |
predictions = self.predict(X) | |
scores = predictions == y | |
return sum(scores) / len(scores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment