/** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClassifier(new NaiveBayesMultinomialUpdateable(), argv); } }
/** * Generates the classifier. * * @param instances set of instances serving as training data * @throws Exception if the classifier has not been generated successfully */ public void buildClassifier(Instances instances) throws Exception { initializeClassifier(instances); //enumerate through the instances m_wordsPerClass = new double[m_numClasses]; for (int i = 0; i < m_numClasses; i++) { m_wordsPerClass[i] = m_numAttributes - 1; } for (Instance instance : instances) { updateClassifier(instance); } }
ArffLoader loader = new ArffLoader(); loader.setFile(new File(""));//file is valid Instances structure = loader.getStructure(); structure.setClassIndex(0); // train NaiveBayes NaiveBayesMultinomialUpdateable n = new NaiveBayesMultinomialUpdateable(); FilteredClassifier f = new FilteredClassifier(); StringToWordVector s = new StringToWordVector(); f.setFilter(s); f.setClassifier(n); f.buildClassifier(structure); Instance current; while ((current = loader.getNextInstance(structure)) != null) n.updateClassifier(current); // output generated model System.out.println(n);
/** Creates a default NaiveBayesMultinomialUpdateable */ public Classifier getClassifier() { return new NaiveBayesMultinomialUpdateable(); }
/** * Returns a string describing this classifier * @return a description of the classifier suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Class for building and using an updateable multinomial Naive Bayes classifier. " + "For more information see,\n\n" + getTechnicalInformation().toString() + "\n\n" + "The core equation for this classifier:\n\n" + "P[Ci|D] = (P[D|Ci] x P[Ci]) / P[D] (Bayes' rule)\n\n" + "where Ci is class i and D is a document."; }
/** * Returns a string representation of the classifier. * * @return a string representation of the classifier */ public String toString() { StringBuffer result = new StringBuffer("The class counts (including Laplace correction)\n-----------------------------------------------\n"); for(int c = 0; c<m_numClasses; c++) result.append(m_headerInfo.classAttribute().value(c)).append("\t"). append(Utils.doubleToString(m_probOfClass[c], getNumDecimalPlaces())).append("\n"); result.append("\nThe probability of a word given the class\n-----------------------------------------\n\t"); for(int c = 0; c<m_numClasses; c++) result.append(m_headerInfo.classAttribute().value(c)).append("\t"); result.append("\n"); for(int w = 0; w<m_numAttributes; w++) { if (w != m_headerInfo.classIndex()) { result.append(m_headerInfo.attribute(w).name()).append("\t"); for(int c = 0; c<m_numClasses; c++) result.append(Utils.doubleToString(m_probOfWordGivenClass[c][w] / m_wordsPerClass[c], getNumDecimalPlaces())).append("\t"); result.append("\n"); } } return result.toString(); }
/** Creates a default NaiveBayesMultinomialUpdateable */ public Classifier getClassifier() { return new NaiveBayesMultinomialUpdateable(); }
/** * Returns a string describing this classifier * @return a description of the classifier suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Class for building and using an updateable multinomial Naive Bayes classifier. " + "For more information see,\n\n" + getTechnicalInformation().toString() + "\n\n" + "The core equation for this classifier:\n\n" + "P[Ci|D] = (P[D|Ci] x P[Ci]) / P[D] (Bayes' rule)\n\n" + "where Ci is class i and D is a document."; }
/** * Returns a string representation of the classifier. * * @return a string representation of the classifier */ public String toString() { StringBuffer result = new StringBuffer("The class counts (including Laplace correction)\n-----------------------------------------------\n"); for(int c = 0; c<m_numClasses; c++) result.append(m_headerInfo.classAttribute().value(c)).append("\t"). append(Utils.doubleToString(m_probOfClass[c], getNumDecimalPlaces())).append("\n"); result.append("\nThe probability of a word given the class\n-----------------------------------------\n\t"); for(int c = 0; c<m_numClasses; c++) result.append(m_headerInfo.classAttribute().value(c)).append("\t"); result.append("\n"); for(int w = 0; w<m_numAttributes; w++) { if (w != m_headerInfo.classIndex()) { result.append(m_headerInfo.attribute(w).name()).append("\t"); for(int c = 0; c<m_numClasses; c++) result.append(Utils.doubleToString(m_probOfWordGivenClass[c][w] / m_wordsPerClass[c], getNumDecimalPlaces())).append("\t"); result.append("\n"); } } return result.toString(); }
/** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClassifier(new NaiveBayesMultinomialUpdateable(), argv); } }
/** * Generates the classifier. * * @param instances set of instances serving as training data * @throws Exception if the classifier has not been generated successfully */ public void buildClassifier(Instances instances) throws Exception { initializeClassifier(instances); //enumerate through the instances m_wordsPerClass = new double[m_numClasses]; for (int i = 0; i < m_numClasses; i++) { m_wordsPerClass[i] = m_numAttributes - 1; } for (Instance instance : instances) { updateClassifier(instance); } }