Weka文件读取及导出

1,weka文件读取分3类,分别为:ARFF(weka自身文件读取),CSV(文本文件读取),数据库直接读取


1.1,ARFF(weka自身文件读取),比较容易直接使用Instances类即可


1.2,CSV文件读取需要使用dataSource类


[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.FileReader;  
  3.   
  4. import weka.core.Instances;  
  5. import weka.core.converters.ConverterUtils.DataSource;  
  6.   
  7. public class readfile {  
  8.   
  9.     public static void main(String[] args) throws Exception {  
  10.           
  11.         /* 
  12.          *  arff文件读入 
  13.          */  
  14.           
  15.         Instances date=new Instances(new BufferedReader(new FileReader("data\\ionosphere.arff")));  
  16.   
  17.         date.setClassIndex(date.numAttributes()-1);  
  18.         System.out.println(date);  
  19.           
  20.         /* 
  21.          *  CSV文件读入 
  22.          */  
  23.       
  24.         DataSource source =new DataSource("data\\testcsv.csv");  
  25.         Instances data=source.getDataSet();  
  26.           
  27.         if(data.classIndex()==-1)  
  28.             data.setClassIndex(data.numAttributes()-1);  
  29.           
  30.         System.out.println(data);  
  31.           
  32.           
  33.     }  
  34.       
  35. }  


2,weka中文件导出,导出格式应该只有ARFF一种,即weka自身文件格式


[java]  view plain  copy
  1. import weka.core.Instances;  
  2. import weka.core.converters.ArffSaver;  
  3. import weka.core.converters.CSVLoader;  
  4.   
  5. import java.io.File;  
  6.   
  7. public class CSV2ARFF {  
  8.       
  9.     /** 
  10.      * takes 2 arguments: 
  11.      * - CSV input file 
  12.      * - ARFF output file 
  13.      */  
  14.       
  15.     public static void main(String[] args) throws Exception {  
  16.           
  17.         // load CSV  
  18.         CSVLoader loader = new CSVLoader();  
  19.         loader.setSource(new File("data\\testcsv.csv"));  
  20.         Instances data = loader.getDataSet();  
  21.         data.setClassIndex(2);  
  22.        
  23.         // save ARFF  
  24.         ArffSaver saver = new ArffSaver();  
  25.         saver.setInstances(data);  
  26.         saver.setFile(new File("data\\testcsv.arff"));  
  27.         //saver.setDestination(new File(args[1]));  
  28.         saver.writeBatch();  
  29.           
  30.     }  
  31.       
  32. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值