Example usage for weka.core Instance setValue

List of usage examples for weka.core Instance setValue

Introduction

In this page you can find the example usage for weka.core Instance setValue.

Prototype

public void setValue(Attribute att, String value);

Source Link

Document

Sets a value of an nominal or string attribute to the given value.

Usage

From source file:org.opentox.qsar.processors.predictors.SimplePredictor.java

License:Open Source License

/**
 * Perform the prediction which is based on the serialized model file on the server.
 * @param data//w  ww .j a va  2 s . c  o  m
 *      Input data for with respect to which the predicitons are calculated
 * @return
 *      A dataset containing the compounds submitted along with their predicted values.
 * @throws QSARException
 *      In case the prediction (as a whole) is not feasible. If the prediction is not
 *      feasible for a single instance, the prediction is set to <code>?</code> (unknown/undefined/missing).
 *      If the prediction is not feasible for all instances, an exception (QSARException) is thrown.
 */
@Override
public Instances predict(final Instances data) throws QSARException {

    Instances dataClone = new Instances(data);
    /**
     * IMPORTANT!
     * String attributes have to be removed from the dataset before
     * applying the prediciton
     */
    dataClone = new AttributeCleanup(ATTRIBUTE_TYPE.string).filter(dataClone);

    /**
     * Set the class attribute of the incoming data to any arbitrary attribute
     * (Choose the last for instance).
     */
    dataClone.setClass(dataClone.attribute(model.getDependentFeature().getURI()));

    /**
     *
     * Create the Instances that will host the predictions. This object contains
     * only two attributes: the compound_uri and the target feature of the model.
     */
    Instances predictions = null;
    FastVector attributes = new FastVector();
    final Attribute compoundAttribute = new Attribute("compound_uri", (FastVector) null);
    final Attribute targetAttribute = dataClone.classAttribute();
    attributes.addElement(compoundAttribute);
    attributes.addElement(targetAttribute);

    predictions = new Instances("predictions", attributes, 0);
    predictions.setClassIndex(1);

    Instance predictionInstance = new Instance(2);
    try {
        final Classifier cls = (Classifier) SerializationHelper.read(filePath);

        for (int i = 0; i < data.numInstances(); i++) {
            try {
                String currentCompound = data.instance(i).stringValue(0);
                predictionInstance.setValue(compoundAttribute, currentCompound);

                if (targetAttribute.type() == Attribute.NUMERIC) {
                    double clsLabel = cls.classifyInstance(dataClone.instance(i));
                    predictionInstance.setValue(targetAttribute, clsLabel);
                } else if (targetAttribute.type() == Attribute.NOMINAL) {
                    double[] clsLable = cls.distributionForInstance(dataClone.instance(i));
                    int indexForNominalElement = maxInArray(clsLable).getPosition();
                    Enumeration nominalValues = targetAttribute.enumerateValues();
                    int counter = 0;
                    String nomValue = "";
                    while (nominalValues.hasMoreElements()) {
                        if (counter == indexForNominalElement) {
                            nomValue = nominalValues.nextElement().toString();
                            break;
                        }
                        counter++;
                    }
                    predictionInstance.setValue(targetAttribute, nomValue);

                    predictionInstance.setValue(targetAttribute, cls.classifyInstance(dataClone.instance(i)));
                }

                predictions.add(predictionInstance);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }

    } catch (Exception ex) {
    }

    return predictions;
}

From source file:org.processmining.analysis.clusteranalysis.ClusterDecisionAnalyzer.java

License:Open Source License

public Instances getDataInfo() {
    // create attribute information
    FastVector attributeInfo = new FastVector();
    // make attribute
    // clean the relevant attribute list and re-fill based on new selection
    // scope//w ww  . j  a v  a  2  s  .  c om
    for (int i = 0; i < agProfiles.numberOfItems(); i++) {
        if (checks[i].isSelected()) {
            String name = CpnUtils.replaceSpecialCharacters(agProfiles.getItemKey(i));
            Attribute wekaAtt = new Attribute(name);
            attributeInfo.addElement(wekaAtt);
        }
    }
    // for target concept
    FastVector my_nominal_values = new FastVector(clusters.getClusters().size());
    Attribute targetConcept = null;
    for (Cluster aCluster : clusters.getClusters()) {
        my_nominal_values.addElement(aCluster.getName());
    }
    targetConcept = new Attribute("Cluster", my_nominal_values);
    attributeInfo.addElement(targetConcept);
    attributeInfo.trimToSize();

    // learning
    Instances data = new Instances("Clustering", attributeInfo, 0);
    data.setClassIndex(data.numAttributes() - 1);

    for (Cluster aCluster : clusters.getClusters()) {
        String clusterName = aCluster.getName();
        for (Integer i : aCluster.getTraceIndices()) {
            Instance instance0 = new Instance(attributeInfo.size());
            for (int j = 0; j < agProfiles.numberOfItems(); j++) {
                if (checks[j].isSelected()) {
                    String name = CpnUtils.replaceSpecialCharacters(agProfiles.getItemKey(j));
                    Attribute wekaAtt = data.attribute(name);
                    if (wekaAtt != null) {
                        double doubleAttValue = (new Double(agProfiles.getValue(i, j))).doubleValue();
                        instance0.setValue(wekaAtt, doubleAttValue);
                    } else {
                        System.out.println("fail to add");
                    }
                }
            }
            instance0.setDataset(data);
            instance0.setClassValue(clusterName);
            data.add(instance0);
        }
    }
    return data;
}

From source file:org.processmining.analysis.decisionmining.DecisionMiningLogTrace.java

License:Open Source License

/**
 * Helper method for reading the mapp of data attributes (key,value) pairs,
 * and adding them to the current learning instance.
 * /*ww w .jav a 2  s . c om*/
 * @param data
 *            the map of data attribues
 * @param instance
 *            the current learning instance
 * @param dataset
 *            the current learning problem (contains attribute info)
 */
private void evaluateDataForInstance(Map<String, String> data, Instance instance, Instances dataset) {
    Iterator<Entry<String, String>> dataIterator = data.entrySet().iterator();
    while (dataIterator.hasNext()) {
        Entry<String, String> currentDataEntry = dataIterator.next();
        String attName = currentDataEntry.getKey();
        String attValue = currentDataEntry.getValue();

        // replace white spaces and special characters by underscores both
        // in name and value
        attName = CpnUtils.replaceSpecialCharacters(attName);
        attValue = CpnUtils.replaceSpecialCharacters(attValue);

        // check whether current attribute is contained in relevant
        // attributes
        Attribute wekaAtt = dataset.attribute(attName);
        if (wekaAtt != null) {
            if (wekaAtt.isNominal() == true) {
                // corresponding string can be directly provided
                instance.setValue(wekaAtt, attValue);
            } else if (wekaAtt.isNumeric() == true) {
                // value must be converted into double first
                try {
                    double doubleAttValue = (new Double(attValue)).doubleValue();
                    instance.setValue(wekaAtt, doubleAttValue);
                } catch (NumberFormatException ex) {
                    // the attribute did not contain a parsable number
                    Message.add("attribute " + attName + " appears not to be a numeric attribute!", 2);
                }
            }
        }
    }
}

From source file:org.processmining.analysis.decisionmining.DecisionMiningLogTraceForAuLdg.java

License:Open Source License

/**
 * Helper method for reading the mapp of data attributes (key,value) pairs,
 * and adding them to the current learning instance.
 * /*w  w w  .java  2  s  . co m*/
 * @param data
 *          the map of data attribues
 * @param instance
 *          the current learning instance
 * @param dataset
 *          the current learning problem (contains attribute info)
 */
private void evaluateDataForInstance(Map<String, String> data, Instance instance, Instances dataset) {
    Iterator<Entry<String, String>> dataIterator = data.entrySet().iterator();
    while (dataIterator.hasNext()) {
        Entry<String, String> currentDataEntry = dataIterator.next();
        String attName = currentDataEntry.getKey();
        String attValue = currentDataEntry.getValue();

        // replace white spaces and special characters by underscores both in name
        // and value
        attName = CpnUtils.replaceSpecialCharacters(attName);
        attValue = CpnUtils.replaceSpecialCharacters(attValue);

        // check whether current attribute is contained in relevant attributes
        Attribute wekaAtt = dataset.attribute(attName);
        if (wekaAtt != null) {
            if (wekaAtt.isNominal() == true) {
                // corresponding string can be directly provided
                instance.setValue(wekaAtt, attValue);
            } else if (wekaAtt.isNumeric() == true) {
                // value must be converted into double first
                try {
                    double doubleAttValue = (new Double(attValue)).doubleValue();
                    instance.setValue(wekaAtt, doubleAttValue);
                } catch (NumberFormatException ex) {
                    // the attribute did not contain a parsable number
                    Message.add("attribute " + attName + " appears not to be a numeric attribute!", 2);
                }
            }
        }
    }
}

From source file:org.processmining.analysis.traceclustering.profile.AggregateProfile.java

License:Open Source License

public Instances getWekaData() {
    Instances data = null;/* ww  w  . j  a  v a2  s  .  c  o  m*/

    // create attribute information
    FastVector attributeInfo = new FastVector();
    // make attribute
    // clean the relevant attribute list and re-fill based on new selection
    // scope
    for (int i = 0; i < numberOfItems(); i++) {
        String name = CpnUtils.replaceSpecialCharacters(getItemKey(i));
        Attribute wekaAtt = new Attribute(name);
        attributeInfo.addElement(wekaAtt);
    }
    attributeInfo.trimToSize();
    data = new Instances("Clustering", attributeInfo, 0);
    try {
        for (int i = 0; i < getLog().numberOfInstances(); i++) {
            Instance instance0 = new Instance(attributeInfo.size());
            for (int j = 0; j < numberOfItems(); j++) {
                String name = CpnUtils.replaceSpecialCharacters(getItemKey(j));
                Attribute wekaAtt = data.attribute(name);
                if (wekaAtt != null) {
                    double doubleAttValue = (new Double(getValue(i, j))).doubleValue();
                    instance0.setValue(wekaAtt, doubleAttValue);
                } else {
                    Message.add("Weka Error: fail to add", Message.ERROR);
                }
            }
            instance0.setDataset(data);
            data.add(instance0);
        }
    } catch (Exception c) {
        Message.add("Weka Error: " + c.toString(), Message.ERROR);
    }

    return data;
}

From source file:org.prom5.analysis.clusteranalysis.ClusterDecisionAnalyzer.java

License:Open Source License

public Instances getDataInfo() {
    // create attribute information
    FastVector attributeInfo = new FastVector();
    // make attribute
    // clean the relevant attribute list and re-fill based on new selection scope
    for (int i = 0; i < agProfiles.numberOfItems(); i++) {
        if (checks[i].isSelected()) {
            String name = CpnUtils.replaceSpecialCharacters(agProfiles.getItemKey(i));
            Attribute wekaAtt = new Attribute(name);
            attributeInfo.addElement(wekaAtt);
        }//from  www. j a va 2  s . c o m
    }
    // for target concept
    FastVector my_nominal_values = new FastVector(clusters.getClusters().size());
    Attribute targetConcept = null;
    for (Cluster aCluster : clusters.getClusters()) {
        my_nominal_values.addElement(aCluster.getName());
    }
    targetConcept = new Attribute("Cluster", my_nominal_values);
    attributeInfo.addElement(targetConcept);
    attributeInfo.trimToSize();

    // learning
    Instances data = new Instances("Clustering", attributeInfo, 0);
    data.setClassIndex(data.numAttributes() - 1);

    for (Cluster aCluster : clusters.getClusters()) {
        String clusterName = aCluster.getName();
        for (Integer i : aCluster.getTraceIndices()) {
            Instance instance0 = new Instance(attributeInfo.size());
            for (int j = 0; j < agProfiles.numberOfItems(); j++) {
                if (checks[j].isSelected()) {
                    String name = CpnUtils.replaceSpecialCharacters(agProfiles.getItemKey(j));
                    Attribute wekaAtt = data.attribute(name);
                    if (wekaAtt != null) {
                        double doubleAttValue = (new Double(agProfiles.getValue(i, j))).doubleValue();
                        instance0.setValue(wekaAtt, doubleAttValue);
                    } else {
                        System.out.println("fail to add");
                    }
                }
            }
            instance0.setDataset(data);
            instance0.setClassValue(clusterName);
            data.add(instance0);
        }
    }
    return data;
}

From source file:org.prom5.analysis.decisionmining.DecisionMiningLogTrace.java

License:Open Source License

/**
 * Helper method for reading the mapp of data attributes (key,value) pairs, 
 * and adding them to the current learning instance.
 * @param data the map of data attribues
 * @param instance the current learning instance
 * @param dataset the current learning problem (contains attribute info)
 *//*w  w w.j  ava2  s .  co m*/
private void evaluateDataForInstance(Map<String, String> data, Instance instance, Instances dataset) {
    Iterator<Entry<String, String>> dataIterator = data.entrySet().iterator();
    while (dataIterator.hasNext()) {
        Entry<String, String> currentDataEntry = dataIterator.next();
        String attName = currentDataEntry.getKey();
        String attValue = currentDataEntry.getValue();

        // replace white spaces and special characters by underscores both in name and value
        attName = CpnUtils.replaceSpecialCharacters(attName);
        attValue = CpnUtils.replaceSpecialCharacters(attValue);

        // check whether current attribute is contained in relevant attributes
        Attribute wekaAtt = dataset.attribute(attName);
        if (wekaAtt != null) {
            if (wekaAtt.isNominal() == true) {
                // corresponding string can be directly provided
                instance.setValue(wekaAtt, attValue);
            } else if (wekaAtt.isNumeric() == true) {
                // value must be converted into double first
                try {
                    double doubleAttValue = (new Double(attValue)).doubleValue();
                    instance.setValue(wekaAtt, doubleAttValue);
                } catch (NumberFormatException ex) {
                    // the attribute did not contain a parsable number
                    Message.add("attribute " + attName + " appears not to be a numeric attribute!", 2);
                }
            }
        }
    }
}

From source file:org.prom5.analysis.traceclustering.profile.AggregateProfile.java

License:Open Source License

public Instances getWekaData() {
    Instances data = null;//from www .  j a v a2s .  co m

    // create attribute information
    FastVector attributeInfo = new FastVector();
    // make attribute
    // clean the relevant attribute list and re-fill based on new selection scope
    for (int i = 0; i < numberOfItems(); i++) {
        String name = CpnUtils.replaceSpecialCharacters(getItemKey(i));
        Attribute wekaAtt = new Attribute(name);
        attributeInfo.addElement(wekaAtt);
    }
    attributeInfo.trimToSize();
    data = new Instances("Clustering", attributeInfo, 0);
    try {
        for (int i = 0; i < getLog().numberOfInstances(); i++) {
            Instance instance0 = new Instance(attributeInfo.size());
            for (int j = 0; j < numberOfItems(); j++) {
                String name = CpnUtils.replaceSpecialCharacters(getItemKey(j));
                Attribute wekaAtt = data.attribute(name);
                if (wekaAtt != null) {
                    double doubleAttValue = (new Double(getValue(i, j))).doubleValue();
                    instance0.setValue(wekaAtt, doubleAttValue);
                } else {
                    Message.add("Weka Error: fail to add", Message.ERROR);
                }
            }
            instance0.setDataset(data);
            data.add(instance0);
        }
    } catch (Exception c) {
        Message.add("Weka Error: " + c.toString(), Message.ERROR);
    }

    return data;
}

From source file:org.wkwk.classifier.MyC45.java

@Override
public void buildClassifier(Instances data) throws Exception {
    getCapabilities().testWithFail(data);

    for (int i = 0; i < data.numAttributes(); i++) {
        Attribute attr = data.attribute(i);
        for (int j = 0; j < 10; j++) {
            Instance instance = data.instance(j);
            if (instance.isMissing(attr)) {
                instance.setValue(attr, fillMissingValue(data, attr));
            }/*from  w w  w  .  jav a 2  s . c  o m*/
        }
    }

    data.deleteWithMissingClass();
    makeTree(data);
}

From source file:oxis.yologp.YOLogPDescriptor.java

License:Open Source License

/**
 * Predict the LogP.//from   w  w  w .j a va  2  s.c o m
 *
 */
private void predict() throws Exception {

    Instances instances = buildDataset();

    Map<Object, Object> properties;
    for (DrugStruct drugStruct : listDrug) {

        if (drugStruct.drug.getProperty("flag")) {
            properties = drugStruct.drug.getProperties();
            Instance instance = new DenseInstance(instances.numAttributes()); //28 + 1024
            instance.setDataset(instances);
            for (Object propKey : properties.keySet()) {
                if (!(propKey.equals("hash") || propKey.equals("flag") || propKey.equals("smiles"))) {
                    try {
                        instance.setValue(instances.attribute(propKey.toString()),
                                Double.parseDouble(properties.get(propKey).toString()));
                    } catch (NullPointerException ex) {
                        Logger.getLogger(YOLogPDescriptor.class.getName()).log(Level.WARNING,
                                "Property not used: {0}", propKey.toString());
                    }
                }
            }

            double predicted = model.classifyInstance(instance);
            predicted = Math.round(predicted * 100) / 100.0d;
            instance.setClassValue(predicted);
            instances.add(instance);
            drugStruct.drug.setProperty("predicted", predicted);
        }
    }
}