-
Notifications
You must be signed in to change notification settings - Fork 2
/
nbsvm.py
62 lines (50 loc) · 1.96 KB
/
nbsvm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
from scipy.sparse import spmatrix, coo_matrix
from sklearn.base import BaseEstimator
from sklearn.linear_model.base import LinearClassifierMixin, SparseCoefMixin
from sklearn.svm import LinearSVC
class NBSVM(BaseEstimator, LinearClassifierMixin, SparseCoefMixin):
def __init__(self, alpha=1, C=1, beta=0.25, fit_intercept=False):
self.alpha = alpha
self.C = C
self.beta = beta
self.fit_intercept = fit_intercept
def fit(self, X, y):
self.classes_ = np.unique(y)
if len(self.classes_) == 2:
coef_, intercept_ = self._fit_binary(X, y)
self.coef_ = coef_
self.intercept_ = intercept_
else:
coef_, intercept_ = zip(*[
self._fit_binary(X, y == class_)
for class_ in self.classes_
])
self.coef_ = np.concatenate(coef_)
self.intercept_ = np.array(intercept_).flatten()
return self
def _fit_binary(self, X, y):
p = np.asarray(self.alpha + X[y == 1].sum(axis=0)).flatten()
q = np.asarray(self.alpha + X[y == 0].sum(axis=0)).flatten()
r = np.log(p/np.abs(p).sum()) - np.log(q/np.abs(q).sum())
b = np.log((y == 1).sum()) - np.log((y == 0).sum())
if isinstance(X, spmatrix):
indices = np.arange(len(r))
r_sparse = coo_matrix(
(r, (indices, indices)),
shape=(len(r), len(r))
)
X_scaled = X * r_sparse
else:
X_scaled = X * r
lsvc = LinearSVC(
C=self.C,
fit_intercept=self.fit_intercept,
max_iter=10000
).fit(X_scaled, y)
mean_mag = np.abs(lsvc.coef_).mean()
coef_ = (1 - self.beta) * mean_mag * r + \
self.beta * (r * lsvc.coef_)
intercept_ = (1 - self.beta) * mean_mag * b + \
self.beta * lsvc.intercept_
return coef_, intercept_