-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
132 lines (106 loc) · 3.34 KB
/
example.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
# -*- coding: utf8 -*-
import cPickle
import bz2
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import LinearSVC
import numpy as np
def get_label_encoder(labels):
"""Generate a label encoder given a list of labels.
This can be used to convert text labels to numerical values.
"""
le = LabelEncoder()
le.fit(labels)
return le
def encode_targets(y1, y2, le):
"""Convert text labels to numerical values using a LabelEncoder."""
y1_t = le.transform(y1)
y2_t = le.transform(y2)
return y1_t, y2_t
def load_data():
"""Load the wikipedia articles."""
f = bz2.BZ2File('features.pkl.bz2', 'rb')
data = cPickle.load(f)
f.close()
return data['X1'], data['X2'], data['y1'], data['y2']
def svc(X1, X2, y1_t, y2_t):
"""Generate a Support Vector Machine classifier with a linear kernal."""
text_clf = Pipeline([
(
'vect',
TfidfVectorizer(
analyzer='char',
binary=True,
lowercase=False,
max_features=50000,
ngram_range=(2, 3),
norm='l2',
stop_words=None,
use_idf=True,
),
),
('clf', LinearSVC(loss='l2', C=1, dual=True, verbose=2)),
])
model = text_clf.fit(X1, y1_t)
print 'fit done'
predicted = text_clf.predict(X2)
print np.mean(predicted == y2_t)
return text_clf
def sgd(X1, X2, y1_t, y2_t):
"""Generate a Stochastic Gradient Descent classifier."""
text_clf = Pipeline([
(
'vect',
TfidfVectorizer(
analyzer='char',
binary=True,
lowercase=False,
max_features=50000,
ngram_range=(2, 3),
norm='l2',
stop_words=None,
use_idf=True,
),
),
(
'clf',
SGDClassifier(
alpha=0.0000061,
fit_intercept=False,
loss='hinge',
n_iter=67,
penalty='l1',
shuffle=True,
warm_start=True,
),
),
])
model = text_clf.fit(X1, y1_t)
print 'fit done'
predicted = text_clf.predict(X2)
print np.mean(predicted == y2_t)
return text_clf
def generate_label_encoder(labels):
"""Given a bunch of labels, generate a LabelEncoder for them."""
labels = set(labels)
le = get_label_encoder(labels)
return le
if __name__ == "__main__":
"""Example usage"""
X1, X2, y1, y2 = load_data()
le = generate_label_encoder(list(y1) + list(y2))
y1_t, y2_t = encode_targets(y1, y2, le)
svc_clf = svc(X1, X2, y1_t, y2_t)
sgd_clf = sgd(X1, X2, y1_t, y2_t)
print sgd_clf.predict([
u'Insert some text to classify here!',
u'Insérer du texte à classer ici!',
u'Infoga en text att klassificera här!',
u'Inserire un testo di classificare qui!',
u'Legen Sie einen Text hier zu klassifizieren!',
u'Introduzca un texto para clasificar aquí!',
u'Introduceți un text pentru a clasifica aici!',
])
# ['en', 'fr', 'sv', 'it', 'de', 'es', 'ro']