Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok. I found the solution myself. The central part of the solution is the method <code>Instances#insertAttributeAt</code>, which inserts a new attribute as the last one if the second parameter is <code>model.numAttributes()</code>. Here is some example code for numerical attributes. It is easy to adapt to other types of attributes as well:</p> <pre><code> Map&lt;String,String&gt; currentInstanceFeatures = currentInstance.getFeatures(); Instances model = null; try { if (targetFile.exists()) { FileReader in = new FileReader(targetFile); try { BufferedReader reader = new BufferedReader(in); ArffReader arff = new ArffReader(reader); model = arff.getData(); } finally { IOUtils.closeQuietly(in); } } else { FastVector schema = new FastVector(); model = new Instances("model", schema, 1); } Instance newInstance = new SparseInstance(0); newInstance.setDataset(model); for(Map.Entry&lt;String,String&gt; feature:currentInstanceFeatures.entrySet()) { Attribute attribute = model.attribute(feature.getKey()); if (attribute == null) { attribute = new Attribute(feature.getKey()); model.insertAttributeAt(attribute, model.numAttributes()); attribute = model.attribute(feature.getKey()); } newInstance.setValue(attribute, feature.getValue()); } model.add(newInstance); model.compactify(); ArffSaver saver = new ArffSaver(); saver.setInstances(model); saver.setFile(targetFile); LOGGER.debug("Saving dataset to: " + targetFile.getAbsoluteFile()); saver.writeBatch(); } catch (IOException e) { throw new IllegalArgumentException(e); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload