List of usage examples for weka.classifiers.bayes NaiveBayes NaiveBayes
NaiveBayes
From source file:PEBL.TwoStep.java
public static void main(String[] args) throws Exception { ConverterUtils.DataSource source = new ConverterUtils.DataSource( "Z:\\\\shared from vm\\\\fourthset\\\\mixed.csv"); Instances data = source.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); }//from w w w .j a v a2 s . c o m NumericToNominal nmf = new NumericToNominal(); nmf.setInputFormat(data); data = Filter.useFilter(data, nmf); // build a c4.5 classifier String[] options = new String[1]; // options[0] = "-C 0.25 -M 2"; // unpruned tree options[0] = "-K"; NaiveBayes c = new NaiveBayes(); // new instance of tree c.setOptions(options); // set the options c.buildClassifier(data); // build classifier // eval Evaluation eval = new Evaluation(data); eval.crossValidateModel(c, data, 10, new Random(1)); System.out.println(eval.toSummaryString()); System.out.println(eval.toMatrixString()); System.out.println(eval.toClassDetailsString()); System.out.println("--- model learned on mixed set ---"); // load unlabeled data ConverterUtils.DataSource s = new ConverterUtils.DataSource( "Z:\\\\shared from vm\\\\fourthset\\\\unlabelled.csv"); Instances unlabeled = s.getDataSet(); // set class attribute unlabeled.setClassIndex(unlabeled.numAttributes() - 1); nmf = new NumericToNominal(); nmf.setInputFormat(unlabeled); unlabeled = Filter.useFilter(unlabeled, nmf); // label instances for (int i = 0; i < unlabeled.numInstances(); i++) { double classZero = c.distributionForInstance(unlabeled.instance(i))[0]; double classOne = c.distributionForInstance(unlabeled.instance(i))[1]; System.out.print( "classifying: " + unlabeled.instance(i) + " : " + classZero + " - " + classOne + " == class: "); if (classZero > classOne) { System.out.print("0"); unlabeled.instance(i).setClassValue("0"); } else { System.out.print("1"); unlabeled.instance(i).setClassValue("1"); } System.out.println(""); } // save labeled data // BufferedWriter writer = new BufferedWriter( // new FileWriter("Z:\\\\shared from vm\\\\thirdset\\\\relabelled.arff")); // writer.write(labeled.toString()); // writer.newLine(); // writer.flush(); // writer.close(); ArffSaver saver = new ArffSaver(); saver.setInstances(unlabeled); saver.setFile(new File("Z:\\shared from vm\\thirdset\\relabelled.arff")); // saver.setDestination(new File("Z:\\shared from vm\\thirdset\\relabelled.arff")); // **not** necessary in 3.5.4 and later saver.writeBatch(); }
From source file:qa.experiment.ProcessFeatureVector.java
public String trainAndPredict(String[] processNames, String question) throws Exception { FastVector fvWekaAttribute = generateWEKAFeatureVector(processNames); Instances trainingSet = new Instances("Rel", fvWekaAttribute, bowFeature.size() + 1); trainingSet.setClassIndex(bowFeature.size()); int cnt = 0;/* ww w . j a v a 2 s . c o m*/ for (int i = 0; i < arrProcessFeature.size(); i++) { String[] names = arrProcessFeature.get(i).getProcessName().split("\\|"); int sim = isNameFuzzyMatch(processNames, names); if (sim != -1) { // System.out.println("match " + arrProcessFeature.get(i).getProcessName()); ArrayList<String> featureVector = arrProcessFeature.get(i).getFeatureVectors(); for (int j = 0; j < featureVector.size(); j++) { Instance trainInstance = new Instance(bowFeature.size() + 1); String[] attrValues = featureVector.get(j).split("\t"); // System.out.println(trainInstance.numAttributes()); // System.out.println(fvWekaAttribute.size()); for (int k = 0; k < bowFeature.size(); k++) { trainInstance.setValue((Attribute) fvWekaAttribute.elementAt(k), Integer.parseInt(attrValues[k])); } trainInstance.setValue((Attribute) fvWekaAttribute.elementAt(bowFeature.size()), processNames[sim]); trainingSet.add(trainInstance); //System.out.println(cnt); cnt++; } } } Classifier cl = new NaiveBayes(); cl.buildClassifier(trainingSet); Instance inst = new Instance(bowFeature.size() + 1); //String[] tokenArr = tokens.toArray(new String[tokens.size()]); for (int j = 0; j < bowFeature.size(); j++) { List<String> tokens = slem.tokenize(question); String[] tokArr = tokens.toArray(new String[tokens.size()]); int freq = getFrequency(bowFeature.get(j), tokArr); inst.setValue((Attribute) fvWekaAttribute.elementAt(j), freq); } inst.setDataset(trainingSet); int idxMax = ArrUtil.getIdxMax(cl.distributionForInstance(inst)); return processNames[idxMax]; }
From source file:sentinets.TrainModel.java
License:Open Source License
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"); }
From source file:statistics.BinaryStatisticsEvaluator.java
@Override public double[][] getConfusionMatrix(Instances Training_Instances, Instances Testing_Instances, String classifier) {//from www . j a va 2 s .c o m Classifier cModel = null; if ("NB".equals(classifier)) { cModel = (Classifier) new NaiveBayes(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } else if ("DT".equals(classifier)) { cModel = (Classifier) new J48(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } else if ("SVM".equals(classifier)) { cModel = (Classifier) new SMO(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } else if ("KNN".equals(classifier)) { cModel = (Classifier) new IBk(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } //Test the model Evaluation eTest; try { eTest = new Evaluation(Training_Instances); eTest.evaluateModel(cModel, Testing_Instances); //Print the result String strSummary = eTest.toSummaryString(); System.out.println(strSummary); String strSummary1 = eTest.toMatrixString(); System.out.println(strSummary1); String strSummary2 = eTest.toClassDetailsString(); System.out.println(strSummary2); //Get the confusion matrix double[][] cmMatrix = eTest.confusionMatrix(); return cmMatrix; } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:Temp.StoreClassifier.java
License:Open Source License
public void trainClassifier() throws Exception { classifier = new NaiveBayes(); classifier.buildClassifier(instances); }
From source file:textmining.TextMining.java
/** * Naive Bayes/* ww w . j a va 2s . c o m*/ * * @param instances * @return string * @throws Exception */ private static String C_NaiveBayes(Instances instances) throws Exception { Classifier naiveBayes = (Classifier) new NaiveBayes(); String[] options = weka.core.Utils.splitOptions(""); return setOptions(naiveBayes, instances, options); }
From source file:Tubes.Classification.java
public static void main(String[] args) throws FileNotFoundException, IOException, Exception { StringToWordVector filter = new StringToWordVector(); File training = new File(classTrain); File testing = new File(classTest); BufferedReader readTrain = new BufferedReader(new FileReader(training)); BufferedReader readTest = new BufferedReader(new FileReader(testing)); Instances dataTrain = new Instances(readTrain); Instances dataTest = new Instances(readTest); filter.setInputFormat(dataTrain);//from w w w.j a va2 s . c om dataTrain = Filter.useFilter(dataTrain, filter); dataTrain.setClassIndex(dataTrain.numAttributes() - 1); dataTest.setClassIndex(dataTest.numAttributes() - 1); Classification classify = new Classification(); NaiveBayes bayes = new NaiveBayes(); // RandomForest rf = new RandomForest(); // BayesNet bayesNet = new BayesNet(); LibSVM libSVM = new LibSVM(); System.out.println("==========================Naive Bayes Evaluation==========================="); Evaluation eval = classify.runClassifier(bayes, dataTrain, dataTest); System.out.println(eval.toSummaryString() + "\n"); System.out.println(eval.toClassDetailsString() + "\n"); System.out.println(eval.toMatrixString() + "\n"); System.out.println("==========================================================================="); // // ====System.out.println("==============================Random Forest================================"); // Evaluation eval2 = classify.runClassifier(rf, dataTrain, dataTest); // System.out.println(eval2.toSummaryString() + "\n"); // System.out.println(eval2.toClassDetailsString() + "\n"); // System.out.println(eval2.toMatrixString() + "\n"); // System.out.println("======================================================================="); // // System.out.println("==============================Bayesian Network================================"); // Evaluation eval3 = classify.runClassifier(bayesNet, dataTrain, dataTest); // System.out.println(eval3.toSummaryString() + "\n"); // System.out.println(eval3.toClassDetailsString() + "\n"); // System.out.println(eval3.toMatrixString() + "\n"); // System.out.println("==========================================================================="); System.out.println("==============================LibSVM================================"); libSVM.setCacheSize(512); // MB libSVM.setNormalize(true); libSVM.setShrinking(true); libSVM.setKernelType(new SelectedTag(LibSVM.KERNELTYPE_LINEAR, LibSVM.TAGS_KERNELTYPE)); libSVM.setDegree(3); libSVM.setSVMType(new SelectedTag(LibSVM.SVMTYPE_C_SVC, LibSVM.TAGS_SVMTYPE)); Evaluation eval4 = classify.runClassifier(libSVM, dataTrain, dataTest); System.out.println(eval4.toSummaryString() + "\n"); System.out.println(eval4.toClassDetailsString() + "\n"); System.out.println(eval4.toMatrixString() + "\n"); System.out.println("==========================================================================="); }
From source file:view.centerPanels.ClassificationPnlCenterTrainNew.java
public ClassificationPnlCenterTrainNew() { initComponents();// w ww . j a v a2 s . co m Instances data = Data.getInstance().getInstances(); try { nb = new NaiveBayes(); data.setClassIndex(data.numAttributes() - 1); Evaluation eval = new Evaluation(data); nb.buildClassifier(data); eval.evaluateModel(nb, data); jTextArea1.setText(eval.toMatrixString()); // System.out.println(eval.toMatrixString()); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex); } setSize(MainGUI.getInstance().getPnlCenter().getWidth(), MainGUI.getInstance().getPnlCenter().getHeight()); setVisible(true); }
From source file:wedt.project.BayesClassifier.java
BayesClassifier() {
cls = new NaiveBayes();
}
From source file:wekimini.learning.NaiveBayesModelBuilder.java
public NaiveBayesModelBuilder() { classifier = new NaiveBayes(); }