📄 classifiertree.java
字号:
.append(") {\n");
text.append(" p = ")
.append(m_localModel.distribution().maxClass(0))
.append(";\n");
text.append(" } ");
for (int i = 0; i < m_sons.length; i++) {
text.append("else if (" + m_localModel.sourceExpression(i, m_train)
+ ") {\n");
if (m_sons[i].m_isLeaf) {
text.append(" p = "
+ m_localModel.distribution().maxClass(i) + ";\n");
} else {
StringBuffer [] sub = m_sons[i].toSource(className);
text.append(sub[0]);
atEnd.append(sub[1]);
}
text.append(" } ");
if (i == m_sons.length - 1) {
text.append('\n');
}
}
text.append(" return p;\n }\n");
result[0] = new StringBuffer(" p = " + className + ".N");
result[0].append(Integer.toHexString(m_localModel.hashCode()) + printID)
.append("(i);\n");
result[1] = text.append(atEnd);
}
return result;
}
/**
* Returns number of leaves in tree structure.
*/
public int numLeaves() {
int num = 0;
int i;
if (m_isLeaf)
return 1;
else
for (i=0;i<m_sons.length;i++)
num = num+m_sons[i].numLeaves();
return num;
}
/**
* Returns number of nodes in tree structure.
*/
public int numNodes() {
int no = 1;
int i;
if (!m_isLeaf)
for (i=0;i<m_sons.length;i++)
no = no+m_sons[i].numNodes();
return no;
}
/**
* Prints tree structure.
*/
public String toString() {
try {
StringBuffer text = new StringBuffer();
if (m_isLeaf) {
text.append(": ");
text.append(m_localModel.dumpLabel(0, m_train));
} else
dumpTree(1, text);
text.append("\r\n\r\nNumber of Leaves : \t" + numLeaves() + "\r\n");
text.append("\r\nSize of the tree : \t" + numNodes() + "\r\n");
return text.toString();
} catch (Exception e) {
return "Can't print classification tree.";
}
}
/**
* Returns a newly created tree.
*
* @param data
* the training data
* @exception Exception
* if something goes wrong
*/
protected ClassifierTree getNewTree(Instances data) throws Exception {
ClassifierTree newTree = new ClassifierTree(m_toSelectModel);
newTree.buildTree(data, false);
return newTree;
}
/**
* Returns a newly created tree.
*
* @param data the training data
* @param test the pruning data.
* @exception Exception if something goes wrong
*/
protected ClassifierTree getNewTree(Instances train, Instances test)
throws Exception {
ClassifierTree newTree = new ClassifierTree(m_toSelectModel);
newTree.buildTree(train, test, false);
return newTree;
}
/**
* Help method for printing tree structure.
*
* @exception Exception if something goes wrong
*/
private void dumpTree(int depth,StringBuffer text)
throws Exception {
int i,j;
for (i = 0; i < m_sons.length; i++) {
text.append("\r\n");
for (j = 0; j < depth; j++)
{
if (j==depth-1) text.append("+---");
else text.append("| ");
}
text.append(m_localModel.leftSide(m_train));
text.append(m_localModel.rightSide(i, m_train));
// Create statistic distribution string
StringBuffer statStringBuffer = new StringBuffer();
double[] stats = getM_localModel().distribution().getClassDist();
double total=0;
// Calculate total
for(int index=0; index<stats.length; index++){
total += stats[index];
}
statStringBuffer.append("Total:(" + NumberFormatter.decimalFormat(total,0) + ") ");
// Calculate distribution
for(int index=0; index<stats.length; index++){
statStringBuffer.append(getClassLabel(index)+":(" + NumberFormatter.decimalFormat(stats[index],0) + "," + weka.core.Utils.roundDouble(100*stats[index]/total, NumberFormatter.MAX_FRACTION_DIGIT)+"%) ");
}
if (m_sons[i].m_isLeaf) {
text.append(": ");
text.append(m_localModel.dumpLabel(i, m_train));
text.append("\r\n");
for (j = 0; j < depth; j++)
text.append("| ");
text.append(statStringBuffer);
// Record the leaf node content,e.g., "petallength <= 4.9: Iris-A (48.-/1.0)"
// TWang Jan 26, 2005.
m_sons[i].setM_NodeContent(new String(m_localModel.leftSide(m_train) + "" + m_localModel.rightSide(i, m_train) + ":" + m_localModel.dumpLabel(i, m_train)), m_sons[i].m_localModel.distribution().getClassDist());
} else{
text.append("\r\n");
for (j = 0; j < depth; j++)
text.append("| ");
text.append(statStringBuffer);
// Record the internal node content,e.g., "petallength > 4.9"
// TWang Jan 26, 2005.
m_sons[i].setM_NodeContent( new String(m_localModel.leftSide(m_train) + "" + m_localModel.rightSide(i, m_train)), m_sons[i].m_localModel.distribution().getClassDist());
m_sons[i].dumpTree(depth + 1, text);
}
}
}
/**
* Help method for printing tree structure as a graph.
*
* @exception Exception if something goes wrong
*/
private void graphTree(StringBuffer text) throws Exception {
for (int i = 0; i < m_sons.length; i++) {
text.append("N" + m_id
+ "->" +
"N" + m_sons[i].m_id +
" [label=\"" + m_localModel.rightSide(i,m_train).trim() +
"\"]\n");
if (m_sons[i].m_isLeaf) {
text.append("N" + m_sons[i].m_id +
" [label=\""+m_localModel.dumpLabel(i,m_train)+"\" "+
"shape=box style=filled ");
if (m_train != null && m_train.numInstances() > 0) {
text.append("data =\n" + m_sons[i].m_train + "\n");
text.append(",\n");
}
text.append("]\n");
} else {
text.append("N" + m_sons[i].m_id +
" [label=\""+m_sons[i].m_localModel.leftSide(m_train) +
"\" ");
if (m_train != null && m_train.numInstances() > 0) {
text.append("data =\n" + m_sons[i].m_train + "\n");
text.append(",\n");
}
text.append("]\n");
m_sons[i].graphTree(text);
}
}
}
/**
* Prints the tree in prefix form
*/
private void prefixTree(StringBuffer text) throws Exception {
text.append("[");
text.append(m_localModel.leftSide(m_train)+":");
for (int i = 0; i < m_sons.length; i++) {
if (i > 0) {
text.append(",\n");
}
text.append(m_localModel.rightSide(i, m_train));
}
for (int i = 0; i < m_sons.length; i++) {
if (m_sons[i].m_isLeaf) {
text.append("[");
text.append(m_localModel.dumpLabel(i,m_train));
text.append("]");
} else {
m_sons[i].prefixTree(text);
}
}
text.append("]");
}
/**
* Help method for computing class probabilities of
* a given instance.
*
* @exception Exception if something goes wrong
*/
private double getProbsLaplace(int classIndex, Instance instance, double weight)
throws Exception {
double [] weights;
double prob = 0;
int treeIndex;
int i;
if (m_isLeaf) {
return weight * localModel().classProbLaplace(classIndex, instance, -1);
} else {
treeIndex = localModel().whichSubset(instance);
if (treeIndex == -1) {
weights = localModel().weights(instance);
for (i = 0; i < m_sons.length; i++) {
if (!son(i).m_isEmpty) {
if (!son(i).m_isLeaf) {
prob += son(i).getProbsLaplace(classIndex, instance,
weights[i] * weight);
} else {
prob += weight * weights[i] *
localModel().classProbLaplace(classIndex, instance, i);
}
}
}
return prob;
} else {
if (son(treeIndex).m_isLeaf) {
return weight * localModel().classProbLaplace(classIndex, instance,
treeIndex);
} else {
return son(treeIndex).getProbsLaplace(classIndex, instance, weight);
}
}
}
}
/**
* Help method for computing class probabilities of
* a given instance.
*
* @exception Exception if something goes wrong
*/
private double getProbs(int classIndex, Instance instance, double weight)
throws Exception {
double[] weights;
double prob = 0;
int treeIndex;
int i;
if (m_isLeaf) {
return weight * localModel().classProb(classIndex, instance, -1);
} else {
treeIndex = localModel().whichSubset(instance);
if (treeIndex == -1) {
weights = localModel().weights(instance);
for (i = 0; i < m_sons.length; i++) {
if (!son(i).m_isEmpty) {
prob += son(i).getProbs(classIndex, instance,
weights[i] * weight);
}
}
return prob;
} else {
if (son(treeIndex).m_isEmpty) {
return weight
* localModel().classProb(classIndex, instance,
treeIndex);
} else {
return son(treeIndex)
.getProbs(classIndex, instance, weight);
}
}
}
}
/**
* Method just exists to make program easier to read.
*/
private ClassifierSplitModel localModel() {
return (ClassifierSplitModel)m_localModel;
}
/**
* Method just exists to make program easier to read.
*/
private ClassifierTree son(int index) {
return (ClassifierTree)m_sons[index];
}
/**
* TWang.
* @return Returns the m_NodeContent.
*/
public ClassifierSplitModel getClassifierSplitModel() {
return (ClassifierSplitModel)m_localModel;
}
/**
* TWang.
* @return Returns the m_NodeContent.
*/
public String getM_NodeContent() {
return m_NodeContent;
}
public double[] get_NodeClassDist() {
return m_NodeClassDist;
}
public String getClassLabel(int index) throws Exception
{
return localModel().getClassLabel(index,m_train);
}
/**
* TWang.
* @param nodeContent The m_NodeContent to set.
*/
public void setM_NodeContent(String a_nodeContent, double[] a_classDist) {
m_NodeContent = null;
m_NodeContent = a_nodeContent;
m_NodeClassDist = null;
m_NodeClassDist = a_classDist;
}
/**
* TWang.
* @param nodeContent The m_NodeContent to set.
*/
/**
* ONLY call this method after calling the classifyInstance(instance_A)
* method. It will return the probablity of classifiing instance_A into
* the returned class.
*
* TWang.
* @param nodeContent The m_NodeContent to set.
*/
public double getProbablilty() {
return m_ClassPro;
}
public ClassifierSplitModel getM_localModel() {
return m_localModel;
}
public void setM_localModel(ClassifierSplitModel model) {
m_localModel = model;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -