下面列出了怎么用weka.core.converters.ArffLoader的API类实例代码及写法,或者点击链接到github查看源代码。
@param fileName
* @return
* @throws IOException
*/
public static Instances getDataSet(String fileName) throws IOException {
/**
* we can set the file i.e., loader.setFile("finename") to load the data
*/
int classIdx = 1;
/** the arffloader to load the arff file */
ArffLoader loader = new ArffLoader();
//loader.setFile(new File(fileName));
/** load the traing data */
loader.setSource(LogisticRegressionDemo.class.getResourceAsStream("/" + fileName));
/**
* we can also set the file like loader3.setFile(new
* File("test-confused.arff"));
*/
Instances dataSet = loader.getDataSet();
/** set the index based on the data given in the arff files */
dataSet.setClassIndex(classIdx);
return dataSet;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
ArffLoader loader = new ArffLoader();
loader.setSource(new File("/Users/admin/Documents/NetBeansProjects/Datasets/weather.arff"));
Instances data = loader.getDataSet();
CSVSaver saver = new CSVSaver();
saver.setInstances(data);
saver.setFile(new File("weather.csv"));
saver.writeBatch();
}
public void trainClassifier(String trainfile,String modelpath) throws Exception{
Classifier m_classifier = new RandomForest();
File inputFile = new File(trainfile);
ArffLoader atf = new ArffLoader();
atf.setFile(inputFile);
Instances instancesTrain = atf.getDataSet();
instancesTrain.setClassIndex(6);
m_classifier.buildClassifier(instancesTrain);
saveModel(m_classifier, modelpath);
}
/**
* Return the data set loaded from the Arff file at @param path
*/
public static Instances loadDataFromArffFile(String path) throws IOException{
ArffLoader loader = new ArffLoader();
loader.setSource(new File(path));
Instances data = loader.getDataSet();
System.out.println("\nHeader of dataset:\n");
System.out.println(new Instances(data, 0));
return data;
}