📄 m5base.java
字号:
protected boolean getGenerateRules() { return m_generateRules; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String useUnsmoothedTipText() { return "Whether to use unsmoothed predictions."; } /** * Use unsmoothed predictions * * @param s true if unsmoothed predictions are to be used */ public void setUseUnsmoothed(boolean s) { m_unsmoothedPredictions = s; } /** * Get whether or not smoothing is being used * * @return true if unsmoothed predictions are to be used */ public boolean getUseUnsmoothed() { return m_unsmoothedPredictions; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String buildRegressionTreeTipText() { return "Whether to generate a regression tree/rule instead of a model tree/rule."; } /** * Get the value of regressionTree. * * @return Value of regressionTree. */ public boolean getBuildRegressionTree() { return m_regressionTree; } /** * Set the value of regressionTree. * * @param newregressionTree Value to assign to regressionTree. */ public void setBuildRegressionTree(boolean newregressionTree) { m_regressionTree = newregressionTree; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String minNumInstancesTipText() { return "The minimum number of instances to allow at a leaf node."; } /** * Set the minimum number of instances to allow at a leaf node * * @param minNum the minimum number of instances */ public void setMinNumInstances(double minNum) { m_minNumInstances = minNum; } /** * Get the minimum number of instances to allow at a leaf node * * @return a <code>double</code> value */ public double getMinNumInstances() { return m_minNumInstances; } /** * Returns default capabilities of the classifier, i.e., of LinearRegression. * * @return the capabilities of this classifier */ public Capabilities getCapabilities() { return new LinearRegression().getCapabilities(); } /** * Generates the classifier. * * @param data set of instances serving as training data * @throws Exception if the classifier has not been generated * 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(); m_instances = new Instances(data); m_replaceMissing = new ReplaceMissingValues(); m_replaceMissing.setInputFormat(m_instances); m_instances = Filter.useFilter(m_instances, m_replaceMissing); m_nominalToBinary = new NominalToBinary(); m_nominalToBinary.setInputFormat(m_instances); m_instances = Filter.useFilter(m_instances, m_nominalToBinary); m_removeUseless = new RemoveUseless(); m_removeUseless.setInputFormat(m_instances); m_instances = Filter.useFilter(m_instances, m_removeUseless); m_instances.randomize(new Random(1)); m_ruleSet = new FastVector(); Rule tempRule; if (m_generateRules) { Instances tempInst = m_instances; do { tempRule = new Rule(); tempRule.setSmoothing(!m_unsmoothedPredictions); tempRule.setRegressionTree(m_regressionTree); tempRule.setUnpruned(m_useUnpruned); tempRule.setSaveInstances(false); tempRule.setMinNumInstances(m_minNumInstances); tempRule.buildClassifier(tempInst); m_ruleSet.addElement(tempRule); // System.err.println("Built rule : "+tempRule.toString()); tempInst = tempRule.notCoveredInstances(); } while (tempInst.numInstances() > 0); } else { // just build a single tree tempRule = new Rule(); tempRule.setUseTree(true); // tempRule.setGrowFullTree(true); tempRule.setSmoothing(!m_unsmoothedPredictions); tempRule.setSaveInstances(m_saveInstances); tempRule.setRegressionTree(m_regressionTree); tempRule.setUnpruned(m_useUnpruned); tempRule.setMinNumInstances(m_minNumInstances); Instances temp_train; temp_train = m_instances; tempRule.buildClassifier(temp_train); m_ruleSet.addElement(tempRule); // save space m_instances = new Instances(m_instances, 0); // System.err.print(tempRule.m_topOfTree.treeToString(0)); } } /** * Calculates a prediction for an instance using a set of rules * or an M5 model tree * * @param inst the instance whos class value is to be predicted * @return the prediction * @throws Exception if a prediction can't be made. */ public double classifyInstance(Instance inst) throws Exception { Rule temp; double prediction = 0; boolean success = false; m_replaceMissing.input(inst); inst = m_replaceMissing.output(); m_nominalToBinary.input(inst); inst = m_nominalToBinary.output(); m_removeUseless.input(inst); inst = m_removeUseless.output(); if (m_ruleSet == null) { throw new Exception("Classifier has not been built yet!"); } if (!m_generateRules) { temp = (Rule) m_ruleSet.elementAt(0); return temp.classifyInstance(inst); } boolean cont; int i; for (i = 0; i < m_ruleSet.size(); i++) { cont = false; temp = (Rule) m_ruleSet.elementAt(i); try { prediction = temp.classifyInstance(inst); success = true; } catch (Exception e) { cont = true; } if (!cont) { break; } } if (!success) { System.out.println("Error in predicting (DecList)"); } return prediction; } /** * Returns a description of the classifier * * @return a description of the classifier as a String */ public String toString() { StringBuffer text = new StringBuffer(); Rule temp; if (m_ruleSet == null) { return "Classifier hasn't been built yet!"; } if (m_generateRules) { text.append("M5 " + ((m_useUnpruned == true) ? "unpruned " : "pruned ") + ((m_regressionTree == true) ? "regression " : "model ") + "rules "); if (!m_unsmoothedPredictions) { text.append("\n(using smoothed linear models) "); } text.append(":\n"); text.append("Number of Rules : " + m_ruleSet.size() + "\n\n"); for (int j = 0; j < m_ruleSet.size(); j++) { temp = (Rule) m_ruleSet.elementAt(j); text.append("Rule: " + (j + 1) + "\n"); text.append(temp.toString()); } } else { temp = (Rule) m_ruleSet.elementAt(0); text.append(temp.toString()); } return text.toString(); } /** * Returns an enumeration of the additional measure names * @return an enumeration of the measure names */ public Enumeration enumerateMeasures() { Vector newVector = new Vector(1); newVector.addElement("measureNumRules"); return newVector.elements(); } /** * Returns the value of the named measure * @param additionalMeasureName the name of the measure to query for its value * @return the value of the named measure * @throws Exception if the named measure is not supported */ public double getMeasure(String additionalMeasureName) { if (additionalMeasureName.compareToIgnoreCase("measureNumRules") == 0) { return measureNumRules(); } else { throw new IllegalArgumentException(additionalMeasureName + " not supported (M5)"); } } /** * return the number of rules * @return the number of rules (same as # linear models & * # leaves in the tree) */ public double measureNumRules() { if (m_generateRules) { return m_ruleSet.size(); } return ((Rule)m_ruleSet.elementAt(0)).m_topOfTree.numberOfLinearModels(); } public RuleNode getM5RootNode() { Rule temp = (Rule) m_ruleSet.elementAt(0); return temp.getM5RootNode(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -