使用weka的select attribute

原文:http://blog.csdn.net/anqiang1984/article/details/4048177


package com.csdn;


import java.io.File;

import weka.attributeSelection.InfoGainAttributeEval;

import weka.attributeSelection.Ranker;

import weka.classifiers.Classifier;

import weka.core.Instances;

import weka.core.converters.ArffLoader;

 

public class SimpleAttributeSelection {

    public static void main(String[] args) {

       Instances trainIns = null;

      

       try{

           /*

            * 1.读入训练

            * 在此我们将训练样本和测试样本是由weka提供的segment数据集构成的

            */

           File file= new File("C://Program Files//Weka-3-6//data//segment-challenge.arff");

           ArffLoader loader = new ArffLoader();

           loader.setFile(file);

           trainIns = loader.getDataSet();

          

           //在使用样本之前一定要首先设置instancesclassIndex,否则在使用instances对象是会抛出异常

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

          

           /*

            * 2.初始化搜索算法(search method)及属性评测算法(attribute evaluator

            */

           Ranker rank = new Ranker();

           InfoGainAttributeEval eval = new InfoGainAttributeEval();

          

           /*

            * 3.根据评测算法评测各个属性

            */

           eval.buildEvaluator(trainIns);

           //System.out.println(rank.search(eval, trainIns));

          

           /*

            * 4.按照特定搜索算法对属性进行筛选

            * 在这里使用的Ranker算法仅仅是属性按照InfoGain的大小进行排序

            */

           int[] attrIndex = rank.search(eval, trainIns);

          

           /*

            * 5.打印结果信息

            * 在这里我们了属性的排序结果同时将每个属性的InfoGain信息打印出来

            */

           StringBuffer attrIndexInfo = new StringBuffer();

           StringBuffer attrInfoGainInfo = new StringBuffer();

           attrIndexInfo.append("Selected attributes:");

           attrInfoGainInfo.append("Ranked attributes:/n");

           for(int i = 0; i < attrIndex.length; i ++){

              attrIndexInfo.append(attrIndex[i]);

              attrIndexInfo.append(",");

             

              attrInfoGainInfo.append(eval.evaluateAttribute(attrIndex[i]));

              attrInfoGainInfo.append("/t");

              attrInfoGainInfo.append((trainIns.attribute(attrIndex[i]).name()));

              attrInfoGainInfo.append("/n");

           }

           System.out.println(attrIndexInfo.toString());

           System.out.println(attrInfoGainInfo.toString());

          

       }catch(Exception e){

           e.printStackTrace();

       }

    }

 

}

在这个实例中,我用了InfoGain的属性选择类来进行特征选择。InfoGainAttributeEval主要是计算出各个属性的InfoGain信息。同时在weka中为属性选择方法配备的有搜索算法(seacher method),在这里我们用最简单的Ranker类。它对属性进行了简单的排序。在Weka中我们还可以对搜索算法设置一些其它的属性,例如设置搜索的属性集,阈值等等,如果有需求大家可以进行详细的设置。

在最后我们打印了一些结果信息,打印了各个属性的InfoGain的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值