本文整理匯總了Java中weka.classifiers.bayes.NaiveBayes類的典型用法代碼示例。如果您正苦於以下問題:Java NaiveBayes類的具體用法?Java NaiveBayes怎麽用?Java NaiveBayes使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NaiveBayes類屬於weka.classifiers.bayes包,在下文中一共展示了NaiveBayes類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: LearnNaiveBays
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
@Override
public void LearnNaiveBays() throws Exception
{
trainedData.setClassIndex(trainedData.numAttributes()-1);
filter=new StringToWordVector();
classifier=new FilteredClassifier();
classifier.setFilter(filter);
classifier.setClassifier(new NaiveBayes());
classifier.buildClassifier(trainedData);
}
示例2: getClassifierClassName
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
/**
* Get classifier's class name by a short name
* */
public static String getClassifierClassName(String classifierName) {
String className = "";
switch (classifierName) {
case "SGD":
className = SGD.class.toString();
break;
case "SGDText":
className = SGDText.class.toString();
break;
case "J48":
className = J48.class.toString();
break;
case "PART":
className = PART.class.toString();
break;
case "NaiveBayes":
className = NaiveBayes.class.toString();
break;
case "NBUpdateable":
className = NaiveBayesUpdateable.class.toString();
break;
case "AdaBoostM1":
className = AdaBoostM1.class.toString();
break;
case "LogitBoost":
className = LogitBoost.class.toString();
break;
case "Bagging":
className = Bagging.class.toString();
break;
case "Stacking":
className = Stacking.class.toString();
break;
case "AdditiveRegression":
className = AdditiveRegression.class.toString();
break;
case "Apriori":
className = Apriori.class.toString();
break;
default:
className = SGD.class.toString();
}
className = className.substring(6);
return className;
}
示例3: generateModel
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
public void generateModel(){
nb = new NaiveBayes();
try {
nb.buildClassifier(iris);
} catch (Exception e) {
}
}
示例4: testScoreWithClassifierSomeMissingFields
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
@Test
public void testScoreWithClassifierSomeMissingFields() throws Exception {
Instances train = new Instances(new BufferedReader(new StringReader(
CorrelationMatrixMapTaskTest.IRIS)));
train.setClassIndex(train.numAttributes() - 1);
NaiveBayes bayes = new NaiveBayes();
bayes.buildClassifier(train);
WekaScoringMapTask task = new WekaScoringMapTask();
Remove r = new Remove();
r.setAttributeIndices("1");
r.setInputFormat(train);
Instances test = Filter.useFilter(train, r);
task.setModel(bayes, train, test);
assertTrue(task.getMissingMismatchAttributeInfo().length() > 0);
assertTrue(task.getMissingMismatchAttributeInfo().equals(
"sepallength missing from incoming data\n"));
assertEquals(3, task.getPredictionLabels().size());
for (int i = 0; i < test.numInstances(); i++) {
assertEquals(3, task.processInstance(test.instance(i)).length);
}
}
示例5: runExps
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
public void runExps(){
Classifier c1 = new SMO();
Classifier c2 = new J48();
Classifier c3 = new NaiveBayes();
trainModel(c1, "SVM");
trainModel(c2, "J48");
trainModel(c3, "Naive Bayes");
}
示例6: instance
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
/**
* Gets the appropriate subclass explainer (from the standard set that has been implemented in the toolkit),
* depending on the classifier model (e.g. NaiveBayes, J48).
* @param enactor
* @param classifierWrapper
* @return
*/
public static WekaExplainer<?> instance(Enactor enactor, ClassifierWrapper classifierWrapper) throws Exception {
Classifier classifier = classifierWrapper.getClassifier();
if (classifier instanceof NaiveBayes) {
return new NaiveBayesExplainer(enactor, classifierWrapper);
}
else if (classifier instanceof J48) {
return new J48Explainer(enactor, classifierWrapper);
}
else {
return null; // TODO: doesn't support other Weka Classifiers yet
}
}
示例7: testMultipleModels
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
@Test
public void testMultipleModels() throws FOSException {
ModelConfig modelConfig = new ModelConfig();
modelConfig.setProperty(WekaModelConfig.CLASSIFIER_IMPL, Vote.class.getName());
modelConfig.setProperty(WekaModelConfig.CLASSIFIER_CONFIG, "-R MAX -B \""+J48.class.getName()+"\" -B \"" + NaiveBayes.class.getName() + "\"");
MultipleClassifiersCombiner classifier = (MultipleClassifiersCombiner)WekaClassifierFactory.create(modelConfig);
Assert.assertEquals(2,classifier.getClassifiers().length);
Assert.assertEquals(J48.class,classifier.getClassifiers()[0].getClass());
Assert.assertEquals(NaiveBayes.class,classifier.getClassifiers()[1].getClass());
}
示例8: WekaManager
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
private WekaManager() {
super();
// set a Classifier (Naives Bayes)
classifier = new NaiveBayes();
classifier.setUseKernelEstimator(true);
atts = new ArrayList<Attribute>();
attsTesting = new ArrayList<Attribute>();
attClassVals = new ArrayList<String>();
metaData= new ArrayList<String>();
mListeners = new ArrayList<IWekaListener>();
isTrainingDataReceived = false;
}
示例9: initializeModel
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
/**
* Builds the classifier for the given training model
*/
private void initializeModel(CommonConfig config)
throws ConfigurationException
{
// Train the classifier
logger.info("Training the classifier...");
File arffFile = new File(modelDir + "/" + this.getClass().getSimpleName() + ".arff");
classifier = new NaiveBayes();
try {
Instances data = DataSource.read(arffFile.getAbsolutePath());
data.setClassIndex(data.numAttributes() - 1);
classifier.buildClassifier(data);
} catch (Exception e) {
throw new ConfigurationException(e);
}
}
示例10: Classification
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
public Classification(ArrayList<ClassifierType> cType) {
cls = new Classifier[cType.size()];
eval = new Evaluation[cType.size()];
for(int i = 0; i < cType.size();i++){
switch(cType.get(i)){
// TODO Will we use J48 or ID3 implementation of decision trees?
case J48:
cls[i] = new J48();
break;
case NAIVE_BAYES:
// If bType == Incremental then cls = new UpdateableNaiveBayes(); else
cls[i] = new NaiveBayes();
break;
case IBK:
cls[i] = new IBk();
break;
case COSINE:
cls[i] = useCosine();
// TODO Add other cases: Decision Rule, KNN and so on.
}
}
}
示例11: train
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
public void train(TrainData data, TermVocabulary vocab) throws Exception {
this.vocab = vocab;
Instances trainset = buildInstances();
for (Pair<String, BytecodeData.MethodData> p : data.data()) {
double[] instV = seqToFeats(p.getRight().getExtOpcodeSeq());
instV[instV.length - 1] = (double) vocab.termToPos(p.getKey());
Instance inst = new DenseInstance(1.0, instV);
inst.setDataset(trainset);
trainset.add(inst);
}
nb = new NaiveBayes();
nb.buildClassifier(trainset);
}
示例12: testScoreWithClassifier
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
@Test
public void testScoreWithClassifier() throws Exception {
Instances train = new Instances(new BufferedReader(new StringReader(
CorrelationMatrixMapTaskTest.IRIS)));
train.setClassIndex(train.numAttributes() - 1);
NaiveBayes bayes = new NaiveBayes();
bayes.buildClassifier(train);
WekaScoringMapTask task = new WekaScoringMapTask();
task.setModel(bayes, train, train);
assertEquals(0, task.getMissingMismatchAttributeInfo().length());
assertEquals(3, task.getPredictionLabels().size());
for (int i = 0; i < train.numInstances(); i++) {
assertEquals(3, task.processInstance(train.instance(i)).length);
}
}
示例13: learn
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
/**
* This method trains the classifier on the loaded dataset.
*/
public void learn() {
try {
trainData.setClassIndex(0);
filter = new StringToWordVector();
filter.setAttributeIndices("last");
classifier = new FilteredClassifier();
classifier.setFilter(filter);
classifier.setClassifier(new NaiveBayes());
classifier.buildClassifier(trainData);
// Uncomment to see the classifier
System.out.println("===== Training on filtered (training) dataset =====");
//System.out.println(classifier);
} catch (Exception e) {
System.out.println("Problem found when training");
}
}
示例14: trainClassifier
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
public void trainClassifier(Classifier classifier, File trainingDataset,
FileOutputStream trainingModel, Integer
crossValidationFoldNumber) throws Exception {
CSVLoader csvLoader = new CSVLoader();
csvLoader.setSource(trainingDataset);
Instances instances = csvLoader.getDataSet();
switch(classifier) {
case KNN:
int K = (int) Math.ceil(Math.sqrt(instances.numInstances()));
this.classifier = new IBk(K);
break;
case NB:
this.classifier = new NaiveBayes();
}
if(instances.classIndex() == -1) {
instances.setClassIndex(instances.numAttributes() - 1);
}
this.classifier.buildClassifier(instances);
if(crossValidationFoldNumber > 0) {
Evaluation evaluation = new Evaluation(instances);
evaluation.crossValidateModel(this.classifier, instances, crossValidationFoldNumber,
new Random(1));
kappa = evaluation.kappa();
fMeasure = evaluation.weightedFMeasure();
confusionMatrix = evaluation.toMatrixString("Confusion matrix: ");
}
ObjectOutputStream outputStream = new ObjectOutputStream(trainingModel);
outputStream.writeObject(this.classifier);
outputStream.flush();
outputStream.close();
}
示例15: main
import weka.classifiers.bayes.NaiveBayes; //導入依賴的package包/類
/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception {
DataSource source = new DataSource("src/files/letter.arff");
int folds = 10;
int runs = 30;
Classifier cls = new NaiveBayes();
Instances data = source.getDataSet();
data.setClassIndex(16);
System.out.println("#seed \t correctly instances \t percentage of corrects\n");
for (int i = 1; i <= runs; i++) {
Evaluation eval = new Evaluation(data);
eval.crossValidateModel(cls, data, folds, new Random(i));
System.out.println("#" + i + "\t" + summary(eval));
}
}