November 2017
Intermediate to advanced
1035 pages
11h 31m
English
To mitigate this problem, we have a very useful class named GridSearchCV within the
sklearn.grid_search module. What we have been doing with our
calc_params function is a kind of grid search in one dimension. With GridSearchCV, we can specify a grid of any number of parameters and parameter values to traverse. It will train the classifier for each combination and obtain a cross-validation accuracy to evaluate each one.
Let's use it to adjust the C and the gamma parameters at the same time.
>>> from sklearn.grid_search import GridSearchCV >>> parameters = { >>> 'svc__gamma': np.logspace(-2, 1, 4), >>> 'svc__C': np.logspace(-1, 1, 3), >>> } >>> clf = Pipeline([ >>> ('vect', TfidfVectorizer( >>> stop_words=stop_words, >>> token_pattern=ur"\b[a-z0-9_\-\.]+[a-z][a-z0- ...Read now
Unlock full access