Example usage for weka.core Instances numAttributes

List of usage examples for weka.core Instances numAttributes

Introduction

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

Prototype


publicint numAttributes() 

Source Link

Document

Returns the number of attributes.

Usage

From source file:gr.auth.ee.lcs.ArffTrainTestLoader.java

License:Open Source License

/**
 * Load instances into the global train store and create test set.
 * //from  ww w  .ja v a 2 s . c om
 * @param filename
 *            the .arff filename to be used
 * @param generateTestSet
 *            true if a test set is going to be generated
 * @throws IOException
 *             if the input file is not found
 */
public final void loadInstances(final String filename, final boolean generateTestSet) throws IOException {
    // Open .arff
    final Instances set = InstancesUtility.openInstance(filename);
    if (set.classIndex() < 0) {
        set.setClassIndex(set.numAttributes() - 1);
    }
    set.randomize(new Random());

    if (generateTestSet) {
        final int numOfFolds = (int) SettingsLoader.getNumericSetting("NumberOfFolds", 10);
        final int fold = (int) Math.floor(Math.random() * numOfFolds);
        trainSet = set.trainCV(numOfFolds, fold);
        testSet = set.testCV(numOfFolds, fold);
    } else {
        trainSet = set;
    }

    myLcs.instances = InstancesUtility.convertIntancesToDouble(trainSet);
    myLcs.labelCardinality = InstancesUtility.getLabelCardinality(trainSet);

}

From source file:gr.auth.ee.lcs.ArffTrainTestLoader.java

License:Open Source License

/**
 * Load instances into the global train store and create test set.
 * /*from   ww  w.j  av  a2  s . c  o m*/
 * @param filename
 *            the .arff filename to be used
 * @param testFile
 *            the test file to be loaded
 * @throws IOException
 *             if the input file is not found
 */
public final void loadInstancesWithTest(final String filename, final String testFile) throws IOException {

    // Open .arff
    final Instances set = InstancesUtility.openInstance(filename);

    if (set.classIndex() < 0)
        set.setClassIndex(set.numAttributes() - 1);
    set.randomize(new Random());
    trainSet = set;

    myLcs.instances = InstancesUtility.convertIntancesToDouble(trainSet);
    myLcs.labelCardinality = InstancesUtility.getLabelCardinality(trainSet);
    testSet = InstancesUtility.openInstance(testFile);

    myLcs.trainSet = trainSet;
    myLcs.testSet = testSet;

    myLcs.testInstances = InstancesUtility.convertIntancesToDouble(testSet);

    System.out.println("Label cardinality: " + myLcs.labelCardinality);

}

From source file:gr.auth.ee.lcs.data.representations.complex.ComplexRepresentation.java

License:Open Source License

/**
 * Arff Loader./*  ww w .ja v  a 2 s . c  o  m*/
 * 
 * @param inputArff
 *            the input .arff
 * @param precisionBits
 *            bits used for precision
 * @param labels
 *            the number of labels (classes) in the set
 * @param generalizationRate
 *            the attribute generalization rate (P#)
 * @param lcs
 *            the LCS instance the representation belongs to
 * @throws IOException
 *             when .arff not found
 */
protected ComplexRepresentation(final String inputArff, final int precisionBits, final int labels,
        final double generalizationRate, final double clusteringAttributeGeneralizationRate,
        final AbstractLearningClassifierSystem lcs) throws IOException {

    this.myLcs = lcs;
    final Instances instances = InstancesUtility.openInstance(inputArff);

    this.numberOfLabels = labels;
    attributeList = new AbstractAttribute[instances.numAttributes()];
    this.attributeGeneralizationRate = generalizationRate;
    this.clusteringAttributeGeneralizationRate = clusteringAttributeGeneralizationRate;
    this.precision = precisionBits;
}

From source file:gr.auth.ee.lcs.data.representations.complex.ComplexRepresentation.java

License:Open Source License

/**
 * Build the representation for some instances.
 * //  ww  w.  ja va  2s .c  o m
 * @param instances
 *            the instances
 */

protected void buildRepresentationFromInstance(final Instances instances) {

    for (int i = 0; i < (instances.numAttributes() - numberOfLabels); i++) {

        final String attributeName = instances.attribute(i).name();

        if (instances.attribute(i).isNominal()) {

            String[] attributeNames = new String[instances.attribute(i).numValues()];

            final Enumeration<?> values = instances.attribute(i).enumerateValues();

            for (int j = 0; j < attributeNames.length; j++) {
                attributeNames[j] = (String) values.nextElement();
            }

            // Create boolean or generic nominal
            if (attributeNames.length > 2)
                attributeList[i] = new ComplexRepresentation.NominalAttribute(this.chromosomeSize,
                        attributeName, attributeNames, attributeGeneralizationRate);
            else
                attributeList[i] = new ComplexRepresentation.BooleanAttribute(chromosomeSize, attributeName,
                        attributeGeneralizationRate);

        } else if (instances.attribute(i).isNumeric()) {
            float minValue, maxValue;
            minValue = (float) instances.instance(0).toDoubleArray()[i];
            maxValue = minValue;
            for (int sample = 0; sample < instances.numInstances(); sample++) {
                final float currentVal = (float) instances.instance(sample).toDoubleArray()[i];
                if (currentVal > maxValue)
                    maxValue = currentVal;
                if (currentVal < minValue)
                    minValue = currentVal;
            }
            attributeList[i] = new ComplexRepresentation.IntervalAttribute(this.chromosomeSize, attributeName,
                    minValue, maxValue, precision, attributeGeneralizationRate);
        }
    }

    createClassRepresentation(instances);
}

From source file:gr.auth.ee.lcs.data.representations.complex.SingleClassRepresentation.java

License:Open Source License

@Override
protected void createClassRepresentation(final Instances instances) {

    if (instances.classIndex() < 0)
        instances.setClassIndex(instances.numAttributes() - 1);

    // Rule Consequents
    final Enumeration<?> classNames = instances.classAttribute().enumerateValues();
    final String[] ruleConsequents = new String[instances.numClasses()];
    this.ruleConsequents = ruleConsequents;
    for (int i = 0; i < instances.numClasses(); i++)
        ruleConsequents[i] = (String) classNames.nextElement();

    attributeList[attributeList.length - 1] = new UniLabel(chromosomeSize, "class", ruleConsequents);

}

From source file:gr.auth.ee.lcs.utilities.InstancesUtility.java

License:Open Source License

/**
 * Perform the conversion.//from   w ww. j  a  v  a  2s. co  m
 * 
 * @param set
 *            the set containing the instances
 * @return a double[][] containing the instances and their respective
 *         attributes
 */
public static double[][] convertIntancesToDouble(final Instances set) {
    if (set == null)
        return null;

    final double[][] result = new double[set.numInstances()][set.numAttributes()];
    for (int i = 0; i < set.numInstances(); i++) {

        for (int j = 0; j < set.numAttributes(); j++) {
            result[i][j] = set.instance(i).value(j);
        }
    }

    return result;

}

From source file:gr.auth.ee.lcs.utilities.InstancesUtility.java

License:Open Source License

/**
 * Returns the label cardinality of the specified set.
 * //from  w ww.java2s .c o m
 */
public static double getLabelCardinality(final Instances set) {
    if (set == null)
        return -1;

    int numberOfLabels = (int) SettingsLoader.getNumericSetting("numberOfLabels", 1);
    double sumOfLabels = 0;

    for (int i = 0; i < set.numInstances(); i++) {
        for (int j = set.numAttributes() - numberOfLabels; j < set.numAttributes(); j++) {
            sumOfLabels += set.instance(i).value(j);
        }
    }

    if (set.numInstances() != 0) {

        return (double) (sumOfLabels / set.numInstances());
    }
    return 0;
}

From source file:gr.auth.ee.lcs.utilities.InstancesUtility.java

License:Open Source License

/**
 * Splits the .arff input dataset to |number-of-distinct-label-combinations| Instances which are stored in the partitions[] array. 
 * Called by initializePopulation() as a preparatory step to clustering.
 * @throws Exception //  w  w  w. j  a v  a  2 s .  co m
 * 
 * */

public static Instances[] partitionInstances(final AbstractLearningClassifierSystem lcs, final String filename)
        throws Exception {

    // Open .arff
    final Instances set = InstancesUtility.openInstance(filename);
    if (set.classIndex() < 0) {
        set.setClassIndex(set.numAttributes() - 1);
    }
    //set.randomize(new Random());
    int numberOfLabels = (int) SettingsLoader.getNumericSetting("numberOfLabels", 1);

    // the partitions vector holds the indices      
    String stringsArray[] = new String[lcs.instances.length];
    int indicesArray[] = new int[lcs.instances.length];

    // convert each instance's labelset into a string and store it in the stringsArray array
    for (int i = 0; i < set.numInstances(); i++) {
        stringsArray[i] = "";
        indicesArray[i] = i;

        for (int j = set.numAttributes() - numberOfLabels; j < set.numAttributes(); j++) {
            stringsArray[i] += (int) set.instance(i).value(j);
        }
    }

    // contains the indicesVector(s)
    Vector<Vector> mothershipVector = new Vector<Vector>();

    String baseString = "";
    for (int i = 0; i < set.numInstances(); i++) {

        baseString = stringsArray[i];
        if (baseString.equals(""))
            continue;
        Vector<Integer> indicesVector = new Vector<Integer>();

        for (int j = 0; j < set.numInstances(); j++) {
            if (baseString.equals(stringsArray[j])) {
                stringsArray[j] = "";
                indicesVector.add(j);
            }
        }
        mothershipVector.add(indicesVector);
    }

    Instances[] partitions = new Instances[mothershipVector.size()];

    for (int i = 0; i < mothershipVector.size(); i++) {
        partitions[i] = new Instances(set, mothershipVector.elementAt(i).size());
        for (int j = 0; j < mothershipVector.elementAt(i).size(); j++) {
            Instance instanceToAdd = set.instance((Integer) mothershipVector.elementAt(i).elementAt(j));
            partitions[i].add(instanceToAdd);
        }
    }
    /*
     * up to here, the partitions array has been formed. it contains the split dataset by label combinations
     * it holds both the attributes and the labels, but for clustering the input should only be the attributes,
     * so we need to delete the labels. this is taken care of by initializePopulation()
     */
    return partitions;
}

From source file:gr.auth.ee.lcs.utilities.InstancesUtility.java

License:Open Source License

public static Instances[] partitionInstances(final AbstractLearningClassifierSystem lcs,
        final Instances trainSet) throws Exception {

    // Open .arff
    final Instances set = trainSet;
    if (set.classIndex() < 0) {
        set.setClassIndex(set.numAttributes() - 1);
    }//from ww  w . j a v a2 s.  c o  m
    //set.randomize(new Random());
    int numberOfLabels = (int) SettingsLoader.getNumericSetting("numberOfLabels", 1);

    // the partitions vector holds the indices      
    String stringsArray[] = new String[trainSet.numInstances()];
    int indicesArray[] = new int[trainSet.numInstances()];

    // convert each instance's labelset into a string and store it in the stringsArray array
    for (int i = 0; i < set.numInstances(); i++) {
        stringsArray[i] = "";
        indicesArray[i] = i;

        for (int j = set.numAttributes() - numberOfLabels; j < set.numAttributes(); j++) {
            stringsArray[i] += (int) set.instance(i).value(j);
        }
    }

    // contains the indicesVector(s)
    Vector<Vector> mothershipVector = new Vector<Vector>();

    String baseString = "";
    for (int i = 0; i < set.numInstances(); i++) {

        baseString = stringsArray[i];
        if (baseString.equals(""))
            continue;
        Vector<Integer> indicesVector = new Vector<Integer>();

        for (int j = 0; j < set.numInstances(); j++) {
            if (baseString.equals(stringsArray[j])) {
                stringsArray[j] = "";
                indicesVector.add(j);
            }
        }
        mothershipVector.add(indicesVector);
    }

    Instances[] partitions = new Instances[mothershipVector.size()];

    for (int i = 0; i < mothershipVector.size(); i++) {
        partitions[i] = new Instances(set, mothershipVector.elementAt(i).size());
        for (int j = 0; j < mothershipVector.elementAt(i).size(); j++) {
            Instance instanceToAdd = set.instance((Integer) mothershipVector.elementAt(i).elementAt(j));
            partitions[i].add(instanceToAdd);
        }
    }
    /*
     * up to here, the partitions array has been formed. it contains the split dataset by label combinations
     * it holds both the attributes and the labels, but for clustering the input should only be the attributes,
     * so we need to delete the labels. this is taken care of by initializePopulation()
     */
    return partitions;
}