List of usage examples for weka.core Attribute setStringValue
public void setStringValue(String value)
From source file:org.pentaho.di.scoring.WekaScoringData.java
License:Open Source License
/** * Helper method that constructs an Instance to input to the Weka model based * on incoming Kettle fields and pre-constructed attribute-to-field mapping * data.//from w ww. j a v a2 s . c o m * * @param inputMeta a <code>RowMetaInterface</code> value * @param inputRow an <code>Object</code> value * @param mappingIndexes an <code>int</code> value * @param model a <code>WekaScoringModel</code> value * @return an <code>Instance</code> value */ private Instance constructInstance(RowMetaInterface inputMeta, Object[] inputRow, int[] mappingIndexes, WekaScoringModel model, boolean freshVector) { Instances header = model.getHeader(); // Re-use this array (unless told otherwise) to avoid an object creation if (m_vals == null || freshVector) { m_vals = new double[header.numAttributes()]; } for (int i = 0; i < header.numAttributes(); i++) { if (mappingIndexes[i] >= 0) { try { Object inputVal = inputRow[mappingIndexes[i]]; Attribute temp = header.attribute(i); ValueMetaInterface tempField = inputMeta.getValueMeta(mappingIndexes[i]); int fieldType = tempField.getType(); // Check for missing value (null or empty string) if (tempField.isNull(inputVal)) { m_vals[i] = Utils.missingValue(); continue; } switch (temp.type()) { case Attribute.NUMERIC: { if (fieldType == ValueMetaInterface.TYPE_BOOLEAN) { Boolean b = tempField.getBoolean(inputVal); if (b.booleanValue()) { m_vals[i] = 1.0; } else { m_vals[i] = 0.0; } } else if (fieldType == ValueMetaInterface.TYPE_INTEGER) { Long t = tempField.getInteger(inputVal); m_vals[i] = t.longValue(); } else { Double n = tempField.getNumber(inputVal); m_vals[i] = n.doubleValue(); } } break; case Attribute.NOMINAL: { String s = tempField.getString(inputVal); // now need to look for this value in the attribute // in order to get the correct index int index = temp.indexOfValue(s); if (index < 0) { // set to missing value m_vals[i] = Utils.missingValue(); } else { m_vals[i] = index; } } break; case Attribute.STRING: { String s = tempField.getString(inputVal); // Set the attribute in the header to contain just this string value temp.setStringValue(s); m_vals[i] = 0.0; break; } default: m_vals[i] = Utils.missingValue(); } } catch (Exception e) { m_vals[i] = Utils.missingValue(); } } else { // set to missing value m_vals[i] = Utils.missingValue(); } } Instance newInst = new DenseInstance(1.0, m_vals); newInst.setDataset(header); return newInst; }