Example usage for weka.core Instance value

List of usage examples for weka.core Instance value

Introduction

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

Prototype

public double value(Attribute att);

Source Link

Document

Returns an instance's attribute value in internal format.

Usage

From source file:cn.edu.xjtu.dbmine.source.NaiveBayes.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test 
 * instance./* w ww  .  ja  va 2s . co m*/
 *
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 * @exception Exception if there is a problem generating the prediction
 */
public double[] distributionForInstance(Instance instance) throws Exception {

    if (m_UseDiscretization) {
        m_Disc.input(instance);
        instance = m_Disc.output();
    }
    double[] probs = new double[m_NumClasses];
    for (int j = 0; j < m_NumClasses; j++) {
        probs[j] = m_ClassDistribution.getProbability(j);
    }
    Enumeration enumAtts = instance.enumerateAttributes();
    int attIndex = 0;
    while (enumAtts.hasMoreElements()) {
        Attribute attribute = (Attribute) enumAtts.nextElement();
        if (!instance.isMissing(attribute)) {
            double temp, max = 0;
            for (int j = 0; j < m_NumClasses; j++) {
                temp = Math.max(1e-75,
                        Math.pow(m_Distributions[attIndex][j].getProbability(instance.value(attribute)),
                                m_Instances.attribute(attIndex).weight()));
                probs[j] *= temp;
                if (probs[j] > max) {
                    max = probs[j];
                }
                if (Double.isNaN(probs[j])) {
                    throw new Exception("NaN returned from estimator for attribute " + attribute.name() + ":\n"
                            + m_Distributions[attIndex][j].toString());
                }
            }
            if ((max > 0) && (max < 1e-75)) { // Danger of probability underflow
                for (int j = 0; j < m_NumClasses; j++) {
                    probs[j] *= 1e75;
                }
            }
        }
        attIndex++;
    }

    // Display probabilities
    Utils.normalize(probs);
    return probs;
}

From source file:cn.edu.xjtu.dbmine.StringToWordVector.java

License:Open Source License

/**
 * Converts the instance w/o normalization.
 * //from   w ww.j av  a2  s. com
 * @oaram instance the instance to convert
 * @param v
 * @return the conerted instance
 */
private int convertInstancewoDocNorm(Instance instance, FastVector v) {

    // Convert the instance into a sorted set of indexes
    TreeMap contained = new TreeMap();

    // Copy all non-converted attributes from input to output
    int firstCopy = 0;
    for (int i = 0; i < getInputFormat().numAttributes(); i++) {
        if (!m_SelectedRange.isInRange(i)) {
            if (getInputFormat().attribute(i).type() != Attribute.STRING) {
                // Add simple nominal and numeric attributes directly
                if (instance.value(i) != 0.0) {
                    contained.put(new Integer(firstCopy), new Double(instance.value(i)));
                }
            } else {
                if (instance.isMissing(i)) {
                    contained.put(new Integer(firstCopy), new Double(Instance.missingValue()));
                } else {

                    // If this is a string attribute, we have to first add
                    // this value to the range of possible values, then add
                    // its new internal index.
                    if (outputFormatPeek().attribute(firstCopy).numValues() == 0) {
                        // Note that the first string value in a
                        // SparseInstance doesn't get printed.
                        outputFormatPeek().attribute(firstCopy)
                                .addStringValue("Hack to defeat SparseInstance bug");
                    }
                    int newIndex = outputFormatPeek().attribute(firstCopy)
                            .addStringValue(instance.stringValue(i));
                    contained.put(new Integer(firstCopy), new Double(newIndex));
                }
            }
            firstCopy++;
        }
    }

    for (int j = 0; j < instance.numAttributes(); j++) {
        // if ((getInputFormat().attribute(j).type() == Attribute.STRING)
        if (m_SelectedRange.isInRange(j) && (instance.isMissing(j) == false)) {

            m_Tokenizer.tokenize(instance.stringValue(j));

            while (m_Tokenizer.hasMoreElements()) {
                String word = (String) m_Tokenizer.nextElement();
                if (this.m_lowerCaseTokens == true)
                    word = word.toLowerCase();
                word = m_Stemmer.stem(word);
                Integer index = (Integer) m_Dictionary.get(word);
                if (index != null) {
                    if (m_OutputCounts) { // Separate if here rather than
                        // two lines down to avoid
                        // hashtable lookup
                        Double count = (Double) contained.get(index);
                        if (count != null) {
                            contained.put(index, new Double(count.doubleValue() + 1.0));
                        } else {
                            contained.put(index, new Double(1));
                        }
                    } else {
                        contained.put(index, new Double(1));
                    }
                }
            }
        }
    }

    // Doing TFTransform
    if (m_TFTransform == true) {
        Iterator it = contained.keySet().iterator();
        for (int i = 0; it.hasNext(); i++) {
            Integer index = (Integer) it.next();
            if (index.intValue() >= firstCopy) {
                double val = ((Double) contained.get(index)).doubleValue();
                val = Math.log(val + 1);
                contained.put(index, new Double(val));
                Tfcontained.put(index, new Double(val));
                ;
            }
        }
    }

    // Doing IDFTransform
    if (m_IDFTransform == true) {
        Iterator it = contained.keySet().iterator();
        for (int i = 0; it.hasNext(); i++) {
            Integer index = (Integer) it.next();
            if (index.intValue() >= firstCopy) {
                double val = ((Double) contained.get(index)).doubleValue();
                val = val * Math.log(m_NumInstances / ((double) m_DocsCounts[index.intValue()] + 0.01));
                contained.put(index, new Double(val));
            }
        }
    }

    // Convert the set to structures needed to create a sparse instance.
    double[] values = new double[contained.size()];
    int[] indices = new int[contained.size()];
    Iterator it = contained.keySet().iterator();
    for (int i = 0; it.hasNext(); i++) {
        Integer index = (Integer) it.next();
        Double value = (Double) contained.get(index);
        values[i] = value.doubleValue();
        indices[i] = index.intValue();
    }

    Instance inst = new SparseInstance(instance.weight(), values, indices, outputFormatPeek().numAttributes());
    inst.setDataset(outputFormatPeek());

    v.addElement(inst);

    return firstCopy;
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

protected Instances runExp(Instances samplePoints, int round, String postfix, boolean resuming) {
    Instances retval = null;// w w  w  .  ja  va  2  s  .c  o  m
    try {
        //DataIOFile.saveDataToArffFile("data/zyqTestRange.arff", samplePoints);

        if (resuming) {
            samplePoints = manager.collectPerfs(samplePoints, perfAttName);
        }

        retval = manager.runExp(samplePoints, perfAttName);
        //we output the result set for future debugging and testing purposes
        DataIOFile.saveDataToArffFile("data/trainingBestConf" + round + "_" + postfix + ".arff", samplePoints);

        //evict all bad configurations
        Attribute perfAtt = retval.attribute(perfAttName);
        Iterator<Instance> itr = retval.iterator();
        ArrayList<Integer> toRemove = new ArrayList<Integer>();
        Instance next;
        while (itr.hasNext()) {
            next = itr.next();
            if (next.value(perfAtt) == -1)
                toRemove.add(retval.indexOf(next));
        }
        while (!toRemove.isEmpty())
            retval.remove(toRemove.remove(0));
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (allInstances == null) {
        allInstances = new Instances(retval);
    } else {
        allInstances.addAll(retval);
    }

    return retval;
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static ArrayList<Attribute> scaleDownDetour(Instances previousSet, Instance center) {
    ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
    int attNum = center.numAttributes();

    int pos = previousSet.attribute(PerformanceAttName).index();

    //traverse each dimension
    Enumeration<Instance> enu;
    double minDis;
    for (int i = 0; i < attNum; i++) {
        if (i == pos)
            continue;

        enu = previousSet.enumerateInstances();
        minDis = Double.MAX_VALUE;

        while (enu.hasMoreElements()) {
            Instance ins = enu.nextElement();
            if (!ins.equals(center))
                minDis = Math.min((double) ((int) (Math.abs(ins.value(i) - center.value(i)) * 100)) / 100.0,
                        minDis);/*  w ww  .  jav  a 2  s  .co m*/
        }

        //now we set the range
        Properties p1 = new Properties();
        double upper = center.value(i) + minDis, lower = center.value(i) - minDis;

        TreeSet<Double> detourSet = new TreeSet<Double>();
        detourSet.add(upper);
        detourSet.add(lower);
        detourSet.add(previousSet.attribute(i).getUpperNumericBound());
        detourSet.add(previousSet.attribute(i).getLowerNumericBound());
        switch (detourSet.size()) {
        case 1:
            upper = lower = detourSet.first();
            break;
        case 2:
            upper = detourSet.last();
            lower = detourSet.first();
            break;
        case 3:
            upper = lower = detourSet.higher(detourSet.first());
            break;
        default://case 4:
            upper = detourSet.lower(detourSet.last());
            lower = detourSet.higher(detourSet.first());
            break;
        }

        p1.setProperty("range", "[" + String.valueOf(lower) + "," + String.valueOf(upper) + "]");
        ProtectedProperties prop1 = new ProtectedProperties(p1);

        localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
    }

    return localAtts;
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static Map<Attribute, Double> instanceToMap(Instance ins) {
    HashMap<Attribute, Double> retval = new HashMap<Attribute, Double>();
    Enumeration<Attribute> enu = ins.enumerateAttributes();
    while (enu.hasMoreElements()) {
        Attribute temp = enu.nextElement();
        retval.put(temp, ins.value(temp));
    }/* w  w w  .ja  v  a2  s . co  m*/
    return retval;
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static void getBestPerfFrom(String path) {
    try {/*from   ww  w .j  av  a  2  s  .c  om*/
        BestConf bestconf = new BestConf();
        Instances trainingSet = DataIOFile.loadDataFromArffFile(path);
        Instance best = trainingSet.firstInstance();
        //set the best configuration to the cluster
        Map<Attribute, Double> attsmap = new HashMap<Attribute, Double>();
        for (int i = 0; i < best.numAttributes() - 1; i++) {
            attsmap.put(best.attribute(i), best.value(i));
        }

        double bestPerf = bestconf.setOptimal(attsmap, "getBestPerfFrom");
        System.out.println("=========================================");
        System.err.println("The actual performance for the best point is : " + bestPerf);
        System.out.println("=========================================");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cn.ict.zyq.bestConf.bestConf.RBSoDDSOptimization.java

License:Open Source License

@Override
public void optimize(String preLoadDatasetPath) {
    ResumeParams rParams = resumePrepareTry();
    boolean justAfterResume = rParams.isResuming;

    //detect whether we need to resume
    if (rParams.isResuming)
        preLoadDatasetPath = null;/*from  w w  w. j  a  v  a  2 s  .c  o m*/

    double tempBest;

    while (opParams.currentround < RRSMaxRounds) {
        //is it a global search
        if (samplePoints == null || rParams.propsRound < opParams.currentround) {
            props = bestconf.getAttributes();
            saveProps(props, opParams.currentround, opParams.subround);//for resumability
            opParams.saveToFile();
        }

        if (opParams.currentround != 0 || opParams.subround != 0) {
            if (!justAfterResume || (justAfterResume && (rParams.samplePointRound < opParams.currentround
                    || rParams.samplePointSubRound < opParams.subround))) {
                //let's do the sampling
                ((DDSSampler) sampler).setCurrentRound(opParams.currentround);
                samplePoints = sampler.getMultiDimContinuous(props, InitialSampleSetSize, false, bestconf);
                saveSamplePoints(samplePoints, opParams.currentround, opParams.subround);
            }

            if (!justAfterResume || (justAfterResume && rParams.trainingRound < opParams.currentround
                    || rParams.trainingSubRound < opParams.subround)) {
                //traverse the set and initiate the experiments
                trainingSet = bestconf.runExp(samplePoints, opParams.currentround,
                        "RRS" + String.valueOf(opParams.subround), justAfterResume);
                saveTrainingSet(trainingSet, opParams.currentround, opParams.subround);
            }
        } else {//(currentround==0 && subround==0)
            if (preLoadDatasetPath == null) {

                if (samplePoints == null) {
                    //let's do the sampling
                    ((DDSSampler) sampler).setCurrentRound(opParams.currentround);
                    samplePoints = sampler.getMultiDimContinuous(props, InitialSampleSetSize, false, bestconf);
                    samplePoints.add(0, bestconf.defltSettings.firstInstance());
                    saveSamplePoints(samplePoints, opParams.currentround, opParams.subround);
                }
                if (trainingSet == null) {
                    //traverse the set and initiate the experiments
                    trainingSet = bestconf.runExp(samplePoints, opParams.currentround,
                            "RRS" + String.valueOf(opParams.subround), justAfterResume);
                    saveTrainingSet(trainingSet, opParams.currentround, opParams.subround);
                }
            } else {
                try {
                    bestconf.allInstances = DataIOFile.loadDataFromArffFile(preLoadDatasetPath);
                    bestconf.allInstances.setClassIndex(bestconf.allInstances.numAttributes() - 1);
                    samplePoints = trainingSet = new Instances(bestconf.allInstances);

                    saveSamplePoints(samplePoints, opParams.currentround, opParams.subround);
                    saveTrainingSet(trainingSet, opParams.currentround, opParams.subround);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //get the point with the best performance
        Instance tempIns = BestConf.findBestPerf(trainingSet);
        tempBest = tempIns.value(trainingSet.numAttributes() - 1);
        if (tempBest > opParams.currentBest || (justAfterResume && tempBest == opParams.currentBest
                && (rParams.propsRound < opParams.currentround || rParams.propsSubRound < opParams.subround))) {
            System.err.println("Previous best is " + opParams.currentBest + "; Current best is " + tempBest);

            opParams.currentBest = tempBest;
            opParams.currentIns = tempIns;
            opParams.saveToFile();

            try {
                //output the best instance of this round
                Instances bestInstances = new Instances(samplePoints, 1);
                bestInstances.add(opParams.currentIns);
                DataIOFile.saveDataToArffFile("data/trainingBestConf_RRS_" + opParams.currentround + "_"
                        + opParams.subround + "_" + opParams.currentBest + ".arff", bestInstances);
            } catch (IOException e) {
                e.printStackTrace();
            }

            //let's search locally
            if (!justAfterResume || (justAfterResume && rParams.propsRound < opParams.currentround
                    || rParams.propsSubRound < opParams.subround)) {
                props = ConfigSampler.scaleDownDetour(trainingSet, tempIns);
                saveProps(props, opParams.currentround, opParams.subround);//for resumability
            }

            opParams.subround++;
            opParams.saveToFile();
        } else {//let's do the restart
            samplePoints = null;

            opParams.currentround++;
            opParams.subround = 0;
            opParams.saveToFile();

            System.err.println("Entering into round " + opParams.currentround);
            /*if(opParams.currentround>=RRSMaxRounds)
               break;*/
        }

        justAfterResume = false;
    } //RRS search

    System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    System.err.println("We are ending the optimization experiments!");
    System.err.println("Please wait and don't shutdown!");
    System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

    //output the best
    Map<Attribute, Double> attsmap = BestConf.instanceToMap(opParams.currentIns);
    System.out.println(attsmap.toString());

    //set the best configuration to the cluster
    System.err.println("The best performance is : " + opParams.currentBest);

    System.out.println("=========================================");
    TxtFileOperation.writeToFile("bestConfOutput_RRS", attsmap.toString() + "\n");

    System.out.println("=========================================");

    //output the whole trainings dataset
    try {
        DataIOFile.saveDataToArffFile("data/trainingAllRSS.arff", bestconf.allInstances);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cn.ict.zyq.bestConf.bestConf.sampler.ConfigSampler.java

License:Open Source License

private static ArrayList<Attribute> scaleDownNeighbordists(Instances previousSet, Instance center) {
    ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
    int attNum = center.numAttributes();

    int pos = -1;
    if (previousSet.attribute(PerformanceAttName) != null)
        pos = previousSet.attribute(PerformanceAttName).index();

    //traverse each dimension
    Enumeration<Instance> enu;
    double[] minDists = new double[2];
    double val;
    for (int i = 0; i < attNum; i++) {
        if (i == pos)
            continue;

        enu = previousSet.enumerateInstances();
        minDists[0] = 1 - Double.MAX_VALUE;
        minDists[1] = Double.MAX_VALUE;

        while (enu.hasMoreElements()) {
            Instance ins = enu.nextElement();
            if (!ins.equals(center)) {
                val = ins.value(i) - center.value(i);
                if (val < 0)
                    minDists[0] = Math.max((double) ((int) ((ins.value(i) - center.value(i)) * 1000)) / 1000.0,
                            minDists[0]);
                else
                    minDists[1] = Math.min((double) ((int) ((ins.value(i) - center.value(i)) * 1000)) / 1000.0,
                            minDists[1]);
            }//from  w  w w.j av  a2s  .  c om
        }

        //now we set the range
        Properties p1 = new Properties();
        double upper = center.value(i) + minDists[1], lower = center.value(i) + minDists[0];

        TreeSet<Double> detourSet = new TreeSet<Double>();
        detourSet.add(upper);
        detourSet.add(lower);
        detourSet.add(previousSet.attribute(i).getUpperNumericBound());
        detourSet.add(previousSet.attribute(i).getLowerNumericBound());
        switch (detourSet.size()) {
        case 1:
            upper = lower = detourSet.first();
            break;
        case 2:
            upper = detourSet.last();
            lower = detourSet.first();
            break;
        case 3:
            upper = lower = detourSet.higher(detourSet.first());
            break;
        default://case 4:
            upper = detourSet.lower(detourSet.last());
            lower = detourSet.higher(detourSet.first());
            break;
        }

        p1.setProperty("range", "[" + String.valueOf(lower) + "," + String.valueOf(upper) + "]");
        ProtectedProperties prop1 = new ProtectedProperties(p1);

        localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
    }

    return localAtts;
}

From source file:cn.ict.zyq.bestConf.bestConf.sampler.ConfigSampler.java

License:Open Source License

private static ArrayList<Attribute> scaleDownMindists(Instances previousSet, Instance center) {
    ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
    int attNum = center.numAttributes();

    int pos = previousSet.attribute(PerformanceAttName).index();

    //traverse each dimension
    Enumeration<Instance> enu;
    double minDis;
    for (int i = 0; i < attNum; i++) {
        if (i == pos)
            continue;

        enu = previousSet.enumerateInstances();
        minDis = Double.MAX_VALUE;

        while (enu.hasMoreElements()) {
            Instance ins = enu.nextElement();
            if (!ins.equals(center))
                minDis = Math.min((double) ((int) (Math.abs(ins.value(i) - center.value(i)) * 1000)) / 1000.0,
                        minDis);//from  www .j  a  v a 2s  .  c o  m
        }

        //now we set the range
        Properties p1 = new Properties();
        double upper = center.value(i) + minDis, lower = center.value(i) - minDis;

        TreeSet<Double> detourSet = new TreeSet<Double>();
        detourSet.add(upper);
        detourSet.add(lower);
        detourSet.add(previousSet.attribute(i).getUpperNumericBound());
        detourSet.add(previousSet.attribute(i).getLowerNumericBound());
        switch (detourSet.size()) {
        case 1:
            upper = lower = detourSet.first();
            break;
        case 2:
            upper = detourSet.last();
            lower = detourSet.first();
            break;
        case 3:
            upper = lower = detourSet.higher(detourSet.first());
            break;
        default://case 4:
            upper = detourSet.lower(detourSet.last());
            lower = detourSet.higher(detourSet.first());
            break;
        }

        p1.setProperty("range", "[" + String.valueOf(lower) + "," + String.valueOf(upper) + "]");
        ProtectedProperties prop1 = new ProtectedProperties(p1);

        localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
    }

    return localAtts;
}

From source file:cn.ict.zyq.bestConf.cluster.Main.AutoTestAdjust.java

License:Open Source License

public static String getMD5(Instance ins) {
    StringBuffer name = new StringBuffer("");
    for (int i = 0; i < ins.numAttributes() - 2; i++) {
        name.append(Math.round(ins.value(ins.attribute(i))) + ",");
    }/*from   w  ww.  j  a  va2s  .  c om*/
    return getMD5(name.toString());
}