Ai Forums Home Welcome Guest    Monday, September 06, 2010
Ai Site > Ai Forums > The Artificial Intelligence Forum > Naive Bayes Classifier Last PostsLoginRegisterWhy Register
Topic: Naive Bayes Classifier

djmccabie
posted 3/15/2010  20:28Reply with quote
Ok my task involves the implementation of a naive Bayes classifier for the task of differentiating between utterances of the words "yes" and "no". I have been given code and data. I need to adapt two classes.


I can run the command



java naivebayes.PlotSound 12



and this will plot a sound wave for the 12th example from the data set. The data set has 165 examples. Examples 1-82 are "yes" and examples 83-165 are "no".





The classes I need to adapt are called "YesNoClassifier" and "Classifier"





I have 3 tasks:



1. Modify the code in the main method of YesNoClassifier in order to return the percentage of errors that the classifier makes on all 165 examples in the data set.



2. Complete the code in the classify (double[] featureVector) method of the classifier class. This should implement a naive Bayes classifier that uses all of the feature vector components. Once implemented this method, evaluate its performance in comparison to the single feature approach.



3. Create a new constructor method for the Classifier class which estimates the priors p(C_1) and p(C_2) from the data.









--------------Here is the current code for the class “YesNoClassifier”----------------------------



package naivebayes;

import java.util.*;

import javagently.*;



/**

* Using a naive Bayes classifier to distinguish utterances of the word yes from the word no

*/



public class YesNoClassifier {



public static void main(String[] args)

{

// Read in MFCC data



String mfccDataDirectory = "data/yesno/mfcc/";

Data yesData = new Data (mfccDataDirectory+"yes");

Data noData = new Data (mfccDataDirectory+"no");



// Build a naive Bayes classifier



Classifier classifier = new Classifier(yesData.getMeanMfcc(),noData.getMeanMfcc(),0.5);



// Compute the probability of being in class one for the first yes example

// using the 1st time-averaged MFCC as the feature



int featureNumber = 0; // Using this MFCC component (0 is 1st component)

int exampleNumber = 0; // Classifying this example



double answer = classifier.classify(yesData.getMeanMfcc(exampleNumber),featureNumber);



System.out.println("The probability of this example being a yes is "+Float.toString((float) answer));

}

}





------------------And here is the code for Classifier-----------------



package naivebayes;

/** A two class naive Bayes classifier. This class stores the prior probability

* of each class, p(C1) and p(C2), and the conditional probabilities p(x|C1) and p(x|C2) which are modelled as

* normal densities for each feature vector component. The constructor method fits the conditional probabilities

* by setting the mean and variance of these equal to the empirical mean and variance of data from each

* class.

*/


public class Classifier {


private double priorClass1; // p(C1) - prior probability for Class 1

private double priorClass2; // p(C2) - prior probability for Class 2

private Normal[] pxGivenClass1; // p(x|C1) for each feature dimension

private Normal[] pxGivenClass2; // p(x|C2) for each feature dimension

private int d; // Dimension of feature vector

/**

* This constructor method fits the parameters of two normal densities

* and stores the priors for each class

*/

public Classifier (double[][] featureClass1, double[][] featureClass2, double pC1) {

priorClass1 = pC1;

priorClass2 = 1.0 - pC1; // The prior probabilities for each class must sum to one

d = featureClass1.length;


// Fit a normal density for each feature dimension


pxGivenClass1 = new Normal[d];

pxGivenClass2 = new Normal[d];

for (int i=0;i<d;i++){

pxGivenClass1[i] = new Normal(featureClass1[i]);
pxGivenClass2[i] = new Normal(featureClass2[i]);

}
}
/**

* This method returns the probability of being in class 1 using only data from one feature

* which is in featureVector[featureNo]
*/
public double classify (double[] featureVector, int featureNo) {

// Numerator of Bayes' theorem

double numerator = pxGivenClass1[featureNo].density(featureVector[featureNo])*priorClass1;

// Denominator of Bayes' theorem

double denominator = numerator + pxGivenClass2[featureNo].density(featureVector[featureNo])*priorClass2;

return numerator/denominator;

}

/**
* This method should be modified in order to return the probability of being in class 1 using all the

* components of the feature vector
*/
public double classify (double[] featureVector) {
// Your code should go here
return 0.5;
}
}





Please could anybody please help me with this as I don't have a clue where to even start!



  1  
'Send Send email to user    Reply with quote Reply with quote    Edit message Edit message

Forums Home    Hal and other child machines    Alan and other chatbots    Language Mind and Consciousness  
Contact Us Terms of Use