-
Notifications
You must be signed in to change notification settings - Fork 2
/
svm_grid_serach.py
70 lines (53 loc) · 2.04 KB
/
svm_grid_serach.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
import sklearn.datasets
from pprint import pprint
from time import time
import logging
print('Loading dataset ...');
# load all data from files
twenty_all = sklearn.datasets.load_files("./remail",
categories=None, load_content=True, shuffle=False, encoding="latin1", random_state=42, decode_error='strict')
print('dataset loaded');
# split the train and test data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(twenty_all.data, twenty_all.target, test_size=0.1)
print('training started');
# feed training data into svm
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', SGDClassifier()),
])
# uncommenting more parameters will give better exploring power but will
# increase processing time in a combinatorial way
parameters = {
'vect__max_df': (0.5, 0.75, 1.0),
#'vect__max_features': (None, 5000, 10000, 50000),
'vect__ngram_range': ((1, 1), (1, 2)), # unigrams or bigrams
'tfidf__use_idf': (True, False),
'tfidf__norm': ('l1', 'l2'),
'clf__alpha': (0.00001, 0.000001),
'clf__penalty': ('l2', 'elasticnet'),
#'clf__n_iter': (10, 50, 80),
}
from sklearn.model_selection import GridSearchCV
if __name__ == "__main__":
# multiprocessing requires the fork to happen in a __main__ protected
# block
# find the best parameters for both the feature extraction and the
# classifier
grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=1)
print("Performing grid search...")
t0 = time()
grid_clf = grid_search.fit(X_train, y_train)
print("done in %0.3fs" % (time() - t0))
print('Training complete');
print('Testing trained model');
# validation of the trained model
import numpy as np
predicted = grid_clf.predict(X_test)
print('Test Result:');
print(np.mean(predicted == y_test))