Example usage for weka.experiment InstanceQuery setDatabaseURL

List of usage examples for weka.experiment InstanceQuery setDatabaseURL

Introduction

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

Prototype

public void setDatabaseURL(String newDatabaseURL) 

Source Link

Document

Set the value of DatabaseURL.

Usage

From source file:adams.flow.transformer.WekaExperimentEvaluation.java

License:Open Source License

/**
 * Loads the experimental results./*  w w  w .ja v a  2  s  . c om*/
 *
 * @param exp      the experiment to evaluate
 * @return      the results
 * @throws Exception    If reading fails.
 */
protected Instances getData(weka.experiment.Experiment exp) throws Exception {
    Instances result;
    String tmpStr;
    DatabaseResultListener dbListener;
    InstanceQuery query;

    // load experiment data
    if (exp.getResultListener() instanceof CSVResultListener) {
        try {
            result = DataSource
                    .read(((CSVResultListener) exp.getResultListener()).getOutputFile().getAbsolutePath());
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Failed to get instances from CSV:", e);
            throw new Exception("Error reading experiment from file:\n" + e);
        }
    } else if (exp.getResultListener() instanceof DatabaseResultListener) {
        dbListener = (DatabaseResultListener) exp.getResultListener();
        try {
            tmpStr = dbListener.getResultsTableName(exp.getResultProducer());
            query = new InstanceQuery();
            query.setDatabaseURL(dbListener.getDatabaseURL());
            query.setUsername(dbListener.getUsername());
            query.setPassword(dbListener.getPassword());
            query.setDebug(dbListener.getDebug());
            query.connectToDatabase();
            result = query.retrieveInstances("SELECT * FROM " + tmpStr);
            query.disconnectFromDatabase();
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Failed to get instances from database:", e);
            throw new Exception("Error reading experiment from database:\n" + e);
        }
    } else {
        throw new Exception(
                "Unsupported ResultListener '" + exp.getResultListener().getClass().getName() + "'!");
    }

    return result;
}

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

License:Open Source License

/**
 * pops up SQL Viewer for SQL statement.
 *///  www  . j av 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;//  w ww  .  java2 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  www.  ja  va  2  s  .  c om*/
    // 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 {
    // ---//from   www .ja  v a2s  .  c o  m
    // 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 {/*from   w  w  w .j a  v a  2s .  c o m*/
        //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: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 ww  w  . j av a  2s .  co  m*/
            "SELECT judul,full_text,label FROM artikel NATURAL JOIN artikel_kategori_verified NATURAL JOIN kategori");
    data = query.retrieveInstances();
}