List of usage examples for weka.experiment InstanceQuery InstanceQuery
public InstanceQuery() throws Exception
From source file:adams.flow.transformer.WekaExperimentEvaluation.java
License:Open Source License
/** * Loads the experimental results./*ww w .j a va2s . 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. */// w w w . j a v a 2s . c om 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 w w .j a v a2s. c o m*/ // --- // 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 ww w.j a v a 2 s . 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 { // ---//from w ww . jav a 2 s . com // 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;/* w w w . j a v a 2s . c o m*/ try { //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);//from ww w . jav a 2 s.co 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. ja va2s. com "SELECT judul,full_text,label FROM artikel NATURAL JOIN artikel_kategori_verified NATURAL JOIN kategori"); data = query.retrieveInstances(); }
From source file:org.datahack.forecast.BayExample.java
public static void main(String[] args) { try {/*from w w w . ja v a 2s . com*/ // path to the Australian wine data included with the time series forecasting // package File output = File.createTempFile("tempWineData", "arff"); output.deleteOnExit(); String dataFileName = "sample-data/wine.arff"; URL resource = BayExample.class.getResource("/" + dataFileName); FileUtils.copyURLToFile(resource, output); String pathToWineData = output.getPath(); // load the wine data Instances wine = new Instances(new BufferedReader(new FileReader(pathToWineData))); InstanceQuery q = new InstanceQuery(); // new forecaster WekaForecaster forecaster = new WekaForecaster(); // set the targets we want to forecast. This method calls // setFieldsToLag() on the lag maker object for us forecaster.setFieldsToForecast("Fortified,Dry-white"); // default underlying classifier is SMOreg (SVM) - we'll use // gaussian processes for regression instead forecaster.setBaseForecaster(new GaussianProcesses()); forecaster.getTSLagMaker().setTimeStampField("Date"); // date time stamp forecaster.getTSLagMaker().setMinLag(1); forecaster.getTSLagMaker().setMaxLag(12); // monthly data // add a month of the year indicator field forecaster.getTSLagMaker().setAddMonthOfYear(true); // add a quarter of the year indicator field forecaster.getTSLagMaker().setAddQuarterOfYear(true); // build the model forecaster.buildForecaster(wine, System.out); // prime the forecaster with enough recent historical data // to cover up to the maximum lag. In our case, we could just supply // the 12 most recent historical instances, as this covers our maximum // lag period forecaster.primeForecaster(wine); // forecast for 12 units (months) beyond the end of the // training data List<List<NumericPrediction>> forecast = forecaster.forecast(12, System.out); // output the predictions. Outer list is over the steps; inner list is over // the targets for (int i = 0; i < 12; i++) { List<NumericPrediction> predsAtStep = forecast.get(i); for (int j = 0; j < 2; j++) { NumericPrediction predForTarget = predsAtStep.get(j); System.out.print("" + predForTarget.predicted() + " "); } System.out.println(); } // we can continue to use the trained forecaster for further forecasting // by priming with the most recent historical data (as it becomes available). // At some stage it becomes prudent to re-build the model using current // historical data. } catch (Exception ex) { ex.printStackTrace(); } }
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 w w w. j av a2 s. co m data = query.retrieveInstances(); StringToWordVector nts = new StringToWordVector(); nts.setInputFormat(data); data = Filter.useFilter(data, nts); }