forked from petewarden/MLloWorld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
69 lines (59 loc) · 2.36 KB
/
train.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
#!/usr/bin/env python
from sklearn import utils
from sklearn import metrics
from sklearn import linear_model
from sklearn import svm
from sklearn import grid_search
from sklearn import pipeline
import numpy as np
import sys
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.externals import joblib
from mlloutils import expand_to_vectors
if len(sys.argv) < 3:
print >> sys.stderr, ('Usage: python ' + sys.argv[0]
+ ' <training csv input> <persistent model output>')
exit(0)
# Grab the training data
training_name = sys.argv[1]
print >> sys.stderr, 'Loading training set from '+training_name
training_vectors, training_target = expand_to_vectors(
training_name, 1, 2, [6, 7, 8], 9, True)
print "%d vectors with dimension %d" % training_vectors.shape
# Normalize the sparse positive features using the TF-IDF normalizer as field
# 6, 7 and 8 are word occurrences in text fields
tfidf = TfidfTransformer()
training_vectors = tfidf.fit_transform(training_vectors)
# Shuffle the samples as SGD models assume i.i.d.
training_vectors, training_target = utils.shuffle(
training_vectors, training_target, random_state=0)
# Create a naive classifier
models = [
(linear_model.sparse.SGDClassifier(n_iter=5),
{'alpha': np.logspace(-7, -4, 5)}),
# (svm.sparse.LinearSVC(),
# {'C': np.logspace(4, 7, 5)}),
]
# Uncomment the previous lines if you want to use liblinear to fit a LinearSVC model
# (much slower than SGD without improvement)
best_score = None
best_clf = None
for clf, grid in models:
msg = 'Training model %r using grid search for best hyper-param: %r' % (
clf, grid)
print >> sys.stderr, msg
gs = grid_search.GridSearchCV(
clf, grid, score_func=metrics.f1_score, verbose=1,
fit_params={'class_weight': 'auto'})
gs.fit(training_vectors, training_target)
print "Best model: %r" % gs.best_estimator
print "Best score: %0.3f" % gs.best_score
if best_clf is None or best_score < gs.best_score:
best_clf = gs.best_estimator
best_score = gs.best_score
model_name = sys.argv[2]
print >> sys.stderr, 'Saving classifier to ' + model_name
# combine the normalizer and the classifier in a compound model so that the
# same normalization will be applied as a preprocessing on the test set
p = pipeline.Pipeline([('tfidf', tfidf), ('clf', best_clf)])
joblib.dump(p, model_name)