Java Code Examples for weka.core.Attribute#value()

The following examples show how to use weka.core.Attribute#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Test.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the test represented by a string in Prolog notation.
 *
 * @return a string representing the test in Prolog notation
 */   
public String toPrologString() {
  Attribute att = m_Dataset.attribute(m_AttIndex);
  StringBuffer str = new StringBuffer();
  String attName = m_Dataset.attribute(m_AttIndex).name();
  if (att.isNumeric()) {
    str = str.append(attName + " ");
    if (m_Not) str = str.append(">= " + Utils.doubleToString(m_Split, 3));
    else str = str.append("< " + Utils.doubleToString(m_Split, 3));
  } else {
    String value = att.value((int)m_Split);
  
    if (value == "false") { str = str.append("not(" + attName + ")"); }      
    else { str = str.append(attName); }
  }
return str.toString();
}
 
Example 2
Source File: ADTree.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the legend of the tree, describing how results are to be interpreted.
 *
 * @return a string containing the legend of the classifier
 */
public String legend() {
  
  Attribute classAttribute = null;
  if (m_trainInstances == null) return "";
  try {classAttribute = m_trainInstances.classAttribute();} catch (Exception x){};
  return ("-ve = " + classAttribute.value(0) +
   ", +ve = " + classAttribute.value(1));
}
 
Example 3
Source File: UnivariateNominalMultiwaySplit.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String branchForInstance(Instance inst) {
  Attribute att = inst.dataset().attribute(m_splitAttNames.get(0));
  if (att == null || inst.isMissing(att)) {
    return null;
  }
  return att.value((int) inst.value(att));
}
 
Example 4
Source File: TwoWayNominalSplit.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the string describing the comparision the split depends on for a particular
 * branch. i.e. the right hand side of the description of the split.
 *
 * @param branchNum the branch of the split
 * @param dataset the dataset that the split is based on
 * @return a string describing the comparison
 */
public String comparisonString(int branchNum, Instances dataset) {

  Attribute att = dataset.attribute(attIndex);
  if (att.numValues() != 2) 
    return ((branchNum == 0 ? "= " : "!= ") + att.value(trueSplitValue));
  else return ("= " + (branchNum == 0 ?
	 att.value(trueSplitValue) :
	 att.value(trueSplitValue == 0 ? 1 : 0)));
}
 
Example 5
Source File: Test.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gives a string representation of the test, starting from the comparison
 * symbol.
 *
 * @return a string representing the test
 */   
private String testComparisonString() {
  Attribute att = m_Dataset.attribute(m_AttIndex);
  if (att.isNumeric()) {
    return ((m_Not ? ">= " : "< ") + Utils.doubleToString(m_Split,3));
  }
  else {
    if (att.numValues() != 2) 
      return ((m_Not ? "!= " : "= ") + att.value((int)m_Split));
    else return ("= " 
                 + (m_Not ?
    att.value((int)m_Split == 0 ? 1 : 0) : att.value((int)m_Split)));
  }
}
 
Example 6
Source File: Test.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gives a string representation of the test in Prolog notation, starting
 * from the comparison symbol.
 *
 * @return a string representing the test in Prolog notation
 */   
private String testPrologComparisonString() {
  Attribute att = m_Dataset.attribute(m_AttIndex);
  if (att.isNumeric()) {
    return ((m_Not ? ">= " : "< ") + Utils.doubleToString(m_Split,3));
  }
  else {
    if (att.numValues() != 2) 
      return ((m_Not ? "!= " : "= ") + att.value((int)m_Split));
    else return ("= " 
                 + (m_Not ? att.value((int)m_Split == 0 ? 1 : 0) 
                        : att.value((int)m_Split)));
  }
}
 
Example 7
Source File: WekaInstance.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private Object transformAttributeValueToData(final Attribute att) {
	if (att.isNominal() || att.isString() || att.isRelationValued() || att.isDate() || att.isRegular()) {
		return att.value((int) this.getElement().value(att));
	} else {
		return this.getElement().value(att);
	}
}
 
Example 8
Source File: MekaInstance.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
private Object transformAttributeValueToData(final Attribute att) {
	if (att.isNominal() || att.isString() || att.isRelationValued() || att.isDate() || att.isRegular()) {
		return att.value((int) this.getElement().value(att));
	} else {
		return this.getElement().value(att);
	}
}
 
Example 9
Source File: Utils.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Copies the attribute name and values of a given nominal attribute
 * @param oldAttribute attribute to copy
 * @return duplicated nominal attribute
 */
public static Attribute copyNominalAttribute(Attribute oldAttribute) {
  String[] classValues = new String[oldAttribute.numValues()];
  for (int classValI = 0; classValI < oldAttribute.numValues(); classValI++) {
    classValues[classValI] = oldAttribute.value(classValI);
  }
  return new Attribute(oldAttribute.name(), Arrays.asList(classValues));
}
 
Example 10
Source File: Utils.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Copies the attribute name and values of a given nominal attribute
 * @param oldAttribute attribute to copy
 * @return duplicated nominal attribute
 */
public static Attribute copyNominalAttribute(Attribute oldAttribute) {
  String[] classValues = new String[oldAttribute.numValues()];
  for (int classValI = 0; classValI < oldAttribute.numValues(); classValI++) {
    classValues[classValI] = oldAttribute.value(classValI);
  }
  return new Attribute(oldAttribute.name(), Arrays.asList(classValues));
}
 
Example 11
Source File: InputMappedClassifier.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
private boolean regenerateMapping() throws Exception {
  loadModel(m_modelPath); // load a model (if specified)
  
  if (m_modelHeader == null) {
    return false;
  }
  
  m_attributeMap = new int[m_modelHeader.numAttributes()];
  m_attributeStatus = new int[m_modelHeader.numAttributes()];
  m_nominalValueMap = new int[m_modelHeader.numAttributes()][];
  
  for (int i = 0; i < m_modelHeader.numAttributes(); i++) {
    String modelAttName = m_modelHeader.attribute(i).name();
    m_attributeStatus[i] = NO_MATCH;
    
    for (int j = 0; j < m_inputHeader.numAttributes(); j++) {
      String incomingAttName = m_inputHeader.attribute(j).name();
      if (stringMatch(modelAttName, incomingAttName)) {
        m_attributeMap[i] = j;
        m_attributeStatus[i] = OK;
        
        Attribute modelAtt = m_modelHeader.attribute(i);
        Attribute incomingAtt = m_inputHeader.attribute(j);
        
        // check types
        if (modelAtt.type() != incomingAtt.type()) {
          m_attributeStatus[i] = TYPE_MISMATCH;
          break;
        }          
        
        // now check nominal values (number, names...)
        if (modelAtt.numValues() != incomingAtt.numValues()) {
          System.out.println("[InputMappedClassifier] Warning: incoming nominal " +
          		"attribute " + incomingAttName + " does not have the same " +
          				"number of values as model attribute "
          		+ modelAttName);
          
        }
        
        if (modelAtt.isNominal() && incomingAtt.isNominal()) {
          int[] valuesMap = new int[incomingAtt.numValues()];
          for (int k = 0; k < incomingAtt.numValues(); k++) {
            String incomingNomValue = incomingAtt.value(k);
            int indexInModel = modelAtt.indexOfValue(incomingNomValue);
            if (indexInModel < 0) {
              valuesMap[k] = NO_MATCH;
            } else {
              valuesMap[k] = indexInModel;
            }
          }
          m_nominalValueMap[i] = valuesMap;
        }
      }
    }
  }

  
  return true;
}