python - Get a classification report stating the class wise precision and recall for multinomial Naive Bayes using 10 fold cross validation -


i have following piece of code uses nb classifier multi class classification problem. function preforms cross validation storing accuracies , printing average later. instead want classification report specifying class wise precision , recall, instead of mean accuracy score in end.

   import random    sklearn import cross_validation    sklearn.naive_bayes import multinomialnb     def multinomial_nb_with_cv(x_train, y_train):         random.shuffle(x)         kf = cross_validation.kfold(len(x), n_folds=10)         acc = []         train_index, test_index in kf:             y_true = y_train[test_index]             clf = multinomialnb().fit(x_train[train_index],                      y_train[train_index])             y_pred = clf.predict(x_train[test_index])             acc.append(accuracy_score(y_true, y_pred)) 

if not perform cross validation have is:

    sklearn.metrics import classification_report     sklearn.naive_bayes import multinomialnb      def multinomial_nb(x_train, y_train, x_test, y_test):         clf = multinomialnb().fit(x_train, y_train)         y_pred = clf.predict(x_test)         y_true = y_test         print classification_report(y_true, y_pred) 

and gives me report this:

        precision    recall  f1-score   support        0       0.50      0.24      0.33       221       1       0.00      0.00      0.00        18       2       0.00      0.00      0.00        27       3       0.00      0.00      0.00        28       4       0.00      0.00      0.00        32       5       0.04      0.02      0.02        57       6       0.00      0.00      0.00        26       7       0.00      0.00      0.00        25       8       0.00      0.00      0.00        43       9       0.00      0.00      0.00        99      10       0.63      0.98      0.76       716      avg / total       0.44      0.59      0.48      1292 

how can similar report in case of cross validation?

you can use cross_val_predict generate cross-validation prediction , use classification_report.

from sklearn.datasets import make_classification sklearn.cross_validation import cross_val_predict sklearn.naive_bayes import gaussiannb sklearn.metrics import classification_report  # generate artificial data 11 classes x, y = make_classification(n_samples=2000, n_features=20, n_informative=10, n_classes=11, random_state=0)  # classifier, assume gaussiannb here non-integer data x estimator = gaussiannb() # generate cross-validation prediction 10 fold stratified sampling y_pred = cross_val_predict(estimator, x, y, cv=10) y_pred.shape  out[91]: (2000,)  # generate report print(classification_report(y, y_pred))               precision    recall  f1-score   support            0       0.47      0.36      0.41       181           1       0.38      0.46      0.41       181           2       0.45      0.53      0.48       182           3       0.29      0.45      0.35       183           4       0.37      0.33      0.35       183           5       0.40      0.44      0.42       182           6       0.27      0.13      0.17       183           7       0.47      0.44      0.45       182           8       0.34      0.27      0.30       182           9       0.41      0.44      0.42       179          10       0.42      0.41      0.41       182  avg / total       0.39      0.39      0.38      2000 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -