2011-11-13 68 views
9

我想在我的java代碼中使用weka API。我用J48樹分級分類我在MySQL數據庫中的數據集,但我有此錯誤:錯誤在java代碼中使用WEKA API時:Class Attribute Not Set?

Trying to add database driver (JDBC): RmiJdbc.RJDriver - Error, not in CLASSPATH? 
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH? 
Trying to add database driver (JDBC): com.mckoi.JDBCDriver - Error, not in CLASSPATH? 
Trying to add database driver (JDBC): org.hsqldb.jdbcDriver - Error, not in CLASSPATH? 
weka.core.UnassignedClassException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Class attribute not set! 
     at weka.core.Capabilities.test(Capabilities.java:1086) 
     at weka.core.Capabilities.test(Capabilities.java:1018) 
     at weka.core.Capabilities.testWithFail(Capabilities.java:1297) 
..... 

這是我的代碼:

try{ 
     DatabaseLoader loader = new DatabaseLoader(); 
     loader.setSource("jdbc:mysql://localhost:3306/cuaca","root","491754"); 
     loader.setQuery("select * from data_training"); 
     Instances data = loader.getDataSet(); 

     jTextArea1.append(data.toString()); 

     String[] options = new String[1]; 
     options[0] = "U"; // unpruned tree 
     J48 tree = new J48(); // new instance of tree 
     tree.setOptions(options); // set the option 
     tree.buildClassifier(data); // build classifier 
     //Evaluation eval=new Evaluation(data); 
     //eval.crossValidateModel(tree, data, 10, new Random(1)); 
     //jTextArea1.append(eval.toSummaryString("\n HASILNYA \n", false)); 

}catch(IOException ioe){ioe.printStackTrace();} 
     catch(Exception e){e.printStackTrace();} 

數據集竟出現在文本區域:

@relation data_training 

@attribute Tmean numeric 
@attribute Tmax numeric 
@attribute Td numeric 
@attribute RH numeric 
@attribute SLP numeric 
@attribute STP numeric 
@attribute Wind_ave numeric 
@attribute Wind_max numeric 
@attribute Hujan {Y,T} 

@data 
25.9,31.6,23.1,93.4,1008.5,998.2,2.6,12.9,Y 
27.6,31.4,22.3,87,1009.6,999.3,0.8,3.1,T 
27.4,32.6,21.9,86.1,1009.4,999.2,4.5,14.3,T 
27.6,32.2,22.4,87.3,1009.1,998.9,2.4,8.2,T 

我想知道爲什麼會出現錯誤(Class Attribute Not Set !!)

+1

我也用Metaclassifier。但我有類似的錯誤:類索引(未設置)! –

回答

32

您應該設置對於您的數據集的類索引在傳遞到分類器之前。您的分類器必須知道哪個是您的結果變量。
這些行後...

loader.setQuery("select * from data_training"); 
Instances data = loader.getDataSet(); 

添加以下內容:

data.setClassIndex(data.numAttributes() - 1); 

如果Hujan是你的類屬性(結果變量)

Api Docs for more Info

+0

@Angga Raditya我建議你接受這個答案。 –

+0

@iinception你能告訴我什麼是'classIndex'的用法嗎? –

+0

如果需要其他屬性作爲類屬性,是否有辦法將此屬性移動/交換到最終位置(而不是僅使用其索引)? – brunodd

0

嘗試

data.setClassIndex(1); 
  1. 你類指數