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:test20November.ArffFileLoader.java

public ArffFileLoader(String filename) throws FileNotFoundException, IOException {
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    ArffLoader.ArffReader arff = new ArffLoader.ArffReader(reader);
    Instances data = arff.getData();/*w  w  w  .jav a2  s . c o m*/
    data.setClassIndex(data.numAttributes() - 1);

    attributes = new String[data.numInstances()][data.numAttributes() - 1];
    labels = new String[data.numInstances()];

    for (int i = 0; i < data.numInstances(); i++) {
        Instance instance = data.instance(i);
        for (int j = 0; j < instance.numAttributes() - 1; j++) {
            attributes[i][j] = instance.stringValue(j);
        }
        labels[i] = instance.stringValue(instance.numAttributes() - 1);
    }

    attributesLegalValues = new String[data.numAttributes() - 1][];
    for (int i = 0; i < data.numAttributes() - 1; i++) {
        attributesLegalValues[i] = (String[]) Collections.list(data.attribute(i).enumerateValues())
                .toArray(new String[data.attribute(i).numValues()]);
    }

    labelsLegalValues = (String[]) Collections.list(data.attribute(data.numAttributes() - 1).enumerateValues())
            .toArray(new String[data.attribute(data.numAttributes() - 1).numValues()]);
}

From source file:text_clustering.Cobweb.java

License:Open Source License

/**
 * Adds an instance to the clusterer./*from   w ww .jav  a2s.c o m*/
 *
 * @param newInstance the instance to be added
 * @throws Exception    if something goes wrong
 */
public void updateClusterer(Instance newInstance) throws Exception {
    m_numberOfClustersDetermined = false;

    if (m_cobwebTree == null) {
        m_cobwebTree = new CNode(newInstance.numAttributes(), newInstance);
    } else {
        m_cobwebTree.addInstance(newInstance);
    }
}

From source file:tml.utils.DistanceLib.java

License:Apache License

public static double kullbackLeibler(Instance inst1, Instance inst2) {

    double divergence = 0.0;
    for (int i = 0; i < inst1.numAttributes(); ++i) {
        if (inst1.value(i) != 0 && inst2.value(i) != 0) {
            divergence += inst1.value(i) * Math.log(inst1.value(i) / inst2.value(i));
        }/* ww w . j  a  v a  2 s .c o  m*/
    }
    divergence /= Math.log(2);
    return divergence;
}

From source file:tml.utils.DistanceLib.java

License:Apache License

public static double jensenShannon(Instance inst1, Instance inst2) {

    Instance averageInst = new Instance(inst1.numAttributes());
    for (int i = 0; i < inst1.numAttributes(); i++) {
        averageInst.setValue(i, (inst1.value(i) + inst2.value(i)) / 2);
    }/*from www  .  ja v  a  2 s. co m*/

    double divergence = (kullbackLeibler(inst1, averageInst) + kullbackLeibler(inst2, averageInst)) / 2;
    return divergence;
}

From source file:tr.gov.ulakbim.jDenetX.classifiers.Perceptron.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {

    //Init Perceptron
    if (this.reset) {
        this.reset = false;
        this.numberAttributes = inst.numAttributes();
        this.numberClasses = inst.numClasses();
        this.weightAttribute = new double[inst.numClasses()][inst.numAttributes()];
        for (int i = 0; i < inst.numClasses(); i++) {
            for (int j = 0; j < inst.numAttributes(); j++) {
                weightAttribute[i][j] = 0.2 * Math.random() - 0.1;
            }/*from   w  ww  . j a v a  2s  .c  o  m*/
        }
    }

    double[] preds = new double[inst.numClasses()];
    for (int i = 0; i < inst.numClasses(); i++) {
        preds[i] = prediction(inst, i);
    }
    double learningRatio = learningRatioOption.getValue();

    int actualClass = (int) inst.classValue();
    for (int i = 0; i < inst.numClasses(); i++) {
        double actual = (i == actualClass) ? 1.0 : 0.0;
        double delta = (actual - preds[i]) * preds[i] * (1 - preds[i]);
        for (int j = 0; j < inst.numAttributes() - 1; j++) {
            this.weightAttribute[i][j] += learningRatio * delta * inst.value(j);
        }
        this.weightAttribute[i][inst.numAttributes() - 1] += learningRatio * delta;
    }
}

From source file:tr.gov.ulakbim.jDenetX.clusterers.CobWeb.java

License:Open Source License

/**
 * Adds an instance to the clusterer.//from   w  ww  .  j a v  a 2  s.c  o m
 *
 * @param newInstance the instance to be added
 * @throws Exception if something goes wrong
 */
// public void updateClusterer(Instance newInstance) throws Exception {
@Override
public void trainOnInstanceImpl(Instance newInstance) { //throws Exception {
    m_numberOfClustersDetermined = false;

    if (m_cobwebTree == null) {
        m_cobwebTree = new CNode(newInstance.numAttributes(), newInstance);
    } else {
        m_cobwebTree.addInstance(newInstance);
    }
}

From source file:tr.gov.ulakbim.jDenetX.experiments.wrappers.EvalActiveBoostingID.java

License:Open Source License

protected void selfTrain(Instance testInst) {
    int maxInstances = this.maxInstancesOption.getValue();
    int poolSizeRatio = poolSizeOption.getValue();
    int poolLimit = maxInstances / poolSizeRatio;
    int poolCount = 0;
    VotedInstancePool vInstPool = SelfOzaBoostID.getVotedInstancePool();
    noOfClassesInPool = vInstPool.getNoOfClasses();

    System.out.println("No of instances in the pool: " + vInstPool.getSize());
    System.out.println("No of classes in the pool: " + noOfClassesInPool);

    if (vInstPool.getSize() > 10) {
        ArrayList<Attribute> attrs = new ArrayList<Attribute>();
        for (int i = 0; i < testInst.numAttributes(); i++) {
            attrs.add(testInst.attribute(i));
        }// w  w  w  . j a va 2  s  .  c  o  m
        Instances instances = new Instances("instances", attrs, vInstPool.getSize());
        Iterator instanceIt = vInstPool.iterator();
        System.out.println("Size of pool: " + vInstPool.getSize());

        while (instanceIt.hasNext() && poolCount < poolLimit) {
            VotedInstance vInstance = (VotedInstance) instanceIt.next();
            ((Instances) instances).add(vInstance.getInstance());
            poolCount++;
        }

        System.out.println("Size of instances: " + instances.size());
        instances = clusterInstances(instances);
        InstanceStream activeStream = new CachedInstancesStream((Instances) instances);

        System.out.println("Selftraining have been started");
        System.out.println("Number of self training instances: " + instances.numInstances());

        long treeSize = vInstPool.getSize();
        long limit = treeSize / SAMPLING_LIMIT;
        Instance inst = null;

        for (long j = 0; j < limit && activeStream.hasMoreInstances(); j++) {
            inst = activeStream.nextInstance();
            if (inst.numAttributes() == attrs.size()) {
                model.trainOnInstance(inst);
            }
        }
    }

}

From source file:tr.gov.ulakbim.jDenetX.streams.filters.AddNoiseFilter.java

License:Open Source License

public Instance nextInstance() {
    Instance inst = (Instance) this.inputStream.nextInstance().copy();
    for (int i = 0; i < inst.numAttributes(); i++) {
        double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue()
                : this.attNoiseFractionOption.getValue();
        if (inst.attribute(i).isNominal()) {
            DoubleVector obs = (DoubleVector) this.attValObservers.get(i);
            if (obs == null) {
                obs = new DoubleVector();
                this.attValObservers.set(i, obs);
            }//from ww  w .  j a v  a  2 s.  c om
            int originalVal = (int) inst.value(i);
            if (!inst.isMissing(i)) {
                obs.addToValue(originalVal, inst.weight());
            }
            if ((this.random.nextDouble() < noiseFrac) && (obs.numNonZeroEntries() > 1)) {
                do {
                    inst.setValue(i, this.random.nextInt(obs.numValues()));
                } while (((int) inst.value(i) == originalVal) || (obs.getValue((int) inst.value(i)) == 0.0));
            }
        } else {
            GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i);
            if (obs == null) {
                obs = new GaussianEstimator();
                this.attValObservers.set(i, obs);
            }
            obs.addObservation(inst.value(i), inst.weight());
            inst.setValue(i, inst.value(i) + this.random.nextGaussian() * obs.getStdDev() * noiseFrac);
        }
    }
    return inst;
}

From source file:tubes2.myClusterers.EuclideanDistance.java

@Override
public double distanceOf(Instance a, Instance b) throws Exception {
    if (a.numAttributes() != b.numAttributes())
        throw new Exception("number of attributes doesn't match");
    double sumSquares = 0;
    for (int i = 0; i < a.numAttributes(); i++) {
        double Error = a.value(i) - b.value(i);
        sumSquares += Error * Error;
    }//from   w w  w  . jav a2 s. c  o m
    return Math.sqrt(sumSquares);
}

From source file:tubes2.myClusterers.myKMeans.java

public boolean instanceDifferent(Instance a, Instance b) {
    for (int i = 0; i < a.numAttributes(); i++) {
        if (a.value(i) != b.value(i))
            return true;
    }//w  ww  . ja  v  a 2  s . c  o m
    return false;
}