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:DocClassifier.java

public Instance createInstance(File file) {
    BufferedReader reader = null;
    try {//from   w  w w  .  jav a 2s .  c om
        Instance inst = new Instance(this.attrList.size());
        reader = new BufferedReader(new FileReader(file));
        Map<Attribute, Double> tFreqMap = new HashMap<Attribute, Double>();
        while (reader.ready()) {
            String line = reader.readLine();
            String[] words = line.split(" ");
            for (int i = 0; i < attrList.size() - 1; ++i) {
                Attribute attr = (Attribute) attrList.elementAt(i);
                int tFreq = termFreq(attr.name(), words);
                Double prevFreq = tFreqMap.get(attr);
                tFreqMap.put(attr, ((prevFreq != null) ? (prevFreq + tFreq) : tFreq));
            }
        }
        normalizeVector(tFreqMap);
        // System.err.println("\nInstance:");
        for (Attribute attr : tFreqMap.keySet()) {
            inst.setValue(attr, tFreqMap.get(attr) * idfMap.get(attr.name()));
            //System.err.print(attr.name()+":"+inst.value(attr));
        }
        inst.setValue((Attribute) attrList.lastElement(), file.getName().substring(0, 3).toLowerCase());
        return inst;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(DocClassifier.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(DocClassifier.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(DocClassifier.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return null;
}

From source file:CopiaSeg3.java

public static void main(String[] args) throws Exception {

    BufferedReader datafile = readDataFile("breast-cancer-wisconsin.arff");

    Instances data = new Instances(datafile);
    data.setClassIndex(data.numAttributes() - 1);

    // Elije el nmero de particiones para la valicacin (4 = 75% Train, 25% Test)
    Instances[] split = split(data, 4);/*from   w  w  w .j  av  a  2  s. c  om*/

    // Separa los conjuntos en los arrays trainning y testing
    Instances trainingSplits = split[0];
    Instances testingSplits = split[1];

    // Elegir un conjunto de clasificadores
    Classifier[] models = { new MultilayerPerceptron()
            //, new J48 
            //, ...
    };

    FastVector fvWekaAttributes = new FastVector(9);

    // Ejecutar cada clasificador
    for (int j = 0; j < models.length; j++) {

        // Collect every group of predictions for current model in a FastVector
        FastVector predictions = new FastVector();

        // For each training-testing split pair, train and test the classifier
        Evaluation validation = simpleClassify(models[j], trainingSplits, testingSplits);
        predictions.appendElements(validation.predictions());

        // Uncomment to see the summary for each training-testing pair.
        System.out.println(models[j].toString());

        // Calculate overall accuracy of current classifier on all splits
        double accuracy = calculateAccuracy(predictions);

        //            // Print current classifier's name and accuracy in a complicated, but nice-looking way.
        System.out.println(models[j].getClass().getSimpleName() + " Accuracy: "
                + String.format("%.2f%%", accuracy) + "\n=====================");
        //            
        //            // Step 4: use the classifier
        //            // For real world applications, the actual use of the classifier is the ultimate goal. Heres the simplest way to achieve that. Lets say weve built an instance (named iUse) as explained in step 2:
        //            // Specify that the instance belong to the training set
        //            // in order to inherit from the set description

        Instance iUse = new DenseInstance(9);
        iUse.setValue((Attribute) predictions.elementAt(0), 4);
        iUse.setValue((Attribute) predictions.elementAt(1), 8);
        iUse.setValue((Attribute) predictions.elementAt(2), 8);
        iUse.setValue((Attribute) predictions.elementAt(3), 5);
        iUse.setValue((Attribute) predictions.elementAt(4), 4);
        iUse.setValue((Attribute) predictions.elementAt(5), 5);
        iUse.setValue((Attribute) predictions.elementAt(6), 10);
        iUse.setValue((Attribute) predictions.elementAt(7), 4);
        iUse.setValue((Attribute) predictions.elementAt(8), 1);

        iUse.setDataset(trainingSplits);
        //
        //            // Get the likelihood of each classes
        // fDistribution[0] is the probability of being positive?
        // fDistribution[1] is the probability of being negative?
        double[] fDistribution = models[j].distributionForInstance(iUse);

        System.out.println("Probabilidad positivo: " + fDistribution[0]);
        System.out.println("Probabilidad negativo: " + fDistribution[1]);
    }

}

From source file:PredictMention.java

protected void setTestData(String title, String description, String keywords) {
    testData = new Instances(trainedData);
    testData.clear();// w  w  w  .ja v  a  2 s .  com
    Instance inst = new DenseInstance(4);
    inst.setDataset(testData);
    inst.setValue(0, title);
    inst.setValue(1, description);
    inst.setValue(2, keywords);
    inst.setMissing(3);
    testData.add(inst);

}

From source file:CJWeka.java

License:Open Source License

/** Convert a sting of floats separated by spaces into an Instance
 *//*from  w w  w  . ja v  a  2 s  . c om*/
private Instance floatstringToInst(String floatvalues, Instances ii, boolean hasClass) {
    String[] flostr = floatvalues.split(" ");
    int nvals = flostr.length;
    Instance i = new DenseInstance(nvals);
    int j;

    if (hasClass)
        nvals--;

    for (j = 0; j < nvals; j++) {
        if (!flostr[j].equals("")) {
            Float f = new Float(flostr[j]);
            i.setValue(j, f);
        }
    }

    i.setDataset(ii);

    if (hasClass) {
        Attribute clsAttrib = ii.classAttribute();
        //clsAttrib.addStringValue(flostr[j]);
        i.setValue(clsAttrib, flostr[j]);
    }

    return i;
}

From source file:PCADetector.java

License:Apache License

public Instances getInstances() {
    int numAtts = m_oriDataMatrix.size();
    if (numAtts < 0)
        return null;
    ArrayList<Attribute> atts = new ArrayList<Attribute>(numAtts);
    for (int att = 0; att < numAtts; att++) {
        atts.add(new Attribute(Integer.toString(att), att));
    }//from www. j a  v a 2 s  .c o m
    int numInstances = m_oriDataMatrix.get(0).size();
    if (numInstances <= 0)
        return null;
    Instances dataset = new Instances("MetricInstances", atts, numInstances);
    for (int inst = 0; inst < numInstances; inst++) {
        Instance newInst = new DenseInstance(numAtts);
        for (int att = 0; att < numAtts; att++) {
            newInst.setValue(att, m_oriDataMatrix.get(att).get(inst));
        }
        dataset.add(newInst);
    }
    return dataset;
}

From source file:HierarchicalClusterer.java

License:Open Source License

/** calculate the distance between two clusters 
 * @param cluster1 list of indices of instances in the first cluster
 * @param cluster2 dito for second cluster
 * @return distance between clusters based on link type
 *///from   w w  w  . ja v  a2  s  .c  o  m
double getDistance(double[][] fDistance, Vector<Integer> cluster1, Vector<Integer> cluster2) {
    double fBestDist = Double.MAX_VALUE;
    switch (m_nLinkType) {
    case SINGLE:
        // find single link distance aka minimum link, which is the closest distance between
        // any item in cluster1 and any item in cluster2
        fBestDist = Double.MAX_VALUE;
        for (int i = 0; i < cluster1.size(); i++) {
            int i1 = cluster1.elementAt(i);
            for (int j = 0; j < cluster2.size(); j++) {
                int i2 = cluster2.elementAt(j);
                double fDist = fDistance[i1][i2];
                if (fBestDist > fDist) {
                    fBestDist = fDist;
                }
            }
        }
        break;
    case COMPLETE:
    case ADJCOMLPETE:
        // find complete link distance aka maximum link, which is the largest distance between
        // any item in cluster1 and any item in cluster2
        fBestDist = 0;
        for (int i = 0; i < cluster1.size(); i++) {
            int i1 = cluster1.elementAt(i);
            for (int j = 0; j < cluster2.size(); j++) {
                int i2 = cluster2.elementAt(j);
                double fDist = fDistance[i1][i2];
                if (fBestDist < fDist) {
                    fBestDist = fDist;
                }
            }
        }
        if (m_nLinkType == COMPLETE) {
            break;
        }
        // calculate adjustment, which is the largest within cluster distance
        double fMaxDist = 0;
        for (int i = 0; i < cluster1.size(); i++) {
            int i1 = cluster1.elementAt(i);
            for (int j = i + 1; j < cluster1.size(); j++) {
                int i2 = cluster1.elementAt(j);
                double fDist = fDistance[i1][i2];
                if (fMaxDist < fDist) {
                    fMaxDist = fDist;
                }
            }
        }
        for (int i = 0; i < cluster2.size(); i++) {
            int i1 = cluster2.elementAt(i);
            for (int j = i + 1; j < cluster2.size(); j++) {
                int i2 = cluster2.elementAt(j);
                double fDist = fDistance[i1][i2];
                if (fMaxDist < fDist) {
                    fMaxDist = fDist;
                }
            }
        }
        fBestDist -= fMaxDist;
        break;
    case AVERAGE:
        // finds average distance between the elements of the two clusters
        fBestDist = 0;
        for (int i = 0; i < cluster1.size(); i++) {
            int i1 = cluster1.elementAt(i);
            for (int j = 0; j < cluster2.size(); j++) {
                int i2 = cluster2.elementAt(j);
                fBestDist += fDistance[i1][i2];
            }
        }
        fBestDist /= (cluster1.size() * cluster2.size());
        break;
    case MEAN: {
        // calculates the mean distance of a merged cluster (akak Group-average agglomerative clustering)
        Vector<Integer> merged = new Vector<Integer>();
        merged.addAll(cluster1);
        merged.addAll(cluster2);
        fBestDist = 0;
        for (int i = 0; i < merged.size(); i++) {
            int i1 = merged.elementAt(i);
            for (int j = i + 1; j < merged.size(); j++) {
                int i2 = merged.elementAt(j);
                fBestDist += fDistance[i1][i2];
            }
        }
        int n = merged.size();
        fBestDist /= (n * (n - 1.0) / 2.0);
    }
        break;
    case CENTROID:
        // finds the distance of the centroids of the clusters
        double[] fValues1 = new double[m_instances.numAttributes()];
        for (int i = 0; i < cluster1.size(); i++) {
            Instance instance = m_instances.instance(cluster1.elementAt(i));
            for (int j = 0; j < m_instances.numAttributes(); j++) {
                fValues1[j] += instance.value(j);
            }
        }
        double[] fValues2 = new double[m_instances.numAttributes()];
        for (int i = 0; i < cluster2.size(); i++) {
            Instance instance = m_instances.instance(cluster2.elementAt(i));
            for (int j = 0; j < m_instances.numAttributes(); j++) {
                fValues2[j] += instance.value(j);
            }
        }
        for (int j = 0; j < m_instances.numAttributes(); j++) {
            fValues1[j] /= cluster1.size();
            fValues2[j] /= cluster2.size();
        }
        // set up two instances for distance function
        Instance instance1 = (Instance) m_instances.instance(0).copy();
        Instance instance2 = (Instance) m_instances.instance(0).copy();
        for (int j = 0; j < m_instances.numAttributes(); j++) {
            instance1.setValue(j, fValues1[j]);
            instance2.setValue(j, fValues2[j]);
        }
        fBestDist = m_DistanceFunction.distance(instance1, instance2);
        break;
    case WARD: {
        // finds the distance of the change in caused by merging the cluster.
        // The information of a cluster is calculated as the error sum of squares of the
        // centroids of the cluster and its members.
        double ESS1 = calcESS(cluster1);
        double ESS2 = calcESS(cluster2);
        Vector<Integer> merged = new Vector<Integer>();
        merged.addAll(cluster1);
        merged.addAll(cluster2);
        double ESS = calcESS(merged);
        fBestDist = ESS * merged.size() - ESS1 * cluster1.size() - ESS2 * cluster2.size();
    }
        break;
    }
    return fBestDist;
}

From source file:HierarchicalClusterer.java

License:Open Source License

/** calculated error sum-of-squares for instances wrt centroid **/
double calcESS(Vector<Integer> cluster) {
    double[] fValues1 = new double[m_instances.numAttributes()];
    for (int i = 0; i < cluster.size(); i++) {
        Instance instance = m_instances.instance(cluster.elementAt(i));
        for (int j = 0; j < m_instances.numAttributes(); j++) {
            fValues1[j] += instance.value(j);
        }/*w w w . j  a va2s.c o  m*/
    }
    for (int j = 0; j < m_instances.numAttributes(); j++) {
        fValues1[j] /= cluster.size();
    }
    // set up two instances for distance function
    Instance centroid = (Instance) m_instances.instance(cluster.elementAt(0)).copy();
    for (int j = 0; j < m_instances.numAttributes(); j++) {
        centroid.setValue(j, fValues1[j]);
    }
    double fESS = 0;
    for (int i = 0; i < cluster.size(); i++) {
        Instance instance = m_instances.instance(cluster.elementAt(i));
        fESS += m_DistanceFunction.distance(centroid, instance);
    }
    return fESS / cluster.size();
}

From source file:adams.flow.transformer.WekaInstanceEvaluator.java

License:Open Source License

/**
 * Executes the flow item.//from   ww  w.  j av  a  2  s .co m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    Instance newInst;
    double eval;

    result = null;

    // the Instance to evaluate
    inst = (Instance) m_InputToken.getPayload();

    // obtain dataset first?
    if (m_Header == null)
        result = setUpEvaluator();

    // generate new header?
    if ((result == null) && (m_Header == null))
        result = generateHeader(inst);

    // generate evaluation
    if (result == null) {
        try {
            eval = m_Evaluator.evaluate(inst);
            if (isLoggingEnabled())
                getLogger().info("Evaluation " + eval + " for instance: " + inst);
            m_Filter.input(inst);
            m_Filter.batchFinished();
            newInst = m_Filter.output();
            newInst.setValue(newInst.dataset().attribute(m_AttributeName), eval);
            m_OutputToken = new Token(newInst);
        } catch (Exception e) {
            m_OutputToken = null;
            result = handleException("Failed to evaluate instance: " + inst, e);
        }
    }

    return result;
}

From source file:adams.flow.transformer.WekaSetInstanceValue.java

License:Open Source License

/**
 * Executes the flow item./*from www  . j av  a 2 s .c om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    int index;

    result = null;

    inst = (Instance) m_InputToken.getPayload();
    inst = (Instance) inst.copy();
    m_Index.setData(inst.dataset());
    index = m_Index.getIntIndex();

    try {
        if (m_Value.equals("?")) {
            inst.setMissing(index);
        } else {
            switch (inst.attribute(index).type()) {
            case Attribute.NUMERIC:
                inst.setValue(index, Utils.toDouble(m_Value));
                break;

            case Attribute.DATE:
                inst.setValue(index, inst.attribute(index).parseDate(m_Value));
                break;

            case Attribute.NOMINAL:
            case Attribute.STRING:
                inst.setValue(index, m_Value);
                break;

            case Attribute.RELATIONAL:
                result = "Relational attributes cannot be set!";
                break;

            default:
                result = "Unhandled attribute type: " + inst.attribute(index).type();
            }
        }
    } catch (Exception e) {
        result = handleException("Failed to set value: " + m_Index.getIndex() + " -> " + m_Value, e);
    }

    // broadcast data
    if (result == null)
        m_OutputToken = new Token(inst);

    return result;
}

From source file:adams.gui.visualization.instances.InstancesTableModel.java

License:Open Source License

/**
 * Sets the value in the cell at columnIndex and rowIndex to aValue. but only
 * the value and the value can be changed. Ignores operation if value hasn't
 * changed./*from  w  w  w. j av  a2s.  c o m*/
 *
 * @param aValue the new value
 * @param rowIndex the row index
 * @param columnIndex the column index
 * @param notify whether to notify the listeners
 */
public void setValueAt(Object aValue, int rowIndex, int columnIndex, boolean notify) {
    int type;
    int index;
    String tmp;
    Instance inst;
    Attribute att;
    Object oldValue;
    boolean different;
    int offset;

    offset = 1;
    if (m_ShowWeightsColumn)
        offset++;

    oldValue = getValueAt(rowIndex, columnIndex);
    different = !("" + oldValue).equals("" + aValue);
    if (!different)
        return;

    if (!m_IgnoreChanges)
        addUndoPoint();

    type = getType(rowIndex, columnIndex);
    index = columnIndex - offset;
    inst = m_Data.instance(rowIndex);
    att = inst.attribute(index);

    // missing?
    if (aValue == null) {
        inst.setValue(index, Utils.missingValue());
    } else {
        tmp = aValue.toString();

        switch (type) {
        case Attribute.DATE:
            try {
                att.parseDate(tmp);
                inst.setValue(index, att.parseDate(tmp));
            } catch (Exception e) {
                // ignore
            }
            break;

        case Attribute.NOMINAL:
            if (att.indexOfValue(tmp) > -1)
                inst.setValue(index, att.indexOfValue(tmp));
            break;

        case Attribute.STRING:
            inst.setValue(index, tmp);
            break;

        case Attribute.NUMERIC:
            try {
                inst.setValue(index, Double.parseDouble(tmp));
            } catch (Exception e) {
                // ignore
            }
            break;

        case Attribute.RELATIONAL:
            try {
                inst.setValue(index, inst.attribute(index).addRelation((Instances) aValue));
            } catch (Exception e) {
                // ignore
            }
            break;

        default:
            throw new IllegalArgumentException("Unsupported Attribute type: " + type + "!");
        }
    }

    // notify only if the value has changed!
    if (notify)
        notifyListener(new TableModelEvent(this, rowIndex, columnIndex));
}