Examples of Instances


Examples of weka.core.Instances

  break;
      default:
  throw new Exception("Unknown attribute type");
      }
    }
    Instances result = new Instances("QueryResult", attribInfo,
             instances.size());
    for (int i = 0; i < instances.size(); i++) {
      result.add((Instance)instances.elementAt(i));
    }
    close(rs);
  
    return result;
  }
View Full Code Here

Examples of weka.core.Instances

   * @param insts the training data.
   * @throws Exception if a classifier can't be built
   */
  public void buildClassifier(Instances insts) throws Exception {

    Instances newInsts;

    // can classifier handle the data?
    getCapabilities().testWithFail(insts);

    // remove instances with missing class
    insts = new Instances(insts);
    insts.deleteWithMissingClass();
   
    if (m_Classifier == null) {
      throw new Exception("No base classifier has been set!");
    }
    m_ZeroR = new ZeroR();
    m_ZeroR.buildClassifier(insts);

    m_TwoClassDataset = null;

    int numClassifiers = insts.numClasses();
    if (numClassifiers <= 2) {

      m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, 1);
      m_Classifiers[0].buildClassifier(insts);

      m_ClassFilters = null;

    } else if (m_Method == METHOD_1_AGAINST_1) {
      // generate fastvector of pairs
      FastVector pairs = new FastVector();
      for (int i=0; i<insts.numClasses(); i++) {
  for (int j=0; j<insts.numClasses(); j++) {
    if (j<=i) continue;
    int[] pair = new int[2];
    pair[0] = i; pair[1] = j;
    pairs.addElement(pair);
  }
      }

      numClassifiers = pairs.size();
      m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, numClassifiers);
      m_ClassFilters = new Filter[numClassifiers];
      m_SumOfWeights = new double[numClassifiers];

      // generate the classifiers
      for (int i=0; i<numClassifiers; i++) {
  RemoveWithValues classFilter = new RemoveWithValues();
  classFilter.setAttributeIndex("" + (insts.classIndex() + 1));
  classFilter.setModifyHeader(true);
  classFilter.setInvertSelection(true);
  classFilter.setNominalIndicesArr((int[])pairs.elementAt(i));
  Instances tempInstances = new Instances(insts, 0);
  tempInstances.setClassIndex(-1);
  classFilter.setInputFormat(tempInstances);
  newInsts = Filter.useFilter(insts, classFilter);
  if (newInsts.numInstances() > 0) {
    newInsts.setClassIndex(insts.classIndex());
    m_Classifiers[i].buildClassifier(newInsts);
    m_ClassFilters[i] = classFilter;
          m_SumOfWeights[i] = newInsts.sumOfWeights();
  } else {
    m_Classifiers[i] = null;
    m_ClassFilters[i] = null;
  }
      }

      // construct a two-class header version of the dataset
      m_TwoClassDataset = new Instances(insts, 0);
      int classIndex = m_TwoClassDataset.classIndex();
      m_TwoClassDataset.setClassIndex(-1);
      m_TwoClassDataset.deleteAttributeAt(classIndex);
      FastVector classLabels = new FastVector();
      classLabels.addElement("class0");
View Full Code Here

Examples of weka.core.Instances

    System.err.println(o.synopsis()+"\n"+o.description());
  }
  System.exit(1);
      }
    
      Instances aha = iq.retrieveInstances();
      iq.disconnectFromDatabase();
      // query returned no result -> exit
      if (aha == null)
        return;
      // The dataset may be large, so to make things easier we'll
      // output an instance at a time (rather than having to convert
      // the entire dataset to one large string)
      System.out.println(new Instances(aha, 0));
      for (int i = 0; i < aha.numInstances(); i++) {
  System.out.println(aha.instance(i));
      }
    } catch(Exception e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
    }
View Full Code Here

Examples of weka.core.Instances

  /**
   * undoes the last action
   */
  public void undo() {
    File                  tempFile;
    Instances             inst;
    ObjectInputStream     ooi;
   
    if (canUndo()) {
      // load file
      tempFile = (File) m_UndoList.get(m_UndoList.size() - 1);
View Full Code Here

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

Examples of weka.core.Instances

   * @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

Examples of weka.core.Instances

          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

Examples of weka.core.Instances

  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

Examples of weka.core.Instances

                 

      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

Examples of weka.core.Instances

      // 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
TOP
Copyright © 2018 www.massapi.com. 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.