Example #1
0
  /**
   * Open a saved (and serialized) ClassifierSet.
   *
   * @param path the path of the ClassifierSet to be opened
   * @param sizeControlStrategy the ClassifierSet's
   * @param lcs the lcs which the new set will belong to
   * @return the opened classifier set
   */
  public static ClassifierSet openClassifierSet(
      final String path,
      final IPopulationControlStrategy sizeControlStrategy,
      final AbstractLearningClassifierSystem lcs) {
    FileInputStream fis = null;
    ObjectInputStream in = null;
    ClassifierSet opened = null;

    try {
      fis = new FileInputStream(path);
      in = new ObjectInputStream(fis);

      opened = (ClassifierSet) in.readObject();
      opened.myISizeControlStrategy = sizeControlStrategy;

      for (int i = 0; i < opened.getNumberOfMacroclassifiers(); i++) {
        final Classifier cl = opened.getClassifier(i);
        cl.setLCS(lcs);
      }

      in.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }

    return opened;
  }
 public Measurement[] getModelMeasurements() {
   List<Measurement> measurementList = new LinkedList<Measurement>();
   measurementList.add(new Measurement("model training instances", trainingWeightSeenByModel()));
   measurementList.add(new Measurement("model serialized size (bytes)", measureByteSize()));
   Measurement[] modelMeasurements = getModelMeasurementsImpl();
   if (modelMeasurements != null) {
     for (Measurement measurement : modelMeasurements) {
       measurementList.add(measurement);
     }
   }
   // add average of sub-model measurements
   Classifier[] subModels = getSubClassifiers();
   if ((subModels != null) && (subModels.length > 0)) {
     List<Measurement[]> subMeasurements = new LinkedList<Measurement[]>();
     for (Classifier subModel : subModels) {
       if (subModel != null) {
         subMeasurements.add(subModel.getModelMeasurements());
       }
     }
     Measurement[] avgMeasurements =
         Measurement.averageMeasurements(
             subMeasurements.toArray(new Measurement[subMeasurements.size()][]));
     for (Measurement measurement : avgMeasurements) {
       measurementList.add(measurement);
     }
   }
   return measurementList.toArray(new Measurement[measurementList.size()]);
 }
  @Override
  public Map<LookupElement, StringBuilder> getRelevanceStrings() {
    final LinkedHashMap<LookupElement, StringBuilder> map =
        new LinkedHashMap<LookupElement, StringBuilder>();
    for (LookupElement item : myItems) {
      map.put(item, new StringBuilder());
    }
    final MultiMap<CompletionSorterImpl, LookupElement> inputBySorter =
        groupItemsBySorter(new ArrayList<LookupElement>(map.keySet()));

    if (inputBySorter.size() > 1) {
      for (LookupElement element : map.keySet()) {
        map.get(element).append(obtainSorter(element)).append(": ");
      }
    }

    for (CompletionSorterImpl sorter : inputBySorter.keySet()) {
      final LinkedHashMap<LookupElement, StringBuilder> subMap =
          new LinkedHashMap<LookupElement, StringBuilder>();
      for (LookupElement element : inputBySorter.get(sorter)) {
        subMap.put(element, map.get(element));
      }
      Classifier<LookupElement> classifier = myClassifiers.get(sorter);
      if (classifier != null) {
        classifier.describeItems(subMap, createContext(false));
      }
    }

    return map;
  }
  public static void main(String[] args) {
    final Wine[] wines = new Wine[] {new RedWine(), new Champagne()};

    final Classifier c = new Classifier();
    for (Wine w : wines) {
      c.classify(w);
    }
  }
Example #5
0
 @Override
 public int measureByteSize() {
   int size = (int) SizeOf.sizeOf(this);
   for (Classifier classifier : this.ensemble) {
     size += classifier.measureByteSize();
   }
   return size;
 }
  /**
   * Gets the classifier specification string, which contains the class name of the classifier and
   * any options to the classifier
   *
   * @param index the index of the classifier string to retrieve, starting from 0.
   * @return the classifier string, or the empty string if no classifier has been assigned (or the
   *     index given is out of range).
   */
  protected String getClassifierSpec(int index) {

    if (m_Classifiers.length < index) {
      return "";
    }
    Classifier c = getClassifier(index);
    return c.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) c).getOptions());
  }
Example #7
0
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    final Classifier<String, String> bayes = new BayesClassifier<String, String>();

    System.out.print("Please enter a training file: ");
    String trainingFile = input.nextLine();
    System.out.print("Please enter a testing file: ");
    String testingFile = input.nextLine();
    boolean firstline = true;
    String attribute;
    int intAttribute = 0;
    try (BufferedReader br = new BufferedReader(new FileReader(trainingFile))) {
      for (String line; (line = br.readLine()) != null; ) {
        if (firstline) {
          String[] r = line.split("\\s");
          System.out.println("Please choose an attribute (by number):");
          System.out.println("Attribute:");
          for (int i = 0; i < r.length; i++) {
            System.out.println((i + 1) + ". " + r[i]);
          }
          System.out.println("Attribute: ");
          attribute = input.nextLine();
          intAttribute = Integer.parseInt(attribute) - 1;
          firstline = false;
          continue;
        }

        String[] r = line.split("\\s");
        if (!line.equals("")) bayes.learn(r[intAttribute], Arrays.asList(r));
        else continue;
      }
      // line is not visible here.
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try (BufferedReader br = new BufferedReader(new FileReader(testingFile))) {
      for (String line; (line = br.readLine()) != null; ) {
        String temp = bayes.classify(Arrays.asList(line)).getCategory();
        System.out.println(temp);
      }
      // line is not visible here.
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    System.out.println("Done");
  }
Example #8
0
 @Override
 public Category getCategory(String threadName, StackTraceElement element) {
   for (Classifier classifier : list) {
     Category category = classifier.getCategory(threadName, element);
     if (category != null) {
       return category;
     }
   }
   return new UnknownCategory();
 }
Example #9
0
 @Override
 public void resetLearningImpl() {
   this.ensemble = new Classifier[this.ensembleSizeOption.getValue()];
   Classifier baseLearner = (Classifier) getPreparedClassOption(this.baseLearnerOption);
   baseLearner.resetLearning();
   for (int i = 0; i < this.ensemble.length; i++) {
     this.ensemble[i] = baseLearner.copy();
   }
   instConfCount = 0;
   this.scms = new double[this.ensemble.length];
   this.swms = new double[this.ensemble.length];
 }
 /**
  * Takes the message and adds a header to it.
  *
  * @param mail the mail being processed
  * @throws MessagingException if an error arises during message processing
  */
 public void service(Mail mail) {
   try {
     MimeMessage message = mail.getMessage();
     Classifier classifier = new Classifier(message);
     String classification = classifier.getClassification();
     // if ( !classification.equals("Normal") ) {
     message.setHeader(headerName, classification);
     message.saveChanges();
     // }
   } catch (javax.mail.MessagingException me) {
     log("Error classifying message: " + me.getMessage());
   }
 }
Example #11
0
  @Override
  public void trainC(ClassificationDataSet dataSet, ExecutorService threadPool) {
    final int models = baseClassifiers.size();
    final int C = dataSet.getClassSize();
    weightsPerModel = C == 2 ? 1 : C;
    ClassificationDataSet metaSet =
        new ClassificationDataSet(
            weightsPerModel * models, new CategoricalData[0], dataSet.getPredicting());

    List<ClassificationDataSet> dataFolds = dataSet.cvSet(folds);
    // iterate in the order of the folds so we get the right dataum weights
    for (ClassificationDataSet cds : dataFolds)
      for (int i = 0; i < cds.getSampleSize(); i++)
        metaSet.addDataPoint(
            new DenseVector(weightsPerModel * models),
            cds.getDataPointCategory(i),
            cds.getDataPoint(i).getWeight());

    // create the meta training set
    for (int c = 0; c < baseClassifiers.size(); c++) {
      Classifier cl = baseClassifiers.get(c);
      int pos = 0;
      for (int f = 0; f < dataFolds.size(); f++) {
        ClassificationDataSet train = ClassificationDataSet.comineAllBut(dataFolds, f);
        ClassificationDataSet test = dataFolds.get(f);
        if (threadPool == null) cl.trainC(train);
        else cl.trainC(train, threadPool);
        for (int i = 0;
            i < test.getSampleSize();
            i++) // evaluate and mark each point in the held out fold.
        {
          CategoricalResults pred = cl.classify(test.getDataPoint(i));
          if (C == 2)
            metaSet.getDataPoint(pos).getNumericalValues().set(c, pred.getProb(0) * 2 - 1);
          else {
            Vec toSet = metaSet.getDataPoint(pos).getNumericalValues();
            for (int j = weightsPerModel * c; j < weightsPerModel * (c + 1); j++)
              toSet.set(j, pred.getProb(j - weightsPerModel * c));
          }

          pos++;
        }
      }
    }

    // train the meta model
    if (threadPool == null) aggregatingClassifier.trainC(metaSet);
    else aggregatingClassifier.trainC(metaSet, threadPool);

    // train the final classifiers, unless folds=1. In that case they are already trained
    if (folds != 1) {
      for (Classifier cl : baseClassifiers)
        if (threadPool == null) cl.trainC(dataSet);
        else cl.trainC(dataSet, threadPool);
    }
  }
Example #12
0
 @Override
 public Counter<L> scoresOf(Datum<L, F> example) {
   Counter<L> scores = new ClassicCounter<>();
   for (L label : labelIndex) {
     Map<L, String> posLabelMap = new ArrayMap<>();
     posLabelMap.put(label, POS_LABEL);
     Datum<String, F> binDatum = GeneralDataset.mapDatum(example, posLabelMap, NEG_LABEL);
     Classifier<String, F> binaryClassifier = getBinaryClassifier(label);
     Counter<String> binScores = binaryClassifier.scoresOf(binDatum);
     double score = binScores.getCount(POS_LABEL);
     scores.setCount(label, score);
   }
   return scores;
 }
Example #13
0
  public static void main(String[] args) throws IOException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.out.println("Hello OpenCV " + Core.VERSION);

    getTrainData();

    List<Mat> traindataList = new ArrayList<Mat>();
    List<Integer> trainLabelList = new ArrayList<Integer>();
    readTrainData(outDataPath + "Arbys100Datas.csv", traindataList, trainLabelList);

    Classifier classifier = new Classifier();
    classifier.train(traindataList, trainLabelList);
    classifier.test(traindataList, trainLabelList);
    classifier.save(outDataPath + "svm.xml");
  }
Example #14
0
 /**
  * Returns a classifier's numerosity (the number of microclassifiers).
  *
  * @param aClassifier the classifier
  * @return the given classifier's numerosity
  */
 public final int getClassifierNumerosity(final Classifier aClassifier) {
   for (int i = 0; i < myMacroclassifiers.size(); i++) {
     if (myMacroclassifiers.elementAt(i).myClassifier.getSerial() == aClassifier.getSerial())
       return this.myMacroclassifiers.elementAt(i).numerosity;
   }
   return 0;
 }
  /**
   * Parses a given list of options. Valid options are:
   *
   * <p>-B classifierstring <br>
   * Classifierstring should contain the full class name of a scheme included for selection followed
   * by options to the classifier (required, option should be used once for each classifier).
   *
   * <p>
   *
   * @param options the list of options as an array of strings
   * @exception Exception if an option is not supported
   */
  public void setOptions(String[] options) throws Exception {

    // Iterate through the schemes
    Vector classifiers = new Vector();
    while (true) {
      String classifierString = Utils.getOption('B', options);
      if (classifierString.length() == 0) {
        break;
      }
      String[] classifierSpec = Utils.splitOptions(classifierString);
      if (classifierSpec.length == 0) {
        throw new IllegalArgumentException("Invalid classifier specification string");
      }
      String classifierName = classifierSpec[0];
      classifierSpec[0] = "";
      classifiers.addElement(Classifier.forName(classifierName, classifierSpec));
    }
    if (classifiers.size() == 0) {
      classifiers.addElement(new weka.classifiers.rules.ZeroR());
    }
    Classifier[] classifiersArray = new Classifier[classifiers.size()];
    for (int i = 0; i < classifiersArray.length; i++) {
      classifiersArray[i] = (Classifier) classifiers.elementAt(i);
    }
    setClassifiers(classifiersArray);
  }
  public static void trainAndValidate(
      Map<String, List<FeatureVector>> songs, Map<String, List<FeatureVector>> testSongs)
      throws Exception {
    List<String> genres =
        Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
            .stream()
            .map((n) -> Classify.intToGenre(n))
            .collect(Collectors.toList());
    System.out.println("Binary logistics classifier on every genre:");
    System.out.println("\t\tPositiv\tFalsePos\tFalseNeg\tAccuracy");
    for (String genre : genres) {
      Classifier c = new LogisticsRegressionClassifier(genre);
      c.train(songs);
      int positive = 0;
      int total = 0;
      int falsePositive = 0;
      int falseNegative = 0;

      for (String label : songs.keySet()) {
        for (FeatureVector features : songs.get(label)) {
          String result = c.classify(features);
          if (result != null) {
            if (genre.equals(label)) {
              positive++;
            } else {
              falsePositive++;
            }
          } else {
            if (label.equals(genre)) {
              falseNegative++;
            }
          }
          total++;
        }
      }
      System.out.println(
          Classify.shortenGenre(genre)
              + "\t"
              + positive
              + "\t\t"
              + falsePositive
              + "\t\t\t"
              + falseNegative
              + "\t\t\t"
              + (positive * 1.0) / (positive + falseNegative + falsePositive));
    }
  }
Example #17
0
  /**
   * Calculates the class membership probabilities for the given test instance.
   *
   * @param instance the instance to be classified
   * @return predicted class probability distribution
   * @exception Exception if distribution can't be computed successfully
   */
  public double[] distributionForInstance(Instance instance) throws Exception {
    if (instance.classAttribute().isNumeric()) {
      throw new UnsupportedClassTypeException("Decorate can't handle a numeric class!");
    }
    double[] sums = new double[instance.numClasses()], newProbs;
    Classifier curr;

    for (int i = 0; i < m_Committee.size(); i++) {
      curr = (Classifier) m_Committee.get(i);
      newProbs = curr.distributionForInstance(instance);
      for (int j = 0; j < newProbs.length; j++) sums[j] += newProbs[j];
    }
    if (Utils.eq(Utils.sum(sums), 0)) {
      return sums;
    } else {
      Utils.normalize(sums);
      return sums;
    }
  }
 public static void printStat(Classifier cl1, Classifier cl2, double cnt1, double cnt2, double p) {
   System.out.println(
       cl1.toString()
           + " ------- "
           + cl2.toString()
           + "\n"
           + "Accuracy :"
           + cl1.getAccuracy()
           + " ------- "
           + cl2.getAccuracy()
           + "\n"
           + "# correct: "
           + cnt1
           + " ------- "
           + cnt2
           + "\n"
           + "p <= "
           + p);
 }
Example #19
0
  /**
   * Copy constructor
   *
   * @param toCopy the object to copy
   */
  public Stacking(Stacking toCopy) {
    this.folds = toCopy.folds;
    this.weightsPerModel = toCopy.weightsPerModel;
    if (toCopy.aggregatingClassifier != null) {
      this.aggregatingClassifier = toCopy.aggregatingClassifier.clone();
      this.baseClassifiers = new ArrayList<Classifier>(toCopy.baseClassifiers.size());
      for (Classifier bc : toCopy.baseClassifiers) this.baseClassifiers.add(bc.clone());

      if (toCopy.aggregatingRegressor == toCopy.aggregatingClassifier) // supports both
      {
        aggregatingRegressor = (Regressor) aggregatingClassifier;
        baseRegressors = (List) baseClassifiers; // ugly type easure exploitation...
      }
    } else // we are doing with regressors only
    {
      this.aggregatingRegressor = toCopy.aggregatingRegressor.clone();
      this.baseRegressors = new ArrayList<Regressor>(toCopy.baseRegressors.size());
      for (Regressor br : toCopy.baseRegressors) this.baseRegressors.add(br.clone());
    }
  }
  /**
   * Test a feature collection where each feature will be in it's own bin.
   *
   * <p>Creates a feature collection with five features 1-5. Then uses the quantile function to put
   * these features in 5 bins. Each bin should have a single feature.
   *
   * @throws Exception
   */
  public void testSingleBin() throws Exception {

    // create a feature collection with five features values 1-5
    SimpleFeatureType dataType = DataUtilities.createType("classification.test1", "id:0,value:int");
    int iVal[] = new int[] {1, 2, 3, 4, 5};
    SimpleFeature[] myfeatures = new SimpleFeature[iVal.length];
    for (int i = 0; i < iVal.length; i++) {
      myfeatures[i] =
          SimpleFeatureBuilder.build(
              dataType,
              new Object[] {new Integer(i + 1), new Integer(iVal[i])},
              "classification.test1" + (i + 1));
    }
    MemoryDataStore store = new MemoryDataStore();
    store.createSchema(dataType);
    store.addFeatures(myfeatures);
    FeatureCollection<SimpleFeatureType, SimpleFeature> myFeatureCollection =
        store.getFeatureSource("test1").getFeatures();

    // run the quantile function
    org.opengis.filter.expression.Expression function =
        ff.function("Quantile", ff.property("value"), ff.literal(5));
    Classifier classifier = (Classifier) function.evaluate(myFeatureCollection);

    // verify the results
    assertNotNull(classifier);
    assertEquals(classifier.getClass(), RangedClassifier.class);
    RangedClassifier range = (RangedClassifier) classifier;
    assertEquals(5, range.getSize());

    for (int i = 0; i < 5; i++) {
      assertTrue(i + 1 == ((Number) range.getMin(i)).doubleValue());
      if (i != 4) {
        assertTrue(i + 2 == ((Number) range.getMax(i)).doubleValue());
        assertEquals((i + 1) + ".." + (i + 2), range.getTitle(i));
      } else {
        assertTrue(i + 1 == ((Number) range.getMax(i)).doubleValue());
        assertEquals((i + 1) + ".." + (i + 1), range.getTitle(i));
      }
    }
  }
Example #21
0
  /**
   * Adds a classifier with the a given numerosity to the set. It checks if the classifier already
   * exists and increases its numerosity. It also checks for subsumption and updates the set's
   * numerosity.
   *
   * @param thoroughAdd to thoroughly check addition
   * @param macro the macroclassifier to add to the set
   */
  public final void addClassifier(final Macroclassifier macro, final boolean thoroughAdd) {

    final int numerosity = macro.numerosity;
    // Add numerosity to the Set
    this.totalNumerosity += numerosity;

    // Subsume if possible
    if (thoroughAdd) {
      final Classifier aClassifier = macro.myClassifier;
      for (int i = 0; i < myMacroclassifiers.size(); i++) {
        final Classifier theClassifier = myMacroclassifiers.elementAt(i).myClassifier;
        if (theClassifier.canSubsume()) {
          if (theClassifier.isMoreGeneral(aClassifier)) {
            // Subsume and control size...
            myMacroclassifiers.elementAt(i).numerosity += numerosity;
            if (myISizeControlStrategy != null) {
              myISizeControlStrategy.controlPopulation(this);
            }
            return;
          }
        } else if (theClassifier.equals(aClassifier)) { // Or it can't
          // subsume but
          // it is equal
          myMacroclassifiers.elementAt(i).numerosity += numerosity;
          if (myISizeControlStrategy != null) {
            myISizeControlStrategy.controlPopulation(this);
          }
          return;
        }
      }
    }

    /*
     * No matching or subsumable more general classifier found. Add and
     * control size...
     */
    this.myMacroclassifiers.add(macro);
    if (myISizeControlStrategy != null) {
      myISizeControlStrategy.controlPopulation(this);
    }
  }
Example #22
0
  /**
   * Removes a micro-classifier from the set. It either completely deletes it (if the classsifier's
   * numerosity is 0) or by decreasing the numerosity.
   *
   * @param aClassifier the classifier to delete
   */
  public final void deleteClassifier(final Classifier aClassifier) {

    int index;
    final int macroSize = myMacroclassifiers.size();
    for (index = 0; index < macroSize; index++) {
      if (myMacroclassifiers.elementAt(index).myClassifier.getSerial() == aClassifier.getSerial())
        break;
    }

    if (index == macroSize) return;
    deleteClassifier(index);
  }
  @Override
  public void addElement(
      Lookup lookup, LookupElement element, LookupElementPresentation presentation) {
    StatisticsWeigher.clearBaseStatisticsInfo(element);

    final String invariant =
        presentation.getItemText()
            + "###"
            + getTailTextOrSpace(presentation)
            + "###"
            + presentation.getTypeText();
    element.putUserData(PRESENTATION_INVARIANT, invariant);

    CompletionSorterImpl sorter = obtainSorter(element);
    Classifier<LookupElement> classifier = myClassifiers.get(sorter);
    if (classifier == null) {
      myClassifiers.put(sorter, classifier = sorter.buildClassifier(new AlphaClassifier(lookup)));
    }
    classifier.addElement(element);

    super.addElement(lookup, element, presentation);
  }
  public static double statSign(Classifier cl1, Classifier cl2) {
    System.out.println("PERFORMING STATISTICAL SIGNIFICANCE TESTTING");

    // correct examples
    int[] cExm1 = cl1.getStatSign();
    int[] cExm2 = cl2.getStatSign();

    int N = cExm1.length;
    System.out.println("Number of test examples: " + N);
    if (cExm1.length != cExm2.length) {
      System.err.println("Different Test data to be compared");
      return -1;
    }

    double cnt1 = 0;
    double cnt2 = 0;
    for (int i = 0; i < N; i++) {
      if (cExm1[i] == cExm2[i]) {
        cnt1 += 0.5;
        cnt2 += 0.5;
      } else if (cExm1[i] > cExm2[i]) {
        cnt1++;
      } else cnt2++;
    }
    cnt1 = Math.round(cnt1);
    cnt2 = Math.round(cnt2);

    //		int sum = 0;
    //		for(int i=0;i<=cnt1;i++)
    //		{
    //			sum+=Bernoulli(N, i);
    //		}
    //		return 2*sum;
    Communicator com = new Communicator();
    double p = com.getPforZ(calcZ((int) cnt1, (int) cnt2));
    printStat(cl1, cl2, cnt1, cnt2, p);

    return p;
  }
Example #25
0
  public static void main(String[] args) {
    try {
      Classifier.getInstance().init();
    } catch (Exception e) {
      System.out.println("Failed to init classifier");
      e.printStackTrace();
    }

    Utils.startThreadWithName(new KeyboardInputRunnable(KEYBOARD_PORT), "keyboard-input");
    Utils.startThreadWithName(new GestureInputRunnable(GESTURE_PORT), "gesture-input");
    Utils.startThreadWithName(new GetSettingsRunnable(GET_SETTINGS_PORT), "get-settings");
    Utils.startThreadWithName(new SaveSettingsRunnable(SAVE_SETTINGS_PORT), "save-settings");
  }
  /**
   * Returns an enumeration describing the available options.
   *
   * @return an enumeration of all the available options.
   */
  public Enumeration listOptions() {

    Vector newVector = new Vector(7);

    newVector.addElement(
        new Option(
            "\tThe index of the class attribute.\n" + "\t(default last)",
            "c",
            1,
            "-c <class index>"));
    newVector.addElement(
        new Option(
            "\tThe name of the arff file used for the decomposition.",
            "t",
            1,
            "-t <name of arff file>"));
    newVector.addElement(
        new Option(
            "\tThe number of instances placed in the training pool.\n"
                + "\tThe remainder will be used for testing. (default 100)",
            "T",
            1,
            "-T <training pool size>"));
    newVector.addElement(new Option("\tThe random number seed used.", "s", 1, "-s <seed>"));
    newVector.addElement(
        new Option(
            "\tThe number of training repetitions used.\n" + "\t(default 50)", "x", 1, "-x <num>"));
    newVector.addElement(new Option("\tTurn on debugging output.", "D", 0, "-D"));
    newVector.addElement(
        new Option(
            "\tFull class name of the learner used in the decomposition.\n"
                + "\teg: weka.classifiers.bayes.NaiveBayes",
            "W",
            1,
            "-W <classifier class name>"));

    if ((m_Classifier != null) && (m_Classifier instanceof OptionHandler)) {
      newVector.addElement(
          new Option(
              "",
              "",
              0,
              "\nOptions specific to learner " + m_Classifier.getClass().getName() + ":"));
      Enumeration enu = ((OptionHandler) m_Classifier).listOptions();
      while (enu.hasMoreElements()) {
        newVector.addElement(enu.nextElement());
      }
    }
    return newVector.elements();
  }
  /**
   * Parses a given list of options.
   *
   * <p>
   * <!-- options-start -->
   * Valid options are:
   *
   * <p>
   *
   * <pre> -c &lt;class index&gt;
   *  The index of the class attribute.
   *  (default last)</pre>
   *
   * <pre> -t &lt;name of arff file&gt;
   *  The name of the arff file used for the decomposition.</pre>
   *
   * <pre> -T &lt;training pool size&gt;
   *  The number of instances placed in the training pool.
   *  The remainder will be used for testing. (default 100)</pre>
   *
   * <pre> -s &lt;seed&gt;
   *  The random number seed used.</pre>
   *
   * <pre> -x &lt;num&gt;
   *  The number of training repetitions used.
   *  (default 50)</pre>
   *
   * <pre> -D
   *  Turn on debugging output.</pre>
   *
   * <pre> -W &lt;classifier class name&gt;
   *  Full class name of the learner used in the decomposition.
   *  eg: weka.classifiers.bayes.NaiveBayes</pre>
   *
   * <pre>
   * Options specific to learner weka.classifiers.rules.ZeroR:
   * </pre>
   *
   * <pre> -D
   *  If set, classifier is run in debug mode and
   *  may output additional info to the console</pre>
   *
   * <!-- options-end -->
   * Options after -- are passed to the designated sub-learner.
   *
   * <p>
   *
   * @param options the list of options as an array of strings
   * @throws Exception if an option is not supported
   */
  public void setOptions(String[] options) throws Exception {

    setDebug(Utils.getFlag('D', options));

    String classIndex = Utils.getOption('c', options);
    if (classIndex.length() != 0) {
      if (classIndex.toLowerCase().equals("last")) {
        setClassIndex(0);
      } else if (classIndex.toLowerCase().equals("first")) {
        setClassIndex(1);
      } else {
        setClassIndex(Integer.parseInt(classIndex));
      }
    } else {
      setClassIndex(0);
    }

    String trainIterations = Utils.getOption('x', options);
    if (trainIterations.length() != 0) {
      setTrainIterations(Integer.parseInt(trainIterations));
    } else {
      setTrainIterations(50);
    }

    String trainPoolSize = Utils.getOption('T', options);
    if (trainPoolSize.length() != 0) {
      setTrainPoolSize(Integer.parseInt(trainPoolSize));
    } else {
      setTrainPoolSize(100);
    }

    String seedString = Utils.getOption('s', options);
    if (seedString.length() != 0) {
      setSeed(Integer.parseInt(seedString));
    } else {
      setSeed(1);
    }

    String dataFile = Utils.getOption('t', options);
    if (dataFile.length() == 0) {
      throw new Exception("An arff file must be specified" + " with the -t option.");
    }
    setDataFileName(dataFile);

    String classifierName = Utils.getOption('W', options);
    if (classifierName.length() == 0) {
      throw new Exception("A learner must be specified with the -W option.");
    }
    setClassifier(Classifier.forName(classifierName, Utils.partitionOptions(options)));
  }
Example #28
0
  @Override
  public CategoricalResults classify(DataPoint data) {
    Vec w = new DenseVector(weightsPerModel * baseClassifiers.size());
    if (weightsPerModel == 1)
      for (int i = 0; i < baseClassifiers.size(); i++)
        w.set(i, baseClassifiers.get(i).classify(data).getProb(0) * 2 - 1);
    else {
      for (int i = 0; i < baseClassifiers.size(); i++) {
        CategoricalResults pred = baseClassifiers.get(i).classify(data);
        for (int j = 0; j < weightsPerModel; j++) w.set(i * weightsPerModel + j, pred.getProb(j));
      }
    }

    return aggregatingClassifier.classify(new DataPoint(w));
  }
  public static void main(String[] args) {
    String testPath = "C:\\Users\\king\\Desktop\\垃圾短信数据\\垃圾短信数据\\test.txt";
    String trainPath = "C:\\Users\\king\\Desktop\\垃圾短信数据\\垃圾短信数据\\train.txt";
    // 输出文件的路径
    String outPutPath = "C:\\Users\\king\\Desktop\\";
    // 输出文件的文件名
    String fileName = "第7次";

    Date start = new Date(System.currentTimeMillis());

    Classifier.doClassify(testPath, trainPath, outPutPath, fileName);

    Date end = new Date(System.currentTimeMillis());

    System.out.println(start);
    System.out.println(end);
  }
  /**
   * Replace the class values of the instances from the current iteration with residuals ater
   * predicting with the supplied classifier.
   *
   * @param data the instances to predict
   * @param c the classifier to use
   * @param useShrinkage whether shrinkage is to be applied to the model's output
   * @return a new set of instances with class values replaced by residuals
   * @throws Exception if something goes wrong
   */
  private Instances residualReplace(Instances data, Classifier c, boolean useShrinkage)
      throws Exception {
    double pred, residual;
    Instances newInst = new Instances(data);

    for (int i = 0; i < newInst.numInstances(); i++) {
      pred = c.classifyInstance(newInst.instance(i));
      if (useShrinkage) {
        pred *= getShrinkage();
      }
      // +++ LEF +++
      // should this be squared here???
      residual = newInst.instance(i).classValue() - pred;
      newInst.instance(i).setClassValue(residual);
    }
    // System.err.print(newInst);
    return newInst;
  }