import javax.print.attribute.DateTimeSyntax;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.trees.J48;
import weka.classifiers.functions.SMO;
public class Classification {
public static void main(String[] args) throws Exception{
//load dataset
DataSource source = new DataSource("j:/weka/wekadataset/iris.arff");
Instances dataset = source.getDataSet();
// set the class to the last class attribute
dataset.setClassIndex(dataset.numAttributes()-1);
//create and build classifier
J48 tree= new J48();
tree.buildClassifier(dataset);
}
}
//我在半监督学习中使用J48。在每次迭代之后,我必须访问标记的数据,以便我可以在下一次迭代中使用它。 我的问题在这里。如何访问每个步骤的标记数据?我应该更改J48源代码以便我可以看到它或者有办法执行此操作(在Java中为此目的,是否有方法?)
答案 0 :(得分:0)
//write the modified dataset by j48 into the new file and
// write in the output
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0; i<k ; i++){
Instance newInstance = instance01.instance(i);
//write attributes
for(int j=0;j<newInstance.numAttributes()-1;j++){
String AttribValue =Double.toString(newInstance.value(j));
System.out.print(newInstance.value(j)+"-");
bw.write(AttribValue+",");
}
//write the class
double actualClass = tree.classifyInstance(newInstance);
String actual =newInstance.classAttribute().value((int)actualClass);
System.out.println("class="+actual);
bw.write(actual);
bw.newLine();
}
bw.close();
答案 1 :(得分:0)
//now compare the source file and the modified dataset in the second
//file. write the differences in the third file and then rename this file
//to the second file
FileOutputStream fos1 = new FileOutputStream(new File
("j:/weka/wekadataset/new folder/iris-semi02.arff"));//third file
BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(fos1));
BufferedReader br1 = new BufferedReader(new FileReader(
"j:/weka/wekadataset/iris-semi.arff"));//
BufferedReader br2 = new BufferedReader(new FileReader(
"j:/weka/wekadataset/new folder/iris-semi01.arff"));
String line1=br1.readLine();
String line2=br2.readLine();
while(!line1.startsWith("@DATA")){line1=br1.readLine();}
line1=br1.readLine();
while(line1!=null && line2!=null ){
if(!line1.equals(line2)){
bw1.write(line2);
bw1.newLine();
}
line1=br1.readLine();
line2=br2.readLine();
}
bw1.close();