📄 subsetsizeforwardselection.java
字号:
return "Set the type of the search."; } /** * Set the type * * @param t * the Linear Forward Selection type */ public void setType(SelectedTag t) { if (t.getTags() == TAGS_TYPE) { m_linearSelectionType = t.getSelectedTag().getID(); } } /** * Get the type * * @return the Linear Forward Selection type */ public SelectedTag getType() { return new SelectedTag(m_linearSelectionType, TAGS_TYPE); } /** * Returns the tip text for this property * * @return tip text for this property suitable for displaying in the * explorer/experimenter gui */ public String subsetSizeEvaluatorTipText() { return "Subset evaluator to use for subset size determination."; } /** * Set the subset evaluator to use for subset size determination. * * @param eval * the subset evaluator to use for subset size determination. */ public void setSubsetSizeEvaluator(ASEvaluation eval) throws Exception { if (!SubsetEvaluator.class.isInstance(eval)) { throw new Exception(eval.getClass().getName() + " is no subset evaluator."); } m_setSizeEval = (SubsetEvaluator) eval; } /** * Get the subset evaluator used for subset size determination. * * @return the evaluator used for subset size determination. */ public ASEvaluation getSubsetSizeEvaluator() { return m_setSizeEval; } /** * Returns the tip text for this property * * @return tip text for this property suitable for displaying in the * explorer/experimenter gui */ public String numSubsetSizeCVFoldsTipText() { return "Number of cross validation folds for subset size determination"; } /** * Set the number of cross validation folds for subset size determination * (default = 5). * * @param f * number of folds */ public void setNumSubsetSizeCVFolds(int f) { m_numFolds = f; } /** * Get the number of cross validation folds for subset size determination * (default = 5). * * @return number of folds */ public int getNumSubsetSizeCVFolds() { return m_numFolds; } /** * Returns the tip text for this property * * @return tip text for this property suitable for displaying in the * explorer/experimenter gui */ public String seedTipText() { return "Seed for cross validation subset size determination. (default = 1)"; } /** * Seed for cross validation subset size determination. (default = 1) * * @param s * seed */ public void setSeed(int s) { m_seed = s; } /** * Seed for cross validation subset size determination. (default = 1) * * @return seed */ public int getSeed() { return m_seed; } /** * Returns the tip text for this property * * @return tip text for this property suitable for displaying in the * explorer/experimenter gui */ public String verboseTipText() { return "Turn on verbose output for monitoring the search's progress."; } /** * Set whether verbose output should be generated. * * @param d * true if output is to be verbose. */ public void setVerbose(boolean b) { m_verbose = b; } /** * Get whether output is to be verbose * * @return true if output will be verbose */ public boolean getVerbose() { return m_verbose; } /** * Gets the current settings of LinearForwardSelection. * * @return an array of strings suitable for passing to setOptions() */ public String[] getOptions() { String[] evaluatorOptions = new String[0]; if ((m_setSizeEval != null) && (m_setSizeEval instanceof OptionHandler)) { evaluatorOptions = ((OptionHandler) m_setSizeEval).getOptions(); } String[] options = new String[15 + evaluatorOptions.length]; int current = 0; if (m_performRanking) { options[current++] = "-I"; } options[current++] = "-K"; options[current++] = "" + m_numUsedAttributes; options[current++] = "-T"; options[current++] = "" + m_linearSelectionType; options[current++] = "-F"; options[current++] = "" + m_numFolds; options[current++] = "-S"; options[current++] = "" + m_seed; options[current++] = "-Z"; options[current++] = "" + m_verbose; if (m_setSizeEval != null) { options[current++] = "-E"; options[current++] = m_setSizeEval.getClass().getName(); } options[current++] = "--"; System.arraycopy(evaluatorOptions, 0, options, current, evaluatorOptions.length); current += evaluatorOptions.length; while (current < options.length) { options[current++] = ""; } return options; } /** * returns a description of the search as a String * * @return a description of the search */ public String toString() { StringBuffer LFSString = new StringBuffer(); LFSString.append("\tSubset Size Forward Selection.\n"); LFSString.append("\tLinear Forward Selection Type: "); if (m_linearSelectionType == TYPE_FIXED_SET) { LFSString.append("fixed-set\n"); } else { LFSString.append("fixed-width\n"); } LFSString.append("\tNumber of top-ranked attributes that are used: " + m_numUsedAttributes + "\n"); LFSString.append( "\tNumber of cross validation folds for subset size determination: " + m_numFolds + "\n"); LFSString.append("\tSeed for cross validation subset size determination: " + m_seed + "\n"); LFSString.append("\tTotal number of subsets evaluated: " + m_totalEvals + "\n"); LFSString.append("\tMerit of best subset found: " + Utils.doubleToString(Math.abs(m_bestMerit), 8, 3) + "\n"); return LFSString.toString(); } /** * Searches the attribute subset space by subset size forward selection * * @param ASEvaluator * the attribute evaluator to guide the search * @param data * the training instances. * @return an array (not necessarily ordered) of selected attribute indexes * @exception Exception * if the search can't be completed */ public int[] search(ASEvaluation ASEval, Instances data) throws Exception { m_totalEvals = 0; if (!(ASEval instanceof SubsetEvaluator)) { throw new Exception(ASEval.getClass().getName() + " is not a " + "Subset evaluator!"); } if (m_setSizeEval == null) { m_setSizeEval = (SubsetEvaluator) ASEval; } m_numAttribs = data.numAttributes(); if (m_numUsedAttributes > m_numAttribs) { System.out.println( "Decreasing number of top-ranked attributes to total number of attributes: " + data.numAttributes()); m_numUsedAttributes = m_numAttribs; } Instances[] trainData = new Instances[m_numFolds]; Instances[] testData = new Instances[m_numFolds]; LFSMethods[] searchResults = new LFSMethods[m_numFolds]; Random random = new Random(m_seed); Instances dataCopy = new Instances(data); dataCopy.randomize(random); if (dataCopy.classAttribute().isNominal()) { dataCopy.stratify(m_numFolds); } for (int f = 0; f < m_numFolds; f++) { trainData[f] = dataCopy.trainCV(m_numFolds, f, random); testData[f] = dataCopy.testCV(m_numFolds, f); } LFSMethods LSF = new LFSMethods(); int[] ranking; if (m_performRanking) { ((SubsetEvaluator) ASEval).buildEvaluator(data); ranking = LSF.rankAttributes(data, (SubsetEvaluator) ASEval, m_verbose); } else { ranking = new int[m_numAttribs]; for (int i = 0; i < ranking.length; i++) { ranking[i] = i; } } int maxSubsetSize = 0; for (int f = 0; f < m_numFolds; f++) { if (m_verbose) { System.out.println("perform search on internal fold: " + (f + 1) + "/" + m_numFolds); } m_setSizeEval.buildEvaluator(trainData[f]); searchResults[f] = new LFSMethods(); searchResults[f].forwardSearch(m_cacheSize, new BitSet(m_numAttribs), ranking, m_numUsedAttributes, m_linearSelectionType == TYPE_FIXED_WIDTH, 1, -1, trainData[f], m_setSizeEval, m_verbose); maxSubsetSize = Math.max(maxSubsetSize, searchResults[f].getBestGroup().cardinality()); } if (m_verbose) { System.out.println( "continue searches on internal folds to maxSubsetSize (" + maxSubsetSize + ")"); } for (int f = 0; f < m_numFolds; f++) { if (m_verbose) { System.out.print("perform search on internal fold: " + (f + 1) + "/" + m_numFolds + " with starting set "); LFSMethods.printGroup(searchResults[f].getBestGroup(), trainData[f].numAttributes()); } if (searchResults[f].getBestGroup().cardinality() < maxSubsetSize) { m_setSizeEval.buildEvaluator(trainData[f]); searchResults[f].forwardSearch(m_cacheSize, searchResults[f].getBestGroup(), ranking, m_numUsedAttributes, m_linearSelectionType == TYPE_FIXED_WIDTH, 1, maxSubsetSize, trainData[f], m_setSizeEval, m_verbose); } } double[][] testMerit = new double[m_numFolds][maxSubsetSize + 1]; for (int f = 0; f < m_numFolds; f++) { for (int s = 1; s <= maxSubsetSize; s++) { if (HoldOutSubsetEvaluator.class.isInstance(m_setSizeEval)) { m_setSizeEval.buildEvaluator(trainData[f]); testMerit[f][s] = ((HoldOutSubsetEvaluator) m_setSizeEval).evaluateSubset(searchResults[f].getBestGroupOfSize( s), testData[f]); } else { m_setSizeEval.buildEvaluator(testData[f]); testMerit[f][s] = m_setSizeEval.evaluateSubset(searchResults[f].getBestGroupOfSize( s)); } } } double[] avgTestMerit = new double[maxSubsetSize + 1]; int finalSubsetSize = -1; for (int s = 1; s <= maxSubsetSize; s++) { for (int f = 0; f < m_numFolds; f++) { avgTestMerit[s] = ((avgTestMerit[s] * f) + testMerit[f][s]) / (double) (f + 1); } if ((finalSubsetSize == -1) || (avgTestMerit[s] > avgTestMerit[finalSubsetSize])) { finalSubsetSize = s; } if (m_verbose) { System.out.println("average merit for subset-size " + s + ": " + avgTestMerit[s]); } } if (m_verbose) { System.out.println("performing final forward selection to subset-size: " + finalSubsetSize); } ((SubsetEvaluator) ASEval).buildEvaluator(data); LSF.forwardSearch(m_cacheSize, new BitSet(m_numAttribs), ranking, m_numUsedAttributes, m_linearSelectionType == TYPE_FIXED_WIDTH, 1, finalSubsetSize, data, (SubsetEvaluator) ASEval, m_verbose); m_totalEvals = LSF.getNumEvalsTotal(); m_bestMerit = LSF.getBestMerit(); return attributeList(LSF.getBestGroup()); } /** * Reset options to default values */ protected void resetOptions() { m_performRanking = true; m_numUsedAttributes = 50; m_linearSelectionType = TYPE_FIXED_SET; m_setSizeEval = new ClassifierSubsetEval(); m_numFolds = 5; m_seed = 1; m_totalEvals = 0; m_cacheSize = 1; m_verbose = false; } /** * converts a BitSet into a list of attribute indexes * * @param group * the BitSet to convert * @return an array of attribute indexes */ protected int[] attributeList(BitSet group) { int count = 0; // count how many were selected for (int i = 0; i < m_numAttribs; i++) { if (group.get(i)) { count++; } } int[] list = new int[count]; count = 0; for (int i = 0; i < m_numAttribs; i++) { if (group.get(i)) { list[count++] = i; } } return list; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -