Example usage for weka.core Instances classIndex

List of usage examples for weka.core Instances classIndex

Introduction

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

Prototype


publicint classIndex() 

Source Link

Document

Returns the class attribute's index.

Usage

From source file:naivebayes.TucilWeka.java

public static Instances readDataSet(String filepath) {
    //Membaca dataset

    Instances data = null;
    try {/*from ww w  .j  av a2s. c o m*/
        data = DataSource.read(filepath);
    } catch (Exception ex) {
        Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (int i = 0; i < data.numAttributes(); i++) {
        System.out.println(i + ". " + data.attribute(i));
    }
    Scanner scan = new Scanner(System.in);
    System.out.print("Class Index : ");
    int temp = scan.nextInt();
    data.setClassIndex(temp);
    if (temp == 26) {
        // data.deleteAttributeAt(27);
    } else
    //jika 27
    {
        //data.deleteAttributeAt(26);
    }
    for (int i = 0; i < data.numAttributes(); i++) {
        System.out.println(i + ". " + data.attribute(i));
    }
    System.out.println("Index kelasnya: " + data.classIndex());
    // data.setClassIndex(data.numAttributes()-1);
    Instances retval = filterDiscretize(data);
    return retval;
}

From source file:NaiveBayesPckge.InputNaiveBayes.java

/**
 * This class is used to read file .arff
 * @param pathFile path of the file/*ww w . j av a 2  s . c  om*/
 * @return 
 */
public Instances readFileUseWeka(String pathFile) {

    Instances instance = null;

    try {
        System.out.println("read file . . .");
        ConverterUtils.DataSource source = new ConverterUtils.DataSource(pathFile);
        instance = source.getDataSet();
        if (instance.classIndex() == -1) {
            instance.setClassIndex(instance.numAttributes() - 1);
        }
        System.out.println("file " + pathFile + " has been loaded");
    } catch (Exception e) {
        System.out.println("There is a problem when reading .arff file : " + e);
    }

    return instance;
}

From source file:NaiveBayesPckge.NaiveBayesMain.java

public static void printCoba(Instances instance) {
    System.out.println("");
    System.out.println("1. first instance : " + instance.firstInstance());
    System.out.println("2. banyaknya atribut :" + instance.numAttributes());
    System.out.println("3. " + instance.attribute(0).numValues());
    System.out.println("4. " + instance.attribute(0).weight());
    System.out.println("5. " + instance.attribute(instance.numAttributes() - 1).numValues());
    System.out.println("6. " + instance.get(0));
    System.out.println("7. " + instance.get(0).stringValue(4));
    System.out.println("8. " + instance.numInstances());
    System.out.println("9. " + instance.attribute(instance.numAttributes() - 1).numValues());
    System.out.println("10. " + instance.get(1).stringValue(instance.numAttributes() - 1));
    System.out.println("11. " + instance.attribute(instance.numAttributes() - 1).value(0));
    System.out.println("12. " + instance.attribute(instance.numAttributes() - 1).name());
    System.out.println("13. " + instance.numClasses());
    System.out.println("14. Banyaknya kelas yang diuji : " + instance.classIndex());
    //        System.out.println("15. " + (String.valueOf(instance.attribute(0).value(34)).equals(String.valueOf(4.3))));
    //        System.out.println("16. " + instance);
}

From source file:naive_bayes.Naive_bayes.java

public Instances readFile() throws Exception {
    ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:\\Users\\acer\\Desktop\\mush.arff");
    Instances data = source.getDataSet();
    if (data.classIndex() == -1) {
        data.setClassIndex(classidx);/*www  . j  a v  a  2  s  .c  om*/
    }
    return data;
}

From source file:net.paudan.evosvm.LibLINEAR.java

License:Open Source License

private boolean isOnlyNumeric(Instances insts) {
    for (int i = 0; i < insts.numAttributes(); i++) {
        if (i != insts.classIndex()) {
            if (!insts.attribute(i).isNumeric()) {
                return false;
            }//from   w ww.  j  a  v  a2 s  .  com
        }
    }
    return true;
}

From source file:net.sf.jclal.activelearning.oracle.ConsoleHumanOracle.java

License:Open Source License

/**
 * Extracts the classes from single-label dataset.
 *
 * @param labeled The labeled set/*  w ww  .  j a v a  2  s.  c  om*/
 * @return An array of string
 */
private String[] valueClasses(Instances labeled) {

    int classIndex = labeled.classIndex();
    String[] valueClasses = new String[labeled.attribute(classIndex).numValues()];

    for (int i = 0; i < valueClasses.length; i++) {
        valueClasses[i] = labeled.attribute(classIndex).value(i);
    }

    return valueClasses;
}

From source file:net.sf.jclal.activelearning.singlelabel.querystrategy.VarianceReductionQueryStrategy.java

License:Open Source License

/**
 *
 * Analyzes how informative is an instance.
 *
 * @param instance The instance to query.
 * @return The utility of the instance./*from w  w w  .j  a va2 s . c  o m*/
 */
@Override
public double utilityInstance(Instance instance) {

    Instances unlabeled = getUnlabelledData().getDataset();

    if (unlabelledSize != unlabeled.numInstances()) {
        unlabelledSize = unlabeled.numInstances();

        //it is initialized q_sub_i
        int n = unlabeled.numInstances();
        double[] q = new double[n];
        //1. q_sub_i = 1/n, i = 1, 2, ..., n
        //Arrays.fill(q, 1.0 / n);
        //further on it fills, to optimize

        //it is initialized pi_sub_i
        //2. pi_sub_i
        double[] piSubI = getPiSubI(unlabeled);

        //to create the Fisher matrix
        int dimensionMatrix = unlabeled.numAttributes() - 1;
        int classIndex = unlabeled.classIndex();

        Matrix matrixFisher = null;
        try {
            matrixFisher = new Matrix(dimensionMatrix, dimensionMatrix);
        } catch (Exception ex) {
            Logger.getLogger(VarianceReductionQueryStrategy.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (int i = 0; i < piSubI.length; i++) {
            double mult = piSubI[i] * (1 - piSubI[i]);

            //the values of the instance are had
            double[] atributos = unlabeled.instance(i).toDoubleArray();

            //the attribute class is eliminated, only the features are left
            double[] vectorX = DatasetUtils.copyFeatures(atributos, classIndex);

            Matrix current = null;
            try {
                current = new Matrix(vectorX.length, vectorX.length);
            } catch (Exception ex) {
                Logger.getLogger(VarianceReductionQueryStrategy.class.getName()).log(Level.SEVERE, null, ex);
            }

            productVector(current, vectorX);

            //it multiplies current * multi
            current.timesEquals(mult);

            //it adds current to matrixFisher
            //plusEquals saves the result in matrixFisher
            matrixFisher.plusEquals(current);

        }

        double factorRegularizationValue = getFactorRegularization();

        Matrix identity = Matrix.identity(dimensionMatrix, dimensionMatrix);

        identity.timesEquals(factorRegularizationValue);

        //the result joins to matrixFisher
        matrixFisher.plusEquals(identity);

        //do eigen decomposition
        EigenvalueDecomposition eigen = matrixFisher.eig();

        //in case of file, the matrix v takes the matrix file from eigen
        //in this case eigen cant not be destroy for the moment
        Matrix v = eigen.getV();

        double[] landa = eigen.getRealEigenvalues();

        double epsilonValue = getEpsilon();

        //variable copies of q to know if there has been some change
        double[] copiaQ = new double[q.length];
        Arrays.fill(copiaQ, 1.0 / n);

        //while it finds change in q, it keeps on iterating
        currentEpsilonIteration = 0;
        do {
            ++currentEpsilonIteration;
            //the value of q is updated
            //in the first iteration it fills with 1.0/n
            System.arraycopy(copiaQ, 0, q, 0, q.length);

            //process of finding f_sub_i
            double[] f = new double[landa.length];
            for (int j = 0; j < f.length; j++) {
                f[j] = 0;

                for (int i = 0; i < n; i++) {
                    double mult = q[i] * piSubI[i] * (1 - piSubI[i]);

                    //the values of the instance are had
                    double[] atributos = unlabeled.instance(i).toDoubleArray();

                    //the attribute class is eliminated, only the features are left
                    double[] vectorX = DatasetUtils.copyFeatures(atributos, classIndex);

                    //it multiplies vector_x with vector_columna of V
                    //vector_x it is: 1 X n
                    //vector_de_V it is: n X 1
                    //result: a number
                    double multVectores = 0;
                    for (int k = 0; k < vectorX.length; k++) {
                        multVectores += vectorX[k] * v.get(k, j);
                    }

                    //the result rises up to the square
                    multVectores *= multVectores;

                    //it joins to f[j]
                    f[j] += mult * multVectores;
                }
            }

            //the first process of finding q of the current iteration       
            for (int i = 0; i < n; i++) {
                double mult = copiaQ[i] * copiaQ[i] * piSubI[i] * (1 - piSubI[i]);

                //the values of the instance are had
                double[] atributos = unlabeled.instance(i).toDoubleArray();

                //the attribute class is eliminated, only the features are left
                double[] vectorX = DatasetUtils.copyFeatures(atributos, classIndex);

                //the following  is realized
                double sumatoria = 0;
                for (int j = 0; j < landa.length; j++) {

                    //it multiplies vector_x with vector_columna of V
                    //vector_x is: 1 X n
                    //vector_de_V is: n X 1
                    //result: a number
                    double multVectores = 0;
                    for (int k = 0; k < vectorX.length; k++) {
                        multVectores += vectorX[k] * v.get(k, j);
                    }

                    //the result multiplies with landa[j]
                    multVectores *= landa[j];

                    //it rises up to the square
                    multVectores *= multVectores;

                    //it splits between the square of f [j]
                    multVectores /= f[j] * f[j];

                    //the sumatoria is added
                    sumatoria += multVectores;
                }

                //the value of copia_q [i] is: mult * sumatoria
                copiaQ[i] = mult * sumatoria;
            }

            //the second step to find q in the iteration

            /*the sum must be out, if it was inside and with copia_q then 
             *one would give priority to the last instance and the last one 
             * would be always chosen
             */
            double suma = 0;
            for (int j = 0; j < n; j++) {
                suma += copiaQ[j];
            }

            for (int i = 0; i < n; i++) {
                copiaQ[i] = copiaQ[i] / suma;
            }

        } while (change(q, copiaQ, epsilonValue));

        //the values are saved
        tempValues = new double[copiaQ.length];

        System.arraycopy(copiaQ, 0, tempValues, 0, copiaQ.length);

    }

    int indice = unlabeled.indexOf(instance);

    return tempValues[indice];
}

From source file:net.sf.markov4jmeter.behaviormodelextractor.extraction.transformation.clustering.AbstractClusteringStrategy.java

License:Apache License

/**
 * This method creates a new instance set based on the available
 * behaviorModelsAbsolute./*from  w ww  .  j  a v  a2s.com*/
 * 
 * @param behaviorModelsAbsolute
 * @return instance set
 */
protected Instances getInstances(BehaviorModelAbsolute[] behaviorModelsAbsolute) throws Exception {

    // init the fastVector with attributesNames from the first
    // behaviorModel.
    FastVector fastVector = getFastVector(behaviorModelsAbsolute[0]);

    // create empty instance set with the number of behaviorModelsRelative.
    Instances instances = new Instances("BehaviorModelAbsoluteInstanceSet", fastVector,
            behaviorModelsAbsolute.length);

    // set the last attribute as class index
    instances.setClassIndex(instances.numAttributes() - 1);

    // Each behaviorModelsRelative will be transformed to an instance. To do
    // that, that transition matrix will be
    // transformed in a vector. Set number of attributes of instance: n x (n
    // +1) exit state
    // Matrix.
    for (BehaviorModelAbsolute behaviorModelAbsolute : behaviorModelsAbsolute) {
        // retieve instance from behaviorModelRelative
        Instance instance = getInstance(behaviorModelAbsolute, instances);
        // add instance to instanceset, at the end of the set
        instances.add(instance);
    }

    // save input data as arff file. This arff file can be opened with weka
    // application.
    ArffSaver saver = new ArffSaver();
    saver.setInstances(instances);
    saver.setFile(new File(CommandLineArgumentsHandler.getOutputDirectory() + "/data_clustering.arff"));
    saver.writeBatch();

    // Remove UseLess
    // weka.filters.unsupervised.attribute.RemoveUseless filterUseLess = new
    // weka.filters.unsupervised.attribute.RemoveUseless();
    // filterUseLess.setInputFormat(instances);
    // Instances returnInstances = Filter.useFilter(instances,
    // filterUseLess);

    // filter instances
    weka.filters.unsupervised.attribute.Remove filter = new weka.filters.unsupervised.attribute.Remove();
    filter.setAttributeIndices("" + (instances.classIndex() + 1));
    filter.setInputFormat(instances);
    Instances filteredInstances = Filter.useFilter(instances, filter);

    return filteredInstances;
}

From source file:neuralnetwork.NeuralNetwork.java

/**
 * @param args the command line arguments
 * @throws java.lang.Exception/*  w w w . j a  v a2 s.  com*/
 */
public static void main(String[] args) throws Exception {

    ConverterUtils.DataSource source;
    source = new ConverterUtils.DataSource("C:\\Users\\Harvey\\Documents\\iris.csv");
    Instances data = source.getDataSet();

    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }

    data.randomize(new Debug.Random(1));

    RemovePercentage trainFilter = new RemovePercentage();
    trainFilter.setPercentage(70);
    trainFilter.setInputFormat(data);
    Instances train = Filter.useFilter(data, trainFilter);

    trainFilter.setInvertSelection(true);
    trainFilter.setInputFormat(data);
    Instances test = Filter.useFilter(data, trainFilter);

    Standardize filter = new Standardize();
    filter.setInputFormat(train);

    Instances newTrain = Filter.useFilter(test, filter);
    Instances newTest = Filter.useFilter(train, filter);

    Classifier nNet = new NeuralNet();
    nNet.buildClassifier(newTrain);
    Evaluation eval = new Evaluation(newTest);
    eval.evaluateModel(nNet, newTest);
    System.out.println(eval.toSummaryString("\nResults\n-------------\n", false));
}

From source file:newclassifier.NewClassifier.java

public void classify(String path) throws Exception {
    // load unlabeled data and set class attribute
    Instances unlabeled = DataSource.read(path);
    unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

    // copy/*from  w  ww.j a v a  2  s  . c o  m*/
    Instances labeled = new Instances(unlabeled);

    // label instances
    for (int i = 0; i < unlabeled.numInstances(); i++) {
        double clsLabel = cls.classifyInstance(unlabeled.instance(i));
        labeled.instance(i).setClassValue(clsLabel);
    }

    // save labeled data
    DataSink.write("labeled.arff", labeled);

    // output prediction
    System.out.println("# - actual - predicted - distribution");
    for (int i = 0; i < labeled.numInstances(); i++) {
        double pred = cls.classifyInstance(labeled.instance(i));
        double[] dist = cls.distributionForInstance(labeled.instance(i));
        System.out.print((i + 1) + " - ");
        System.out.print(labeled.instance(i).toString(labeled.classIndex()) + " - ");
        System.out.print(labeled.classAttribute().value((int) pred) + " - ");
        System.out.println(Utils.arrayToString(dist));
    }
}