Package weka.core

Examples of weka.core.Attribute

Typical usage (code from the main() method of this class):

...
// Create numeric attributes "length" and "weight"
Attribute length = new Attribute("length");
Attribute weight = new Attribute("weight");

// Create list to hold nominal values "first", "second", "third"
List my_nominal_values = new ArrayList(3);
my_nominal_values.add("first");
my_nominal_values.add("second");
my_nominal_values.add("third");

// Create nominal attribute "position"
Attribute position = new Attribute("position", my_nominal_values);
...

@author Eibe Frank (eibe@cs.waikato.ac.nz) @version $Revision: 6889 $


    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    final ArrayList<String> pClasses = new ArrayList<String>();
    for (String clName : PLUGIN_PERSPECTIVES.keySet()) {
      pClasses.add(clName);
      String pName = PLUGIN_PERSPECTIVES.get(clName);
      atts.add(new Attribute(pName));
    }
    Instances perspectiveInstances =
      new Instances("Perspectives", atts, 1);

    boolean[] selectedPerspectives =
View Full Code Here


   * @param att    the JSON object to turn into an Attribute
   * @param classAtt  for storing whether the attribute is the class attribute
   * @return    the Attribute, null in case of an error
   */
  protected static Attribute toAttribute(JSONNode att, boolean[] classAtt) {
    Attribute  result;
    String  name;
    String  type;
    String  dateformat;
    JSONNode  labels;
    ArrayList<String>  values;
    int    i;
    double  weight;
   
    name   = (String) att.getChild(NAME).getValue("noname");
    type   = (String) att.getChild(TYPE).getValue("");
    weight = (Double) att.getChild(WEIGHT).getValue(new Double(1.0));
    if (type.equals(Attribute.typeToString(Attribute.NUMERIC))) {
      result = new Attribute(name);
    }
    else if (type.equals(Attribute.typeToString(Attribute.NOMINAL))) {
      labels = att.getChild(LABELS);
      values = new ArrayList<String>();
      for (i = 0; i < labels.getChildCount(); i++)
  values.add((String)((JSONNode) labels.getChildAt(i)).getValue());
      result = new Attribute(name, values);
    }
    else if (type.equals(Attribute.typeToString(Attribute.DATE))) {
      dateformat = (String) att.getChild(TYPE).getValue("yyyy-MM-dd'T'HH:mm:ss");
      result     = new Attribute(name, dateformat);
    }
    else if (type.equals(Attribute.typeToString(Attribute.STRING))) {
      result = new Attribute(name, (ArrayList<String>) null);
    }
    else {
      System.err.println("Unhandled attribute type '" + type + "'!");
      return null;
    }
    result.setWeight(weight);
   
    return result;
  }
View Full Code Here

    Instances  result;
    JSONNode  header;
    JSONNode  attributes;
    JSONNode  data;
    ArrayList<Attribute>  atts;
    Attribute  att;
    Instance  inst;
    int    i;
    int    classIndex;
    boolean[]  classAtt;
   
View Full Code Here

      sortedPerspectives.add(clName);
    }
    for (String clName : sortedPerspectives) {
      pClasses.add(clName);
      String pName = m_pluginPerspectiveLookup.get(clName);
      atts.add(new Attribute(pName));
    }
    Instances perspectiveInstances =
      new Instances("Perspectives", atts, 1);

    boolean[] selectedPerspectives =
View Full Code Here

      List<String> targets, String timeStampName, boolean dateOnly,
      Periodicity userHint, String skipEntries, Object... missingReport) {

    Instances result = toReplace;

    Attribute timeStampAtt = null;
    TSLagMaker.PeriodicityHandler detected = null;

    List<Integer> missingTargetList = null;
    List<Integer> missingTimeStampList = null;
    List<String> missingTimeStampRows = null;
    if (missingReport.length > 0) {
      missingTargetList = (List<Integer>) missingReport[0];

      if (missingReport.length == 2) {
        missingTimeStampList = (List<Integer>) missingReport[1];
      }

      if (missingReport.length == 3) {
        missingTimeStampRows = (List<String>) missingReport[2];
      }
    }

    if (timeStampName != null && timeStampName.length() > 0) {
      timeStampAtt = toReplace.attribute(timeStampName);

      // must be a non-artificial time stamp
      if (timeStampAtt != null) {
        detected = weka.classifiers.timeseries.core.TSLagMaker
            .determinePeriodicity(result, timeStampName, userHint);

        // check insertMissing (if periodicity is not UNKNOWN)
        /*
         * If we do this first, then we can interpolate the missing target
         * values that will be created for the rows that get inserted
         */
        if (detected.getPeriodicity() != Periodicity.UNKNOWN) {
          insertMissing(toReplace, timeStampAtt, detected, skipEntries,
              missingTimeStampRows);
        }
      }
    }

    // do a quick check to see if we need to replace any missing values
    boolean ok = true;
    for (int i = 0; i < toReplace.numInstances(); i++) {
      if (toReplace.instance(i).hasMissingValue()) {
        // now check against targets and possibly date
        if (!dateOnly) {
          for (String target : targets) {
            int attIndex = toReplace.attribute(target).index();
            if (toReplace.instance(i).isMissing(attIndex)) {
              ok = false;
              break;
            }
          }
          if (!ok) {
            break; // outer loop
          }
        }

        // check date if necessary
        if (timeStampAtt != null) {
          if (toReplace.instance(i).isMissing(timeStampAtt)) {
            ok = false;
            break;
          }
        }
      }
    }

    if (ok) {
      // nothing to do
      return result;
    }

    // process the target(s) first
    if (!dateOnly) {
      for (String target : targets) {
        if (result.attribute(target) != null) {
          int attIndex = result.attribute(target).index();
          double lastNonMissing = weka.core.Utils.missingValue();

          // We won't handle missing target values at the start or end
          // as experiments with using simple linear regression to fill
          // the missing values that are created by default by the lagging
          // process showed inferior performance compared to just letting
          // Weka take care of it via mean/mode replacement

          for (int i = 0; i < result.numInstances(); i++) {
            Instance current = result.instance(i);
            if (current.isMissing(attIndex)) {
              if (!weka.core.Utils.isMissingValue(lastNonMissing)) {
                // Search forward to the next non missing value (if any)
                double futureNonMissing = weka.core.Utils.missingValue();

                double x2 = 2; // number of x steps (lastNonMissing is at 0 on x
                               // axis)
                for (int j = i + 1; j < result.numInstances(); j++) {
                  if (!result.instance(j).isMissing(attIndex)) {
                    futureNonMissing = result.instance(j).value(attIndex);
                    break;
                  }
                  x2++;
                }

                if (!weka.core.Utils.isMissingValue(futureNonMissing)) {
                  // Now do the linear interpolation
                  double offset = lastNonMissing;
                  double slope = (futureNonMissing - lastNonMissing) / x2;

                  // fill in the missing values
                  for (int j = i; j < i + x2; j++) {
                    if (result.instance(j).isMissing(attIndex)) {
                      double interpolated = (((j - i) + 1) * slope) + offset;

                      result.instance(j).setValue(attIndex, interpolated);
                      if (missingTargetList != null) {
                        missingTargetList.add(new Integer(j + 1));
                      }
                    }
                  }
                }
              } else {
                // won't do anything with start/end missing values
              }
            } else {
              lastNonMissing = current.value(attIndex);
            }
          }
        }
      }
    }

    // now check for missing date values (if necessary)
    if (timeStampAtt != null) {

      int attIndex = timeStampAtt.index(); // result.attribute(timeStampName).index();

      double firstNonMissing = result.instance(0).value(attIndex);
      double previousNonMissing = firstNonMissing;
      int firstNonMissingIndex = -1;
      boolean leadingMissingDates = weka.core.Utils
View Full Code Here

              break;
            }
          }
         
          if (ok) {
            availableAtts.add(new Attribute(m_instances.attribute(i).name()));
          }
        } else {
          // this is available
          availableAtts.add(new Attribute(m_instances.attribute(i).name()));
        }
      }
      if (availableAtts.size() > 0) {
        result = new Instances("Overlay",availableAtts, 1);
      }
View Full Code Here

      m_addCustomPeriodicBut.setEnabled(false);
      m_deleteCustomPeriodicBut.setEnabled(false);
      m_savePeriodicBut.setEnabled(false);
      m_loadPeriodicBut.setEnabled(false);
    } else if (m_instances != null) {
      Attribute timeStampAtt = m_instances.attribute(selectedTimeStamp);
      if (timeStampAtt != null && timeStampAtt.isDate()) {
        m_customizeDateDerivedPeriodics.setEnabled(true);
      } else {
        m_customizeDateDerivedPeriodics.setSelected(false);
        m_customizeDateDerivedPeriodics.setEnabled(false);
        m_editCustomPeriodicBut.setEnabled(false);
View Full Code Here

    List<TSEvalModule> evalModulesL = TSEvalModule.getModuleList();
    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    int numMods = 0;
    for (TSEvalModule s : evalModulesL) {
      if (!(s.getEvalName().equals("Error"))) {
        atts.add(new Attribute(s.toString()));
        numMods++;
      }
    }
    Instances modInsts = new Instances("Eval modules", atts, 1);
    m_evaluationModsHeader = modInsts;
View Full Code Here

    m_configHolder.addTab("Output", null, outputOptsHolder, "Configure output");
  }
 
  private Instances createDateDerivedPeriodicList() {
    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    atts.add(new Attribute("AM")); atts.add(new Attribute("DayOfWeek"));
    atts.add(new Attribute("DayOfMonth")); atts.add(new Attribute("NumDaysInMonth"));
    atts.add(new Attribute("Weekend")); atts.add(new Attribute("Month"));
    atts.add(new Attribute("Quarter"));
   
    // Custom periodics
    for (String name : m_customPeriodics.keySet()) {
      atts.add(new Attribute("c_" + name));
    }
   
    Instances insts = new Instances("Periodics", atts, 1);
   
    return insts;
View Full Code Here

          m_dateDerivedPeriodicsHeader = insts;
         
          // make sure that selected stuff stays selected
          boolean[] newSelected = new boolean[insts.numAttributes()];
          for (int i = 0; i < selected.length; i++) {
            Attribute toFind = oldList.attribute(selected[i]);
            Attribute toSet = insts.attribute(toFind.name());
            if (toSet != null) {
              newSelected[toSet.index()] = true;
            }
          }
          try {
            m_dateDerivedPeriodicSelector.setSelectedAttributes(newSelected);
          } catch (Exception e1) {
            e1.printStackTrace();
          }
        }
      }
    });
   
    m_addCustomPeriodicBut.setEnabled(false);
    m_addCustomPeriodicBut.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {       
        ArrayList<CustomPeriodicTest> testList = new ArrayList<CustomPeriodicTest>();
        String fieldName = displayAddEditDialog(testList, null);
        if (fieldName.length() == 0) {         
          // basically do nothing
        } else {
          // Add this new one to the Map
          m_customPeriodics.put(fieldName, testList);

          // what's been selected so far?
          int[] selected = m_dateDerivedPeriodicSelector.getSelectedAttributes();
          Instances insts = createDateDerivedPeriodicList();
          m_dateDerivedPeriodicSelector.setInstances(insts);
          m_dateDerivedPeriodicsHeader = insts;
         
          boolean[] newSelected = new boolean[insts.numAttributes()];
          for (int i = 0; i < selected.length; i++) {
            newSelected[selected[i]] = true;
          }

          // select the newly created field
          newSelected[newSelected.length - 1] = true;
          try {
            m_dateDerivedPeriodicSelector.setSelectedAttributes(newSelected);
          } catch (Exception e1) {
            e1.printStackTrace();
          }
        }
      }
    });
   
    temp.add(checkHolder, BorderLayout.NORTH);
    temp.add(m_dateDerivedPeriodicSelector, BorderLayout.CENTER);
    m_dateDerivedPeriodicSelector.setPreferredScrollableViewportSize(new Dimension(250,80));
    JPanel botP = new JPanel();
    botP.setLayout(new BorderLayout());
    botP.setBorder(BorderFactory.
        createTitledBorder("Date-derived periodic creation"));
    botP.add(temp, BorderLayout.CENTER);
   
    JPanel primaryPeriodicP = new JPanel();
    primaryPeriodicP.setLayout(new BorderLayout());
    //primaryPeriodicP
     
    primaryPeriodicP.add(m_primaryPeriodicCombo, BorderLayout.NORTH);
    JPanel topP = new JPanel();
    topP.setLayout(new BorderLayout());
    topP.setBorder(BorderFactory.createTitledBorder("Periodic attribute"));
    topP.add(primaryPeriodicP, BorderLayout.EAST);
   
    JPanel temp2 = new JPanel();
    temp2.setLayout(new BorderLayout());
    temp2.add(topP, BorderLayout.EAST);
    temp2.add(botP, BorderLayout.CENTER);
//    basePanel.add(primaryPeriodicP, BorderLayout.EAST);
    basePanel.add(temp2, BorderLayout.NORTH);
   
    m_customizeDateDerivedPeriodics.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_instances != null && m_customizeDateDerivedPeriodics.isSelected()) {
          String selectedTimeStamp =
            m_simpleConfig.m_timeStampCombo.getSelectedItem().toString();
          Attribute timeStampAtt = m_instances.attribute(selectedTimeStamp);
         
          if (timeStampAtt != null && timeStampAtt.isDate()) {
            if (m_dateDerivedPeriodicSelector.getSelectionModel() != null) {
              Instances insts = createDateDerivedPeriodicList();
             
              m_dateDerivedPeriodicSelector.setInstances(insts);
              m_dateDerivedPeriodicsHeader = insts;
View Full Code Here

TOP

Related Classes of weka.core.Attribute

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.