Search in sources :

Example 1 with Reorder

use of weka.filters.unsupervised.attribute.Reorder in project umple by umple.

the class EditableBayesNet method setData.

// c'tor
/**
 * Assuming a network structure is defined and we want to learn from data, the
 * data set must be put if correct order first and possibly
 * discretized/missing values filled in before proceeding to CPT learning.
 *
 * @param instances data set to learn from
 * @exception Exception when data sets are not compatible, e.g., a variable is
 *              missing or a variable has different nr of values.
 */
public void setData(Instances instances) throws Exception {
    // sync order of variables
    int[] order = new int[getNrOfNodes()];
    for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {
        String sName = getNodeName(iNode);
        int nNode = 0;
        while (nNode < getNrOfNodes() && !sName.equals(instances.attribute(nNode).name())) {
            nNode++;
        }
        if (nNode >= getNrOfNodes()) {
            throw new Exception("Cannot find node named [[[" + sName + "]]] in the data");
        }
        order[iNode] = nNode;
    }
    Reorder reorderFilter = new Reorder();
    reorderFilter.setAttributeIndicesArray(order);
    reorderFilter.setInputFormat(instances);
    instances = Filter.useFilter(instances, reorderFilter);
    // filter using discretization/missing values filter
    Instances newInstances = new Instances(m_Instances, 0);
    if (m_DiscretizeFilter == null && m_MissingValuesFilter == null) {
        newInstances = normalizeDataSet(instances);
    } else {
        for (int iInstance = 0; iInstance < instances.numInstances(); iInstance++) {
            newInstances.add(normalizeInstance(instances.instance(iInstance)));
        }
    }
    // sanity check
    for (int iNode = 0; iNode < getNrOfNodes(); iNode++) {
        if (newInstances.attribute(iNode).numValues() != getCardinality(iNode)) {
            throw new Exception("Number of values of node [[[" + getNodeName(iNode) + "]]] differs in (discretized) dataset.");
        }
    }
    // if we got this far, all is ok with the data set and
    // we can replace data set of Bayes net
    m_Instances = newInstances;
}
Also used : Instances(weka.core.Instances) Reorder(weka.filters.unsupervised.attribute.Reorder)

Example 2 with Reorder

use of weka.filters.unsupervised.attribute.Reorder in project umple by umple.

the class ArffTableModel method attributeAsClassAt.

/**
 * sets the attribute at the given col index as the new class attribute, i.e.
 * it moves it to the end of the attributes
 *
 * @param columnIndex the index of the column
 */
public void attributeAsClassAt(int columnIndex) {
    Reorder reorder;
    String order;
    int i;
    if ((columnIndex > 0) && (columnIndex < getColumnCount())) {
        addUndoPoint();
        try {
            // build order string (1-based!)
            order = "";
            for (i = 1; i < m_Data.numAttributes() + 1; i++) {
                // skip new class
                if (i == columnIndex) {
                    continue;
                }
                if (!order.equals("")) {
                    order += ",";
                }
                order += Integer.toString(i);
            }
            if (!order.equals("")) {
                order += ",";
            }
            order += Integer.toString(columnIndex);
            // process data
            reorder = new Reorder();
            reorder.setAttributeIndices(order);
            reorder.setInputFormat(m_Data);
            m_Data = Filter.useFilter(m_Data, reorder);
            // set class index
            m_Data.setClassIndex(m_Data.numAttributes() - 1);
        } catch (Exception e) {
            e.printStackTrace();
            undo();
        }
        notifyListener(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
    }
}
Also used : Reorder(weka.filters.unsupervised.attribute.Reorder) TableModelEvent(javax.swing.event.TableModelEvent)

Example 3 with Reorder

use of weka.filters.unsupervised.attribute.Reorder in project ambit-mirror by ideaconsult.

the class CallableWafflesModelCreator method createReference.

/**
 *	TODO download ARFF directly, instead of reading Weka instances in memory
 *	protected AbstractBatchProcessor createBatch(Object target) throws Exception{
 *		if (target == null) throw new Exception("No dataset URI");
 *
 *		File.createTempFile(prefix, suffix);
 *		DownloadTool.download(url, file);
 *		builder.setTrainingData(trainingData);
 *		return null;
 *	}
 */
@Override
protected TaskResult createReference(Connection connection) throws Exception {
    File trainingData = File.createTempFile("wfltrain_", ".arff");
    trainingData.deleteOnExit();
    Instances instances = ((RDFInstancesParser) batch).getInstances();
    instances.deleteAttributeAt(0);
    // sort attributes by name (i.e. attribute URI).
    List<Attribute> sorted = new ArrayList<Attribute>();
    for (int i = 0; i < instances.numAttributes(); i++) sorted.add(instances.attribute(i));
    Collections.sort(sorted, new Comparator<Attribute>() {

        @Override
        public int compare(Attribute o1, Attribute o2) {
            return o1.name().compareTo(o2.toString());
        }
    });
    StringBuilder order = null;
    for (int i = 0; i < sorted.size(); i++) {
        if (order == null)
            order = new StringBuilder();
        else
            order.append(",");
        order.append(sorted.get(i).index() + 1);
    }
    Reorder reorder = new Reorder();
    String[] options = new String[2];
    options[0] = "-R";
    options[1] = order.toString();
    reorder.setOptions(options);
    reorder.setInputFormat(instances);
    instances = Filter.useFilter(instances, reorder);
    SparseToNonSparse sp = new SparseToNonSparse();
    sp.setInputFormat(instances);
    Instances newInstances = Filter.useFilter(instances, sp);
    ArffSaver saver = new ArffSaver();
    saver.setInstances(newInstances);
    saver.setFile(trainingData);
    saver.writeBatch();
    // Leave the header only
    newInstances.delete();
    builder.setHeader(newInstances);
    builder.setTrainingData(trainingData);
    UpdateExecutor<CreateModel> x = new UpdateExecutor<CreateModel>();
    try {
        model = createModel();
        // trainingData.delete();
        CreateModel update = new CreateModel(model);
        x.setConnection(connection);
        x.process(update);
        writeAnnotations(model.getPredicted(), x);
        return new TaskResult(builder.getModelReporter().getURI(model));
    } catch (WekaException e) {
        throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), e);
    } catch (Exception e) {
        Context.getCurrentLogger().severe(e.getMessage());
        throw e;
    } finally {
        try {
            x.close();
        } catch (Exception xx) {
        }
    }
}
Also used : WekaException(weka.core.WekaException) Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList) ArffSaver(weka.core.converters.ArffSaver) WekaException(weka.core.WekaException) ResourceException(org.restlet.resource.ResourceException) Instances(weka.core.Instances) Reorder(weka.filters.unsupervised.attribute.Reorder) CreateModel(ambit2.db.update.model.CreateModel) SparseToNonSparse(weka.filters.unsupervised.instance.SparseToNonSparse) UpdateExecutor(ambit2.db.UpdateExecutor) TaskResult(ambit2.rest.task.TaskResult) ResourceException(org.restlet.resource.ResourceException) RDFInstancesParser(ambit2.rest.dataset.RDFInstancesParser) File(java.io.File)

Example 4 with Reorder

use of weka.filters.unsupervised.attribute.Reorder in project umple by umple.

the class InputMappedClassifierTest method reorderAtts.

protected Instances reorderAtts(Instances data) throws Exception {
    Reorder r = new Reorder();
    String range = "last";
    for (int i = data.numAttributes() - 1; i > 0; i--) {
        range += "," + i;
    }
    r.setAttributeIndices(range);
    r.setInputFormat(data);
    data = Filter.useFilter(data, r);
    return data;
}
Also used : Reorder(weka.filters.unsupervised.attribute.Reorder)

Aggregations

Reorder (weka.filters.unsupervised.attribute.Reorder)4 Instances (weka.core.Instances)2 UpdateExecutor (ambit2.db.UpdateExecutor)1 CreateModel (ambit2.db.update.model.CreateModel)1 RDFInstancesParser (ambit2.rest.dataset.RDFInstancesParser)1 TaskResult (ambit2.rest.task.TaskResult)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 TableModelEvent (javax.swing.event.TableModelEvent)1 ResourceException (org.restlet.resource.ResourceException)1 Attribute (weka.core.Attribute)1 WekaException (weka.core.WekaException)1 ArffSaver (weka.core.converters.ArffSaver)1 SparseToNonSparse (weka.filters.unsupervised.instance.SparseToNonSparse)1