2017-10-07 58 views
0
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中,有沒有方法?)如何訪問標有j48的數據?

回答

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(); 
+0

現在比較源文件這個文件並提取給我們提供j48預測它們的類的差異。 –

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();