Examples of Instances


Examples of weka.core.Instances

    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

Examples of weka.core.Instances

    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

Examples of weka.core.Instances

    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

Examples of weka.core.Instances

      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

Examples of weka.core.Instances

   
    // Do some initialization if this is the first set of the first run
    if (e.getRunNumber() == 1 && e.getSetNumber() == 1) {
//      m_oldText = m_visual.getText();
      // store the training header
      m_trainingSet = new Instances(e.getTrainingSet(), 0);
      m_state = BUILDING_MODEL;
     
      String msg = "[Classifier] " + statusMessagePrefix()
        + " starting executor pool ("
        + getExecutionSlots() + " slots)...";
View Full Code Here

Examples of weka.core.Instances

    }
   
   
    weka.classifiers.Classifier classifierToUse = m_Classifier;
   
    Instances testSet = e.getTestSet();
    if (testSet != null) {
      if (testSet.classIndex() < 0) {
  //        testSet.setClassIndex(testSet.numAttributes() - 1);
        // stop all processing
        stop();
        String errorMessage = statusMessagePrefix()
            + "ERROR: no class attribute set in test data!";
        if (m_log != null) {
          m_log.statusMessage(errorMessage);
          m_log.logMessage("[Classifier] " + errorMessage);
        } else {
          System.err.println("[Classifier] " + errorMessage);
        }
        return;
      }
    }

    // If we just have a test set connection or
    // there is just one run involving one set (and we are not
    // currently building a model), then use the
    // last saved model
    if (classifierToUse != null && m_state == IDLE &&
        (!m_listenees.containsKey("trainingSet") ||
        (e.getMaxRunNumber() == 1 && e.getMaxSetNumber() == 1))) {
      // if this is structure only then just return at this point
      if (e.getTestSet() != null && e.isStructureOnly()) {
        return;
      }
     
      if (classifierToUse instanceof EnvironmentHandler && m_env != null) {
        ((EnvironmentHandler)classifierToUse).setEnvironment(m_env);
      }
     
      if (classifierToUse instanceof weka.classifiers.misc.InputMappedClassifier) {
        // make sure that we have the correct training header (if InputMappedClassifier
        // is loading a model from a file).
        try {
          m_trainingSet =
            ((weka.classifiers.misc.InputMappedClassifier)classifierToUse).
              getModelHeader(m_trainingSet); // this returns the argument if a model is not being loaded
        } catch (Exception e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }
     
      // check that we have a training set/header (if we don't,
      // then it means that no model has been loaded
      if (m_trainingSet == null) {
        stop();
        String errorMessage = statusMessagePrefix()
            + "ERROR: no trained/loaded classifier to use for prediction!";
        if (m_log != null) {
          m_log.statusMessage(errorMessage);
          m_log.logMessage("[Classifier] " + errorMessage);
        } else {
          System.err.println("[Classifier] " + errorMessage);
        }
        return;
      }
     
      testSet = e.getTestSet();
      if (e.getRunNumber() == 1 && e.getSetNumber() == 1) {
        m_currentBatchIdentifier = new Date();
      }
     
      if (testSet != null) {       
        if (!m_trainingSet.equalHeaders(testSet) &&
            !(classifierToUse instanceof weka.classifiers.misc.InputMappedClassifier)) {
          boolean wrapClassifier = false;
          if (!Utils.
              getDontShowDialog("weka.gui.beans.Classifier.AutoWrapInInputMappedClassifier")) {
           
            java.awt.GraphicsEnvironment ge =
              java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
            if (!ge.isHeadless()) {
              JCheckBox dontShow = new JCheckBox("Do not show this message again");
              Object[] stuff = new Object[2];
              stuff[0] = "Data used to train model and test set are not compatible.\n" +
              "Would you like to automatically wrap the classifier in\n" +
              "an \"InputMappedClassifier\" before proceeding?.\n";
              stuff[1] = dontShow;

              int result = JOptionPane.showConfirmDialog(this, stuff,
                  "KnowledgeFlow:Classifier", JOptionPane.YES_OPTION);

              if (result == JOptionPane.YES_OPTION) {
                wrapClassifier = true;
              }

              if (dontShow.isSelected()) {
                String response = (wrapClassifier) ? "yes" : "no";
                try {
                  Utils.
                  setDontShowDialogResponse("weka.gui.explorer.ClassifierPanel.AutoWrapInInputMappedClassifier",
                      response);
                } catch (Exception e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
                }
              }
            } else {
              // running headless, so just go ahead and wrap anyway
              wrapClassifier = true;
            }
          } else {
            // What did the user say - do they want to autowrap or not?
            String response;
            try {
              response = Utils.getDontShowDialogResponse("weka.gui.explorer.ClassifierPanel.AutoWrapInInputMappedClassifier");
              if (response != null && response.equalsIgnoreCase("yes")) {
                wrapClassifier = true;
              }
            } catch (Exception e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }
          }
         
          if (wrapClassifier) {
            weka.classifiers.misc.InputMappedClassifier temp =
              new weka.classifiers.misc.InputMappedClassifier();

            temp.setClassifier(classifierToUse);
            temp.setModelHeader(new Instances(m_trainingSet, 0));
            classifierToUse = temp;
          }         
        }        
       
        if (m_trainingSet.equalHeaders(testSet) ||
            (classifierToUse instanceof weka.classifiers.misc.InputMappedClassifier)) {
          BatchClassifierEvent ce =
            new BatchClassifierEvent(this, classifierToUse,                                      
                new DataSetEvent(this, m_trainingSet),
                new DataSetEvent(this, e.getTestSet()),
           e.getRunNumber(), e.getMaxRunNumber(),
           e.getSetNumber(), e.getMaxSetNumber());
          ce.setGroupIdentifier(m_currentBatchIdentifier.getTime());
         
          if (m_log != null && !e.isStructureOnly()) {
            m_log.statusMessage(statusMessagePrefix() + "Finished.");
          }
          notifyBatchClassifierListeners(ce);         
        } else {
          // if headers do not match check to see if it's
          // just the class that is different and that
          // all class values are missing
          if (testSet.numInstances() > 0) {
            if (testSet.classIndex() == m_trainingSet.classIndex() &&
                testSet.attributeStats(testSet.classIndex()).missingCount ==
                testSet.numInstances()) {
              // now check the other attributes against the training
              // structure
              boolean ok = true;
              for (int i = 0; i < testSet.numAttributes(); i++) {
                if (i != testSet.classIndex()) {
                  ok = testSet.attribute(i).equals(m_trainingSet.attribute(i));
                  if (!ok) {
                    break;
                  }
                }
              }
View Full Code Here

Examples of weka.core.Instances

                                + FILE_EXTENSION);
          }
        }

        weka.classifiers.Classifier temp = null;
        Instances tempHeader = null;
        // KOML ?
        if ((KOML.isPresent()) &&
            (loadFrom.getAbsolutePath().toLowerCase().
             endsWith(KOML.FILE_EXTENSION + FILE_EXTENSION))) {
          Vector v = (Vector) KOML.read(loadFrom.getAbsolutePath());
View Full Code Here

Examples of weka.core.Instances

            saveTo.getAbsolutePath().toLowerCase().
            endsWith(KOML.FILE_EXTENSION + FILE_EXTENSION)) {
          SerializedModelSaver.saveKOML(saveTo,
                                        m_Classifier,
                                        (m_trainingSet != null)
                                        ? new Instances(m_trainingSet, 0)
                                        : null);
          /*          Vector v = new Vector();
          v.add(m_Classifier);
          if (m_trainingSet != null) {
            v.add(new Instances(m_trainingSet, 0));
          }
          v.trimToSize();
          KOML.write(saveTo.getAbsolutePath(), v); */
        } /* XStream */ else if ((XStream.isPresent()) &&
                                 saveTo.getAbsolutePath().toLowerCase().
            endsWith(XStream.FILE_EXTENSION + FILE_EXTENSION)) {

          SerializedModelSaver.saveXStream(saveTo,
                                           m_Classifier,
                                           (m_trainingSet != null)
                                           ? new Instances(m_trainingSet, 0)
                                           : null);
          /*          Vector v = new Vector();
          v.add(m_Classifier);
          if (m_trainingSet != null) {
            v.add(new Instances(m_trainingSet, 0));
          }
          v.trimToSize();
          XStream.write(saveTo.getAbsolutePath(), v); */
        } else /* binary */ {
          ObjectOutputStream os =
            new ObjectOutputStream(new BufferedOutputStream(
                                   new FileOutputStream(saveTo)));
          os.writeObject(m_Classifier);
          if (m_trainingSet != null) {
            Instances header = new Instances(m_trainingSet, 0);
            os.writeObject(header);
          }
          os.close();
        }
        if (m_log != null) {
View Full Code Here

Examples of weka.core.Instances

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

    // remove instances with missing class
    data = new Instances(data);
    data.deleteWithMissingClass();
   
    m_bagger = new Bagging();
    RandomTree rTree = new RandomTree();
View Full Code Here

Examples of weka.core.Instances

   * @param json  the JSON object to convert
   * @param onlyHeader  whether to retrieve only the header
   * @return    the generated Instances object, null if not possible
   */
  protected static Instances toInstances(JSONNode json, boolean onlyHeader) {
    Instances  result;
    JSONNode  header;
    JSONNode  attributes;
    JSONNode  data;
    ArrayList<Attribute>  atts;
    Attribute  att;
    Instance  inst;
    int    i;
    int    classIndex;
    boolean[]  classAtt;
   
    header = json.getChild(HEADER);
    if (header == null) {
      System.err.println("No '" + HEADER + "' section!");
      return null;
    }
    data = json.getChild(DATA);
    if (data == null) {
      System.err.println("No '" + DATA + "' section!");
      return null;
    }
   
    // attributes
    attributes = header.getChild(ATTRIBUTES);
    if (attributes == null) {
      System.err.println("No '" + ATTRIBUTES + "' array!");
      return null;
    }
    atts       = new ArrayList<Attribute>();
    classAtt   = new boolean[1];
    classIndex = -1;
    for (i = 0; i < attributes.getChildCount(); i++) {
      att = toAttribute((JSONNode) attributes.getChildAt(i), classAtt);
      if (att == null) {
  System.err.println("Could not convert attribute #" + (i+1) + "!");
  return null;
      }
      if (classAtt[0])
  classIndex = i;
      atts.add(att);
    }
    result = new Instances(
  header.getChild(RELATION).getValue("unknown").toString(),
  atts,
  (onlyHeader ? 0 : data.getChildCount()));
    result.setClassIndex(classIndex);
   
    // data
    if (!onlyHeader) {
      for (i = 0; i < data.getChildCount(); i++) {
  inst = toInstance((JSONNode) data.getChildAt(i), result);
  if (inst == null) {
    System.err.println("Could not convert instance #" + (i+1) + "!");
    return null;
  }
  result.add(inst);
      }
    }
   
    return result;
  }
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.