List of usage examples for weka.core Utils getOption
public staticString getOption(String flag, String[] options) throws Exception
From source file:WLSVM.java
License:Open Source License
/** * Sets the WLSVM classifier options//from www .j a va 2 s . c o m * */ public void setOptions(String[] options) throws Exception { param = new svm_parameter(); String svmtypeString = Utils.getOption('S', options); if (svmtypeString.length() != 0) { param.svm_type = Integer.parseInt(svmtypeString); } else { param.svm_type = svm_parameter.C_SVC; } String kerneltypeString = Utils.getOption('K', options); if (kerneltypeString.length() != 0) { param.kernel_type = Integer.parseInt(kerneltypeString); } else { param.kernel_type = svm_parameter.RBF; } String degreeString = Utils.getOption('D', options); if (degreeString.length() != 0) { param.degree = (new Double(degreeString)).doubleValue(); } else { param.degree = 3; } String gammaString = Utils.getOption('G', options); if (gammaString.length() != 0) { param.gamma = (new Double(gammaString)).doubleValue(); } else { param.gamma = 0; } String coef0String = Utils.getOption('R', options); if (coef0String.length() != 0) { param.coef0 = (new Double(coef0String)).doubleValue(); } else { param.coef0 = 0; } String nuString = Utils.getOption('N', options); if (nuString.length() != 0) { param.nu = (new Double(nuString)).doubleValue(); } else { param.nu = 0.5; } String cacheString = Utils.getOption('M', options); if (cacheString.length() != 0) { param.cache_size = (new Double(cacheString)).doubleValue(); } else { param.cache_size = 40; } String costString = Utils.getOption('C', options); if (costString.length() != 0) { param.C = (new Double(costString)).doubleValue(); } else { param.C = 1; } String epsString = Utils.getOption('E', options); if (epsString.length() != 0) { param.eps = (new Double(epsString)).doubleValue(); } else { param.eps = 1e-3; } String normString = Utils.getOption('Z', options); if (normString.length() != 0) { normalize = Integer.parseInt(normString); } else { normalize = 0; } String lossString = Utils.getOption('P', options); if (lossString.length() != 0) { param.p = (new Double(lossString)).doubleValue(); } else { param.p = 0.1; } String shrinkingString = Utils.getOption('H', options); if (shrinkingString.length() != 0) { param.shrinking = Integer.parseInt(shrinkingString); } else { param.shrinking = 1; } String probString = Utils.getOption('B', options); if (probString.length() != 0) { param.probability = Integer.parseInt(probString); } else { param.probability = 0; } String weightsString = Utils.getOption('W', options); if (weightsString.length() != 0) { StringTokenizer st = new StringTokenizer(weightsString, " "); int n_classes = st.countTokens(); param.weight_label = new int[n_classes]; param.weight = new double[n_classes]; // get array of doubles from this string int count = 0; while (st.hasMoreTokens()) { param.weight[count++] = atof(st.nextToken()); } param.nr_weight = count; param.weight_label[0] = -1; // label of first class for (int i = 1; i < count; i++) param.weight_label[i] = i; } else { param.nr_weight = 0; param.weight_label = new int[0]; param.weight = new double[0]; } }
From source file:Bilbo.java
License:Open Source License
/** * Parses a given list of options. <p/> * <!-- options-start -->//from w ww . j a va 2 s. co m * Valid options are: <p/> * * <pre> -P * Size of each bag, as a percentage of the * training set size. (default 100)</pre> * * <pre> -O * Calculate the out of bag error.</pre> * * <pre> -represent-copies-using-weights * Represent copies of instances using weights rather than explicitly.</pre> * * <pre> -S <num> * Random number seed. * (default 1)</pre> * * <pre> -num-slots <num> * Number of execution slots. * (default 1 - i.e. no parallelism)</pre> * * <pre> -I <num> * Number of iterations. * (default 10)</pre> * * <pre> -D * If set, classifier is run in debug mode and * may output additional info to the console</pre> * * <pre> -W * Full name of base classifier. * (default: weka.classifiers.trees.REPTree)</pre> * * <pre> * Options specific to classifier weka.classifiers.trees.REPTree: * </pre> * * <pre> -M <minimum number of instances> * Set minimum number of instances per leaf (default 2).</pre> * * <pre> -V <minimum variance for split> * Set minimum numeric class variance proportion * of train variance for split (default 1e-3).</pre> * * <pre> -N <number of folds> * Number of folds for reduced error pruning (default 3).</pre> * * <pre> -S <seed> * Seed for random data shuffling (default 1).</pre> * * <pre> -P * No pruning.</pre> * * <pre> -L * Maximum tree depth (default -1, no maximum)</pre> * * <pre> -I * Initial class value count (default 0)</pre> * * <pre> -R * Spread initial count over all class values (i.e. don't use 1 per value)</pre> * <!-- options-end --> * * Options after -- are passed to the designated classifier.<p> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String bagSize = Utils.getOption('P', options); if (bagSize.length() != 0) { setBagSizePercent(Integer.parseInt(bagSize)); } else { setBagSizePercent(100); } setCalcOutOfBag(Utils.getFlag('O', options)); setRepresentCopiesUsingWeights(Utils.getFlag("represent-copies-using-weights", options)); super.setOptions(options); Utils.checkForRemainingOptions(options); }
From source file:ExperimentDemo.java
License:Open Source License
/** * Expects the following parameters: // ww w . j av a 2 s . c o m * <ul> * <li>-classifier "classifier incl. parameters"</li> * <li>-exptype "classification|regression"</li> * <li>-splittype "crossvalidation|randomsplit"</li> * <li>-runs "# of runs"</li> * <li>-folds "# of cross-validation folds"</li> * <li>-percentage "percentage for randomsplit"</li> * <li>-result "arff file for storing the results"</li> * <li>-t "dataset" (can be supplied multiple times)</li> * </ul> * * @param args the commandline arguments * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { // parameters provided? if (args.length == 0) { System.out.println("\nUsage: weka.examples.experiment.ExperimentDemo\n" + "\t -classifier <classifier incl. parameters>\n" + "\t -exptype <classification|regression>\n" + "\t -splittype <crossvalidation|randomsplit>\n" + "\t -runs <# of runs>\n" + "\t -folds <folds for CV>\n" + "\t -percentage <percentage for randomsplit>\n" + "\t -result <ARFF file for storing the results>\n" + "\t -t dataset (can be supplied multiple times)\n"); System.exit(1); } // 1. setup the experiment System.out.println("Setting up..."); Experiment exp = new Experiment(); exp.setPropertyArray(new Classifier[0]); exp.setUsePropertyIterator(true); String option; // classification or regression option = Utils.getOption("exptype", args); if (option.length() == 0) throw new IllegalArgumentException("No experiment type provided!"); SplitEvaluator se = null; /* * Interface to objects able to generate a fixed set of results for a particular split of a dataset. * The set of results should contain fields related to any settings of the SplitEvaluator (not including the dataset name. * For example, one field for the classifier used to get the results, another for the classifier options, etc). * Possible implementations of SplitEvaluator: StdClassification results, StdRegression results. */ Classifier sec = null; boolean classification = false; if (option.equals("classification")) { classification = true; se = new ClassifierSplitEvaluator(); /* * A SplitEvaluator that produces results for a classification scheme on a nominal class attribute. */ sec = ((ClassifierSplitEvaluator) se).getClassifier(); } else if (option.equals("regression")) { se = new RegressionSplitEvaluator(); sec = ((RegressionSplitEvaluator) se).getClassifier(); } else { throw new IllegalArgumentException("Unknown experiment type '" + option + "'!"); } // crossvalidation or randomsplit option = Utils.getOption("splittype", args); if (option.length() == 0) throw new IllegalArgumentException("No split type provided!"); if (option.equals("crossvalidation")) { CrossValidationResultProducer cvrp = new CrossValidationResultProducer(); /* * Generates for each run, carries out an n-fold cross-validation, using the set SplitEvaluator to generate some results. * If the class attribute is nominal, the dataset is stratified. Results for each fold are generated, so you may wish to use * this in addition with an AveragingResultProducer to obtain averages for each run. */ option = Utils.getOption("folds", args); if (option.length() == 0) throw new IllegalArgumentException("No folds provided!"); cvrp.setNumFolds(Integer.parseInt(option)); cvrp.setSplitEvaluator(se); PropertyNode[] propertyPath = new PropertyNode[2]; /* * Stores information on a property of an object: the class of the object with the property; * the property descriptor, and the current value. */ try { propertyPath[0] = new PropertyNode(se, new PropertyDescriptor("splitEvaluator", CrossValidationResultProducer.class), CrossValidationResultProducer.class); propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier", se.getClass()), se.getClass()); } catch (IntrospectionException e) { e.printStackTrace(); } exp.setResultProducer(cvrp); exp.setPropertyPath(propertyPath); } else if (option.equals("randomsplit")) { RandomSplitResultProducer rsrp = new RandomSplitResultProducer(); rsrp.setRandomizeData(true); option = Utils.getOption("percentage", args); if (option.length() == 0) throw new IllegalArgumentException("No percentage provided!"); rsrp.setTrainPercent(Double.parseDouble(option)); rsrp.setSplitEvaluator(se); PropertyNode[] propertyPath = new PropertyNode[2]; try { propertyPath[0] = new PropertyNode(se, new PropertyDescriptor("splitEvaluator", RandomSplitResultProducer.class), RandomSplitResultProducer.class); propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier", se.getClass()), se.getClass()); } catch (IntrospectionException e) { e.printStackTrace(); } exp.setResultProducer(rsrp); exp.setPropertyPath(propertyPath); } else { throw new IllegalArgumentException("Unknown split type '" + option + "'!"); } // runs option = Utils.getOption("runs", args); if (option.length() == 0) throw new IllegalArgumentException("No runs provided!"); exp.setRunLower(1); exp.setRunUpper(Integer.parseInt(option)); // classifier option = Utils.getOption("classifier", args); if (option.length() == 0) throw new IllegalArgumentException("No classifier provided!"); String[] options = Utils.splitOptions(option); String classname = options[0]; options[0] = ""; Classifier c = (Classifier) Utils.forName(Classifier.class, classname, options); exp.setPropertyArray(new Classifier[] { c }); // datasets boolean data = false; DefaultListModel model = new DefaultListModel(); do { option = Utils.getOption("t", args); if (option.length() > 0) { File file = new File(option); if (!file.exists()) throw new IllegalArgumentException("File '" + option + "' does not exist!"); data = true; model.addElement(file); } } while (option.length() > 0); if (!data) throw new IllegalArgumentException("No data files provided!"); exp.setDatasets(model); // result option = Utils.getOption("result", args); if (option.length() == 0) throw new IllegalArgumentException("No result file provided!"); InstancesResultListener irl = new InstancesResultListener(); irl.setOutputFile(new File(option)); exp.setResultListener(irl); // 2. run experiment System.out.println("Initializing..."); exp.initialize(); System.out.println("Running..."); exp.runExperiment(); System.out.println("Finishing..."); exp.postProcess(); // 3. calculate statistics and output them System.out.println("Evaluating..."); PairedTTester tester = new PairedCorrectedTTester(); /* * Calculates T-Test statistics on data stored in a set of instances. */ Instances result = new Instances(new BufferedReader(new FileReader(irl.getOutputFile()))); tester.setInstances(result); tester.setSortColumn(-1); tester.setRunColumn(result.attribute("Key_Run").index()); if (classification) tester.setFoldColumn(result.attribute("Key_Fold").index()); tester.setDatasetKeyColumns(new Range("" + (result.attribute("Key_Dataset").index() + 1))); tester.setResultsetKeyColumns(new Range("" + (result.attribute("Key_Scheme").index() + 1) + "," + (result.attribute("Key_Scheme_options").index() + 1) + "," + (result.attribute("Key_Scheme_version_ID").index() + 1))); tester.setResultMatrix(new ResultMatrixPlainText()); tester.setDisplayedResultsets(null); tester.setSignificanceLevel(0.05); tester.setShowStdDevs(true); // fill result matrix (but discarding the output) if (classification) tester.multiResultsetFull(0, result.attribute("Percent_correct").index()); else tester.multiResultsetFull(0, result.attribute("Correlation_coefficient").index()); // output results for reach dataset System.out.println("\nResult:"); ResultMatrix matrix = tester.getResultMatrix(); for (int i = 0; i < matrix.getColCount(); i++) { System.out.println(matrix.getColName(i)); System.out.println(" Perc. correct: " + matrix.getMean(i, 0)); System.out.println(" StdDev: " + matrix.getStdDev(i, 0)); } }
From source file:PrincipalComponents.java
License:Open Source License
/** * Parses a given list of options./*from ww w. j av a2s. co m*/ * <p> * <p> * <!-- options-start --> Valid options are: * <p> * <p> * <pre> * -C * Center (rather than standardize) the * data and compute PCA using the covariance (rather * than the correlation) matrix. * </pre> * <p> * <pre> * -R * Retain enough PC attributes to account * for this proportion of variance in the original data. * (default = 0.95) * </pre> * <p> * <pre> * -O * Transform through the PC space and * back to the original space. * </pre> * <p> * <pre> * -A * Maximum number of attributes to include in * transformed attribute names. (-1 = include all) * </pre> * <p> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { resetOptions(); String optionString; optionString = Utils.getOption('R', options); if (optionString.length() != 0) { Double temp; temp = Double.valueOf(optionString); setVarianceCovered(temp.doubleValue()); } optionString = Utils.getOption('A', options); if (optionString.length() != 0) { setMaximumAttributeNames(Integer.parseInt(optionString)); } setTransformBackToOriginal(Utils.getFlag('O', options)); setCenterData(Utils.getFlag('C', options)); }
From source file:CrossValidationExperim.java
License:Open Source License
/** * Executes this example// w w w. ja va 2 s.c o m * * @param args command-line arguments -arff and -xml */ public static void main(String[] args) { try { // e.g. -arff emotions.arff String arffFilename = Utils.getOption("a", args); // e.g. -xml emotions.xml String xmlFilename = Utils.getOption("x", args); System.out.println("Loading the dataset..."); MultiLabelInstances dataset = new MultiLabelInstances(arffFilename, xmlFilename); BinaryRelevance SVM = new BinaryRelevance(new weka.classifiers.functions.SMO()); SVM.setDebug(true); //SVM.build(data); Evaluator eval = new Evaluator(); MultipleEvaluation results; int numFolds = 10; results = eval.crossValidate(SVM, dataset, numFolds); System.out.println(results); } catch (InvalidDataFormatException ex) { Logger.getLogger(CrossValidationExperim.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(CrossValidationExperim.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:TextDirectoryLoader.java
License:Open Source License
/** * Parses a given list of options. <p/> * <!-- options-start -->/*from w ww .ja va 2 s . c o m*/ * Valid options are: <p/> * * <pre> -D * Enables debug output. * (default: off)</pre> * * <pre> -F * Stores the filename in an additional attribute. * (default: off)</pre> * * <pre> -dir <directory> * The directory to work on. * (default: current directory)</pre> * <!-- options-end --> * * @param options the options * @throws Exception if options cannot be set */ public void setOptions(String[] options) throws Exception { setDebug(Utils.getFlag("D", options)); setOutputFilename(Utils.getFlag("F", options)); setDirectory(new File(Utils.getOption("dir", options))); String charSet = Utils.getOption("charset", options); m_charSet = ""; if (charSet.length() > 0) { m_charSet = charSet; } }
From source file:ArrayLoader.java
License:Open Source License
/** * Parses a given list of options. <p/> * <!-- options-start -->/*from www . j ava2 s . c o m*/ * Valid options are: <p/> * * <pre> -N <range> * The range of attributes to force type to be NOMINAL. * 'first' and 'last' are accepted as well. * Examples: "first-last", "1,4,5-27,50-last" * (default: -none-)</pre> * * <pre> -S <range> * The range of attribute to force type to be STRING. * 'first' and 'last' are accepted as well. * Examples: "first-last", "1,4,5-27,50-last" * (default: -none-)</pre> * * <pre> -M <str> * The string representing a missing value. * (default: ?)</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; tmpStr = Utils.getOption('N', options); if (tmpStr.length() != 0) setNominalAttributes(tmpStr); else setNominalAttributes(""); tmpStr = Utils.getOption('S', options); if (tmpStr.length() != 0) setStringAttributes(tmpStr); else setStringAttributes(""); tmpStr = Utils.getOption('M', options); if (tmpStr.length() != 0) setMissingValue(tmpStr); else setMissingValue("?"); }
From source file:BaggingImprove.java
/** * Parses a given list of options./* w w w .j a v a 2s . co m*/ * <p/> * * <!-- options-start --> * Valid options are: * <p/> * * <pre> * -P * Size of each bag, as a percentage of the * training set size. (default 100) * </pre> * * <pre> * -O * Calculate the out of bag error. * </pre> * * <pre> * -S <num> * Random number seed. * (default 1) * </pre> * * <pre> * -I <num> * Number of iterations. * (default 10) * </pre> * * <pre> * -D * If set, classifier is run in debug mode and * may output additional info to the console * </pre> * * <pre> * -W * Full name of base classifier. * (default: weka.classifiers.trees.REPTree) * </pre> * * <pre> * Options specific to classifier weka.classifiers.trees.REPTree: * </pre> * * <pre> * -M <minimum number of instances> * Set minimum number of instances per leaf (default 2). * </pre> * * <pre> * -V <minimum variance for split> * Set minimum numeric class variance proportion * of train variance for split (default 1e-3). * </pre> * * <pre> * -N <number of folds> * Number of folds for reduced error pruning (default 3). * </pre> * * <pre> * -S <seed> * Seed for random data shuffling (default 1). * </pre> * * <pre> * -P * No pruning. * </pre> * * <pre> * -L * Maximum tree depth (default -1, no maximum) * </pre> * * <!-- options-end --> * * Options after -- are passed to the designated classifier. * <p> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ @Override public void setOptions(String[] options) throws Exception { String bagSize = Utils.getOption('P', options); if (bagSize.length() != 0) { setBagSizePercent(Integer.parseInt(bagSize)); } else { setBagSizePercent(100); } setCalcOutOfBag(Utils.getFlag('O', options)); super.setOptions(options); }
From source file:CrossValidationMultipleRuns.java
License:Open Source License
/** * Performs the cross-validation. See Javadoc of class for information * on command-line parameters./* w ww.jav a 2s .c o m*/ * * @param args the command-line parameters * @throws Exception if something goes wrong */ public static void main(String[] args) throws Exception { // loads data and set class index Instances data = DataSource.read(Utils.getOption("t", args)); String clsIndex = Utils.getOption("c", args); if (clsIndex.length() == 0) clsIndex = "last"; if (clsIndex.equals("first")) data.setClassIndex(0); else if (clsIndex.equals("last")) data.setClassIndex(data.numAttributes() - 1); else data.setClassIndex(Integer.parseInt(clsIndex) - 1); // classifier String[] tmpOptions; String classname; tmpOptions = Utils.splitOptions(Utils.getOption("W", args)); classname = tmpOptions[0]; tmpOptions[0] = ""; Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions); // other options int runs = Integer.parseInt(Utils.getOption("r", args)); int folds = Integer.parseInt(Utils.getOption("x", args)); // perform cross-validation for (int i = 0; i < runs; i++) { // randomize data int seed = i + 1; Random rand = new Random(seed); Instances randData = new Instances(data); randData.randomize(rand); //if (randData.classAttribute().isNominal()) // randData.stratify(folds); Evaluation eval = new Evaluation(randData); StringBuilder optionsString = new StringBuilder(); for (String s : cls.getOptions()) { optionsString.append(s); optionsString.append(" "); } // output evaluation System.out.println(); System.out.println("=== Setup run " + (i + 1) + " ==="); System.out.println("Classifier: " + optionsString.toString()); System.out.println("Dataset: " + data.relationName()); System.out.println("Folds: " + folds); System.out.println("Seed: " + seed); System.out.println(); for (int n = 0; n < folds; n++) { Instances train = randData.trainCV(folds, n); Instances test = randData.testCV(folds, n); // build and evaluate classifier Classifier clsCopy = Classifier.makeCopy(cls); clsCopy.buildClassifier(train); eval.evaluateModel(clsCopy, test); System.out.println(eval.toClassDetailsString()); } System.out.println( eval.toSummaryString("=== " + folds + "-fold Cross-validation run " + (i + 1) + " ===", false)); } }
From source file:REPTree.java
License:Open Source License
/** * Parses a given list of options. <p/> * //www.j a v a 2 s . c om <!-- options-start --> * Valid options are: <p/> * * <pre> -M <minimum number of instances> * Set minimum number of instances per leaf (default 2).</pre> * * <pre> -V <minimum variance for split> * Set minimum numeric class variance proportion * of train variance for split (default 1e-3).</pre> * * <pre> -N <number of folds> * Number of folds for reduced error pruning (default 3).</pre> * * <pre> -S <seed> * Seed for random data shuffling (default 1).</pre> * * <pre> -P * No pruning.</pre> * * <pre> -L * Maximum tree depth (default -1, no maximum)</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String minNumString = Utils.getOption('M', options); if (minNumString.length() != 0) { m_MinNum = (double) Integer.parseInt(minNumString); } else { m_MinNum = 2; } String minVarString = Utils.getOption('V', options); if (minVarString.length() != 0) { m_MinVarianceProp = Double.parseDouble(minVarString); } else { m_MinVarianceProp = 1e-3; } String numFoldsString = Utils.getOption('N', options); if (numFoldsString.length() != 0) { m_NumFolds = Integer.parseInt(numFoldsString); } else { m_NumFolds = 3; } String seedString = Utils.getOption('S', options); if (seedString.length() != 0) { m_Seed = Integer.parseInt(seedString); } else { m_Seed = 1; } m_NoPruning = Utils.getFlag('P', options); String depthString = Utils.getOption('L', options); if (depthString.length() != 0) { m_MaxDepth = Integer.parseInt(depthString); } else { m_MaxDepth = -1; } String initialCountString = Utils.getOption('I', options); if (initialCountString.length() != 0) { m_InitialCount = Double.parseDouble(initialCountString); } else { m_InitialCount = 0; } m_SpreadInitialCount = Utils.getFlag('R', options); Utils.checkForRemainingOptions(options); }