public static Instances loadDatasetFromOptions(String options[]) throws Exception { Instances D = null; String filename = Utils.getOption('t', options); if (filename == null || filename.isEmpty()) throw new Exception("[Error] You did not specify a dataset!"); try { DataSource source = new DataSource(filename); D = source.getDataSet(); } catch(Exception e) { e.printStackTrace(); throw new Exception("[Error] Failed to load Instances from file '"+filename+"'."); } return D; }
/** * loadDataset - load a dataset, given command line options specifying an arff file. * @param options command line options, specifying dataset filename * @param T set to 'T' if we want to load a test file (default 't': load train or train-test file) * @return the dataset */ public static Instances loadDataset(String options[], char T) throws Exception { Instances D = null; String filename = Utils.getOption(T, options); // Check for filename if (filename == null || filename.isEmpty()) throw new Exception("[Error] You did not specify a dataset!"); // Check for existence of file File file = new File(filename); if (!file.exists()) throw new Exception("[Error] File does not exist: " + filename); if (file.isDirectory()) throw new Exception("[Error] "+filename+ " points to a directory!"); try { DataSource source = new DataSource(filename); D = source.getDataSet(); } catch(Exception e) { e.printStackTrace(); throw new Exception("[Error] Failed to load Instances from file '"+filename+"'."); } return D; }
/** * loadDataset - load a dataset, given command line options specifying an arff file. * @param options command line options, specifying dataset filename * @param T set to 'T' if we want to load a test file (default 't': load train or train-test file) * @return the dataset */ public static Instances loadDataset(String options[], char T) throws Exception { Instances D = null; String filename = Utils.getOption(T, options); // Check for filename if (filename == null || filename.isEmpty()) throw new Exception("[Error] You did not specify a dataset!"); // Check for existence of file File file = new File(filename); if (!file.exists()) throw new Exception("[Error] File does not exist: " + filename); if (file.isDirectory()) throw new Exception("[Error] "+filename+ " points to a directory!"); try { DataSource source = new DataSource(filename); D = source.getDataSet(); } catch(Exception e) { e.printStackTrace(); throw new Exception("[Error] Failed to load Instances from file '"+filename+"'."); } return D; }
/** * for testing only - takes a data file as input. * * @param args the commandline arguments * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("\nUsage: " + DataSource.class.getName() + " <file>\n"); System.exit(1); } DataSource loader = new DataSource(args[0]); System.out.println("Incremental? " + loader.isIncremental()); System.out.println("Loader: " + loader.getLoader().getClass().getName()); System.out.println("Data:\n"); Instances structure = loader.getStructure(); System.out.println(structure); while (loader.hasMoreElements(structure)) { System.out.println(loader.nextElement(structure)); } Instances inst = loader.getDataSet(); loader = new DataSource(inst); System.out.println("\n\nProxy-Data:\n"); System.out.println(loader.getStructure()); while (loader.hasMoreElements(structure)) { System.out.println(loader.nextElement(inst)); } }
/** * for testing only - takes a data file as input. * * @param args the commandline arguments * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("\nUsage: " + DataSource.class.getName() + " <file>\n"); System.exit(1); } DataSource loader = new DataSource(args[0]); System.out.println("Incremental? " + loader.isIncremental()); System.out.println("Loader: " + loader.getLoader().getClass().getName()); System.out.println("Data:\n"); Instances structure = loader.getStructure(); System.out.println(structure); while (loader.hasMoreElements(structure)) { System.out.println(loader.nextElement(structure)); } Instances inst = loader.getDataSet(); loader = new DataSource(inst); System.out.println("\n\nProxy-Data:\n"); System.out.println(loader.getStructure()); while (loader.hasMoreElements(structure)) { System.out.println(loader.nextElement(inst)); } }
/** * Load data instances from Weka ARFF file to memory for classification * training or testing. * * @param filename * Weka ARFF data file path. * @param className * name used in data to denote attribute which is the class * @return data instances * @throws IOException */ private Instances loadInstancesFromARFF(String filename, String className) throws IOException { DataSource source; try { source = new DataSource(filename); Instances data = source.getDataSet(); Attribute classAttribute = data.attribute(className); data.setClass(classAttribute); return data; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
public class Run { public static void main(String[] args) throws Exception { ConverterUtils.DataSource source1 = new ConverterUtils.DataSource("./data/train.arff"); Instances train = source1.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (train.classIndex() == -1) train.setClassIndex(train.numAttributes() - 1); ConverterUtils.DataSource source2 = new ConverterUtils.DataSource("./data/test.arff"); Instances test = source2.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (test.classIndex() == -1) test.setClassIndex(train.numAttributes() - 1); // model NaiveBayes naiveBayes = new NaiveBayes(); naiveBayes.buildClassifier(train); // this does the trick double label = naiveBayes.classifyInstance(test.instance(0)); test.instance(0).setClassValue(label); System.out.println(test.instance(0).stringValue(4)); } }
public static void main(String[] args) throws Exception { String pathToDataset = args[0]; int startDim = Integer.parseInt(args[1]); int endDim = Integer.parseInt(args[2]); int dimStep = Integer.parseInt(args[3]); double c = 1; out.print("dim "); for (Object o: algorithms) { out.print(o + " "); } out.println(); for (int dim = startDim; dim <= endDim; dim += dimStep) { System.out.print(dim + " "); for (BooleanMatrixDecomposition a: algorithms) { BooleanMatrix matrix = new BooleanMatrix( new DataSource(pathToDataset).getDataSet()); Tuple<BooleanMatrix, BooleanMatrix> result = a.decompose(matrix, Math.min(matrix.getWidth(), dim)); double err = matrix.relativeReconstructionError( result._1.booleanProduct(result._2), c); out.print(err + " "); } out.println(); } } }
/** * returns the full dataset with the specified class index set, can be null * in case of an error. * * @param classIndex the class index for the dataset * @return the full dataset * @throws Exception if resetting of loader fails */ public Instances getDataSet(int classIndex) throws Exception { Instances result; result = getDataSet(); if (result != null) { result.setClassIndex(classIndex); } return result; }
/** * convencience method for loading a dataset in batch mode. * * @param location the dataset to load * @return the dataset * @throws Exception if loading fails */ public static Instances read(String location) throws Exception { DataSource source; Instances result; source = new DataSource(location); result = source.getDataSet(); return result; }
/** * returns the full dataset with the specified class index set, can be null * in case of an error. * * @param classIndex the class index for the dataset * @return the full dataset * @throws Exception if resetting of loader fails */ public Instances getDataSet(int classIndex) throws Exception { Instances result; result = getDataSet(); if (result != null) { result.setClassIndex(classIndex); } return result; }
/** * convencience method for loading a dataset in batch mode. * * @param loader the loader to get the dataset from * @return the dataset * @throws Exception if loading fails */ public static Instances read(Loader loader) throws Exception { DataSource source; Instances result; source = new DataSource(loader); result = source.getDataSet(); return result; }
/** * convencience method for loading a dataset in batch mode from a stream. * * @param stream the stream to load the dataset from * @return the dataset * @throws Exception if loading fails */ public static Instances read(InputStream stream) throws Exception { DataSource source; Instances result; source = new DataSource(stream); result = source.getDataSet(); return result; }
/** * convencience method for loading a dataset in batch mode. * * @param location the dataset to load * @return the dataset * @throws Exception if loading fails */ public static Instances read(String location) throws Exception { DataSource source; Instances result; source = new DataSource(location); result = source.getDataSet(); return result; }
/** * Load a dataset into memory. * * @param location the location of the dataset * * @return the dataset */ public static Instances readInstances(String location) throws Exception { Instances data = new weka.core.converters.ConverterUtils.DataSource(location).getDataSet(); if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1); return data; } }
/** * convencience method for loading a dataset in batch mode from a stream. * * @param stream the stream to load the dataset from * @return the dataset * @throws Exception if loading fails */ public static Instances read(InputStream stream) throws Exception { DataSource source; Instances result; source = new DataSource(stream); result = source.getDataSet(); return result; }
/** * convencience method for loading a dataset in batch mode. * * @param loader the loader to get the dataset from * @return the dataset * @throws Exception if loading fails */ public static Instances read(Loader loader) throws Exception { DataSource source; Instances result; source = new DataSource(loader); result = source.getDataSet(); return result; }
/** * Load a dataset into memory. * * @param location the location of the dataset * * @return the dataset */ public static Instances readInstances(String location) throws Exception { Instances data = new weka.core.converters.ConverterUtils.DataSource(location).getDataSet(); if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1); return data; } }
public void testReuters() throws Exception { final String arffPath = "datasets/text/ReutersCorn-train.arff"; ConverterUtils.DataSource ds = new ConverterUtils.DataSource(arffPath); final Instances data = ds.getDataSet(); Dl4jStringToWord2Vec dl4jw2v = new Dl4jStringToWord2Vec(); dl4jw2v.setInputFormat(data); Instances d = Filter.useFilter(data, dl4jw2v); } }
@Override public void evaluateAttributesFromFile(String corpusName, String featureSetName, String splitName, String file) throws Exception { DataSource ds = new DataSource(file); Instances inst = ds.getDataSet(); String label = FileUtil.parseLabelFromFileName(inst.relationName()); Integer run = FileUtil.parseRunFromFileName(inst.relationName()); Integer fold = FileUtil.parseFoldFromFileName(inst.relationName()); evaluateAttributes(corpusName, featureSetName, splitName, inst, label, run, fold); }