如何使用sci-kit learn识别错误分类的文本文件的ID /名称/标题

时间:2015-04-06 13:34:22

标签: python-2.7 scikit-learn text-classification naivebayes

我正在建立自己的分类器进行文本分类,但目前我正在玩sci-kit学习,以便弄清楚一些事情。我使用NB分类器对我的一些文本文件进行了分类。我使用手动分类为2个类别的26个文本文件,每个文件编号在01-26之间(即'01 .txt'等等)。

我的代码和结果:

import sklearn
from sklearn.datasets import load_files
import numpy as np
bunch = load_files('corpus')

split_pcnt = 0.75 
split_size = int(len(bunch.data) * split_pcnt)
X_train = bunch.data[:split_size]
X_test = bunch.data[split_size:]
y_train = bunch.target[:split_size]
y_test = bunch.target[split_size:]

from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer, HashingVectorizer, CountVectorizer

clf_1 = Pipeline([('vect', CountVectorizer()),
                      ('clf', MultinomialNB()),
    ])

from sklearn.cross_validation import cross_val_score, KFold
from scipy.stats import sem

def evaluate_cross_validation(clf, X, y, K):
    # create a k-fold croos validation iterator of k=5 folds
    cv = KFold(len(y), K, shuffle=True, random_state=0)
    # by default the score used is the one returned by score >>> method of the estimator (accuracy)
    scores = cross_val_score(clf, X, y, cv=cv)
    print scores
    print ("Mean score: {0:.3f} (+/-{1:.3f})").format(np.mean(scores), sem(scores))

clfs = [clf_1]

for clf in clfs:
    evaluate_cross_validation(clf, bunch.data, bunch.target, 5)

[ 0.5  0.4  0.4  0.4  0.6]
Mean score: 0.460 (+/-0.040)

from sklearn import metrics

def train_and_evaluate(clf, X_train, X_test, y_train, y_test):

    clf.fit(X_train, y_train)

    print "Accuracy on training set:"
    print clf.score(X_train, y_train)
    print "Accuracy on testing set:"
    print clf.score(X_test, y_test)
    y_pred = clf.predict(X_test)

    print "Classification Report:"
    print metrics.classification_report(y_test, y_pred)
    print "Confusion Matrix:"
    print metrics.confusion_matrix(y_test, y_pred)


train_and_evaluate(clf_1, X_train, X_test, y_train, y_test)

Accuracy on training set:
1.0

Accuracy on testing set:
0.714285714286

    Classification Report:
                 precision    recall  f1-score   support

              0       0.67      0.67      0.67         3
              1       0.75      0.75      0.75         4

    avg / total       0.71      0.71      0.71         7

    Confusion Matrix:
    [[2 1]
     [1 3]]

我无法弄清楚如何识别错误分类文件的ID,以查看哪些确切文件被错误分类(例如'05 .txt'和'23 .txt')。 sci-kit可以学习吗?

最好的,

guzdeh

2 个答案:

答案 0 :(得分:0)

假设load_files按字母顺序加载文本文件,您只需要错误分类的示例的索引。这可以通过以下方式获得:

misclassified = np.where(y_pred != y_test)
print(misclassified)

train_and_evaluate函数的末尾。因此,如果打印出来,请说[1, 3, 7],文件' 01.txt',' 03.txt'和' 07.txt'被错误分类。

答案 1 :(得分:0)

是的,您必须使用load_files结果的属性filenames

但是,您的示例代码中有两个模型训练和评估周期:一个使用CV,另一个使用简单的训练测试分割。

在火车测试分裂中:

test_filenames = bunch.filenames[split_size:]
misclassified = (y_pred != y_test)
print test_filenames[misscalssified]

此答案不假设文本文件按字母顺序排列或者所有数字都存在。