使用LingPipe对朴素贝叶斯进行数据分类

我想根据内容将某些数据分类到不同的类中。 我使用朴素贝叶斯分类器做了它,我得到一个输出作为它所属的最佳类别。 但是现在我想将除了训练集之外的新闻分类为“其他”课程。 我不能手动将除训练数据之外的每个/每个数据添加到某个类中,因为它有大量其他类别。那么有没有办法对其他数据进行分类?

private static File TRAINING_DIR = new File("4news-train"); private static File TESTING_DIR = new File("4news-test"); private static String[] CATEGORIES = { "c1", "c2", "c3", "others" }; private static int NGRAM_SIZE = 6; public static void main(String[] args) throws ClassNotFoundException, IOException { DynamicLMClassifier classifier = DynamicLMClassifier.createNGramProcess(CATEGORIES, NGRAM_SIZE); for (int i = 0; i < CATEGORIES.length; ++i) { File classDir = new File(TRAINING_DIR, CATEGORIES[i]); if (!classDir.isDirectory()) { String msg = "Could not find training directory=" + classDir + "\nTraining directory not found"; System.out.println(msg); // in case exception gets lost in shell throw new IllegalArgumentException(msg); } String[] trainingFiles = classDir.list(); for (int j = 0; j < trainingFiles.length; ++j) { File file = new File(classDir, trainingFiles[j]); String text = Files.readFromFile(file, "ISO-8859-1"); System.out.println("Training on " + CATEGORIES[i] + "/" + trainingFiles[j]); Classification classification = new Classification(CATEGORIES[i]); Classified classified = new Classified(text, classification); classifier.handle(classified); } } } 

只是序列化对象……这意味着将中间对象写入文件,这将是您的模型……

然后进行测试只需要将数据传递到模型中,无需每次都进行训练……这对您来说会更容易

Naive Bayes在计算时为每个分类提供“信心”

 P(y|x) ~ P(y)P(x|y) 

直到P(x)的归一化,它是x作为y类的一部分的概率。 你可以简单地切断这个值并说出来

 cl(x) = "other" iff max_{over y}(P(y|x)) < T 

其中T可以是例如训练集的最小置信度

 T = min_{over x and y in Training set}( P(y|x) )