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:milk.visualize.PlotPanel.java

License:Open Source License

/**
 * Draws the distribution/*from w ww  .j a  va 2s .  c  om*/
 * @param gx the graphics context
 */
private void paintData(Graphics gx) {
    if ((plotExemplars == null) || (x == null))
        return;

    setFonts(gx);
    int w = this.getWidth();
    boolean[] xlabels = new boolean[w];

    Attribute classAtt = plotExemplars.classAttribute(), id = plotExemplars.idAttribute();
    int hf = m_labelMetrics.getAscent();

    for (int i = 0; i < plotExemplars.numExemplars(); i++) {
        Exemplar ex = plotExemplars.exemplar(i);
        if (classAtt.isNominal())
            gx.setColor((Color) colorList.elementAt((int) ex.classValue()));
        else {
            double r = (ex.classValue() - m_minC) / (m_maxC - m_minC);
            r = (r * 240) + 15;
            gx.setColor(new Color((int) r, 150, (int) (255 - r)));
        }

        double preY = 0;
        for (int j = 0; j < ex.getInstances().numInstances(); j++) {
            Instance ins = ex.getInstances().instance(j);
            double xValue = convertToAttribX(m_XaxisStart);
            double tmp = -(xValue - ins.value(x.index)) * (xValue - ins.value(x.index))
                    / (2.0 * stdDev * stdDev);
            preY += Math.exp(tmp) * ins.weight();
        }
        preY /= ex.getInstances().sumOfWeights();
        preY *= maxY;

        for (int k = m_XaxisStart + 1; k < m_XaxisEnd; k++) {
            double currY = 0;
            for (int l = 0; l < ex.getInstances().numInstances(); l++) {
                Instance ins = ex.getInstances().instance(l);
                double xValue = convertToAttribX(k);
                double tmp = -(xValue - ins.value(x.index)) * (xValue - ins.value(x.index))
                        / (2.0 * stdDev * stdDev);
                currY += Math.exp(tmp) * ins.weight();
            }
            currY /= ex.getInstances().sumOfWeights();
            currY *= maxY;

            // Draw the distribution         
            int plotPreY = (int) convertToPanelY(preY);
            int plotCurrY = (int) convertToPanelY(currY);
            gx.drawLine(k - 1, plotPreY, k, plotCurrY);

            // If peak or valley appears, specify the x value
            if (isUp && (preY > currY)) {
                Font old = gx.getFont();
                gx.setFont(new Font("Monospaced", Font.PLAIN, 10));
                String idvalue = Integer.toString((int) ex.idValue());
                gx.drawString(idvalue, k - 1 - m_labelMetrics.stringWidth(idvalue) / 2, plotCurrY);
                xlabels[k - 1] = true;
                gx.setFont(old);
            }
            isUp = (currY >= preY);
            preY = currY;
        }
    }

    // Draw the number labels on x axis where various peaks gather
    gx.setColor(m_axisColour);
    int start = 0, end = 0;
    while (start < w) {
        if (xlabels[start]) {
            end = start;
            int falseCount = 0;
            while (end < w) {
                while (xlabels[end++])
                    ; // Find the first false from start
                int m = end;
                // Count the number of falses
                for (falseCount = 0; (m < xlabels.length) && (!xlabels[m]); m++, falseCount++)
                    ;

                if ((falseCount < 28) && (m < xlabels.length))
                    end = m;
                else
                    break;
            }
            if (!xlabels[end])
                --end;

            int avg = (start + end) / 2;
            double xValue = convertToAttribX(avg);
            String stringX = Utils.doubleToString(xValue, 1);
            Font old = gx.getFont();
            gx.setFont(new Font("Monospaced", Font.PLAIN, 10));
            gx.drawString(stringX, avg - m_labelMetrics.stringWidth(stringX) / 2, m_YaxisEnd + hf + m_tickSize);
            gx.drawLine(avg, m_YaxisEnd, avg, m_YaxisEnd + m_tickSize);
            gx.setFont(old);
            start = end;
        } else
            ++start;
    }
}

From source file:minorpro.BitsUtility.java

public static String convertToBits(Instance ins) {
    String bits = "";
    for (int i = 0; i < ins.numAttributes() - 1; i++) {
        double d = 0, a = ins.value(i);
        switch (i) {
        case 0:/*from  www.  j ava  2s.  c  o m*/
            d = (a + 7) / 2;
            break;
        case 1:
            d = a - 4;
            break;
        case 2:
        case 3:
            d = a;
            break;
        }

        bits = bits + intToBits(d);
    }
    return bits;
}

From source file:ml.ann.BackPropagation.java

License:Open Source License

private void initInputAndTarget(Instance instance) {
    int classAttributeIdx = neuronTopology.instances.classIndex();
    if (neuronTopology.instances.classAttribute().isNumeric()) {
        neuronTopology.target = new double[1];
        neuronTopology.target[0] = instance.value(classAttributeIdx);
    } else if (neuronTopology.instances.classAttribute().isNominal()) {
        neuronTopology.target = new double[instance.numClasses()];
        for (int i = 0; i < instance.numClasses(); i++) {
            neuronTopology.target[i] = 0;
        }//from  www.  j  av  a  2  s. co  m
        int idxClassValue = (int) instance.classValue();
        neuronTopology.target[idxClassValue] = 1;
    }
    for (int i = 0; i < instance.numAttributes(); i++) {
        if (i == 0) {
            neuronTopology.input[i] = 1;
            neuronTopology.output[0][i] = 1;
        } else {
            neuronTopology.input[i] = instance.value(i - 1);
            neuronTopology.output[0][i] = instance.value(i - 1);
        }
    }
    //        System.out.println(neuronTopology.originInstances.instance(instancesIdx).toString());
}

From source file:ml.ann.BackPropagation.java

License:Open Source License

@Override
public double[] distributionForInstance(Instance instance) throws Exception {

    Instance currentInstance;
    // default model?
    if (useDefaultModel) {
        return zeroR.distributionForInstance(instance);
    }/*w  ww  . j  a  v  a 2 s  .c o m*/

    // Make a copy of the instance so that it isn't modified
    currentInstance = (Instance) instance.copy();

    for (int noa = 0; noa < currentInstance.numAttributes(); noa++) {
        if (noa != neuronTopology.instances.classIndex()) {
            if (attributeRanges[noa] != 0) {
                currentInstance.setValue(noa,
                        (currentInstance.value(noa) - attributeBases[noa]) / attributeRanges[noa]);
            } else {
                currentInstance.setValue(noa, currentInstance.value(noa) - attributeBases[noa]);
            }
        }
    }
    //        resetNetwork();

    //        initInputAndTarget(currentInstance);

    int lastLayerIdx = neuronTopology.numLayers - 1;
    for (int layers = 0; layers < neuronTopology.numLayers; layers++) {
        for (int neu = 0; neu < neuronTopology.numNeuronEachLayer[layers]; neu++) {
            neuronTopology.output[layers][neu] = sigmoidFunction(layers, neu);
        }
    }

    double[] jancuk = new double[neuronTopology.numNeuronEachLayer[lastLayerIdx]];

    for (int i = 0; i < neuronTopology.numNeuronEachLayer[lastLayerIdx]; i++) {
        jancuk[i] = neuronTopology.output[lastLayerIdx][i];
    }

    // now normalize the array
    double count = 0;
    for (int noa = 0; noa < neuronTopology.numClasses; noa++) {
        count += jancuk[noa];
    }
    if (count <= 0) {
        //            return zeroR.distributionForInstance(i);
    }
    for (int noa = 0; noa < neuronTopology.numClasses; noa++) {
        jancuk[noa] /= count;
    }
    return jancuk;
}

From source file:ml.ann.MultiClassPTR.java

@Override
public void buildClassifier(Instances instances) throws Exception {
    initAttributes(instances);/*w w  w.  j a va2  s  .  co m*/

    // REMEMBER: only works if class index is in the last position
    for (int instanceIdx = 0; instanceIdx < instances.numInstances(); instanceIdx++) {
        Instance instance = instances.get(instanceIdx);
        double[] inputInstance = inputInstances[instanceIdx];
        inputInstance[0] = 1.0; // initialize bias value
        for (int attrIdx = 0; attrIdx < instance.numAttributes() - 1; attrIdx++) {
            inputInstance[attrIdx + 1] = instance.value(attrIdx); // the first index of input instance is for bias
        }
    }

    // Initialize target values
    if (instances.classAttribute().isNominal()) {
        for (int instanceIdx = 0; instanceIdx < instances.numInstances(); instanceIdx++) {
            Instance instance = instances.instance(instanceIdx);
            for (int classIdx = 0; classIdx < instances.numClasses(); classIdx++) {
                targetInstances[instanceIdx][classIdx] = 0.0;
            }
            targetInstances[instanceIdx][(int) instance.classValue()] = 1.0;
        }
    } else {
        for (int instanceIdx = 0; instanceIdx < instances.numInstances(); instanceIdx++) {
            Instance instance = instances.instance(instanceIdx);
            targetInstances[instanceIdx][0] = instance.classValue();
        }
    }

    if (algo == 1) {
        setActFunction();
        buildClassifier();
    } else if (algo == 2) {
        buildClassifier();
    } else if (algo == 3) {
        buildClassifierBatch();
    }
}

From source file:ml.ann.MultiClassPTR.java

@Override
public double[] distributionForInstance(Instance instance) throws Exception {
    double[] input = new double[numInput]; // remember the first index use for bias input
    input[0] = 1.0;/*from w  w  w. j  a v a2s.c  o m*/
    for (int attrIdx = 0; attrIdx < instance.numAttributes() - 1; attrIdx++) { // we ignore the class value
        input[attrIdx + 1] = instance.value(attrIdx);
    }

    double[] output = new double[numOutput];
    double totalVal = 0.0;
    System.out.println("=========================");
    for (int outputIdx = 0; outputIdx < numOutput; outputIdx++) {
        output[outputIdx] = 0.0;
        for (int inputIdx = 0; inputIdx < numInput; inputIdx++) {
            output[outputIdx] += input[inputIdx] * weight[inputIdx][outputIdx];
        }
        output[outputIdx] = actFunction(output[outputIdx]);
        System.out.print(output[outputIdx] + " ");
        totalVal += Math.exp(output[outputIdx]);
    }
    for (int out = 0; out < numOutput; out++) {
        output[out] = Math.exp(output[out]) / totalVal;
    }
    return output;
}

From source file:mlda.attributes.MeanKurtosis.java

License:Open Source License

/**
 * Calculate metric value//  ww w  .j a v  a 2s  .c o m
 * 
 * @param mlData Multi-label dataset to which calculate the metric
 * @return Value of the metric
 */
public double calculate(MultiLabelInstances mlData) {
    Instances instances = mlData.getDataSet();
    int nInstances = mlData.getNumInstances();

    double avg;
    double var2;
    double var4;
    double val;
    int nNumeric = 0;
    double mean = 0;

    Set<Attribute> attributesSet = mlData.getFeatureAttributes();

    for (Attribute att : attributesSet) {
        if (att.isNumeric()) {
            nNumeric++;
            avg = instances.meanOrMode(att);
            var2 = 0;
            var4 = 0;

            for (Instance inst : instances) {
                val = inst.value(att);
                var2 += Math.pow(val - avg, 2);
                var4 += Math.pow(val - avg, 4);
            }

            double kurtosis = (nInstances * var4 / Math.pow(var2, 2)) - 3;
            double sampleKurtosis = (kurtosis * (nInstances + 1) + 6) * (nInstances - 1)
                    / ((nInstances - 2) * (nInstances - 3));
            mean += sampleKurtosis;
        }
    }
    if (nNumeric > 0) {
        mean = mean / nNumeric;
    } else {
        mean = Double.NaN;
    }

    this.value = mean;
    return value;
}

From source file:mlda.attributes.MeanSkewnessNumericAttributes.java

License:Open Source License

/**
 * Calculate metric value// www .j  a  va2s .  c  om
 * 
 * @param mlData Multi-label dataset to which calculate the metric
 * @return Value of the metric
 */
public double calculate(MultiLabelInstances mlData) {
    Instances instances = mlData.getDataSet();
    int nInstances = mlData.getNumInstances();

    Set<Attribute> attributesSet = mlData.getFeatureAttributes();

    int nNumeric = 0;
    double mean = 0;
    double avg;
    double var;
    double stdev;

    for (Attribute att : attributesSet) {
        if (att.isNumeric()) {
            nNumeric++;
            avg = instances.meanOrMode(att);
            var = 0;
            for (Instance inst : instances) {
                var += Math.pow(inst.value(att) - avg, 3);
            }
            stdev = Math.sqrt(instances.variance(att));
            mean += nInstances * var / ((nInstances - 1) * (nInstances - 2) * Math.pow(stdev, 3));
        }
    }

    if (nNumeric > 0) {
        this.value = mean / nNumeric;
    } else {
        this.value = Double.NaN;
    }

    //this.value = mean;
    return value;
}

From source file:mlda.labelsRelation.SCUMBLE.java

License:Open Source License

/**
 * Calculate metric value//from w w w.j a  va  2 s .c  om
 * 
 * @param mlData Multi-label dataset to which calculate the metric
 * @return Value of the metric
 */
public double calculate(MultiLabelInstances mlData) {

    double SCUMBLE = 0.0;

    //        ImbalancedFeature [] imbalanced_data =  Utils.getImbalancedWithIR(mlData, Utils.getSortedByFrequency(Utils.getAppearancesPerLabel(mlData)));
    //        
    //        ImbalancedFeature[] new_imbalanced_data = new ImbalancedFeature[imbalanced_data.length];
    //        
    //        for(int i=0; i<imbalanced_data.length; i++){
    //            for(int j=0; j<imbalanced_data.length; j++){
    //                if(mlData.getLabelNames()[i].equals(imbalanced_data[j].getName())){
    //                    new_imbalanced_data[i] = imbalanced_data[j];
    //                }
    //            }
    //        }

    double[] ir = getIRperLabel(mlData);

    int nLabels = mlData.getNumLabels();
    Instances instances = mlData.getDataSet();
    double IRLmean = 0;
    int nActive = 0;
    double prod = 1;
    double sum = 0;

    int[] labelIndices = mlData.getLabelIndices();

    for (Instance inst : instances) {
        IRLmean = 0;
        prod = 1;
        nActive = 0;

        for (int l = 0; l < nLabels; l++) {
            if (inst.value(labelIndices[l]) == 1) {
                prod *= ir[l];
                IRLmean += ir[l];
                nActive++;
            }
            //              else{
            //                 prod *= 0;
            //              }              
        }

        if (nActive == 0) {
            sum += 0;
        } else {
            IRLmean /= nActive;

            //               System.out.println(IRLmean);

            sum += 1 - (Math.pow(prod, 1.0 / nActive) / IRLmean);
        }

    }

    SCUMBLE = sum / mlData.getNumInstances();

    this.value = SCUMBLE;
    return value;
}

From source file:mlda.labelsRelation.SCUMBLE.java

License:Open Source License

public int[] getAppearances(MultiLabelInstances mlData) {
    int[] appearances = new int[mlData.getNumLabels()];
    int[] labelIndices = mlData.getLabelIndices();

    for (Instance instance : mlData.getDataSet()) {
        for (int label = 0; label < mlData.getNumLabels(); label++) {
            if (instance.value(labelIndices[label]) == 1) {
                appearances[label]++;/*from w  ww .  j  a v a2s  .c  om*/
            }
        }
    }

    return appearances;
}