For 10-fold cross-validation, performs 10 separate tests of the classifier, each with a different 9/10 training and 1/10 testing documents. The given classifier is a class (Bayes, KNN, SVM) which is initialized with the given optional parameters.
(Classifier, documents=[], folds=10, **kwargs)
| 1909 | return s |
| 1910 | |
| 1911 | def K_fold_cross_validation(Classifier, documents=[], folds=10, **kwargs): |
| 1912 | """ For 10-fold cross-validation, performs 10 separate tests of the classifier, |
| 1913 | each with a different 9/10 training and 1/10 testing documents. |
| 1914 | The given classifier is a class (Bayes, KNN, SVM) |
| 1915 | which is initialized with the given optional parameters. |
| 1916 | """ |
| 1917 | K = kwargs.pop("K", folds) |
| 1918 | d = [isinstance(d, Document) and (d, d.type) or d for d in documents] |
| 1919 | d = shuffled(d) # Avoid a list sorted by type (because we take successive folds). |
| 1920 | m = [0.0, 0.0, 0.0, 0.0] # Macro-average accuracy, precision, recall & F1-score. |
| 1921 | for i in range(K): |
| 1922 | n = len(d) / float(K) # Test fold size. |
| 1923 | x = int(round(i * n)) # Test fold start index. |
| 1924 | y = int(round(i * n + n)) # Test fold stop index. |
| 1925 | classifier = Classifier(train=d[:x]+d[y:], **kwargs) |
| 1926 | A, P, R, F = classifier.test(d[x:y], **kwargs) |
| 1927 | m[0] += A |
| 1928 | m[1] += P |
| 1929 | m[2] += R |
| 1930 | m[3] += F |
| 1931 | return tuple([v / (K or 1) for v in m]) |
| 1932 | |
| 1933 | kfoldcv = K_fold_cv = k_fold_cv = k_fold_cross_validation = K_fold_cross_validation |
| 1934 |
no test coverage detected
searching dependent graphs…