/** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClassifier(new NaiveBayesMultinomial(), argv); } }
/** * 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 a 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."; }
NaiveBayesMultinomial NBM = new NaiveBayesMultinomial(); NBM = (NaiveBayesMultinomial)weka.core.SerializationHelper.read("D:/MultiNomial.model");
/** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if there is a problem generating the prediction */ public double [] distributionForInstance(Instance instance) throws Exception { double[] probOfClassGivenDoc = new double[m_numClasses]; //calculate the array of log(Pr[D|C]) double[] logDocGivenClass = new double[m_numClasses]; for (int h = 0; h < m_numClasses; h++) { logDocGivenClass[h] = probOfDocGivenClass(instance, h); } double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)]; for (int i = 0; i < m_numClasses; i++) { probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max) * m_probOfClass[i]; } Utils.normalize(probOfClassGivenDoc); return probOfClassGivenDoc; }
/** * Sets up the classifier before any actual instances are processed. */ protected void initializeClassifier(Instances instances) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(instances); m_headerInfo = new Instances(instances, 0); m_numClasses = instances.numClasses(); m_numAttributes = instances.numAttributes(); m_probOfWordGivenClass = new double[m_numClasses][]; // Initialize the matrix of word counts for (int c = 0; c < m_numClasses; c++) { m_probOfWordGivenClass[c] = new double[m_numAttributes]; for (int att = 0; att < m_numAttributes; att++) { m_probOfWordGivenClass[c][att] = 1.0; } } // Initialize class counts m_probOfClass = new double[m_numClasses]; for (int i = 0; i < m_numClasses; i++) { m_probOfClass[i] = 1.0; } }
append(Utils.doubleToString(m_probOfClass[c], getNumDecimalPlaces())).append("\n"); result.append(m_headerInfo.attribute(w).name()).append("\t"); for(int c = 0; c<m_numClasses; c++) result.append(Utils.doubleToString(Math.exp(m_probOfWordGivenClass[c][w]), getNumDecimalPlaces())). append("\t"); result.append("\n");
/** Creates a default NaiveBayesMultinomial */ public Classifier getClassifier() { return new NaiveBayesMultinomial(); }
/** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if there is a problem generating the prediction */ public double [] distributionForInstance(Instance instance) throws Exception { double[] probOfClassGivenDoc = new double[m_numClasses]; //calculate the array of log(Pr[D|C]) double[] logDocGivenClass = new double[m_numClasses]; for (int h = 0; h < m_numClasses; h++) { logDocGivenClass[h] = probOfDocGivenClass(instance, h); } double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)]; for (int i = 0; i < m_numClasses; i++) { probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max) * m_probOfClass[i]; } Utils.normalize(probOfClassGivenDoc); return probOfClassGivenDoc; }
/** * Sets up the classifier before any actual instances are processed. */ protected void initializeClassifier(Instances instances) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(instances); m_headerInfo = new Instances(instances, 0); m_numClasses = instances.numClasses(); m_numAttributes = instances.numAttributes(); m_probOfWordGivenClass = new double[m_numClasses][]; // Initialize the matrix of word counts for (int c = 0; c < m_numClasses; c++) { m_probOfWordGivenClass[c] = new double[m_numAttributes]; for (int att = 0; att < m_numAttributes; att++) { m_probOfWordGivenClass[c][att] = 1.0; } } // Initialize class counts m_probOfClass = new double[m_numClasses]; for (int i = 0; i < m_numClasses; i++) { m_probOfClass[i] = 1.0; } }
append(Utils.doubleToString(m_probOfClass[c], getNumDecimalPlaces())).append("\n"); result.append(m_headerInfo.attribute(w).name()).append("\t"); for(int c = 0; c<m_numClasses; c++) result.append(Utils.doubleToString(Math.exp(m_probOfWordGivenClass[c][w]), getNumDecimalPlaces())). append("\t"); result.append("\n");
initializeClassifier(instances);
/** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClassifier(new NaiveBayesMultinomial(), argv); } }
/** Creates a default NaiveBayesMultinomial */ public Classifier getClassifier() { return new NaiveBayesMultinomial(); }
/** * 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 a 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."; }