Package weka.core

Examples of weka.core.Instances


  /**
   * Returns the instances for this plot
   * @return the instances for this plot
   */
  public Instances getPlotInstances() {
    return new Instances(m_plotInstances);
  }
View Full Code Here


   * @throws IOException throws IOEXception if an instance cannot be saved incrementally.
   */ 
  public void writeIncremental(Instance inst) throws IOException{
 
      int writeMode = getWriteMode();
      Instances structure = getInstances();
      PrintWriter outW = null;
     
      if(getRetrieval() == BATCH || getRetrieval() == NONE)
          throw new IOException("Batch and incremental saving cannot be mixed.");
      if(getWriter() != null)
          outW = new PrintWriter(getWriter());
         
      if(writeMode == WAIT){
        if(structure == null){
            setWriteMode(CANCEL);
            if(inst != null)
                System.err.println("Structure(Header Information) has to be set in advance");
        }
        else
            setWriteMode(STRUCTURE_READY);
        writeMode = getWriteMode();
      }
      if(writeMode == CANCEL){
          if(outW != null)
              outW.close();
          cancel();
      }
      if(writeMode == STRUCTURE_READY){
          setWriteMode(WRITE);
          //write header
          Instances header = new Instances(structure,0);
          if(retrieveFile() == null || outW == null)
              System.out.println(header.toString());
          else{
              outW.print(header.toString());
              outW.print("\n");
              outW.flush();
          }
          writeMode = getWriteMode();
      }
View Full Code Here

          setWriteMode(WAIT);
          return;
      }
     
      PrintWriter outW = new PrintWriter(getWriter());
      Instances data = getInstances();
     
      // header
      Instances header = new Instances(data, 0);
      outW.print(header.toString());
     
      // data
      for (int i = 0; i < data.numInstances(); i++) {
  if (i % 1000 == 0)
    outW.flush();
View Full Code Here

  protected static Evaluation adjustForInputMappedClassifier(Evaluation eval,
      weka.classifiers.Classifier classifier,
      Instances inst, ClassifierErrorsPlotInstances plotInstances) throws Exception {

    if (classifier instanceof weka.classifiers.misc.InputMappedClassifier) {
      Instances mappedClassifierHeader =
        ((weka.classifiers.misc.InputMappedClassifier)classifier).
        getModelHeader(new Instances(inst, 0));

      eval = new Evaluation(new Instances(mappedClassifierHeader, 0));

      if (!eval.getHeader().equalHeaders(inst)) {
        // When the InputMappedClassifier is loading a model,
        // we need to make a new dataset that maps the test instances to
        // the structure expected by the mapped classifier - this is only
        // to ensure that the ClassifierPlotInstances object is configured
        // in accordance with what the embeded classifier was trained with
        Instances mappedClassifierDataset =
          ((weka.classifiers.misc.InputMappedClassifier)classifier).
          getModelHeader(new Instances(mappedClassifierHeader, 0));
        for (int zz = 0; zz < inst.numInstances(); zz++) {
          Instance mapped = ((weka.classifiers.misc.InputMappedClassifier)classifier).
          constructMappedInstance(inst.instance(zz));
          mappedClassifierDataset.add(mapped);
        }
       
        eval.setPriors(mappedClassifierDataset);
        plotInstances.setInstances(mappedClassifierDataset);
        plotInstances.setClassifier(classifier);
        plotInstances.setClassIndex(mappedClassifierDataset.classIndex());
        plotInstances.setEvaluation(eval);
      }
    }
   
    return eval;
View Full Code Here

                 

      if (ce.getTestSet().getDataSet().classAttribute().isNominal() &&
          m_thresholdListeners.size() > 0) {
        ThresholdCurve tc = new ThresholdCurve();
        Instances result = tc.getCurve(m_eval.predictions(), 0);
        result.
          setRelationName(ce.getTestSet().getDataSet().relationName());
        PlotData2D pd = new PlotData2D(result);
        String htmlTitle = "<html><font size=-2>"
          + textTitle;
        String newOptions = "";
        if (classifier instanceof OptionHandler) {
          String[] options =
            ((OptionHandler) classifier).getOptions();
          if (options.length > 0) {
            for (int ii = 0; ii < options.length; ii++) {
              if (options[ii].length() == 0) {
                continue;
              }
              if (options[ii].charAt(0) == '-' &&
                  !(options[ii].charAt(1) >= '0' &&
                      options[ii].charAt(1)<= '9')) {
                newOptions += "<br>";
              }
              newOptions += options[ii];
            }
          }
        }
       
       htmlTitle += " " + newOptions + "<br>"
          + " (class: "
                      +ce.getTestSet().getDataSet().
                        classAttribute().value(0) + ")"
                      + "</font></html>";
        pd.setPlotName(textTitle + " (class: "
                        +ce.getTestSet().getDataSet().
                          classAttribute().value(0) + ")");
        pd.setPlotNameHTML(htmlTitle);
        boolean [] connectPoints =
          new boolean [result.numInstances()];
        for (int jj = 1; jj < connectPoints.length; jj++) {
          connectPoints[jj] = true;
        }
        pd.setConnectPoints(connectPoints);
        ThresholdDataEvent rde =
View Full Code Here

      // clash with any attribute created via the StringToWordVector filter
      atts.add(new Attribute("@@class@@", classes));
     
      String relName = directoryPath.replaceAll("/", "_");
      relName = relName.replaceAll("\\\\", "_").replaceAll(":", "_");
      m_structure = new Instances(relName, atts, 0);   
      m_structure.setClassIndex(m_structure.numAttributes() - 1);
    }
   
    return m_structure;
  }
View Full Code Here

    ArrayList<String> classes = new ArrayList<String>();
    Enumeration enm = getStructure().classAttribute().enumerateValues();
    while (enm.hasMoreElements())
      classes.add((String)enm.nextElement());
   
    Instances data = getStructure();
    int fileCount = 0;
    for (int k = 0; k < classes.size(); k++) {
      String subdirPath = (String) classes.get(k);
      File subdir = new File(directoryPath + File.separator + subdirPath);
      String[] files = subdir.list();
      for (int j = 0; j < files.length; j++) {
  try {
    fileCount++;
    if (getDebug())
      System.err.println(
    "processing " + fileCount + " : " + subdirPath + " : " + files[j]);
   
    double[] newInst = null;
    if (m_OutputFilename)
      newInst = new double[3];
    else
      newInst = new double[2];       
    File txt = new File(directoryPath + File.separator + subdirPath + File.separator + files[j]);
    BufferedReader is;
    if (m_charSet == null || m_charSet.length() == 0) {
      is = new BufferedReader(new InputStreamReader(new FileInputStream(txt)));
    } else {
      is = new BufferedReader(new InputStreamReader(new FileInputStream(txt), m_charSet));
    }
    StringBuffer txtStr = new StringBuffer();
    int c;
    while ((c = is.read()) != -1) {
      txtStr.append((char) c);
    }
   
    newInst[0] = (double) data.attribute(0).addStringValue(txtStr.toString());
    if (m_OutputFilename)
      newInst[1] = (double) data.attribute(1).addStringValue(subdirPath + File.separator + files[j]);
    newInst[data.classIndex()] = (double) k;
    data.add(new DenseInstance(1.0, newInst));
          is.close();
  }
  catch (Exception e) {
    System.err.println("failed to convert file: " + directoryPath + File.separator + subdirPath + File.separator + files[j]);
  }
View Full Code Here

    if (deleteString.toString().length() > 0) {
      Remove af = new Remove();
      af.setAttributeIndices(deleteString.toString());
      af.setInvertSelection(false);
      af.setInputFormat(instances);
      Instances newInst = Filter.useFilter(instances, af);

      return newInst;
    }
    return instances;
  }
View Full Code Here

    double[] confidences, supports;
    int[] indices;
    FastVector[] sortedRuleSet;
    double necSupport=0;

    instances = new Instances(instances);
   
    if (m_removeMissingCols) {
      instances = removeMissingColumns(instances);
    }
    if(m_car && m_metricType != CONFIDENCE)
View Full Code Here

      if (m_log != null) {
        m_log.statusMessage(statusMessagePrefix() + "remove");
      }
     
      //      Instances dataset = m_incrementalEvent.getInstance().dataset();
      Instances dataset = m_incrementalEvent.getStructure();
      // default to the last column if no class is set
      if (dataset.classIndex() < 0) {
        stop();
        String errorMessage = statusMessagePrefix()
            + "ERROR: no class attribute set in incoming stream!";
        if (m_log != null) {
          m_log.statusMessage(errorMessage);
          m_log.logMessage("[" + getCustomName() + "] " + errorMessage);
        } else {
          System.err.println("[" + getCustomName() + "] " + errorMessage);
        }
        return;
       
  // System.err.println("Classifier : setting class index...");
  //dataset.setClassIndex(dataset.numAttributes()-1);
      }
      try {
  // initialize classifier if m_trainingSet is null
  // otherwise assume that classifier has been pre-trained in batch
  // mode, *if* headers match
  if (m_trainingSet == null || !m_trainingSet.equalHeaders(dataset)) {
    if (!(m_ClassifierTemplate instanceof
    weka.classifiers.UpdateableClassifier) &&
    !(m_ClassifierTemplate instanceof
        weka.classifiers.misc.InputMappedClassifier)) {
      stop(); // stop all processing
      if (m_log != null) {
        String msg = (m_trainingSet == null)
    ? statusMessagePrefix()
    + "ERROR: classifier has not been batch "
    +"trained; can't process instance events."
    : statusMessagePrefix()
      + "ERROR: instance event's structure is different from "
      +"the data that "
      + "was used to batch train this classifier; can't continue.";
        m_log.logMessage("[Classifier] " + msg);
        m_log.statusMessage(msg);
      }
      return;
    }
   
    if (m_ClassifierTemplate instanceof
        weka.classifiers.misc.InputMappedClassifier) {
      m_trainingSet = ((weka.classifiers.misc.InputMappedClassifier)m_Classifier).
        getModelHeader(m_trainingSet);
     
/*      // check to see if the classifier that gets loaded is updateable
      weka.classifiers.Classifier tempC =
        ((weka.classifiers.misc.InputMappedClassifier)m_Classifier).getClassifier();
      if (!(tempC instanceof weka.classifiers.UpdateableClassifier)) {
       
      } */
    }
   
    if (m_trainingSet != null &&
        (!dataset.equalHeaders(m_trainingSet))) {
      if (m_log != null) {
        String msg = statusMessagePrefix()
              + " WARNING : structure of instance events differ "
              +"from data used in batch training this "
              +"classifier. Resetting classifier...";
        m_log.logMessage("[Classifier] " + msg);
        m_log.statusMessage(msg);
      }
      m_trainingSet = null;
    }
    if (m_trainingSet == null) {
      // initialize the classifier if it hasn't been trained yet
      m_trainingSet = new Instances(dataset, 0);
      m_Classifier = weka.classifiers.AbstractClassifier.makeCopy(m_ClassifierTemplate);
      if (m_Classifier instanceof EnvironmentHandler && m_env != null) {
        ((EnvironmentHandler)m_Classifier).setEnvironment(m_env);
      }
      m_Classifier.buildClassifier(m_trainingSet);
View Full Code Here

TOP

Related Classes of weka.core.Instances

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.