资源简介
本算法基于weka实现的误分类代价和测试代价基于id3算法的实现。
代码片段和文件信息
package weka.classifiers.trees.testcost;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Vector;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.core.*;
public class id3_entropy_cost extends Classifier implements AdditionalMeasureProducerSerializable{
/**
*
*/
private static final long serialVersionUID = 1L;
class Node implements Serializable{
private Instances m_instances;
private Attribute m_attribute;
private Node [] successor;
Node(Instances insAttribute att){
m_instances=ins;
m_attribute=att;
successor=null;
}
Node(Instances ins){
m_instances=ins;
m_attribute=null;
successor=null;
}
}
private Node m_root;
// private static int m_numClasses; //使用静态变量,保证类个数不为空。
//
// private id3node [] successor;
//
// private Instances m_instances;
//
// private Attribute m_attribute;
double totalcost=0;
public Enumeration enumerateMeasures() {
Vector newVector = new Vector(1);
newVector.addElement(“measureAttributeCost“);
return newVector.elements();
}
public double getMeasure(String additionalMeasureName) {
return measureAttributeCost();
}
public double measureAttributeCost(){
return totalcost;
}
public void buildClassifier (Instances instances) throws Exception {
m_root=buildTree(instances);
}
//递归的构建一棵树
public Node buildTree(Instances instances)throws Exception{
if(instances.numInstances()<=30){
// m_attribute=null;
// m_instances=new Instances(instances);
return new Node(instances);
}
int max=-1;
double temp=0;
double maxValue=0;
for(int i=0;i if(i == instances.classIndex()) continue;
temp=computeEntropyReduce(instancesinstances.attribute(i));
if(temp>maxValue){
maxValue=temp;
max=i;
}
}
if(Utils.eq(maxValue 0)){
// m_attribute=null;
// m_instances=new Instances(instances);
return new Node(instances);
}
else{
Attribute att=instances.attribute(max);
Node node=new Node(instancesatt);
totalcost+=(att.cost());
int m_numAttValues=att.numValues();
Instances SplitInstances[]=new Instances[m_numAttValues];
SplitInstances=splitInstances(instancesatt);
node.successor=new Node[m_numAttValues];
for(int i=0;i node.successor[i]=buildTree(SplitInstances[i]);
}
return node;
}
}
double computeEntropyReduce(Instances instancesAttribute attribute)throws Exception{
double EntropyReduce=computeEntropy(instances);
Instances [] SplitInstances=splitInstances(instancesattribute);
for(int i=0;i
- 上一篇:JAVA用PageOffice动态导出Word文档
- 下一篇:RSA JAVA代码
评论
共有 条评论