Example usage for weka.experiment InstanceQuery retrieveInstances

List of usage examples for weka.experiment InstanceQuery retrieveInstances

Introduction

In this page you can find the example usage for weka.experiment InstanceQuery retrieveInstances.

Prototype

public Instances retrieveInstances() throws Exception 

Source Link

Document

Makes a database query using the query set through the -Q option to convert a table into a set of instances

Usage

From source file:adams.gui.visualization.instance.InstanceExplorer.java

License:Open Source License

/**
 * pops up SQL Viewer for SQL statement.
 *//*from  www  . j a v a 2 s .c  o m*/
public void loadDataFromDatabase() {
    InstanceQuery query;
    List<InstanceContainer> data;
    Instances dataset;
    Instance inst;
    int i;

    if (m_LoadFromDatabaseDialog == null) {
        if ((getParentFrame() != null) && (getParentFrame() instanceof JFrame))
            m_LoadFromDatabaseDialog = new SqlViewerDialog((JFrame) getParentFrame());
        else
            m_LoadFromDatabaseDialog = new SqlViewerDialog(null);
    }

    m_LoadFromDatabaseDialog.setVisible(true);
    if (m_LoadFromDatabaseDialog.getReturnValue() != JOptionPane.OK_OPTION)
        return;

    try {
        showStatus("Executing query: " + m_LoadFromDatabaseDialog.getQuery());
        query = new InstanceQuery();
        query.setDatabaseURL(m_LoadFromDatabaseDialog.getURL());
        query.setUsername(m_LoadFromDatabaseDialog.getUser());
        query.setPassword(m_LoadFromDatabaseDialog.getPassword());
        query.setQuery(m_LoadFromDatabaseDialog.getQuery());
        query.setSparseData(m_LoadFromDatabaseDialog.getGenerateSparseData());
        if (query.isConnected())
            query.disconnectFromDatabase();
        query.connectToDatabase();

        showStatus("Loading data...");
        data = new ArrayList<>();
        dataset = query.retrieveInstances();
        for (i = 0; i < dataset.numInstances(); i++) {
            inst = new Instance();
            inst.set(dataset.instance(i));
            inst.setID(dataset.relationName() + "." + i);
            data.add(getContainerManager().newContainer(inst));
            showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances());
        }
        loadData(dataset, data);
    } catch (Exception e) {
        GUIHelper.showErrorMessage(this, "Failed to load data from database:\n" + Utils.throwableToString(e),
                "Database error");
    }

    showStatus("");
}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainSC() throws Exception {
    String sql;//from w  ww .j a va 2 s  .c  om

    // ---
    // Connect to the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    // ---
    // ---
    // SCA
    // ---
    // ---

    sql = "SELECT ";
    sql += "CR.ratio, CR.class ";
    sql += "FROM Class_ratio AS CR;";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    String[] options = Utils.splitOptions(
            "-D -Q weka.classifiers.bayes.net.search.local.K2 -- -P 1 -S BAYES -E weka.classifiers.bayes.net.estimate.SimpleEstimator -- -A 0.5");
    SCA.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCA ...");
    SCA.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCA...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(SCA, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCA: \n\n", false));

    // ---
    // ---
    // SCB
    // ---
    // ---

    sql = "SELECT ";
    sql += "Data.H2, Data.D2, Data.DX, ";
    sql += "Data.PARENT_CHAR AS PCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCB.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCB ...");
    SCB.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCB...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCB, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCB: \n\n", false));

    // ---
    // ---
    // SCC
    // ---
    // ---

    // ----
    // SCC1
    // ----

    sql = "SELECT ";
    sql += "Data.LH, Data.LD, Data.LDX, Data.LCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.LCLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC1.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC1 ...");
    SCC1.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC1...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC1, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC1: \n\n", false));

    // ----
    // SCC2
    // ----

    sql = "SELECT ";
    sql += "Data.EH, Data.ED, Data.EDX, Data.ECLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.ECLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC2.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC2 ...");
    SCC2.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC2...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC2, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC2: \n\n", false));

    // ----
    // SCC3
    // ----

    sql = "SELECT ";
    sql += "Data.SH, Data.SD, Data.SDX, Data.SCLASS, ";
    sql += "Data.CLASS ";
    sql += "FROM Data ";
    sql += "WHERE ( (Data.SEGERR)=0  AND ( (Data.SCLASS)<>'0' ) );";

    query.setQuery(sql);
    data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      options = Utils.splitOptions("-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    SCC3.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train the classifier
    // ---
    System.out.println("Building SCC3 ...");
    SCC3.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Classifier evaluation
    // ---
    System.out.println("Cross-validation for SCC3...");
    eval = new Evaluation(data);
    eval.crossValidateModel(SCC3, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for SCC3: \n\n", false));

}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainRC() throws Exception {
    // ---/*from  w w w .j  a va  2s.  c  o m*/
    // Retrieve the instances in the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    String sql = "SELECT ";
    sql += "Data.H2, Data.D2, Data.DX, ";
    sql += "Data.CLASS, Data.PARENT_CHAR AS PCLASS, ";
    sql += "Data.RELID ";
    sql += "FROM Data ";
    sql += "WHERE (((Data.SEGERR)=0) AND (Data.PARENT_CHAR<>'0') );";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    //      String[] options = Utils.splitOptions("-L 0.2 -M 0.2 -N 50 -V 0 -S 0 -E 20 -H 5 ");
    String[] options = Utils.splitOptions(
            "-cost-matrix \"[0.0 1.0 1.0 0.1 0.1; 1.0 0.0 1.0 0.1 0.1; 1.0 1.0 0.0 0.1 0.1; 10.0 10.0 10.0 0.0 1.0; 10.0 10.0 10.0 1.0 0.0]\" -S 1 -W weka.classifiers.functions.MultilayerPerceptron -- -L 0.2 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a");
    RC.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train
    // ---
    System.out.println("Building RC...");
    RC.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Evaluation
    // ---
    System.out.println("Cross-validation for RC...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(RC, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for RC: \n\n", false));

}

From source file:core.classification.Classifiers.java

License:Open Source License

public void trainYNC() throws Exception {
    // ---//www.j  a  va  2s.c  om
    // Retrieve the instances in the database
    // ---
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL(dbase);
    query.setUsername("");
    query.setPassword("");

    String sql = "SELECT ";
    sql += "YNCdata.PCLASS, YNCdata.CCLASS, YNCdata.RAREA, YNCdata.H, YNCdata.D, YNCdata.V, ";
    sql += "YNCdata.YN ";
    sql += "FROM YNCdata ";

    query.setQuery(sql);
    Instances data = query.retrieveInstances();

    // ---
    // Setting options
    // ---
    String[] options = Utils.splitOptions("-R -N 3 -Q 1 -M 30");
    YNC.setOptions(options);
    data.setClassIndex(data.numAttributes() - 1);

    // ---
    // Train
    // ---
    System.out.println("Building YC...");
    YNC.buildClassifier(data);
    System.out.println("Done.");

    // ---
    // Evaluation
    // ---
    System.out.println("Cross-validation for YNC...");
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(YNC, data, 10, new Random(1));
    System.out.println("Done.");
    System.out.println(eval.toSummaryString("\n Results for YNC: \n\n", false));

}

From source file:gr.uoc.nlp.opinion.analysis.suggestion.AnalyzeSuggestions.java

private Instances retriveTrainSet() {
    System.out.println("Retrieving dataset from Database..");

    InstanceQuery query;

    try {/*  w  ww  . j  a v  a  2s .com*/
        //initialize database, weka api
        query = new InstanceQuery();

        //set database attributes, weka apit
        query.setDatabaseURL(this.connection.getJdbcUrl());
        query.setUsername(this.connection.getUsername());
        query.setPassword(this.connection.getPassword());
        query.setQuery(this.queryTrainset());

        //retrieve trainset
        Instances data = query.retrieveInstances();
        data.setClassIndex(data.numAttributes() - 1);

        System.out.println("Done retrieving dataset from Database!");

        return data;
    } catch (Exception ex) {
        System.err.println("Abort!");
        Logger.getLogger(AnalyzeArguments.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:hurtowniedanych.FXMLController.java

public void trainAndTestKNN() throws FileNotFoundException, IOException, Exception {

    InstanceQuery instanceQuery = new InstanceQuery();
    instanceQuery.setUsername("postgres");
    instanceQuery.setPassword("szupek");
    instanceQuery.setCustomPropsFile(new File("./src/data/DatabaseUtils.props")); // Wskazanie pliku z ustawieniami dla PostgreSQL

    String query = "select ks.wydawnictwo,ks.gatunek, kl.mia-sto\n" + "from zakupy z,ksiazki ks,klienci kl\n"
            + "where ks.id_ksiazka=z.id_ksiazka and kl.id_klient=z.id_klient";

    instanceQuery.setQuery(query);//  w w w.  java2 s .c  o m
    Instances data = instanceQuery.retrieveInstances();
    data.setClassIndex(data.numAttributes() - 1);

    data.randomize(new Random());
    double percent = 70.0;
    int trainSize = (int) Math.round(data.numInstances() * percent / 100);
    int testSize = data.numInstances() - trainSize;
    Instances trainData = new Instances(data, 0, trainSize);
    Instances testData = new Instances(data, trainSize, testSize);

    int lSasiadow = Integer.parseInt(textFieldKnn.getText());
    System.out.println(lSasiadow);

    IBk ibk = new IBk(lSasiadow);

    // Ustawienie odleglosci
    EuclideanDistance euclidean = new EuclideanDistance(); // euklidesowej
    ManhattanDistance manhatan = new ManhattanDistance(); // miejska  

    LinearNNSearch linearNN = new LinearNNSearch();

    if (comboboxOdleglosc.getSelectionModel().getSelectedItem().equals("Manhatan")) {
        linearNN.setDistanceFunction(manhatan);
    } else {
        linearNN.setDistanceFunction(euclidean);
    }

    ibk.setNearestNeighbourSearchAlgorithm(linearNN); // ustawienie sposobu szukania sasiadow

    // Tworzenie klasyfikatora
    ibk.buildClassifier(trainData);

    Evaluation eval = new Evaluation(trainData);
    eval.evaluateModel(ibk, testData);
    spr.setVisible(true);
    labelKnn.setVisible(true);
    labelOdleglosc.setVisible(true);
    labelKnn.setText(textFieldKnn.getText());
    labelOdleglosc.setText(comboboxOdleglosc.getSelectionModel().getSelectedItem().toString());
    spr.setText(eval.toSummaryString("Wynik:", true));
}

From source file:newsclassifier.NewsClassifier.java

public void LoadDB() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    InstanceQuery query = new InstanceQuery();
    query.setDatabaseURL("jdbc:mysql://localhost:3306/news_aggregator");
    query.setUsername("root");
    query.setPassword("");
    //query.setQuery("SELECT full_text,id_kelas FROM artikel NATURAL JOIN artikel_kategori_verified");
    query.setQuery(/*from   w  w w  .  jav a  2 s. co m*/
            "SELECT judul,full_text,label FROM artikel NATURAL JOIN artikel_kategori_verified NATURAL JOIN kategori");
    data = query.retrieveInstances();
}

From source file:wtute.engine.AnalysisEngine.java

public AnalysisEngine() throws Exception {
    attributeList = new FastVector();
    Class.forName("com.mysql.jdbc.Driver").newInstance();

    InstanceQuery query = new InstanceQuery();
    query.setUsername("root");
    query.setPassword("");
    query.setQuery("select sentence,sentence_quality from sentences");
    //data set is sparse
    query.setSparseData(true);/*from   www. j  a v  a2  s .  c  o  m*/
    data = query.retrieveInstances();
    StringToWordVector nts = new StringToWordVector();
    nts.setInputFormat(data);
    data = Filter.useFilter(data, nts);
}