Created
January 18, 2018 02:48
-
-
Save zaxcie/fdeae333450f30a635a3199b33c2fef7 to your computer and use it in GitHub Desktop.
NbSvmClassifier SKlearn implementation
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
from sklearn.base import BaseEstimator, ClassifierMixin | |
from sklearn.utils.validation import check_X_y, check_is_fitted | |
from sklearn.linear_model import LogisticRegression | |
from scipy import sparse | |
class NbSvmClassifier(BaseEstimator, ClassifierMixin): | |
def __init__(self, C=1.0, dual=False, n_jobs=1): | |
self.C = C | |
self.dual = dual | |
self.n_jobs = n_jobs | |
def predict(self, x): | |
# Verify that model has been fit | |
check_is_fitted(self, ['_r', '_clf']) | |
return self._clf.predict(x.multiply(self._r)) | |
def predict_proba(self, x): | |
# Verify that model has been fit | |
check_is_fitted(self, ['_r', '_clf']) | |
return self._clf.predict_proba(x.multiply(self._r)) | |
def fit(self, x, y): | |
# Check that X and y have correct shape | |
y = y.values | |
x, y = check_X_y(x, y, accept_sparse=True) | |
def pr(x, y_i, y): | |
p = x[y==y_i].sum(0) | |
return (p+1) / ((y==y_i).sum()+1) | |
self._r = sparse.csr_matrix(np.log(pr(x,1,y) / pr(x,0,y))) | |
x_nb = x.multiply(self._r) | |
self._clf = LogisticRegression(C=self.C, dual=self.dual, n_jobs=self.n_jobs).fit(x_nb, y) | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment