randomsubspace.java
来自「Weka」· Java 代码 · 共 522 行 · 第 1/2 页
JAVA
522 行
* * <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 */ public void setOptions(String[] options) throws Exception { String tmpStr; tmpStr = Utils.getOption('P', options); if (tmpStr.length() != 0) setSubSpaceSize(Double.parseDouble(tmpStr)); else setSubSpaceSize(0.5); super.setOptions(options); } /** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { Vector result; String[] options; int i; result = new Vector(); result.add("-P"); result.add("" + getSubSpaceSize()); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); return (String[]) result.toArray(new String[result.size()]); } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String subSpaceSizeTipText() { return "Size of each subSpace: if less than 1 as a percentage of the " + "number of attributes, otherwise the absolute number of attributes."; } /** * Gets the size of each subSpace, as a percentage of the training set size. * * @return the subSpace size, as a percentage. */ public double getSubSpaceSize() { return m_SubSpaceSize; } /** * Sets the size of each subSpace, as a percentage of the training set size. * * @param value the subSpace size, as a percentage. */ public void setSubSpaceSize(double value) { m_SubSpaceSize = value; } /** * calculates the number of attributes * * @param total the available number of attributes * @param fraction the fraction - if less than 1 it represents the * percentage, otherwise the absolute number of attributes * @return the number of attributes to use */ protected int numberOfAttributes(int total, double fraction) { int k = (int) Math.round((fraction < 1.0) ? total*fraction : fraction); if (k > total) k = total; if (k < 1) k = 1; return k; } /** * generates an index string describing a random subspace, suitable for * the Remove filter. * * @param indices the attribute indices * @param subSpaceSize the size of the subspace * @param classIndex the class index * @param random the random number generator * @return the generated string describing the subspace */ protected String randomSubSpace(Integer[] indices, int subSpaceSize, int classIndex, Random random) { Collections.shuffle(Arrays.asList(indices), random); StringBuffer sb = new StringBuffer(""); for(int i = 0; i < subSpaceSize; i++) { sb.append(indices[i]+","); } sb.append(classIndex); if (getDebug()) System.out.println("subSPACE = " + sb); return sb.toString(); } /** * builds the classifier. * * @param data the training data to be used for generating the * classifier. * @throws Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class data = new Instances(data); data.deleteWithMissingClass(); // only class? -> build ZeroR model if (data.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(data); return; } else { m_ZeroR = null; } super.buildClassifier(data); Integer[] indices = new Integer[data.numAttributes()-1]; int classIndex = data.classIndex(); int offset = 0; for(int i = 0; i < indices.length+1; i++) { if (i != classIndex) { indices[offset++] = i+1; } } int subSpaceSize = numberOfAttributes(indices.length, getSubSpaceSize()); Random random = data.getRandomNumberGenerator(m_Seed); for (int j = 0; j < m_Classifiers.length; j++) { if (m_Classifier instanceof Randomizable) { ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt()); } FilteredClassifier fc = new FilteredClassifier(); fc.setClassifier(m_Classifiers[j]); m_Classifiers[j] = fc; Remove rm = new Remove(); rm.setOptions(new String[]{"-V", "-R", randomSubSpace(indices,subSpaceSize,classIndex+1,random)}); fc.setFilter(rm); // build the classifier m_Classifiers[j].buildClassifier(data); } } /** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return preedicted class probability distribution * @throws Exception if distribution can't be computed successfully */ public double[] distributionForInstance(Instance instance) throws Exception { // default model? if (m_ZeroR != null) { return m_ZeroR.distributionForInstance(instance); } double[] sums = new double [instance.numClasses()], newProbs; for (int i = 0; i < m_NumIterations; i++) { if (instance.classAttribute().isNumeric() == true) { sums[0] += m_Classifiers[i].classifyInstance(instance); } else { newProbs = m_Classifiers[i].distributionForInstance(instance); for (int j = 0; j < newProbs.length; j++) sums[j] += newProbs[j]; } } if (instance.classAttribute().isNumeric() == true) { sums[0] /= (double)m_NumIterations; return sums; } else if (Utils.eq(Utils.sum(sums), 0)) { return sums; } else { Utils.normalize(sums); return sums; } } /** * Returns description of the bagged classifier. * * @return description of the bagged classifier as a string */ public String toString() { // only ZeroR model? if (m_ZeroR != null) { StringBuffer buf = new StringBuffer(); buf.append(this.getClass().getName().replaceAll(".*\\.", "") + "\n"); buf.append(this.getClass().getName().replaceAll(".*\\.", "").replaceAll(".", "=") + "\n\n"); buf.append("Warning: No model could be built, hence ZeroR model is used:\n\n"); buf.append(m_ZeroR.toString()); return buf.toString(); } if (m_Classifiers == null) { return "RandomSubSpace: No model built yet."; } StringBuffer text = new StringBuffer(); text.append("All the base classifiers: \n\n"); for (int i = 0; i < m_Classifiers.length; i++) text.append(m_Classifiers[i].toString() + "\n\n"); return text.toString(); } /** * Main method for testing this class. * * @param args the options */ public static void main(String[] args) { runClassifier(new RandomSubSpace(), args); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?