-
Notifications
You must be signed in to change notification settings - Fork 13
/
nbsvm2.py
194 lines (147 loc) · 5.69 KB
/
nbsvm2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Multiclass Naive Bayes SVM (NB-SVM)
https://github.com/lrei/nbsvm
Luis Rei <[email protected]>
@lmrei
http://luisrei.com
Learns a multiclass (OneVsRest) classifier based on word ngrams.
Uses scikit learn. Reads input from TSV files.
Licensed under a Creative Commons Attribution-NonCommercial 4.0
International License.
Based on a work at https://github.com/mesnilgr/nbsvm:
Naive Bayes SVM by Grégoire Mesnil
"""
import sys
import os
import pandas as pd
import argparse
from sklearn.pipeline import Pipeline
from scipy.sparse import csr_matrix
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import accuracy_score
import six
from abc import ABCMeta
import numpy as np
from scipy import sparse
from scipy.sparse import issparse
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils import check_X_y, check_array
from sklearn.utils.extmath import safe_sparse_dot
from sklearn.preprocessing import normalize, binarize, LabelBinarizer
from sklearn.svm import LinearSVC
class NBSVM(six.with_metaclass(ABCMeta, BaseEstimator, ClassifierMixin)):
def __init__(self, alpha=1.0, C=1.0, max_iter=10000):
self.alpha = alpha
self.max_iter = max_iter
self.C = C
self.svm_ = [] # fuggly
def fit(self, X, y):
X, y = check_X_y(X, y, 'csr')
_, n_features = X.shape
labelbin = LabelBinarizer()
Y = labelbin.fit_transform(y)
self.classes_ = labelbin.classes_
if Y.shape[1] == 1:
Y = np.concatenate((1 - Y, Y), axis=1)
# LabelBinarizer().fit_transform() returns arrays with dtype=np.int64.
# so we don't have to cast X to floating point
Y = Y.astype(np.float64)
# Count raw events from data
n_effective_classes = Y.shape[1]
self.class_count_ = np.zeros(n_effective_classes, dtype=np.float64)
self.ratios_ = np.full((n_effective_classes, n_features), self.alpha,
dtype=np.float64)
self._compute_ratios(X, Y)
# flugglyness
for i in range(n_effective_classes):
X_i = X.multiply(self.ratios_[i])
svm = LinearSVC(C=self.C, max_iter=self.max_iter)
Y_i = Y[:,i]
svm.fit(X_i, Y_i)
self.svm_.append(svm)
return self
def predict(self, X):
n_effective_classes = self.class_count_.shape[0]
n_examples = X.shape[0]
D = np.zeros((n_effective_classes, n_examples))
for i in range(n_effective_classes):
X_i = X.multiply(self.ratios_[i])
D[i] = self.svm_[i].decision_function(X_i)
return self.classes_[np.argmax(D, axis=0)]
def _compute_ratios(self, X, Y):
"""Count feature occurrences and compute ratios."""
if np.any((X.data if issparse(X) else X) < 0):
raise ValueError("Input X must be non-negative")
self.ratios_ += safe_sparse_dot(Y.T, X) # ratio + feature_occurrance_c
normalize(self.ratios_, norm='l1', axis=1, copy=False)
row_calc = lambda r: np.log(np.divide(r, (1 - r)))
self.ratios_ = np.apply_along_axis(row_calc, axis=1, arr=self.ratios_)
check_array(self.ratios_)
self.ratios_ = sparse.csr_matrix(self.ratios_)
#p_c /= np.linalg.norm(p_c, ord=1)
#ratios[c] = np.log(p_c / (1 - p_c))
def f1_class(pred, truth, class_val):
n = len(truth)
truth_class = 0
pred_class = 0
tp = 0
for ii in range(0, n):
if truth[ii] == class_val:
truth_class += 1
if truth[ii] == pred[ii]:
tp += 1
pred_class += 1
continue;
if pred[ii] == class_val:
pred_class += 1
precision = tp / float(pred_class)
recall = tp / float(truth_class)
return (2.0 * precision * recall) / (precision + recall)
def semeval_senti_f1(pred, truth, pos=2, neg=0):
f1_pos = f1_class(pred, truth, pos)
f1_neg = f1_class(pred, truth, neg)
return (f1_pos + f1_neg) / 2.0;
def main(train_file, test_file, ngram=(1, 3)):
print('loading...')
train = pd.read_csv(train_file, delimiter='\t', encoding='utf-8', header=0,
names=['text', 'label'])
# to shuffle:
#train.iloc[np.random.permutation(len(df))]
test = pd.read_csv(test_file, delimiter='\t', encoding='utf-8', header=0,
names=['text', 'label'])
print('vectorizing...')
vect = CountVectorizer()
classifier = NBSVM()
# create pipeline
clf = Pipeline([('vect', vect), ('nbsvm', classifier)])
params = {
'vect__token_pattern': r"\S+",
'vect__ngram_range': ngram,
'vect__binary': True
}
clf.set_params(**params)
#X_train = vect.fit_transform(train['text'])
#X_test = vect.transform(test['text'])
print('fitting...')
clf.fit(train['text'], train['label'])
print('classifying...')
pred = clf.predict(test['text'])
print('testing...')
acc = accuracy_score(test['label'], pred)
f1 = semeval_senti_f1(pred, test['label'])
print('NBSVM: acc=%f, f1=%f' % (acc, f1))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run NBSVM.')
parser.add_argument('--train', help='path of the train tsv')
parser.add_argument('--test', help='path of the test tsv')
parser.add_argument('--ngrams', help='N-grams considered e.g. 1,3 is uni+bi+tri-grams')
args = parser.parse_args()
if args.ngrams:
ngrams = tuple([int(x) for x in args.ngrams.split(',')])
else:
ngrams = (1, 3)
if not args.train or not args.test:
pirnt('try --help')
main(args.train, args.test, ngrams)