Example usage for weka.core Instances firstInstance

List of usage examples for weka.core Instances firstInstance

Introduction

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

Prototype


publicInstance firstInstance() 

Source Link

Document

Returns the first instance in the set.

Usage

From source file:classifier.SellerClassifier.java

private Instance readToInstance(String str) throws Exception {
    File f = new File("buffer.arff");
    try (FileWriter fw = new FileWriter(f, false)) {
        fw.write("@relation buffer\n\n@attribute\ttweet\tstring\n@attribute\tclass\t{0,1}\n\n@data\n");
        fw.write("\"" + str + "\",1\n");
    }//from   ww w  . j av  a2s . co m

    Instances instances = loadData("buffer.arff");

    System.out.println(instances);

    assert (instances != null);
    assert (myFilter != null);
    Instances ins = Filter.useFilter(instances, myFilter);

    return ins.firstInstance();
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static void testCOMT2() throws Exception {
    BestConf bestconf = new BestConf();
    Instances trainingSet = DataIOFile.loadDataFromArffFile("data/trainingBestConf0.arff");
    trainingSet.setClassIndex(trainingSet.numAttributes() - 1);

    Instances samplePoints = LHSInitializer.getMultiDimContinuous(bestconf.getAttributes(),
            InitialSampleSetSize, false);
    samplePoints.insertAttributeAt(trainingSet.classAttribute(), samplePoints.numAttributes());
    samplePoints.setClassIndex(samplePoints.numAttributes() - 1);

    COMT2 comt = new COMT2(samplePoints, COMT2Iteration);

    comt.buildClassifier(trainingSet);/*from  w w  w  .j  av a2 s.  c o  m*/

    Evaluation eval = new Evaluation(trainingSet);
    eval.evaluateModel(comt, trainingSet);
    System.err.println(eval.toSummaryString());

    Instance best = comt.getInstanceWithPossibleMaxY(samplePoints.firstInstance());
    Instances bestInstances = new Instances(trainingSet, 2);
    bestInstances.add(best);
    DataIOFile.saveDataToXrffFile("data/trainingBestConf_COMT2.arff", bestInstances);

    //now we output the training set with the class value updated as the predicted value
    Instances output = new Instances(trainingSet, trainingSet.numInstances());
    Enumeration<Instance> enu = trainingSet.enumerateInstances();
    while (enu.hasMoreElements()) {
        Instance ins = enu.nextElement();
        double[] values = ins.toDoubleArray();
        values[values.length - 1] = comt.classifyInstance(ins);
        output.add(ins.copy(values));
    }
    DataIOFile.saveDataToXrffFile("data/trainingBestConf0_predict.xrff", output);
}

From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java

License:Open Source License

public static void getBestPerfFrom(String path) {
    try {//from  w  w  w .  j a  v a2  s. c om
        BestConf bestconf = new BestConf();
        Instances trainingSet = DataIOFile.loadDataFromArffFile(path);
        Instance best = trainingSet.firstInstance();
        //set the best configuration to the cluster
        Map<Attribute, Double> attsmap = new HashMap<Attribute, Double>();
        for (int i = 0; i < best.numAttributes() - 1; i++) {
            attsmap.put(best.attribute(i), best.value(i));
        }

        double bestPerf = bestconf.setOptimal(attsmap, "getBestPerfFrom");
        System.out.println("=========================================");
        System.err.println("The actual performance for the best point is : " + bestPerf);
        System.out.println("=========================================");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.mechaglot_Alpha2.controller.Calculate.java

License:Creative Commons License

/**
 * /*from   ww w .  jav  a  2 s  .com*/
 * @param in
 *            String representing the calculated String-metric distances,
 *            comma separated.
 * @return Instance The inputted series of numbers (comma separated) as
 *         Instance.
 */

private Instance instanceMaker(String in) {

    String[] s = in.split(",");
    double[] r = new double[s.length];
    for (int t = 0; t < r.length; t++) {
        r[t] = Double.parseDouble(s[t]);
    }

    int sz = r.length - 1;

    ArrayList<Attribute> atts = new ArrayList<Attribute>(sz);

    for (int t = 0; t < sz + 1; t++) {
        atts.add(new Attribute("number" + t, t));
    }

    Instances dataRaw = new Instances("TestInstances", atts, sz);
    dataRaw.add(new DenseInstance(1.0, r));
    Instance first = dataRaw.firstInstance(); //
    int cIdx = dataRaw.numAttributes() - 1;
    dataRaw.setClassIndex(cIdx);

    return first;

}

From source file:com.relationalcloud.main.Explanation.java

License:Open Source License

/**
 * @param args//from w  ww.j av  a  2  s  .c  om
 */
public static void main(String[] args) {

    // LOADING PROPERTY FILE AND DRIVER
    Properties ini = new Properties();
    try {
        ini.load(new FileInputStream(System.getProperty("prop")));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Register jdbcDriver
    try {
        Class.forName(ini.getProperty("driver"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    // LOAD PROPERTIES FROM CONFIGURATION FILE
    String connection = ini.getProperty("conn");
    String schemaname = ini.getProperty("schema");

    String user = ini.getProperty("user");
    String password = ini.getProperty("password");
    String txnLogTable = ini.getProperty("txnLogTable");
    String numb_trans_to_process = ini.getProperty("Explanation.numTxnsToExtractTemplates");

    int numPart = Integer.parseInt(ini.getProperty("numPartitions"));

    // Initialize the Justification Handler
    ExplanationHandler jh = new ExplanationHandler(ini);

    System.out.println("Loading and processing " + jh.schemaname + " traces... considering prop file :"
            + jh.dbPropertyFile);

    try {

        // CREATE A DB CONNEctioN
        Connection conn = DriverManager.getConnection(connection + schemaname, user, password);
        Connection infschema_conn = DriverManager.getConnection(connection + "information_schema", user,
                password);

        Schema schema = SchemaLoader.loadSchemaFromDB(infschema_conn, schemaname);

        // ANALYZE WORKLOADS EXTRACTING TABLES, ATTRIBUTES AND FREQUENCIES
        ExplanationWorkloadPrepocessor wa = ExplanationHandler.analyzeWorkload(txnLogTable,
                numb_trans_to_process, schemaname, conn, schema);

        // FOR EACH TABLE CLASSIFY AND POPULATE JUSTIFICATION COLUMN
        for (String tableProcessed : wa.getAllTableNames()) {

            System.out.println("-------------------------------------------");
            System.out.println("ANALYZING TABLE " + tableProcessed);

            // FETCH THE INSTANCE FROM THE DB AND SAMPLE IT
            Instances data = jh.generateInstancesForTable(tableProcessed, wa.getFeatures(tableProcessed), conn);

            // IF THERE IS ONLY THE PARTITION LABEL, SKIP THE TABLE
            if (data.numAttributes() < 2) {
                System.out.println("No transactions touches this table, nothing to be done.");
                continue;
            }
            // INSTANTIATE THE CLASSIFIER
            String[] options;
            options = new String[3];
            options[0] = "-P";
            options[1] = "-C";
            options[2] = ini.getProperty("Explanation.j48PruningConfidence");
            J48 classifier = new J48(); // new instance of tree
            classifier.setOptions(options); // set the options

            Boolean attributeFilter = true;
            // ATTRIBUTE FILTERING
            Instances newData;
            if (data.numClasses() > 1 && attributeFilter) {
                AttributeSelection filter = new AttributeSelection();

                //FIXME TRYING ALTERNATIVE ATTRIBUTE SELECTION STRATEGIES
                //InfoGainAttributeEval eval = new InfoGainAttributeEval();
                //Ranker search = new Ranker();
                //search.setNumToSelect(Integer.parseInt(ini.getProperty("Explanation.maxNumberOfAttribute","2")));
                CfsSubsetEval eval = new CfsSubsetEval();
                GreedyStepwise search = new GreedyStepwise();

                search.setSearchBackwards(true);
                filter.setEvaluator(eval);
                filter.setSearch(search);
                filter.setInputFormat(data);
                newData = Filter.useFilter(data, filter);
            } else {
                newData = data;
            }

            String atts = "";
            Enumeration e = newData.enumerateAttributes();
            ArrayList<String> attributesForPopulation = new ArrayList<String>();
            while (e.hasMoreElements()) {
                String s = ((Attribute) e.nextElement()).name();
                attributesForPopulation.add(s);
                atts += s + ", ";
            }
            atts = atts.substring(0, atts.length() - 2);

            System.out.println("Attribute filtering reduced " + (data.numAttributes() - 1) + " to "
                    + (newData.numAttributes() - 1) + " (" + atts + ")");

            data = null;
            System.gc();

            if (newData.numInstances() < 1) {
                System.err.println("The are no data in the table, skipping classification");
                continue;
            }

            if (newData.numInstances() > 0) {
                if (newData.classAttribute().numValues() > 1) {
                    // TRAIN THE CLASSIFIER AND PRINT OUT CLASSIFIER RULES
                    ExplanationHandler.trainClassifier(newData, classifier);

                    if (classifier.measureNumLeaves() == 1) {

                        int partitionvalue = (int) classifier.classifyInstance(newData.firstInstance());
                        System.out.println(
                                "The classifier decided to put all the tuplesi in the table in one partition: "
                                        + partitionvalue);
                        if (Boolean.parseBoolean(ini.getProperty("Explanation.populateExplainedColumn"))) {
                            jh.populateExplainedColumn(tableProcessed, partitionvalue, attributesForPopulation,
                                    conn);
                        }

                    }

                    // POPULATING THE justifiedpartition column with the result of this
                    // classifier if required
                    else if (Boolean.parseBoolean(ini.getProperty("Explanation.populateExplainedColumn"))) {
                        jh.populateJustifiedColumn(tableProcessed, classifier, attributesForPopulation, conn,
                                numPart, newData.classAttribute().enumerateValues());
                    }

                } else { // easy case... the class attribute is unary!!
                    int partitionvalue = ((int) newData.firstInstance()
                            .value(newData.firstInstance().classIndex()));
                    System.out.println("The table is all stored in one partition, no need to use classifier");
                    if (Boolean.parseBoolean(ini.getProperty("Explanation.populateExplainedColumn"))) {
                        jh.populateExplainedColumn(tableProcessed, partitionvalue, attributesForPopulation,
                                conn);
                    }
                }
            } else
                throw new Exception("The Instances is empty");

        }

        // SET HASH PARTITION / REPLICATED PARTITION
        if (Boolean.parseBoolean(ini.getProperty("Explanation.populateHashColumn"))) {
            jh.populateHashPartition(conn);
        }

        if (Boolean.parseBoolean(ini.getProperty("Explanation.populateReplicatedColumn"))) {
            jh.populateReplicatedPartition(conn,
                    Boolean.parseBoolean(ini.getProperty("Explanation.defaultReplicate")));
        }

        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.sensyscal.activityrecognition2.utils.Classifiers.java

License:LGPL

public static int customKnnClassifier(double[] newInstanceArray) {
    // TODO Auto-generated method stub
    ts1 = new Timestamp(System.currentTimeMillis());
    int activityId = 0;
    String classLabel = "";
    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    ArrayList<String> classVal = new ArrayList<String>();
    classVal.add("STANDING");
    classVal.add("SITTING");
    classVal.add("LYINGDOWN");
    classVal.add("WALKING");
    atts.add(new Attribute("class", classVal));
    atts.add(new Attribute("1_1_2_1"));
    atts.add(new Attribute("1_1_3_1"));
    atts.add(new Attribute("1_1_9_2"));
    atts.add(new Attribute("2_1_3_1"));
    atts.add(new Attribute("2_1_4_1"));
    atts.add(new Attribute("2_1_9_2"));
    Instances dataUnlabeled = new Instances("TestInstances", atts, 0);
    dataUnlabeled.add(new DenseInstance(1.0, newInstanceArray));
    dataUnlabeled.setClassIndex(0);/*from w w w .  j ava  2 s.c o  m*/
    try {
        activityId = (int) (MonitoringWorkerThread.cls.classifyInstance(dataUnlabeled.firstInstance()));
        classLabel = dataUnlabeled.firstInstance().classAttribute().value(activityId);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ts = new Timestamp(System.currentTimeMillis());
    // Log.e("classifyActivity Knn," -> Impiegati:
    // "+(ts.getTime()-ts1.getTime())+" ms;\n");
    return getActivityIDofClassLabel(classLabel);
}

From source file:com.sensyscal.activityrecognition2.utils.Classifiers.java

License:LGPL

public static int customJRipClassifier(double[] newInstanceArray) {
    // TODO Auto-generated method stub
    ts1 = new Timestamp(System.currentTimeMillis());
    int activityId = 0;
    String classLabel = "";
    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    ArrayList<String> classVal = new ArrayList<String>();
    classVal.add("STANDING");
    classVal.add("WALKING");
    classVal.add("SITTING");
    classVal.add("LYINGDOWN");
    atts.add(new Attribute("class", classVal));
    atts.add(new Attribute("1_1_2_1"));
    atts.add(new Attribute("1_1_3_1"));
    atts.add(new Attribute("1_1_9_2"));
    atts.add(new Attribute("2_1_3_1"));
    atts.add(new Attribute("2_1_4_1"));
    atts.add(new Attribute("2_1_9_2"));

    Instances dataUnlabeled = new Instances("TestInstances", atts, 0);
    dataUnlabeled.add(new DenseInstance(1.0, newInstanceArray));
    dataUnlabeled.setClassIndex(0);//from ww  w .j  a  v a  2  s. co m
    try {
        activityId = (int) MonitoringWorkerThread.cls.classifyInstance(dataUnlabeled.firstInstance());
        Log.i("classifyActivity JRip ---->", activityId + "");
        classLabel = dataUnlabeled.firstInstance().classAttribute().value((int) activityId);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ts = new Timestamp(System.currentTimeMillis());
    // Log.i("classifyActivity JRip"," -> Impiegati: "+(ts.getTime()-ts1.getTime())+" ms;\n");
    return getActivityIDofClassLabel(classLabel);
}

From source file:com.sensyscal.activityrecognition2.utils.Classifiers.java

License:LGPL

public static int customJ48Classifier(double[] newInstanceArray) {
    // TODO Auto-generated method stub
    ts1 = new Timestamp(System.currentTimeMillis());
    int activityId = 0;
    String classLabel = "";
    ArrayList<Attribute> atts = new ArrayList<Attribute>();
    ArrayList<String> classVal = new ArrayList<String>();
    classVal.add("STANDING");
    classVal.add("SITTING");
    classVal.add("LYINGDOWN");
    classVal.add("WALKING");
    atts.add(new Attribute("class", classVal));
    atts.add(new Attribute("1_1_2_1"));
    atts.add(new Attribute("1_1_3_1"));
    atts.add(new Attribute("1_1_9_2"));
    atts.add(new Attribute("2_1_3_1"));
    atts.add(new Attribute("2_1_4_1"));
    atts.add(new Attribute("2_1_9_2"));
    Instances dataUnlabeled = new Instances("TestInstances", atts, 0);
    dataUnlabeled.add(new DenseInstance(1.0, newInstanceArray));
    dataUnlabeled.setClassIndex(0);/*  www.  j  a v  a 2s  .  com*/
    try {
        activityId = (int) getJ48ActivityId(
                MonitoringWorkerThread.cls.classifyInstance(dataUnlabeled.firstInstance()));
        classLabel = dataUnlabeled.firstInstance().classAttribute().value((int) activityId);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ts = new Timestamp(System.currentTimeMillis());
    // Log.e("classifyActivity J48"," -> Impiegati: "+(ts.getTime()-ts1.getTime())+" ms;\n");
    return activityId;// getActivityIDofClassLabel(classLabel);
}

From source file:cz.vse.fis.keg.entityclassifier.core.salience.EntitySaliencer.java

License:Open Source License

public void computeSalience(List<Entity> entities) {
    try {/*from  w w  w.j a  v  a  2s  .co m*/
        if (!initialized) {
            initialize();
            initialized = true;
        }

        ArrayList<SEntity> processedEntities = new ArrayList<SEntity>();

        for (Entity e : entities) {
            SEntity entityMention = new SEntity();
            entityMention.setBeginIndex(e.getStartOffset().intValue());
            entityMention.setEntityType(e.getEntityType());

            ArrayList<Type> types = e.getTypes();
            ArrayList<String> loggedURIs = new ArrayList<String>();

            if (types != null) {
                for (Type t : types) {
                    String entityURI = t.getEntityURI();

                    if (!loggedURIs.contains(entityURI)) {
                        loggedURIs.add(entityURI);
                        entityMention.getUrls().add(entityURI);
                    }
                }
            }

            boolean entityAlreadyLogged = false;

            for (SEntity sEntity : processedEntities) {
                boolean isThisEntitySame = false;
                ArrayList<String> entityURIs1 = sEntity.getUrls();
                ArrayList<String> entityURIs2 = entityMention.getUrls();

                for (String eURI1 : entityURIs1) {
                    for (String eURI2 : entityURIs2) {
                        if (!entityAlreadyLogged) {
                            if (eURI1.equals(eURI2)) {
                                entityAlreadyLogged = true;
                                isThisEntitySame = true;
                                sEntity.setNumOccurrences(sEntity.getNumOccurrences() + 1);
                            }
                        }
                    }
                }

                if (isThisEntitySame) {
                    for (String uri : entityMention.getUrls()) {
                        if (!sEntity.getUrls().contains(uri)) {
                            sEntity.getUrls().add(uri);
                        }
                    }
                }
            }

            // Entity seen for first time in the document.
            if (!entityAlreadyLogged) {
                entityMention.setNumOccurrences(1);
                processedEntities.add(entityMention);
            }
        }

        // Preparing the test data container.
        FastVector attributes = new FastVector(6);
        attributes.add(new Attribute("beginIndex"));
        attributes.add(new Attribute("numUniqueEntitiesInDoc"));
        attributes.add(new Attribute("numOfOccurrencesOfEntityInDoc"));
        attributes.add(new Attribute("numOfEntityMentionsInDoc"));

        FastVector entityTypeNominalAttVal = new FastVector(2);
        entityTypeNominalAttVal.addElement("named_entity");
        entityTypeNominalAttVal.addElement("common_entity");

        Attribute entityTypeAtt = new Attribute("type", entityTypeNominalAttVal);
        attributes.add(entityTypeAtt);
        FastVector classNominalAttVal = new FastVector(3);
        classNominalAttVal.addElement("not_salient");
        classNominalAttVal.addElement("less_salient");
        classNominalAttVal.addElement("most_salient");
        Attribute classAtt = new Attribute("class", classNominalAttVal);
        attributes.add(classAtt);
        Instances evalData = new Instances("MyRelation", attributes, 0);

        evalData.setClassIndex(evalData.numAttributes() - 1);

        for (int i = 0; i < processedEntities.size(); i++) {

            String entityType = "";
            if (processedEntities.get(i).getEntityType().equals("named entity")) {
                entityType = "named_entity";
            } else if (processedEntities.get(i).getEntityType().equals("common entity")) {
                entityType = "common_entity";
            } else {
            }
            Instance inst = new DenseInstance(6);
            inst.setValue(evalData.attribute(0), processedEntities.get(i).getBeginIndex()); // begin index
            inst.setValue(evalData.attribute(1), processedEntities.size()); // num of unique entities in doc
            inst.setValue(evalData.attribute(2), processedEntities.get(i).getNumOccurrences()); // num of entity occurrences in doc
            inst.setValue(evalData.attribute(3), entities.size()); // num of entity mentions in doc
            inst.setValue(evalData.attribute(4), entityType); // type of the entity
            evalData.add(inst);

        }

        for (int i = 0; i < processedEntities.size(); i++) {
            SEntity sEntity = processedEntities.get(i);
            int classIndex = (int) classifier.classifyInstance(evalData.get(i));
            String classLabel = evalData.firstInstance().classAttribute().value(classIndex);
            double pred[] = classifier.distributionForInstance(evalData.get(i));
            double probability = pred[classIndex];

            double salienceScore = pred[1] * 0.5 + pred[2];
            sEntity.setSalienceScore(salienceScore);
            sEntity.setSalienceConfidence(probability);
            sEntity.setSalienceClass(classLabel);

            for (Entity e : entities) {
                ArrayList<Type> types = e.getTypes();
                if (types != null) {
                    for (Type t : types) {
                        if (sEntity.getUrls().contains(t.getEntityURI())) {
                            Salience s = new Salience();
                            s.setClassLabel(classLabel);
                            DecimalFormat df = new DecimalFormat("0.000");
                            double fProbability = df.parse(df.format(probability)).doubleValue();
                            double fSalience = df.parse(df.format(salienceScore)).doubleValue();
                            s.setConfidence(fProbability);
                            s.setScore(fSalience);
                            t.setSalience(s);
                        }
                    }
                }
            }
        }

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

From source file:de.ugoe.cs.cpdp.dataselection.SetWiseEMContextSelection.java

License:Apache License

/**
 * Delete projects where the project context does not match the training project
 * /*from w ww  .java2  s. c  om*/
 * @param testdata
 * @param traindataSet
 * @param attribute
 */
protected void removeWrongContext(Instances testdata, SetUniqueList<Instances> traindataSet, String attribute) {
    Set<Instances> remove = new HashSet<Instances>();
    for (Instances traindata : traindataSet) {
        if (traindata.firstInstance().value(traindata.attribute(attribute)) != testdata.firstInstance()
                .value(testdata.attribute(attribute))) {
            remove.add(traindata);
            // Console.traceln(Level.WARNING,
            // "rmove attribute "+attribute+" test:
            // "+testdata.firstInstance().value(testdata.attribute(attribute))+" train:
            // "+traindata.firstInstance().value(traindata.attribute(attribute)));
        }
    }

    // now delete the projects from set
    for (Instances i : remove) {
        traindataSet.remove(i);
        // Console.traceln(Level.INFO, "removing training project from set");
    }
}