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);
/** * Gets the filter specification string, which contains the class name of the * filter and any options to the filter * * @return the filter string. */ protected String getFilterSpec() { Filter c = getFilter(); if (c instanceof OptionHandler) { return c.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) c).getOptions()); } return c.getClass().getName(); }
getCapabilities().testWithFail(data); Random r = (data.numInstances() > 0) ? data.getRandomNumberGenerator(getSeed()) : new Random(getSeed()); data = setUp(data, r); if (!data.allInstanceWeightsIdentical() && !(m_Classifier instanceof WeightedInstancesHandler)) { data = resampleAttributes(data, false, r); getClassifier().getCapabilities().testWithFail(data); throw new Exception("Classifier: " + getClassifierSpec() + " is not an IterativeClassifier");
/** * Main method for testing this class. * * @param argv should contain the following arguments: -t training file [-T * test file] [-c class index] */ public static void main(String[] argv) { runClassifier(new FilteredClassifier(), argv); } }
/** * Builds the classifier to generate a partition. (If the base classifier * supports this.) */ public void generatePartition(Instances data) throws Exception { if (m_Classifier instanceof PartitionGenerator) buildClassifier(data); else throw new Exception( "Classifier: " + getClassifierSpec() + " cannot generate a partition"); }
1. filteredData = new Instances(new BufferedReader(new FileReader("/Users/Passionate/Desktop/train_std.arff"))); 2. Instances filteredTests= new Instances(new BufferedReader(new FileReader("/Users/Passionate/Desktop/test_std.arff"))); 3. filteredData.setClassIndex(filteredData.attribute("@@class@@").index()); 4. Classifier classifier=new SMO(); 5. classifier.buildClassifier(filteredData); 6. FilteredClassifier filteredClassifier=new FilteredClassifier(); 7. filteredClassifier.setClassifier(classifier); 8. Evaluation eval = new Evaluation(filteredData); 9. eval.evaluateModel(filteredClassifier, filteredTests); **// Error line.** 10. System.out.println(eval.toSummaryString("\nResults\n======\n", false));
/** * returns the configured FilteredClassifier. Since the base classifier is * determined heuristically, derived tests might need to adjust it. * * @return the configured FilteredClassifier */ protected FilteredClassifier getFilteredClassifier() { FilteredClassifier result; result = new FilteredClassifier(); result.setFilter(getFilter()); result.setClassifier(new weka.classifiers.rules.ZeroR()); return result; }
FilteredClassifier fc = new FilteredClassifier(); fc.setSeed(random.nextInt()); fc.setClassifier(m_Classifiers[j]); m_Classifiers[j] = fc; Remove rm = new Remove(); rm.setOptions(new String[]{"-V", "-R", randomSubSpace(indices, subSpaceSize, classIndex + 1, random)}); fc.setFilter(rm);
/** * returns the configured FilteredClassifier. Since the base classifier is * determined heuristically, derived tests might need to adjust it. * * @return the configured FilteredClassifier */ protected FilteredClassifier getFilteredClassifier() { FilteredClassifier result; result = super.getFilteredClassifier(); ((NominalToString) result.getFilter()).setAttributeIndexes("1"); result.setClassifier(new ZeroR()); return result; }
filterString = defaultFilterString(); setFilter((Filter) Utils.forName(Filter.class, filterName, filterSpec)); setDoNotCheckForModifiedClassAttribute(Utils.getFlag("doNotCheckForModifiedClassAttribute", options));
/** * returns data generated for the FilteredClassifier test * * @return the dataset for the FilteredClassifier * @throws Exception if generation of data fails */ protected Instances getFilteredClassifierData() throws Exception{ TestInstances test; Instances result; test = TestInstances.forCapabilities(m_FilteredClassifier.getCapabilities()); test.setClassIndex(TestInstances.CLASS_IS_LAST); result = test.generate(); return result; }
/** * Set the base learner. * * @param value the classifier to use. */ @Override public void setClassifier(Classifier value) { if (!(value instanceof UpdateableClassifier)) { throw new IllegalArgumentException("Classifier must be derived from " + UpdateableClassifier.class.getName() + "!"); } else { super.setClassifier(value); } }
/** Creates a default FilteredClassifier */ public Classifier getClassifier() { return new FilteredClassifier(); }
/** * Gets the preferred batch size from the base learner if it implements * BatchPredictor. Returns 1 as the preferred batch size otherwise. * * @return the batch size to use */ public String getBatchSize() { if (getClassifier() instanceof BatchPredictor) { return ((BatchPredictor) getClassifier()).getBatchSize(); } else { return super.getBatchSize(); } }
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception { boolean[] bipartition = new boolean[numLabels]; double[] confidences = new double[numLabels]; Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray()); for (int counter = 0; counter < numLabels; counter++) { double distribution[]; try { distribution = ensemble[counter].distributionForInstance(tempInstance); } catch (Exception e) { System.out.println(e); return null; } int maxIndex = (distribution[0] > distribution[1]) ? 0 : 1; // Ensure correct predictions both for class values {0,1} and {1,0} Attribute classAttribute = ensemble[counter].getFilter().getOutputFormat().classAttribute(); bipartition[chain[counter]] = (classAttribute.value(maxIndex).equals("1")) ? true : false; // The confidence of the label being equal to 1 confidences[chain[counter]] = distribution[classAttribute.indexOfValue("1")]; tempInstance.setValue(labelIndices[chain[counter]], maxIndex); } MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences); return mlo; } }
/** * tests the filter in conjunction with the FilteredClassifier */ public void testFilteredClassifier() { Instances data; int i; // skip this test if a subclass has set the // filtered classifier to null if (m_FilteredClassifier == null) { return; } try { // generate data data = getFilteredClassifierData(); // build classifier m_FilteredClassifier.buildClassifier(data); // test classifier for (i = 0; i < data.numInstances(); i++) { m_FilteredClassifier.classifyInstance(data.instance(i)); } } catch (Exception e) { fail("Problem with FilteredClassifier: " + e.toString()); } }
throws Exception { if (getClassifier() instanceof BatchPredictor) { if (m_ReorderOriginal != null) { insts = Filter.useFilter(insts, m_ReorderOriginal); filteredInsts = Filter.useFilter(filteredInsts, m_ReorderFiltered); return ((BatchPredictor) getClassifier()).distributionsForInstances(filteredInsts); } else { double[][] result = new double[insts.numInstances()][insts.numClasses()]; for (int i = 0; i < insts.numInstances(); i++) { result[i] = distributionForInstance(insts.instance(i));
/** * Build the classifier on the filtered data. * * @param data the training data * @throws Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { if (!(m_Classifier instanceof Randomizable) && !(m_Filter instanceof Randomizable)) { throw new Exception("Either the classifier or the filter must implement the Randomizable interface."); } super.buildClassifier(data); }
protected FilteredClassifier getFilteredClassifier() { FilteredClassifier result = super.getFilteredClassifier(); result.setDoNotCheckForModifiedClassAttribute(true); return result; }
distribution = metaLevelFilteredEnsemble[labelIndex].distributionForInstance(newmetaInstance); } catch (Exception e) { System.out.println(e);