Example usage for weka.core Instances get

List of usage examples for weka.core Instances get

Introduction

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

Prototype



@Override
publicInstance get(int index) 

Source Link

Document

Returns the instance at the given position.

Usage

From source file:eu.linda.analytics.formats.RDFOutputFormat.java

@Override
public void exportData(Analytics analytics, AbstractList dataToExport) {
    if (dataToExport.size() != 0) {
        float timeToExportData = 0;
        long startTimeToExportData = System.currentTimeMillis();

        Instances triplets = (Instances) dataToExport;

        if (!helpfulFuncions.isURLValid(triplets.get(1).toString(0))) {
            helpfulFuncions.updateProcessMessageToAnalyticsTable(
                    "There is no valid URL as analytics input  node. \n So no RDF was created.\n"
                            + "Please select a different output format. ",
                    analytics.getId());//w w  w  . j  av  a 2s. co  m
            return;
        }

        helpfulFuncions.nicePrintMessage("Export to RDF");

        //save rdf file
        String targetFileName = ("results/analyticsID" + analytics.getId() + "_" + analytics.getAlgorithm_name()
                + "_resultdocument").replace("datasets", "results");
        String targetFileNameFullPath = Configuration.analyticsRepo + targetFileName;

        //create rdf file & save
        rdfGenerator = rdfGenerationFactory.createRDF(analytics.getCategory_id());

        Model model = rdfGenerator.generateRDFModel(analytics, dataToExport);
        String fileName = "";
        try {

            FileWriter outToSave = null;
            if (analytics.getExportFormat().equalsIgnoreCase("RDFXML")) {

                fileName = targetFileNameFullPath + ".rdf";
                targetFileName = targetFileName + ".rdf";
                outToSave = new FileWriter(fileName);
                model.write(outToSave, "RDF/XML-ABBREV");

            } else if (analytics.getExportFormat().equalsIgnoreCase("TTL")) {

                fileName = targetFileNameFullPath + ".ttl";
                targetFileName = targetFileName + ".ttl";
                outToSave = new FileWriter(fileName);
                model.write(outToSave, "TTL");

            } else if (analytics.getExportFormat().equalsIgnoreCase("NTRIPLES")) {

                fileName = targetFileNameFullPath + ".nt";
                targetFileName = targetFileName + ".nt";
                outToSave = new FileWriter(fileName);
                model.write(outToSave, "N3");

            }
            outToSave.close();
            System.out.println("RDF File save to:" + fileName);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(GeneralRDFGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GeneralRDFGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

        connectionController.updateLindaAnalytics(targetFileName, "resultdocument", analytics.getId());
        connectionController.updateLindaAnalyticsVersion(analytics.getVersion(), analytics.getId());
        connectionController.updateLindaAnalyticsRDFInfo("", false, analytics.getId());

        // Get elapsed time in milliseconds
        long elapsedTimeToExportData = System.currentTimeMillis() - startTimeToExportData;
        // Get elapsed time in seconds
        timeToExportData = elapsedTimeToExportData / 1000F;
        System.out.println("timeToExportData" + timeToExportData);
        analytics.setTimeToCreate_RDF(timeToExportData);
        connectionController.updateLindaAnalyticsProcessPerformanceTime(analytics);

    } else {
        helpfulFuncions.nicePrintMessage("There are no data to be exported to RDF");
        if (!analytics.getResultdocument().equalsIgnoreCase("")) {
            helpfulFuncions.cleanPreviousInfo(analytics);
        }
    }

}

From source file:feature_construction.GeneticProgramming.java

License:LGPL

public static double[][] convertInstancesToInputFeaturesArray(String fileName) {
    // Create instances (file that contains the inputs to feed through the program)
    double[][] inputFeatures;

    try {//from  w  w  w  .  j  av  a2 s  .c  om
        //load CSV
        CSVLoader loaderInputs = new CSVLoader();
        loaderInputs.setSource(new File(fileName));
        Instances inputSet = loaderInputs.getDataSet();
        inputSet.setClassIndex(inputSet.numAttributes() - 1);

        inputFeatures = new double[inputSet.numInstances()][inputSet.numAttributes()];

        // Convert instances to double[][]
        for (int i = 0; i < inputSet.numInstances(); i++) {
            for (int j = 0; j < inputSet.numAttributes(); j++) {
                inputFeatures[i][j] = inputSet.get(i).value(j);
            }
        }

        return inputFeatures;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:FFNN.MultiplePerceptron.java

@Override
public void buildClassifier(Instances i) {
    //iterasi/*from   w w  w  .j  a  v a2 s . c  o  m*/
    for (int itt = 0; itt < itteration; itt++) {
        //            System.out.println("Iterasi ke "+ itt);
        for (int indexInstance = 0; indexInstance < i.numInstances(); indexInstance++) {
            ArrayList<Double> listInput = new ArrayList<>();

            //mengisi nilai listInput dengan nilai di instances
            listInput.add(1.0);//ini bias input
            for (int index = 0; index < i.numAttributes() - 1; index++)
                listInput.add(i.get(indexInstance).value(index));

            ArrayList<Double> listOutputHidden = new ArrayList<>();
            listOutputHidden.add(1.0);//input bias
            //                System.out.println();
            //                System.out.println("Hidden layer");
            listNodeHidden.get(0).setValue(1.0);//bias gak boleh ganti output
            //menghitung output hidden layer
            for (int index = 1; index < listNodeHidden.size(); index++) {//output bias tidak boleh ganti
                double value = listNodeHidden.get(index).output(listInput);
                listNodeHidden.get(index).setValue(value);
                listOutputHidden.add(value);
                //                    System.out.println("neuron "+index+" "+value);
            }

            //                System.out.println();
            //                System.out.println("Output layer");
            //menghitung output output layer
            for (int index = 0; index < listNodeOutput.size(); index++) {
                double value = listNodeOutput.get(index).output(listOutputHidden);
                listNodeOutput.get(index).setValue(value);
                //                    System.out.print(value+" ");

            }

            //            System.out.println(listNodeHidden.get(1).getWeightFromList(0));   
            calculateError(indexInstance);

            updateBobot(i.instance(indexInstance));
        }
    }
    for (int idx = 0; idx < listNodeHidden.size(); idx++) {
        System.out.println("Hidden value " + listNodeHidden.get(idx).getValue());
        System.out.println("Hidden error " + listNodeHidden.get(idx).getError());
        for (int idx2 = 0; idx2 < listNodeHidden.get(idx).getWeightSize(); idx2++)
            System.out.println("Hidden weight" + listNodeHidden.get(idx).getWeightFromList(idx2));
    }
    System.out.println();
    for (int idx = 0; idx < listNodeOutput.size(); idx++) {
        System.out.println("Output value " + listNodeOutput.get(idx).getValue());
        System.out.println("Output error " + listNodeOutput.get(idx).getError());
        for (int idx2 = 0; idx2 < listNodeOutput.get(idx).getWeightSize(); idx2++)
            System.out.println("Output weight" + listNodeOutput.get(idx).getWeightFromList(idx2));
    }
}

From source file:ffnn.TucilWeka.java

public static String testInstance(Classifier cls, Instances testData) {
    String value = null;/*from   ww  w  .  j  ava 2  s .  com*/
    double result = 0;
    try {
        result = cls.classifyInstance(testData.get(0));
    } catch (Exception ex) {
        Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (result == 0) {
        value = "iris-setosa / 0";
    } else if (result == 1) {
        value = "iris-vesicolor / 1";
    } else if (result == 2) {
        value = "iris-virginica / 2";
    } else {
        value = "error";
    }

    return value;
}

From source file:gov.va.chir.tagline.TagLineScorer.java

License:Open Source License

public void applyModel(final Document document) throws Exception {
    // Calculate features at both document and line level      
    extractor.calculateFeatureValues(document);

    final Instances instances = DatasetUtil.createDataset(tagLineModel.getHeader(), document);

    for (int i = 0; i < instances.size(); i++) {
        final double[] probs = fc.distributionForInstance(instances.get(i));

        int maxPos = -1;
        double maxProb = 0.0;

        for (int j = 0; j < probs.length; j++) {
            if (probs[j] > maxProb) {
                maxProb = probs[j];//from  w  w w.  ja  v  a  2 s .  c o m
                maxPos = j;
            }
        }

        if (maxPos < 0) {
            throw new IllegalStateException(
                    String.format("Predicted label array index must not be negative (%d)", maxPos));
        }

        // Set predicted label and probability label to correct line
        final int lineId = (int) instances.get(i).value(lineIdAttr);
        document.getLine(lineId).setPredictedLabel(classAttr.value(maxPos));
        document.getLine(lineId).setPredictedProbability(maxProb);
    }
}

From source file:imba.classifier.FFNNTubes.java

private void setTarget(Instances data) {
    target = new double[nData][nOutput];

    for (int i = 0; i < nData; i++) {
        Instance current = data.get(i);
        for (int j = 0; j < nOutput; j++) {
            if (j == current.classValue()) {
                target[i][j] = 1.0;//from ww w  . ja v  a 2 s .  co  m
            } else {
                target[i][j] = 0.0;
            }
        }
    }
}

From source file:imba.classifier.FFNNTubes.java

@Override
public void buildClassifier(Instances data) throws Exception {
    getCapabilities().testWithFail(data);

    data.deleteWithMissingClass();//from ww w .  ja  v  a 2 s . c om

    nAttribute = data.numAttributes() - 1;
    nOutput = data.numClasses();
    nData = data.size();

    //set target data
    setTarget(data);

    //generate weight
    generateRandomWeight();

    //normalisasi data
    Normalize norm = new Normalize();
    Filter filter = new NominalToBinary();

    norm.setInputFormat(data);

    Instances filteredData = Filter.useFilter(data, norm);

    try {
        filter.setInputFormat(filteredData);

        for (Instance i1 : filteredData) {
            filter.input(i1);
        }

        filter.batchFinished();
    } catch (Exception ex) {
        Logger.getLogger(NBTubes.class.getName()).log(Level.SEVERE, null, ex);
    }

    int z = 0;
    double valMSE = 100.0;
    while ((z <= nEpoch) && (valMSE >= 0.00001)) {
        for (int j = 0; j < nData; j++) {
            feedForward(filteredData.get(j));

            if (nHidden == 0) {
                updateWeight(target[j]);
            } else {
                backPropagation(target[j]);
            }
        }

        countError(filteredData);
        valMSE = countMSE(filteredData);
        System.out.println("ACCURACY " + z + " : " + accuracy);
        System.out.println("MSE " + z + " : " + valMSE);
        z++;
    }
}

From source file:imba.classifier.FFNNTubes.java

private void countError(Instances a) throws Exception {
    int error = 0;
    for (int i = 0; i < nData; i++) {
        double temp = classifyInstance(a.get(i));
        if (temp != a.get(i).classValue()) {
            error = error + 1;/* w  ww. j  ava2  s . c o  m*/
        }
    }
    accuracy = ((double) nData - (double) error) / (double) nData;
}

From source file:imba.classifier.FFNNTubes.java

private double countMSE(Instances a) throws Exception {
    double[] mseTable = new double[nData];
    double result = 0.0;

    for (int i = 0; i < nData; i++) {
        double out = classifyInstance(a.get(i));
        double tar = a.get(i).classValue();
        double selisih = tar - out;
        mseTable[i] = Math.pow(selisih, 2.0) / (double) nData;
    }//from   w w  w . j  a  v a  2  s.c o  m

    double sum = 0.0;
    for (int i = 0; i < nData; i++) {
        sum = sum + mseTable[i];
    }

    result = sum / (double) nData;

    return result;
}

From source file:jmetal.problems.SurvivalAnalysis.java

License:Open Source License

/** 
 * Evaluates a solution /*from  w  ww  .j  ava2 s  . c om*/
 * @param solution The solution to evaluate
 */
public void evaluate(Solution solution) {
    Binary variable;
    int counterSelectedFeatures;

    DataSource source;

    double testStatistic = Double.MAX_VALUE;
    double pValue = Double.MAX_VALUE;
    double ArithmeticHarmonicCutScore = Double.MAX_VALUE;
    //double statScore;
    REXP x;

    variable = ((Binary) solution.getDecisionVariables()[0]);

    counterSelectedFeatures = 0;

    try {
        // read the data file 
        source = new DataSource(this.dataFileName);
        Instances data = source.getDataSet();
        //System.out.print("Data read successfully. ");
        //System.out.print("Number of attributes: " + data.numAttributes());
        //System.out.println(". Number of instances: " + data.numInstances());

        // save the attribute 'T' and 'Censor'
        attTime = data.attribute(data.numAttributes() - 2);
        attCensor = data.attribute(data.numAttributes() - 1);

        // First filter the attributes based on chromosome
        Instances tmpData = this.filterByChromosome(data, solution);

        // Now filter the attribute 'T' and 'Censor'
        Remove filter = new Remove();
        // remove the two last attributes : 'T' and 'Censor'
        filter.setAttributeIndices("" + (tmpData.numAttributes() - 1) + "," + tmpData.numAttributes());
        //System.out.println("After chromosome filtering no of attributes: " + tmpData.numAttributes());
        filter.setInputFormat(tmpData);
        Instances dataClusterer = Filter.useFilter(tmpData, filter);

        // filtering complete

        /*
        // debug: write the filtered dataset
                
         ArffSaver saver = new ArffSaver();
         saver.setInstances(dataClusterer);
         saver.setFile(new File("filteered-data.arff"));
         saver.writeBatch();
        // end debug
                
        */

        // train hierarchical clusterer

        HierarchicalClusterer clusterer = new HierarchicalClusterer();
        clusterer.setOptions(new String[] { "-L", this.HC_LinkType }); // complete linkage clustering
        //Link type (Single, Complete, Average, Mean, Centroid, Ward, Adjusted complete, Neighbor Joining)
        //[SINGLE|COMPLETE|AVERAGE|MEAN|CENTROID|WARD|ADJCOMPLETE|NEIGHBOR_JOINING]

        //clusterer.setDebug(true);
        clusterer.setNumClusters(2);
        clusterer.setDistanceFunction(new EuclideanDistance());
        clusterer.setDistanceIsBranchLength(false); // ?? Should it be changed to false? (Noman)

        clusterer.buildClusterer(dataClusterer);

        double[][] distanceMatrix = clusterer.getDistanceMatrix();
        // save the cluster assignments

        if (this.re == null) { // we are not calling R functions. Therefore parallelization possible

            int[] clusterAssignment = new int[dataClusterer.numInstances()];
            int classOneCnt = 0;
            int classTwoCnt = 0;
            for (int i = 0; i < dataClusterer.numInstances(); ++i) {
                clusterAssignment[i] = clusterer.clusterInstance(dataClusterer.get(i));
                if (clusterAssignment[i] == 0) {
                    ++classOneCnt;
                } else if (clusterAssignment[i] == 1) {
                    ++classTwoCnt;
                }
                //System.out.println("Instance " + i + ": " + clusterAssignment[i]);
            }

            //System.out.println("Class 1 cnt: " + classOneCnt + " Class 2 cnt: " + classTwoCnt);

            // create arrays with time (event occurrence time) and censor data for use with jstat LogRankTest
            double[] time1 = new double[classOneCnt];
            double[] censor1 = new double[classOneCnt];
            double[] time2 = new double[classTwoCnt];
            double[] censor2 = new double[classTwoCnt];

            //data = source.getDataSet();
            for (int i = 0, cnt1 = 0, cnt2 = 0; i < dataClusterer.numInstances(); ++i) {
                //clusterAssignment[i] = clusterer.clusterInstance(dataClusterer.get(i));
                if (clusterAssignment[i] == 0) {
                    time1[cnt1] = data.get(i).value(attTime);
                    censor1[cnt1++] = data.get(i).value(attCensor);
                    //System.out.println("i: " + i + " T: " + time1[cnt1-1]);
                } else if (clusterAssignment[i] == 1) {
                    time2[cnt2] = data.get(i).value(attTime);
                    //System.out.println("i: " + i + " T: " + time2[cnt2-1]);
                    censor2[cnt2++] = data.get(i).value(attCensor);
                    ;
                }
                //System.out.println("Instance " + i + ": " + clusterAssignment[i]);
            }

            //Instances[] classInstances = separateClassInstances(clusterAssignment, this.dataFileName,solution);
            //System.out.println("Class instances seperated");

            // calculate log rank test and p values

            LogRankTest testclass1 = new LogRankTest(time1, time2, censor1, censor2);
            double[] scores = testclass1.logRank();
            testStatistic = scores[0];
            pValue = scores[2];

            ArithmeticHarmonicCutScore = this.getArithmeticHarmonicCutScore(distanceMatrix, clusterAssignment);
            //debug:
            //System.out.println("Calculation by myLibrary: testStatistic: " + scores[0] + " pValue: " + scores[2]);
            //end debug
            //WilcoxonTest testclass1 = new WilcoxonTest(time1, censor1, time2, censor2);
            //testStatistic = testclass1.testStatistic;
            //pValue = testclass1.pValue;true
        } else { // We are calling R for Log Rank test, Parallelization not possible

            String strT = "time <- c(";
            String strC = "censor <- c(";
            String strG = "group <- c(";

            for (int i = 0; i < dataClusterer.numInstances() - 1; ++i) {
                strT = strT + (int) data.get(i).value(attTime) + ",";
                strG = strG + clusterer.clusterInstance(dataClusterer.get(i)) + ",";
                strC = strC + (int) data.get(i).value(attCensor) + ",";
            }

            int tmpi = dataClusterer.numInstances() - 1;
            strT = strT + (int) data.get(tmpi).value(attTime) + ")";
            strG = strG + clusterer.clusterInstance(dataClusterer.get(tmpi)) + ")";
            strC = strC + (int) data.get(tmpi).value(attCensor) + ")";

            this.re.eval(strT);
            this.re.eval(strC);
            this.re.eval(strG);

            //debug
            //System.out.println(strT);
            //System.out.println(strC);
            //System.out.println(strG);
            //end debug

            /** If you are calling surv_test from coin library */
            /*v
            re.eval("library(coin)");
            re.eval("grp <- factor (group)");
            re.eval("result <- surv_test(Surv(time,censor)~grp,distribution=\"exact\")");
                    
            x=re.eval("statistic(result)");
            testStatistic = x.asDouble();
            //x=re.eval("pvalue(result)");
            //pValue = x.asDouble();
            //System.out.println("StatScore: " + statScore + "pValue: " + pValue);
             */

            /** If you are calling survdiff from survival library (much faster) */
            re.eval("library(survival)");
            re.eval("res2 <- survdiff(Surv(time,censor)~group,rho=0)");
            x = re.eval("res2$chisq");
            testStatistic = x.asDouble();
            //System.out.println(x);
            x = re.eval("pchisq(res2$chisq, df=1, lower.tail = FALSE)");
            //x = re.eval("1.0 - pchisq(res2$chisq, df=1)");
            pValue = x.asDouble();
            //debug:
            //System.out.println("Calculation by R: StatScore: " + testStatistic + "pValue: " + pValue);
            //end debug

        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        System.err.println("Can't open the data file.");
        e.printStackTrace();
        System.exit(1);
    }

    /**********
     *  Current Implementation considers two objectives
     *  1. pvalue to be minimized / statistical score to be maximized
     *  2. Number of Features to be maximized/minimized
     */

    // Currently this section implements the OneZeroMax problem - need to modify it
    for (int i = 0; i < variable.getNumberOfBits(); i++)
        if (variable.bits_.get(i))
            counterSelectedFeatures++;

    // OneZeroMax is a maximization problem: multiply by -1 to minimize
    /*
    if (Double.isNaN(testStatistic)){
       solution.setObjective(0,Double.MAX_VALUE);
    }
    else{
       solution.setObjective(0, testStatistic);
    }
    */

    if (this.pValueFlag) {
        solution.setObjective(0, pValue); // pValue to be minimized
    } else {
        solution.setObjective(0, -1.0 * testStatistic); // statistic score to be maximized
    }
    if (this.featureMax) {
        solution.setObjective(1, -1.0 * counterSelectedFeatures); // feature maximized
    } else {
        solution.setObjective(1, counterSelectedFeatures); // feature minimized
    }
    if (this.numberOfObjectives_ == 3) {
        solution.setObjective(2, -1.0 * ArithmeticHarmonicCutScore); // feature maximized
    }
}