weka开发学习

 

读取文件和读取数据库数据

参考链接1和2

你可能要用的最常用的组件(components)是:

l  Instances 你的数据

l  Filter 对数据的预处理

l  Classifiers/Clusterer 被建立在预处理的数据上,分类/聚类

l  Evaluating 评价classifier/clusterer

l  Attribute selection 去除数据中不相关的属性

测试的可用的代码如下:

public static Instances getInstances(String filePath) {
        try {
            filePath = "C:\\Weka-3-8\\data\\iris.arff";
                    
            //3.5.5和3.4.X版本
            Instances data = new Instances( new BufferedReader( new FileReader(filePath) ) );
            // setting class attribute
            // Class Index是指示用于分类的目标属性的下标。在ARFF文件中,它被默认为是最后一个属性,这也就是为什么它被设置成numAttributes-1.
            //你必需在使用一个Weka函数(ex: weka.classifiers.Classifier.buildClassifier(data))之前设置Class Index。
            data.setClassIndex(data.numAttributes() - 1);
            System.out.println( "#################data:" );
            System.out.println( data );

            //3.5.5和更新的版本
            //DataSource类不仅限于读取ARFF文件,它同样可以读取CSV文件和其它格式的文件(基本上Weka可以通过它的转换器(converters)导入所有的文件格式)。
            DataSource source = new DataSource(filePath);
            Instances data2 = source.getDataSet();
            // setting class attribute if the data format does not provide this information
            // E.g., the XRFF format saves the class attribute information as well
            //if (data2.classIndex() == -1)
            data2.setClassIndex(data2.numAttributes() - 1);
            System.out.println(  "#################data2:"  );
            System.out.println( data2 );

            //读取数据库
            InstanceQuery query = new InstanceQuery();
            //数据库配置在weka.jar文件中
            query.setUsername("root");
            query.setPassword("");
            query.setQuery("select * from url_features limit 0,10");//url_features
            // if your data is sparse, then you can say so too
            // query.setSparseData(true);
            Instances data3 = query.retrieveInstances();
            //把数据集全部输入出
            System.out.println( data3 );
            //用numInstances可以获得数据集中有多少样本
            for( int i = 0; i < data3.numInstances(); i++ )
            {
                //instance( i )是得到第i个样本
                System.out.println( data3.instance( i ) );
            }
            return data2;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } 

数据库配置说明参见参考链接3。

分类器

参考链接4

要对数据集进行分类,第一步要指定数据集中哪一列做为类别,如果这一步忘记了(事实上经常会忘记)会出现“Class index is negative (not set)!”这个错误,设置某一列为类别用Instances类的成员方法setClassIndex,要设置最后一列为类别则可以用Instances类的numAttributes()成员方法得到属性的个数再减1。

            Instances m_instances = getInstances(filePath);//这里使用了上面代码的data2的方法
            J48 classifier = new J48();

            //NaiveBayes classifier2 = new NaiveBayes();
            //SMO classifier = new SMO();
            classifier.buildClassifier( m_instances );
            //输出的内容是数据中第0、60、110行的数据的分类结果
            System.out.println( classifier.classifyInstance( m_instances.instance( 0 ) ) );
            System.out.println( classifier.classifyInstance( m_instances.instance( 60 ) ) );
            System.out.println( classifier.classifyInstance( m_instances.instance( 110 ) ) );

分类评价

参考链接5

    //首先初始化一个Evaluation对象,Evaluation类没有无参的构造函数,一般用Instances对象作为构造函数的参数。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值