下面列出了怎么用weka.core.OptionHandler的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector result = new Vector();
result.addElement(new Option(
"\tFull name of base clusterer.\n"
+ "\t(default: " + defaultClustererString() +")",
"W", 1, "-W"));
if (m_Clusterer instanceof OptionHandler) {
result.addElement(new Option(
"",
"", 0, "\nOptions specific to clusterer "
+ m_Clusterer.getClass().getName() + ":"));
Enumeration enu = ((OptionHandler) m_Clusterer).listOptions();
while (enu.hasMoreElements()) {
result.addElement(enu.nextElement());
}
}
return result.elements();
}
/**
* Gets the current settings of the associator.
*
* @return an array of strings suitable for passing to setOptions
*/
public String[] getOptions() {
int i;
Vector<String> result;
String[] options;
result = new Vector<String>();
result.add("-W");
result.add(getAssociator().getClass().getName());
if (getAssociator() instanceof OptionHandler) {
options = ((OptionHandler) getAssociator()).getOptions();
result.add("--");
for (i = 0; i < options.length; i++)
result.add(options[i]);
}
return result.toArray(new String[result.size()]);
}
public static String getDescriptor(final Object o) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
if (o instanceof OptionHandler) {
sb.append("- [");
int i = 0;
for (String s : ((OptionHandler) o).getOptions()) {
if (i++ > 0) {
sb.append(", ");
}
sb.append(s);
}
sb.append("]");
}
return sb.toString();
}
/**
* Gets the current settings of the filter.
*
* @return an array of strings suitable for passing to setOptions
*/
public String [] getOptions() {
String [] generatorOptions = new String [0];
if ((m_partitionGenerator != null) &&
(m_partitionGenerator instanceof OptionHandler)) {
generatorOptions = ((OptionHandler)m_partitionGenerator).getOptions();
}
String [] options = new String [generatorOptions.length + 3];
int current = 0;
if (m_partitionGenerator != null) {
options[current++] = "-W";
options[current++] = getPartitionGenerator().getClass().getName();
}
options[current++] = "--";
System.arraycopy(generatorOptions, 0, options, current,
generatorOptions.length);
current += generatorOptions.length;
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* returns the options of the current setup
*
* @return the current options
*/
public String[] getOptions(){
int i;
Vector<String> result;
String[] options;
result = new Vector<String>();
result.add("-W");
result.add("" + getClusterer().getClass().getName());
options = super.getOptions();
for (i = 0; i < options.length; i++)
result.add(options[i]);
if (getClusterer() instanceof OptionHandler) {
result.add("--");
options = ((OptionHandler) getClusterer()).getOptions();
for (i = 0; i < options.length; i++)
result.add(options[i]);
}
return result.toArray(new String[result.size()]);
}
/**
* Gets the current settings of the Classifier.
*
* @return an array of strings suitable for passing to setOptions
*/
public String [] getOptions() {
String [] superOptions = super.getOptions();
String [] options = new String [superOptions.length + 4];
int current = 0;
options[current++] = "-X"; options[current++] = "" + getNumFolds();
options[current++] = "-M";
options[current++] = getMetaClassifier().getClass().getName() + " "
+ Utils.joinOptions(((OptionHandler)getMetaClassifier()).getOptions());
System.arraycopy(superOptions, 0, options, current,
superOptions.length);
return options;
}
@Override
public IReconstructionPlan getConstructionPlan() {
try {
if (this.wrappedClassifier instanceof MLPipeline) {
MLPipeline pipeline = (MLPipeline) this.wrappedClassifier;
Classifier classifier = pipeline.getBaseClassifier();
ASSearch searcher = pipeline.getPreprocessors().get(0).getSearcher();
ASEvaluation evaluator = pipeline.getPreprocessors().get(0).getEvaluator();
return new ReconstructionPlan(
Arrays.asList(new ReconstructionInstruction(WekaClassifier.class.getMethod("createPipeline", String.class, List.class, String.class, List.class, String.class, List.class), searcher.getClass().getName(),
((OptionHandler) searcher).getOptions(), evaluator.getClass().getName(), ((OptionHandler) evaluator).getOptions(), classifier.getClass().getName(), ((OptionHandler) classifier).getOptions())));
} else {
return new ReconstructionPlan(Arrays.asList(new ReconstructionInstruction(WekaClassifier.class.getMethod("createBaseClassifier", String.class, List.class), this.name, this.getOptionsAsList())));
}
} catch (NoSuchMethodException | SecurityException e) {
throw new UnsupportedOperationException(e);
}
}
/**
* Sets the format of output instances. The derived class should use this
* method once it has determined the outputformat. The
* output queue is cleared.
*
* @param outputFormat the new output format
*/
protected void setOutputFormat(Instances outputFormat) {
if (outputFormat != null) {
m_OutputFormat = outputFormat.stringFreeStructure();
initOutputLocators(m_OutputFormat, null);
// Rename the relation
String relationName = outputFormat.relationName()
+ "-" + this.getClass().getName();
if (this instanceof OptionHandler) {
String [] options = ((OptionHandler)this).getOptions();
for (int i = 0; i < options.length; i++) {
relationName += options[i].trim();
}
}
m_OutputFormat.setRelationName(relationName);
} else {
m_OutputFormat = null;
}
m_OutputQueue = new Queue();
}
/**
* set a parameter to a ParamSet. Parameters are propogated through that object to children, if any parameters
* are specified for the children.
* @param object
* @param paramSet
*/
static void setParams(Object object, ParamSet paramSet) {
try {
if(object instanceof ParamHandler) {
((ParamHandler) object).setParams(paramSet);
} else if(object instanceof OptionHandler) {
((OptionHandler) object).setOptions(paramSet.getOptions());
} else {
throw new IllegalArgumentException("params not settable");
}
} catch(Exception e) {
throw new IllegalArgumentException(e);
}
}
@Test
public void testOptions() throws Exception {
if (wrapper instanceof OptionHandler) {
OptionHandler optionHandler = ((OptionHandler) wrapper);
String[] options = optionHandler.getOptions();
String[] optionsCopy = Arrays.copyOf(options, options.length);
optionHandler.setOptions(optionsCopy);
String[] optionsAfter = optionHandler.getOptions();
Assert.assertArrayEquals(options, optionsAfter);
}
}
/**
* returns the specification of the given object (class, options if an
* instance of OptionHandler)
*
* @param o the object to get the specs as string
* @return the specification string
*/
protected String getSpecification(Object o) {
String result;
result = o.getClass().getName();
if (o instanceof OptionHandler)
result += " " + Utils.joinOptions(((OptionHandler) o).getOptions());
return result.trim();
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector<Option> result = new Vector<Option>();
Enumeration enm = super.listOptions();
while (enm.hasMoreElements())
result.addElement((Option)enm.nextElement());
result.addElement(new Option("\tLevel of Gaussian Noise wrt transformed target." + " (default 1)", "L", 1, "-L <double>"));
result.addElement(new Option("\tWhether to 0=normalize/1=standardize/2=neither. " + "(default 0=normalize)",
"N", 1, "-N"));
result.addElement(new Option("\tThe Kernel to use.\n"
+ "\t(default: weka.classifiers.functions.supportVector.PolyKernel)", "K", 1,
"-K <classname and parameters>"));
result.addElement(new Option("", "", 0, "\nOptions specific to kernel " + getKernel().getClass().getName()
+ ":"));
enm = ((OptionHandler) getKernel()).listOptions();
while (enm.hasMoreElements())
result.addElement((Option)enm.nextElement());
return result.elements();
}
/**
* Gets the KDTree specification string, which contains the class name of
* the KDTree class and any options to the KDTree.
*
* @return the KDTree string.
*/
protected String getKDTreeSpec() {
KDTree c = getKDTree();
if (c instanceof OptionHandler) {
return c.getClass().getName() + " "
+ Utils.joinOptions(((OptionHandler)c).getOptions());
}
return c.getClass().getName();
}
/**
* Returns an enumeration describing the available options..
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector result = new Vector();
result.addElement(new Option(
"\tminimum allowable standard deviation for normal density computation "
+"\n\t(default 1e-6)"
,"M",1,"-M <num>"));
result.addElement(new Option(
"\tClusterer to wrap.\n"
+ "\t(default " + defaultClustererString() + ")",
"W", 1,"-W <clusterer name>"));
if ((m_wrappedClusterer != null) &&
(m_wrappedClusterer instanceof OptionHandler)) {
result.addElement(new Option(
"",
"", 0, "\nOptions specific to clusterer "
+ m_wrappedClusterer.getClass().getName() + ":"));
Enumeration enu = ((OptionHandler)m_wrappedClusterer).listOptions();
while (enu.hasMoreElements()) {
result.addElement(enu.nextElement());
}
}
return result.elements();
}
/**
* Gets the current settings of the clusterer.
*
* @return an array of strings suitable for passing to setOptions()
*/
public String[] getOptions() {
String [] clustererOptions = new String [0];
if ((m_wrappedClusterer != null) &&
(m_wrappedClusterer instanceof OptionHandler)) {
clustererOptions = ((OptionHandler)m_wrappedClusterer).getOptions();
}
String [] options = new String [clustererOptions.length + 5];
int current = 0;
options[current++] = "-M";
options[current++] = ""+getMinStdDev();
if (getClusterer() != null) {
options[current++] = "-W";
options[current++] = getClusterer().getClass().getName();
}
options[current++] = "--";
System.arraycopy(clustererOptions, 0, options, current,
clustererOptions.length);
current += clustererOptions.length;
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* Gets the clusterer specification string, which contains the class name of
* the clusterer and any options to the clusterer
*
* @return the clusterer string
*/
protected String getClustererSpec() {
String result;
Clusterer clusterer;
clusterer = getClusterer();
result = clusterer.getClass().getName();
if (clusterer instanceof OptionHandler)
result += " " + Utils.joinOptions(((OptionHandler) clusterer).getOptions());
return result;
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector result = new Vector();
Enumeration en = super.listOptions();
while (en.hasMoreElements())
result.addElement(en.nextElement());
result.addElement(new Option(
"\tFull name of the clusterer analyzed.\n"
+"\teg: weka.clusterers.SimpleKMeans\n"
+ "\t(default weka.clusterers.SimpleKMeans)",
"W", 1, "-W"));
if ((m_Clusterer != null)
&& (m_Clusterer instanceof OptionHandler)) {
result.addElement(new Option("", "", 0,
"\nOptions specific to clusterer "
+ m_Clusterer.getClass().getName()
+ ":"));
Enumeration enu = ((OptionHandler)m_Clusterer).listOptions();
while (enu.hasMoreElements())
result.addElement(enu.nextElement());
}
return result.elements();
}
/**
* Checks whether the scheme can take command line options.
*
* @return index 0 is true if the clusterer can take options
*/
protected boolean[] canTakeOptions() {
boolean[] result = new boolean[2];
print("options...");
if (m_Clusterer instanceof OptionHandler) {
println("yes");
if (m_Debug) {
println("\n=== Full report ===");
Enumeration enu = ((OptionHandler)m_Clusterer).listOptions();
while (enu.hasMoreElements()) {
Option option = (Option) enu.nextElement();
print(option.synopsis() + "\n"
+ option.description() + "\n");
}
println("\n");
}
result[0] = true;
}
else {
println("no");
result[0] = false;
}
return result;
}
/**
* Checks whether the scheme can take command line options.
*
* @return index 0 is true if the kernel can take options
*/
protected boolean[] canTakeOptions() {
boolean[] result = new boolean[2];
print("options...");
if (m_Kernel instanceof OptionHandler) {
println("yes");
if (m_Debug) {
println("\n=== Full report ===");
Enumeration enu = ((OptionHandler)m_Kernel).listOptions();
while (enu.hasMoreElements()) {
Option option = (Option) enu.nextElement();
print(option.synopsis() + "\n"
+ option.description() + "\n");
}
println("\n");
}
result[0] = true;
}
else {
println("no");
result[0] = false;
}
return result;
}
/**
* Gets the filter specification string, which contains the class name of
* the filter and any options to the filter
*
* @return the filter string.
*/
protected String getFilterSpec() {
Filter c = getFilter();
if (c instanceof OptionHandler)
return c.getClass().getName() + " "
+ Utils.joinOptions(((OptionHandler)c).getOptions());
else
return c.getClass().getName();
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector result = new Vector();
Enumeration en = super.listOptions();
while (en.hasMoreElements())
result.addElement(en.nextElement());
result.addElement(new Option(
"\tFull name of the associator analysed.\n"
+"\teg: weka.associations.Apriori\n"
+ "\t(default weka.associations.Apriori)",
"W", 1, "-W"));
if ((m_Associator != null)
&& (m_Associator instanceof OptionHandler)) {
result.addElement(new Option("", "", 0,
"\nOptions specific to associator "
+ m_Associator.getClass().getName()
+ ":"));
Enumeration enu = ((OptionHandler)m_Associator).listOptions();
while (enu.hasMoreElements())
result.addElement(enu.nextElement());
}
return result.elements();
}
/**
* Checks whether the scheme can take command line options.
*
* @return index 0 is true if the associator can take options
*/
protected boolean[] canTakeOptions() {
boolean[] result = new boolean[2];
print("options...");
if (m_Associator instanceof OptionHandler) {
println("yes");
if (m_Debug) {
println("\n=== Full report ===");
Enumeration enu = ((OptionHandler)m_Associator).listOptions();
while (enu.hasMoreElements()) {
Option option = (Option) enu.nextElement();
print(option.synopsis() + "\n"
+ option.description() + "\n");
}
println("\n");
}
result[0] = true;
}
else {
println("no");
result[0] = false;
}
return result;
}
/**
* Generates an option string to output on the commandline.
*
* @param associator the associator to generate the string for
* @return the option string
*/
protected static String makeOptionString(Associator associator) {
StringBuffer text;
text = new StringBuffer();
// general options
text.append("\nGeneral options:\n\n");
text.append("-t <training file>\n");
text.append("\tThe name of the training file.\n");
text.append("-g <name of graph file>\n");
text.append("\tOutputs the graph representation (if supported) of the associator to a file.\n");
// associator specific options, if any
if (associator instanceof OptionHandler) {
text.append(
"\nOptions specific to "
+ associator.getClass().getName().replaceAll(".*\\.", "") + ":\n\n");
Enumeration enm = ((OptionHandler) associator).listOptions();
while (enm.hasMoreElements()) {
Option option = (Option) enm.nextElement();
text.append(option.synopsis() + "\n");
text.append(option.description() + "\n");
}
}
return text.toString();
}
/**
* Gets the current settings of the filter.
*
* @return an array of strings suitable for passing to setOptions
*/
public String [] getOptions() {
String [] clustererOptions = new String [0];
if ((m_clusterer != null) &&
(m_clusterer instanceof OptionHandler)) {
clustererOptions = ((OptionHandler)m_clusterer).getOptions();
}
String [] options = new String [clustererOptions.length + 5];
int current = 0;
if (!getIgnoredAttributeIndices().equals("")) {
options[current++] = "-I";
options[current++] = getIgnoredAttributeIndices();
}
if (m_clusterer != null) {
options[current++] = "-W";
options[current++] = getDensityBasedClusterer().getClass().getName();
}
options[current++] = "--";
System.arraycopy(clustererOptions, 0, options, current,
clustererOptions.length);
current += clustererOptions.length;
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* Gets the current settings of BestFirst.
* @return an array of strings suitable for passing to setOptions()
*/
public String[] getOptions () {
int current = 0;
String[] evaluatorOptions = new String[0];
if ((m_ASEval != null) &&
(m_ASEval instanceof OptionHandler)) {
evaluatorOptions = ((OptionHandler)m_ASEval).getOptions();
}
String[] options = new String[17+evaluatorOptions.length];
options[current++] = "-R"; options[current++] = ""+m_raceType;
options[current++] = "-L"; options[current++] = ""+getSignificanceLevel();
options[current++] = "-T"; options[current++] = ""+getThreshold();
options[current++] = "-F"; options[current++] = ""+m_xvalType;
if (getGenerateRanking()) {
options[current++] = "-Q";
}
options[current++] = "-N"; options[current++] = ""+getNumToSelect();
options[current++] = "-J"; options[current++] = ""+getSelectionThreshold();
if (getDebug()) {
options[current++] = "-Z";
}
if (getAttributeEvaluator() != null) {
options[current++] = "-A";
options[current++] = getAttributeEvaluator().getClass().getName();
options[current++] = "--";
System.arraycopy(evaluatorOptions, 0, options, current,
evaluatorOptions.length);
current += evaluatorOptions.length;
}
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*
*/
public Enumeration listOptions() {
Vector newVector = new Vector(9);
newVector.addElement(new Option("\tPerform initial ranking to select the" +
"\n\ttop-ranked attributes.", "I", 0, "-I"));
newVector.addElement(new Option(
"\tNumber of top-ranked attributes that are " +
"\n\ttaken into account by the search.", "K", 1, "-K <num>"));
newVector.addElement(new Option(
"\tType of Linear Forward Selection (default = 0).", "T", 1,
"-T <0 = fixed-set | 1 = fixed-width>"));
newVector.addElement(new Option(
"\tSize of lookup cache for evaluated subsets." +
"\n\tExpressed as a multiple of the number of" +
"\n\tattributes in the data set. (default = 1)", "S", 1, "-S <num>"));
newVector.addElement(new Option(
"\tSubset-evaluator used for subset-size determination." + "-- -M",
"E", 1, "-E <subset evaluator>"));
newVector.addElement(new Option("\tNumber of cross validation folds" +
"\n\tfor subset size determination (default = 5).", "F", 1, "-F <num>"));
newVector.addElement(new Option("\tSeed for cross validation" +
"\n\tsubset size determination. (default = 1)", "R", 1, "-R <num>"));
newVector.addElement(new Option("\tverbose on/off", "Z", 0, "-Z"));
if ((m_setSizeEval != null) && (m_setSizeEval instanceof OptionHandler)) {
newVector.addElement(new Option("", "", 0,
"\nOptions specific to " + "evaluator " +
m_setSizeEval.getClass().getName() + ":"));
Enumeration enu = ((OptionHandler) m_setSizeEval).listOptions();
while (enu.hasMoreElements()) {
newVector.addElement(enu.nextElement());
}
}
return newVector.elements();
}
/**
* Gets the clusterer specification string, which contains the class name of
* the clusterer and any options to the clusterer.
*
* @return the clusterer string.
*/
protected String getClustererSpec() {
Clusterer c = getClusterer();
if (c instanceof OptionHandler) {
return c.getClass().getName() + " "
+ Utils.joinOptions(((OptionHandler)c).getOptions());
}
return c.getClass().getName();
}
/**
* Checks whether the scheme can take command line options.
*
* @return index 0 is true if the estimator can take options
*/
protected boolean[] canTakeOptions() {
boolean[] result = new boolean[2];
print("options...");
if (m_Estimator instanceof OptionHandler) {
println("yes");
if (m_Debug) {
println("\n=== Full report ===");
Enumeration enu = ((OptionHandler)m_Estimator).listOptions();
while (enu.hasMoreElements()) {
Option option = (Option) enu.nextElement();
print(option.synopsis() + "\n"
+ option.description() + "\n");
}
println("\n");
}
result[0] = true;
}
else {
println("no");
result[0] = false;
}
return result;
}
/**
* Gets the classifier specification string, which contains the class name of
* the classifier and any options to the classifier.
*
* @return the classifier string.
*/
protected String getClassifierSpec() {
String result;
Classifier c;
c = getClassifier();
result = c.getClass().getName();
if (c instanceof OptionHandler)
result += " " + Utils.joinOptions(((OptionHandler) c).getOptions());
return result;
}
/**
* Gets the current settings of ClassifierSubsetEval
*
* @return an array of strings suitable for passing to setOptions()
*/
public String[] getOptions () {
String[] classifierOptions = new String[0];
if ((m_Classifier != null) &&
(m_Classifier instanceof OptionHandler)) {
classifierOptions = ((OptionHandler)m_Classifier).getOptions();
}
String[] options = new String[6 + classifierOptions.length];
int current = 0;
if (getClassifier() != null) {
options[current++] = "-B";
options[current++] = getClassifier().getClass().getName();
}
if (getUseTraining()) {
options[current++] = "-T";
}
options[current++] = "-H"; options[current++] = getHoldOutFile().getPath();
if (classifierOptions.length > 0) {
options[current++] = "--";
System.arraycopy(classifierOptions, 0, options, current,
classifierOptions.length);
current += classifierOptions.length;
}
while (current < options.length) {
options[current++] = "";
}
return options;
}