List of usage examples for weka.core.converters CSVLoader setFile
@Override public void setFile(File file) throws IOException
From source file:classif.ExperimentsLauncher.java
License:Open Source License
public static Instances[] readTrainAndTest(String name) { File trainFile = new File(datasetsDir + name + "/" + name + "_TRAIN"); if (!new File(trainFile.getAbsolutePath() + ".csv").exists()) { UCR2CSV.run(trainFile, new File(trainFile.getAbsolutePath() + ".csv")); }/*w ww.j a v a 2s. c o m*/ trainFile = new File(trainFile.getAbsolutePath() + ".csv"); File testFile = new File(datasetsDir + name + "/" + name + "_TEST"); if (!new File(testFile.getAbsolutePath() + ".csv").exists()) { UCR2CSV.run(testFile, new File(testFile.getAbsolutePath() + ".csv")); } testFile = new File(testFile.getAbsolutePath() + ".csv"); CSVLoader loader = new CSVLoader(); Instances trainDataset = null; Instances testDataset = null; try { loader.setFile(trainFile); loader.setNominalAttributes("first"); trainDataset = loader.getDataSet(); trainDataset.setClassIndex(0); loader.setFile(testFile); loader.setNominalAttributes("first"); testDataset = loader.getDataSet(); testDataset.setClassIndex(0); } catch (Exception e) { e.printStackTrace(); } return new Instances[] { trainDataset, testDataset }; }
From source file:demo.Demo.java
License:Open Source License
/** * @param args//from w w w .j a v a 2 s . c o m * @throws IOException */ public static void main(String[] args) throws IOException { CSVLoader loader = new CSVLoader(); System.out.println("Downloading dataset..."); URL oracle = new URL("http://repository.seasr.org/Datasets/UCI/csv/mushroom.csv"); File csvFile = File.createTempFile("data-", ".csv"); BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream())); PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream(csvFile))); String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } in.close(); out.close(); System.out.println("Dataset written to: " + csvFile.getAbsolutePath()); loader.setFile(csvFile); loader.setNominalAttributes("first-last"); Instances instances = loader.getDataSet(); String[] variablesNames = new String[instances.numAttributes()]; for (int i = 0; i < variablesNames.length; i++) { variablesNames[i] = instances.attribute(i).name(); } ChordalysisModelling modeller = new ChordalysisModelling(0.05); System.out.println("Learning..."); modeller.buildModel(instances); DecomposableModel bestModel = modeller.getModel(); bestModel.display(variablesNames); System.out.println("The model selected is:"); System.out.println(bestModel.toString(variablesNames)); bestModel.display(variablesNames); }
From source file:demo.DemoInference.java
License:Open Source License
/** * @param args//from ww w . j a v a 2 s .com * @throws IOException */ public static void main(String[] args) throws IOException { CSVLoader loader = new CSVLoader(); System.out.println("Downloading dataset..."); URL oracle = new URL("http://repository.seasr.org/Datasets/UCI/csv/mushroom.csv"); File csvFile = File.createTempFile("data-", ".csv"); BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream())); PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream(csvFile))); String inputLine; while ((inputLine = in.readLine()) != null) { out.println(inputLine); } in.close(); out.close(); System.out.println("Dataset written to: " + csvFile.getAbsolutePath()); loader.setFile(csvFile); loader.setNominalAttributes("first-last"); Instances instances = loader.getDataSet(); String[] variablesNames = new String[instances.numAttributes()]; String[][] outcomes = new String[instances.numAttributes()][]; for (int i = 0; i < variablesNames.length; i++) { variablesNames[i] = instances.attribute(i).name(); outcomes[i] = new String[instances.attribute(i).numValues() + 1];//+1 for missing for (int j = 0; j < outcomes[i].length - 1; j++) { outcomes[i][j] = instances.attribute(i).value(j); } outcomes[i][outcomes[i].length - 1] = "missing"; System.out.println("Dom(" + variablesNames[i] + ") = " + Arrays.toString(outcomes[i])); } ChordalysisModelling modeller = new ChordalysisModelling(0.05); System.out.println("Learning..."); modeller.buildModel(instances); DecomposableModel bestModel = modeller.getModel(); // bestModel.display(variablesNames); System.out.println("The model selected is:"); System.out.println(bestModel.toString(variablesNames)); Inference inference = new Inference(bestModel, variablesNames, outcomes); inference.setProbabilities(modeller.getLattice()); String targetVariable = "population"; System.out.println("initial beliefs on " + targetVariable + " " + Arrays.toString(inference.getBelief(targetVariable))); System.out.println("adding evidence poisonous and convex shape"); inference.addEvidence("class", "e"); inference.addEvidence("cap-shape", "x"); inference.recordEvidence(); System.out.println( "beliefs on " + targetVariable + " " + Arrays.toString(inference.getBelief(targetVariable))); inference.clearEvidences(); System.out.println("reset beliefs"); System.out.println( "reset beliefs on " + targetVariable + " " + Arrays.toString(inference.getBelief(targetVariable))); }
From source file:demo.Run.java
License:Open Source License
/** * @param args/*from w ww. j a v a 2 s . c o m*/ */ public static void main(String[] args) { if (args.length != 4) { System.out.println("Usage:\tjava -Xmx1g -jar Chordalysis.jar dataFile pvalue imageOutputFile useGUI?"); System.out.println("Example:\tjava -Xmx1g -jar Chordalysis.jar dataset.csv 0.05 graph.png false"); System.out.println("\nNote:\t'1g' means that you authorize 1GB of memory. " + "\nNote:\tIt should be adjusted depending upon the size of your data set (mostly required to load the data set)."); return; } System.out.println(); CSVLoader loader = new CSVLoader(); File csvFile = new File(args[0]); if (!csvFile.exists()) { System.out.println("The file doesn't exist"); return; } else { System.out.println("Info:\tUsing the dataset file " + csvFile.getAbsolutePath()); } double pValue = Double.valueOf(args[1]); if (pValue <= 0 || 1 <= pValue) { System.out.println("The p-value should be between 0 and 1 excluded. "); return; } else { System.out.println("Info:\tUsing p=" + pValue); } File outPutFile = new File(args[2]); String[] splitted = outPutFile.getName().split("\\."); if (splitted.length < 2) { System.out.println( "The image output file should declare an extension among \".jpg\", \".png\" or \".gif\""); return; } String extension = splitted[splitted.length - 1]; if (!extension.equals("jpg") && !extension.equals("png") && !extension.equals("gif")) { System.out.println( "The format for the graphical representation of the model should be either jpg, png or gif. "); return; } else { System.out.println("Info:\tExporting result as a " + extension + " file"); } boolean gui = Boolean.parseBoolean(args[3]); if (gui) { System.out.println("Info:\tUsing a graphical user interface"); } else { System.out.println("Info:\tNot using a graphical user interface"); } try { loader.setFile(csvFile); loader.setNominalAttributes("first-last"); Instances instances = loader.getDataSet(); String[] variablesNames = new String[instances.numAttributes()]; for (int i = 0; i < variablesNames.length; i++) { variablesNames[i] = instances.attribute(i).name(); } long start = System.currentTimeMillis(); ChordalysisModelling modeller = new ChordalysisModelling(pValue); modeller.buildModel(instances); DecomposableModel bestModel = modeller.getModel(); if (gui) bestModel.display(variablesNames); System.out .println("The model selected is: (selected in " + (System.currentTimeMillis() - start) + "ms)"); System.out.println(bestModel.toString(variablesNames)); ImageIO.write(bestModel.getImage(variablesNames), extension, outPutFile); } catch (IOException e) { System.out.println("I/O error while loading csv file"); e.printStackTrace(); } }
From source file:demo.RunDot.java
License:Open Source License
/** * @param args// ww w.ja v a2 s . c om */ public static void main(String[] args) { if (args.length != 3) { System.out.println("Usage:\tjava -Xmx1g -jar Chordalysis.jar dataFile pvalue dotOutputFile"); System.out.println("Example:\tjava -Xmx1g -jar Chordalysis.jar dataset.csv 0.05 graph.dot"); System.out.println("\nNote:\t'1g' means that you authorize 1GB of memory. " + "\nNote:\tIt should be adjusted depending upon the size of your data set (mostly required to load the data set)."); return; } System.out.println(); CSVLoader loader = new CSVLoader(); File csvFile = new File(args[0]); if (!csvFile.exists()) { System.out.println("The file doesn't exist"); return; } else { System.out.println("Info:\tUsing the dataset file " + csvFile.getAbsolutePath()); } double pValue = Double.valueOf(args[1]); if (pValue <= 0 || 1 <= pValue) { System.out.println("The p-value should be between 0 and 1 excluded. "); return; } else { System.out.println("Info:\tUsing p=" + pValue); } File outPutFile = new File(args[2]); String[] splitted = outPutFile.getName().split("\\."); if (splitted.length < 2) { System.out.println("The image output file should declare a \".dot\" extension"); return; } try { loader.setFile(csvFile); loader.setNominalAttributes("first-last"); Instances instances = loader.getDataSet(); String[] variablesNames = new String[instances.numAttributes()]; for (int i = 0; i < variablesNames.length; i++) { variablesNames[i] = instances.attribute(i).name(); } long start = System.currentTimeMillis(); ChordalysisModelling modeller = new ChordalysisModelling(pValue); modeller.buildModel(instances); DecomposableModel bestModel = modeller.getModel(); System.out .println("The model selected is: (selected in " + (System.currentTimeMillis() - start) + "ms)"); System.out.println(bestModel.toString(variablesNames)); bestModel.exportDOT(outPutFile, variablesNames); System.out.println( "DOT file exported - note that the variables with no neighbors won't be included in the graph"); } catch (IOException e) { System.out.println("I/O error while loading csv file"); e.printStackTrace(); } }
From source file:demo.RunGUI.java
License:Open Source License
/** * @param args// w w w. ja va 2s .c om */ public static void main(String[] args) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV file", "csv"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); } CSVLoader loader = new CSVLoader(); File csvFile = chooser.getSelectedFile(); if (!csvFile.exists()) { System.out.println("The file doesn't exist"); return; } double pValue = Double.valueOf(JOptionPane.showInputDialog("Desired p-value ]0,1[", 0.05)); if (pValue <= 0 || 1 <= pValue) { System.out.println("The p-value should be between 0 and 1 excluded. "); return; } try { loader.setFile(csvFile); loader.setNominalAttributes("first-last"); Instances instances = loader.getDataSet(); String[] variablesNames = new String[instances.numAttributes()]; for (int i = 0; i < variablesNames.length; i++) { variablesNames[i] = instances.attribute(i).name(); } ChordalysisModelling modeller = new ChordalysisModelling(pValue); modeller.buildModel(instances); DecomposableModel bestModel = modeller.getModel(); System.out.println("The model selected is:"); System.out.println(bestModel.toString(variablesNames)); bestModel.display(variablesNames); } catch (IOException e) { System.out.println("I/O error while loading csv file"); e.printStackTrace(); } }
From source file:demo.RunGUIProof.java
License:Open Source License
/** * @param args//from w w w . j a v a 2 s .c om */ public static void main(String[] args) { JOptionPane.showMessageDialog(null, introductionMessage, "Chordalysis", JOptionPane.INFORMATION_MESSAGE); int result = JOptionPane.showOptionDialog(null, new JTextArea(agreeCitation), "Reference", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); if (result == JOptionPane.NO_OPTION || result == JOptionPane.CLOSED_OPTION) { JOptionPane.showMessageDialog(null, "Chordalysis will now stop, because you do not want to reference its source. ", "Chordalysis", JOptionPane.WARNING_MESSAGE); System.exit(0); } JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV file", "csv"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); File csvFile = null; if (returnVal == JFileChooser.APPROVE_OPTION) { csvFile = chooser.getSelectedFile(); System.out.println("You chose to open: " + csvFile); } else { JOptionPane.showMessageDialog(null, noFileSelectedMessage, "Chordalysis", JOptionPane.ERROR_MESSAGE); return; } CSVLoader loader = new CSVLoader(); if (!csvFile.exists()) { JOptionPane.showMessageDialog(null, noFileMessage, "Chordalysis", JOptionPane.INFORMATION_MESSAGE); return; } double pValue = -1; while (pValue <= 0 || 1 <= pValue) { pValue = Double.valueOf(JOptionPane.showInputDialog("Desired p-value (between 0 and 1)", 0.05)); if (pValue <= 0 || 1 <= pValue) { JOptionPane.showMessageDialog(null, incorrectPValueMessage, "Chordalysis", JOptionPane.WARNING_MESSAGE); } } filter = new FileNameExtensionFilter("PNG or DOT or CSV file or DNE file", "png", "dot", "csv", "dne"); chooser = new JFileChooser(); chooser.setFileFilter(filter); chooser.setDialogTitle("Where to save the graph?"); chooser.setSelectedFile(new File(csvFile.getAbsolutePath() + ".png")); returnVal = chooser.showSaveDialog(null); File graphFile = null; if (returnVal == JFileChooser.APPROVE_OPTION) { graphFile = chooser.getSelectedFile(); System.out.println("You chose to save the graph to: " + graphFile.getAbsolutePath()); } else { JOptionPane.showMessageDialog(null, noFileSelectedMessage, "Chordalysis", JOptionPane.ERROR_MESSAGE); return; } try { loader.setFile(csvFile); returnVal = JOptionPane.showConfirmDialog(null, "Are all of your attribute nominal?", "Chordalysis", JOptionPane.YES_NO_OPTION); if (returnVal == JOptionPane.YES_OPTION) { loader.setNominalAttributes("first-last"); } Instances instances = loader.getDataSet(); String cols = ""; for (int i = 0; i < instances.numAttributes(); i++) { Attribute att = instances.attribute(i); if (!att.isNominal()) { cols += (i + 1) + ","; } } if (!cols.isEmpty()) { cols = cols.substring(0, cols.length() - 1); String message = "Some atributes are not nominal (number " + cols + "), please wait during discretization. "; JOptionPane.showMessageDialog(null, message, "Chordalysis", JOptionPane.INFORMATION_MESSAGE); Discretize discretizer = new Discretize(cols); discretizer.setUseEqualFrequency(true); discretizer.setBins(3); discretizer.setIgnoreClass(true); discretizer.setInputFormat(instances); instances = Filter.useFilter(instances, discretizer); JOptionPane.showMessageDialog(null, "Discretization is now finished.", "Chordalysis", JOptionPane.INFORMATION_MESSAGE); } String[] variablesNames = new String[instances.numAttributes()]; String[][] outcomes = new String[instances.numAttributes()][]; for (int i = 0; i < variablesNames.length; i++) { variablesNames[i] = instances.attribute(i).name(); outcomes[i] = new String[instances.attribute(i).numValues()]; for (int j = 0; j < outcomes[i].length; j++) { outcomes[i][j] = instances.attribute(i).value(j); } } ChordalysisModelling modeller = new ChordalysisModelling(pValue); modeller.buildModel(instances); DecomposableModel bestModel = modeller.getModel(); JOptionPane.showMessageDialog(null, new JTextArea("Chordalysis has now finished analysing your data. " + "\nIf you found something useful, please reference Chordalysis as" + "\n\t- F. Petitjean, G.I. Webb and A. Nicholson, Scaling log-linear analysis to high-dimensional data, ICDM 2013" + "\n\t- F. Petitjean and G.I. Webb, Scaling log-linear analysis to datasets with thousands of variables, SDM 2015" + "\n\nYou can find the output file at: '" + graphFile.getAbsolutePath() + "'"), "Citation", JOptionPane.INFORMATION_MESSAGE); System.out.println("The model selected is:"); System.out.println(bestModel.toString(variablesNames)); if (graphFile.getName().endsWith("dot")) { bestModel.exportDOT(graphFile, variablesNames); } else if (graphFile.getName().endsWith("png")) { ImageIO.write(bestModel.getImage(variablesNames), "png", graphFile); } else if (graphFile.getName().endsWith("dne")) { bestModel.exportBNNetica(graphFile, variablesNames, outcomes); bestModel.exportDOT(new File(graphFile.getAbsolutePath() + ".dot"), variablesNames); ImageIO.write(bestModel.getImage(variablesNames), "png", new File(graphFile.getAbsolutePath() + ".png")); bestModel.saveAssociations(variablesNames, new File(graphFile.getAbsolutePath() + ".csv")); } else { bestModel.saveAssociations(variablesNames, graphFile); } } catch (IOException e) { JOptionPane.showMessageDialog(null, "The file '" + csvFile.getAbsolutePath() + "'\ncannot be read properly.", "Error while reading file", JOptionPane.ERROR_MESSAGE); System.out.println("I/O error while loading csv file"); e.printStackTrace(); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error:" + e.getMessage(), "Chordalysis", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } }
From source file:general.Util.java
/** * load dataset from CSV format//from w ww .j ava 2s. co m * @param filename */ public static void loadCSV(String filename) { try { CSVLoader csv = new CSVLoader(); csv.setFile(new File(filename)); data = csv.getDataSet(); // setting class attribute data.setClassIndex(data.numAttributes() - 1); } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:general.Util.java
/** * show learning statistic result by using test sets * @param testPath test path file//from w ww . j a v a2s .c o m * @param typeTestFile test file */ public static void TestSchema(String testPath, String typeTestFile) { Instances testsets = null; // Load test instances based on file type and path if (typeTestFile.equals("arff")) { FileReader file = null; try { file = new FileReader(testPath); try (BufferedReader reader = new BufferedReader(file)) { testsets = new Instances(reader); } // setting class attribute testsets.setClassIndex(data.numAttributes() - 1); } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (file != null) { file.close(); } } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } } } else if (typeTestFile.equals("csv")) { try { CSVLoader csv = new CSVLoader(); csv.setFile(new File(testPath)); data = csv.getDataSet(); // setting class attribute data.setClassIndex(data.numAttributes() - 1); } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } } // Start evaluate model using instances test and print results try { Evaluation eval = new Evaluation(Util.getData()); eval.evaluateModel(Util.getClassifier(), testsets); System.out.println(eval.toSummaryString("\nResults\n\n", false)); } catch (Exception e) { e.printStackTrace(); } }