Returns the test results for every combination of optional parameters, using K-fold cross-validation for the given classifier (Bayes, KNN, SVM). For example: for (A, P, R, F), p in gridsearch(SVM, data, c=[0.1, 1, 10]): print (A, P, R, F), p > (0.919, 0.9
(Classifier, documents=[], folds=10, **kwargs)
| 1933 | kfoldcv = K_fold_cv = k_fold_cv = k_fold_cross_validation = K_fold_cross_validation |
| 1934 | |
| 1935 | def gridsearch(Classifier, documents=[], folds=10, **kwargs): |
| 1936 | """ Returns the test results for every combination of optional parameters, |
| 1937 | using K-fold cross-validation for the given classifier (Bayes, KNN, SVM). |
| 1938 | For example: |
| 1939 | for (A, P, R, F), p in gridsearch(SVM, data, c=[0.1, 1, 10]): |
| 1940 | print (A, P, R, F), p |
| 1941 | > (0.919, 0.921, 0.919, 0.920), {"c": 10} |
| 1942 | > (0.874, 0.884, 0.865, 0.874), {"c": 1} |
| 1943 | > (0.535, 0.424, 0.551, 0.454), {"c": 0.1} |
| 1944 | """ |
| 1945 | def product(*args): |
| 1946 | # Yields the cartesian product of given iterables: |
| 1947 | # list(product([1, 2], [3, 4])) => [(1, 3), (1, 4), (2, 3), (2, 4)] |
| 1948 | p = [[]] |
| 1949 | for iterable in args: |
| 1950 | p = [x + [y] for x in p for y in iterable] |
| 1951 | for p in p: |
| 1952 | yield tuple(p) |
| 1953 | s = [] # [((A, P, R, F), parameters), ...] |
| 1954 | p = [] # [[("c", 0.1), ("c", 10), ...], |
| 1955 | # [("gamma", 0.1), ("gamma", 0.2), ...], ...] |
| 1956 | for k, v in kwargs.items(): |
| 1957 | p.append([(k, v) for v in v]) |
| 1958 | for p in product(*p): |
| 1959 | p = dict(p) |
| 1960 | s.append((K_fold_cross_validation(Classifier, documents, folds, **p), p)) |
| 1961 | return sorted(s, reverse=True) |
| 1962 | |
| 1963 | #--- NAIVE BAYES CLASSIFIER ------------------------------------------------------------------------ |
| 1964 |
nothing calls this directly
no test coverage detected
searching dependent graphs…