Example usage for weka.core Instance numAttributes

List of usage examples for weka.core Instance numAttributes

Introduction

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

Prototype

public int numAttributes();

Source Link

Document

Returns the number of attributes.

Usage

From source file:WLSVM.java

License:Open Source License

/**
 * Converts an ARFF Instance into a string in the sparse format accepted by
 * LIBSVM/* w  w  w  . j  av a  2s. co m*/
 * 
 * @param instance
 * @return
 */
protected String InstanceToSparse(Instance instance) {
    String line = new String();
    int c = (int) instance.classValue();
    if (c == 0)
        c = -1;
    line = c + " ";
    for (int j = 1; j < instance.numAttributes(); j++) {
        if (j - 1 == instance.classIndex()) {
            continue;
        }
        if (instance.isMissing(j - 1))
            continue;
        if (instance.value(j - 1) != 0)
            line += " " + j + ":" + instance.value(j - 1);
    }
    // System.out.println(line); 
    return (line + "\n");
}

From source file:PrincipalComponents.java

License:Open Source License

/**
 * Convert a pc transformed instance back to the original space
 *
 * @param inst the instance to convert/*from  www. j  av a  2 s.c  o  m*/
 * @return the processed instance
 * @throws Exception if something goes wrong
 */
private Instance convertInstanceToOriginal(Instance inst) throws Exception {
    double[] newVals = null;

    if (m_hasClass) {
        newVals = new double[m_numAttribs + 1];
    } else {
        newVals = new double[m_numAttribs];
    }

    if (m_hasClass) {
        // class is always appended as the last attribute
        newVals[m_numAttribs] = inst.value(inst.numAttributes() - 1);
    }

    for (int i = 0; i < m_eTranspose[0].length; i++) {
        double tempval = 0.0;
        for (int j = 1; j < m_eTranspose.length; j++) {
            tempval += (m_eTranspose[j][i] * inst.value(j - 1));
        }
        newVals[i] = tempval;
        if (!m_center) {
            newVals[i] *= m_stdDevs[i];
        }
        newVals[i] += m_means[i];
    }

    if (inst instanceof SparseInstance) {
        return new SparseInstance(inst.weight(), newVals);
    } else {
        return new DenseInstance(inst.weight(), newVals);
    }
}

From source file:adams.data.conversion.AbstractMatchWekaInstanceAgainstHeader.java

License:Open Source License

/**
 * Checks the instance against the header, whether they are compatible.
 *
 * @param input   the input instance/*from   w  ww  .  j a  va2s . c om*/
 * @return      null if compatible, otherwise error message
 */
protected String isCompatible(Instance input) {
    String result;
    int i;
    int typeInput;
    int typeHeader;

    result = null;
    if (input.numAttributes() != m_Dataset.numAttributes())
        result = "Number of attributes differ";

    if (result == null) {
        for (i = 0; i < m_Dataset.numAttributes(); i++) {
            typeInput = input.attribute(i).type();
            typeHeader = m_Dataset.attribute(i).type();
            if (typeInput == typeHeader)
                continue;
            if ((typeInput == Attribute.NOMINAL) && (typeHeader == Attribute.STRING))
                continue;
            if ((typeInput == Attribute.STRING) && (typeHeader == Attribute.NOMINAL))
                continue;
            result = "Attribute types at #" + (i + 1) + "  are not ";
            break;
        }
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item./*from  www.j  a va 2  s .  co  m*/
 *
 * @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();

    try {
        if (m_AttributeName.length() > 0) {
            index = inst.dataset().attribute(m_AttributeName).index();
        } else {
            m_Index.setMax(inst.numAttributes());
            index = m_Index.getIntIndex();
        }
        if (inst.isMissing(index)) {
            m_OutputToken = new Token("?");
        } else {
            switch (inst.attribute(index).type()) {
            case Attribute.NUMERIC:
                m_OutputToken = new Token(inst.value(index));
                break;

            case Attribute.DATE:
            case Attribute.NOMINAL:
            case Attribute.STRING:
            case Attribute.RELATIONAL:
                m_OutputToken = new Token(inst.stringValue(index));
                break;

            default:
                result = "Unhandled attribute type: " + inst.attribute(index).type();
            }
        }
    } catch (Exception e) {
        result = handleException("Failed to obtain value from instance:\n" + inst, e);
    }

    return result;
}

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

License:Open Source License

/**
 * Turns the row into the appropriate format.
 *
 * @param row      the row to convert//from w  w  w . j  ava 2  s . c  o  m
 * @return      the generated output
 */
protected String createRow(Instance row) {
    StringBuilder result;
    int i;

    result = new StringBuilder();

    switch (m_OutputFormat) {
    case ARFF:
        result.append(row.toString());
        break;

    case CSV:
        for (i = 0; i < row.numAttributes(); i++) {
            if (i > 0)
                result.append(",");
            result.append(row.toString(i));
        }
        break;

    case TAB:
        for (i = 0; i < row.numAttributes(); i++) {
            if (i > 0)
                result.append("\t");
            result.append(row.toString(i));
        }
        break;

    default:
        throw new IllegalStateException("Unhandled output format: " + m_OutputFormat);
    }

    return result.toString();
}

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

License:Open Source License

/**
 * Executes the flow item./* w w  w. ja va 2  s.c  o m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    SequencePlotterContainer cont;
    int[] indices;
    int i;

    result = null;

    inst = (Instance) m_InputToken.getPayload();

    m_Counter++;
    m_Containers.clear();
    m_Attributes.setMax(inst.numAttributes());
    indices = m_Attributes.getIntIndices();

    for (i = 0; i < indices.length; i++) {
        if (inst.attribute(indices[i]).isNominal())
            cont = new SequencePlotterContainer(inst.dataset().attribute(indices[i]).name(),
                    new Double(m_Counter), inst.stringValue(indices[i]));
        else
            cont = new SequencePlotterContainer(inst.dataset().attribute(indices[i]).name(),
                    new Double(m_Counter), inst.value(indices[i]));
        m_Containers.add(cont);
    }

    return result;
}

From source file:adaptedClusteringAlgorithms.MyFarthestFirst.java

License:Open Source License

/**
 * Updates the minimum and maximum values for all the attributes
 * based on a new instance.//  ww w . jav  a2s  . co m
 *
 * @param instance the new instance
 */
private void updateMinMax(Instance instance) {

    for (int j = 0; j < instance.numAttributes(); j++) {
        if (Double.isNaN(m_Min[j])) {
            m_Min[j] = instance.value(j);
            m_Max[j] = instance.value(j);
        } else {
            if (instance.value(j) < m_Min[j]) {
                m_Min[j] = instance.value(j);
            } else {
                if (instance.value(j) > m_Max[j]) {
                    m_Max[j] = instance.value(j);
                }
            }
        }
    }
}

From source file:ANN.MultilayerPerceptron.java

@Override
public double classifyInstance(Instance i) {
    //buat list input
    ArrayList<Double> listInput = new ArrayList<>();
    listInput.add(1.0);//from w w  w.ja  v  a2 s . co m
    for (int idx = 0; idx < i.numAttributes() - 1; idx++) {
        listInput.add(i.value(idx));
    }

    //hitung output layer
    ArrayList<Double> hiddenOutput = new ArrayList<>();
    hiddenOutput.add(1.0);
    for (int idxOutput = 1; idxOutput < listHidden.size(); idxOutput++) {
        output(listHidden, listInput, idxOutput);
        hiddenOutput.add(listHidden.get(idxOutput).getValue());
        //                        System.out.println(outputVal);
    }
    //hitung output layer
    for (int idxOutput = 0; idxOutput < listOutput.size(); idxOutput++) {
        output(listOutput, hiddenOutput, idxOutput);
    }
    double result = maxIdxValue(listOutput);
    return result;
}

From source file:ANN.MultiplePerceptron.java

@Override
public double classifyInstance(Instance i) {
    ArrayList<Double> listInput = new ArrayList<>();
    listInput.add(1.0);//from w ww  .j  ava 2 s.c om
    for (int idxInstanceVal = 0; idxInstanceVal < i.numAttributes() - 1; idxInstanceVal++) {
        listInput.add(i.value(idxInstanceVal));
    }

    ArrayList<Double> listOutputHidden = new ArrayList<>();
    listOutputHidden.add(1.0);
    //set output hidden layer
    for (int idxNodeHidden = 1; idxNodeHidden < listNodeHidden.size(); idxNodeHidden++) {
        double outputVal = listNodeHidden.get(idxNodeHidden).output(listInput);
        listNodeHidden.get(idxNodeHidden).setValue(outputVal);
        listOutputHidden.add(outputVal);
    }

    //set output layer
    for (int idxNodeHidden = 0; idxNodeHidden < listNodeOutput.size(); idxNodeHidden++) {
        double outputVal = listNodeOutput.get(idxNodeHidden).output(listOutputHidden);
        listNodeOutput.get(idxNodeHidden).setValue(outputVal);
        //            System.out.printf("%f ", outputVal);
    }
    //        System.out.println();
    return getIdxMax(listNodeOutput);
}

From source file:ANN.MultiplePerceptron.java

public void calculateWeight(Instance i) {

    ArrayList<Double> listInput = new ArrayList<>();
    listInput.add(1.0);/*from   ww w. j  a v a2  s  .c o m*/
    for (int idxInstanceVal = 0; idxInstanceVal < i.numAttributes(); idxInstanceVal++) {
        listInput.add(i.value(idxInstanceVal));
    }

    //set weight hidden
    //        for (int index = 0; index < listNodeHidden.size();index++) {
    //            for (int indexDalem = 0 ; indexDalem < listNodeHidden.get(index).getWeightSize();indexDalem++) {
    //                double delta = learningRate*listNodeHidden.get(index).getError()*listInput.get(indexDalem);
    //                double newWeight = delta + listNodeHidden.get(index).getWeightFromList(indexDalem);
    //                listNodeHidden.get(index).setWeight(indexDalem, newWeight);
    ////                System.out.println(index+" "+indexDalem+" "+newWeight);
    //            }
    //        }
    for (int idxHidden = 0; idxHidden < listNodeHidden.size(); idxHidden++) {
        for (int idxweight = 0; idxweight < listNodeHidden.get(idxHidden).getWeightSize(); idxweight++) {
            double oldVal = listNodeHidden.get(idxHidden).getWeightFromList(idxweight);
            //                System.out.println(oldVal);
            oldVal += learningRate * listInput.get(idxweight) * listNodeHidden.get(idxHidden).getError();
            listNodeHidden.get(idxHidden).setWeight(idxweight, oldVal);
            //                System.out.println(listNodeHidden.get(idxHidden).getWeightFromList(idxweight));
        }
    }
    //        System.out.println(listNodeOutput.get(0).getWeightSize()+" "+listOutputHidden);
    //set weight output
    for (int index = 0; index < listNodeOutput.size(); index++) {
        for (int indexDalem = 0; indexDalem < listNodeHidden.size(); indexDalem++) {
            double delta = learningRate * listNodeOutput.get(index).getError()
                    * listNodeHidden.get(indexDalem).getValue();
            double newWeight = delta + listNodeOutput.get(index).getWeightFromList(indexDalem);
            listNodeOutput.get(index).setWeight(indexDalem, newWeight);
            //                System.out.println(newWeight);
        }
    }
    //        for (int idxOutput=0; idxOutput < listNodeOutput.size(); idxOutput++) {
    //            for (int idxweight=0;idxweight<listNodeOutput.get(idxOutput).getWeightSize();idxweight++) {
    //                double oldVal = listNodeOutput.get(idxOutput).getWeightFromList(idxweight);
    //                oldVal += learningRate*listOutputHidden.get(idxweight) * listNodeOutput.get(idxOutput).getError();
    //                listNodeOutput.get(idxOutput).setWeight(idxweight, oldVal);
    //            }
    //        }

}