31

I have a small corpus and I want to calculate the accuracy of naive Bayes classifier using 10-fold cross validation, how can do it.

1

5 Answers 5

28

Your options are to either set this up yourself or use something like NLTK-Trainer since NLTK doesn't directly support cross-validation for machine learning algorithms.

I'd recommend probably just using another module to do this for you but if you really want to write your own code you could do something like the following.

Supposing you want 10-fold, you would have to partition your training set into 10 subsets, train on 9/10, test on the remaining 1/10, and do this for each combination of subsets (10).

Assuming your training set is in a list named training, a simple way to accomplish this would be,

num_folds = 10 subset_size = len(training)/num_folds for i in range(num_folds): testing_this_round = training[i*subset_size:][:subset_size] training_this_round = training[:i*subset_size] + training[(i+1)*subset_size:] # train using training_this_round # evaluate against testing_this_round # save accuracy # find mean accuracy over all rounds 
Sign up to request clarification or add additional context in comments.

3 Comments

thank you Jared for your answer, but what I can use the library scikit cross_validation.KFold-learn with the naive Bayes classifier of NLTK ?
I was killing myself trying to get sklearn's cross_validation or Kfold to work with my data -- kept getting errors I couldn't understand. But this worked first time around. Thank you!
Actually, almost the first time around: I had to add an int() around the subset_size definition, otherwise was getting an wrong type for indexing error.
24

Actually there is no need for a long loop iterations that are provided in the most upvoted answer. Also the choice of classifier is irrelevant (it can be any classifier).

Scikit provides cross_val_score, which does all the looping under the hood.

from sklearn.cross_validation import KFold, cross_val_score k_fold = KFold(len(y), n_folds=10, shuffle=True, random_state=0) clf = <any classifier> print cross_val_score(clf, X, y, cv=k_fold, n_jobs=1) 

3 Comments

KFold and cross_val_score have been moved to sklearn.model_selection in v0.18
KFold(n_splits=3, shuffle=False, random_state=None). Also check out the docs
The cross_validation submodule is now deprecated. The substitute is the model_selection submodule. 'from sklearn.model_selection import KFold, cross_val_score': stackoverflow.com/questions/30667525/…
15

I've used both libraries and NLTK for naivebayes sklearn for crossvalidation as follows:

import nltk from sklearn import cross_validation training_set = nltk.classify.apply_features(extract_features, documents) cv = cross_validation.KFold(len(training_set), n_folds=10, indices=True, shuffle=False, random_state=None, k=None) for traincv, testcv in cv: classifier = nltk.NaiveBayesClassifier.train(training_set[traincv[0]:traincv[len(traincv)-1]]) print 'accuracy:', nltk.classify.util.accuracy(classifier, training_set[testcv[0]:testcv[len(testcv)-1]]) 

and at the end I calculated the average accuracy

Comments

1

Modified the second answer:

cv = cross_validation.KFold(len(training_set), n_folds=10, shuffle=True, random_state=None) 

Comments

1

Inspired from Jared's answer, here is a version using a generator:

def k_fold_generator(X, y, k_fold): subset_size = len(X) / k_fold # Cast to int if using Python 3 for k in range(k_fold): X_train = X[:k * subset_size] + X[(k + 1) * subset_size:] X_valid = X[k * subset_size:][:subset_size] y_train = y[:k * subset_size] + y[(k + 1) * subset_size:] y_valid = y[k * subset_size:][:subset_size] yield X_train, y_train, X_valid, y_valid 

I am assuming that your data set X has N data points (= 4 in the example) and D features (= 2 in the example). The associated N labels are stored in y.

X = [[ 1, 2], [3, 4], [5, 6], [7, 8]] y = [0, 0, 1, 1] k_fold = 2 for X_train, y_train, X_valid, y_valid in k_fold_generator(X, y, k_fold): # Train using X_train and y_train # Evaluate using X_valid and y_valid 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.