• weka数据挖掘拾遗(二)---- 特征选择(IG、chi-square)


    一、说明

      IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时)。这里不做详细介绍,有兴趣的可以googling一下。

      chi-square 是一个常用特征筛选方法,在种子词扩展那篇文章中,有详细说明,这里不再赘述。

    二、weka中的使用方法

      1、特征筛选代码

     1 package com.lvxinjian.alg.models.feature;
     2 
     3 import java.nio.charset.Charset;
     4 import java.util.ArrayList;
     5 
     6 import weka.attributeSelection.ASEvaluation;
     7 import weka.attributeSelection.AttributeEvaluator;
     8 import weka.attributeSelection.Ranker;
     9 import weka.core.Instances;
    10 
    11 import com.iminer.tool.common.util.FileTool;
    12 /**
    13  * @Description : 使用Weka的特征筛选方法(目前支持IG、Chi-square)
    14  * @author Lv Xinjian
    15  *
    16  */
    17 public class FeatureSelectorByWeka {
    18     
    19     /**
    20      * @function 使用weka内置的算法筛选特征
    21      * @param eval 特征筛选方法的对象实例
    22      * @param data arff格式的数据
    23      * @param maxNumberOfAttribute 支持的最大的特征个数
    24      * @param outputPath lex输出文件
    25      * @throws Exception
    26      */
    27     public void EvalueAndRank(ASEvaluation eval , Instances data ,int maxNumberOfAttribute , String outputPath) throws Exception
    28     {
    29         Ranker rank = new Ranker();        
    30         eval.buildEvaluator(data);
    31         rank.search(eval, data);
    32         
    33          // 按照特定搜索算法对属性进行筛选 在这里使用的Ranker算法仅仅是属性按照InfoGain/Chi-square的大小进行排序            
    34         int[] attrIndex = rank.search(eval, data);
    35         
    36          // 打印结果信息 在这里我们了属性的排序结果                 
    37         ArrayList<String> attributeWords = new ArrayList<String>();
    38         for (int i = 0; i < attrIndex.length; i++) {
    39             //如果权重等于0,则跳出循环
    40             if (((AttributeEvaluator) eval).evaluateAttribute(attrIndex[i]) == 0)
    41                 break;
    42             if (i >= maxNumberOfAttribute)
    43                 break;
    44             attributeWords.add(i + "	"
    45                     + data.attribute(attrIndex[i]).name() + "	" + "1");
    46         }
    47         FileTool.SaveListToFile(attributeWords, outputPath, false,
    48                 Charset.forName("utf8"));
    49     }
    50 
    51 }
    View Code
      1 package com.lvxinjian.alg.models.feature;
      2 
      3 import java.io.IOException;
      4 
      5 import weka.attributeSelection.ASEvaluation;
      6 import weka.attributeSelection.ChiSquaredAttributeEval;
      7 import weka.attributeSelection.InfoGainAttributeEval;
      8 import weka.core.Instances;
      9 import weka.core.converters.ConverterUtils.DataSource;
     10 
     11 import com.iminer.alg.models.generatefile.ParameterUtils;
     12 
     13 /**
     14  * @Description : IG、Chi-square特征筛选
     15  * @author Lv Xinjian
     16  *
     17  */
     18 public class WekaFeatureSelector extends FeatureSelector{        
     19 
     20     /**
     21      * 最大的特征个数
     22      */
     23     private int maxFeatureNum = 10000;
     24     /**
     25      * 特征文件保存路径
     26      */
     27     private String outputPath = null;
     28     /**
     29      * @Fields rule 对于特征过滤的规则
     30      */
     31     private String classname = "CLASS";
     32     /**
     33      * 特征筛选方法,默认为IG
     34      */
     35     private String selectMethod = "IG";
     36     
     37     private boolean Initialization(String options){        
     38         try {
     39             String [] paramArrayOfString = options.split(" ");
     40             
     41             //初始化特征最大个数 
     42             String maxFeatureNum = ParameterUtils.getOption("maxFeatureNum", paramArrayOfString);
     43             if(maxFeatureNum.length() != 0)
     44                 this.maxFeatureNum = Integer.parseInt(maxFeatureNum);
     45             //初始化类别
     46             String classname = ParameterUtils.getOption("class", paramArrayOfString);
     47             if(classname.length() != 0)
     48                 this.classname = classname;
     49             else{
     50                 System.out.println("use default class name("CLASS")");
     51             }
     52             //初始化特征保存路径
     53             String outputPath = ParameterUtils.getOption("outputPath", paramArrayOfString);
     54             if(outputPath.length() != 0)
     55                 this.outputPath = outputPath;
     56             else{
     57                 System.out.println("please initialze output path.");
     58                 return false;
     59             }
     60             String selectMethod = ParameterUtils.getOption("selectMethod", paramArrayOfString);
     61             if(selectMethod.length() != 0)
     62                 this.selectMethod = selectMethod;
     63             else{
     64                 System.out.println("use default select method(IG)");
     65             }
     66         } catch (Exception e) {
     67             e.printStackTrace();
     68             return false;
     69         }            
     70         return true;
     71     }
     72     @Override
     73     public boolean selectFeature(Object obj ,String options) throws IOException {        
     74         try {
     75             if(!Initialization(options))
     76                 return false;        
     77             Instances data = (Instances)obj;
     78             data.setClass(data.attribute(this.classname));
     79             ASEvaluation selector = null;
     80             if(this.selectMethod.equals("IG"))
     81                 selector = new InfoGainAttributeEval();
     82             else if(this.selectMethod.equals("CHI"))
     83                 selector = new ChiSquaredAttributeEval();
     84             FeatureSelectorByWeka attributeSelector = new FeatureSelectorByWeka();    
     85             attributeSelector.EvalueAndRank(selector, data ,this.maxFeatureNum ,this.outputPath);
     86         } catch (Exception e) {
     87             // TODO Auto-generated catch block
     88             e.printStackTrace();
     89         }
     90         
     91         return true;            
     92     }
     93     
     94     public static void main(String [] args) throws Exception
     95     {
     96         String root = "C:\Users\Administrator\Desktop\12_05\模型训练\1219\";
     97         WekaFeatureSelector selector = new WekaFeatureSelector();
     98         Instances data = DataSource.read(root + "train.Bigram.arff");
     99         String options = "-maxFeatureNum 10000 -outputPath lex.txt";
    100         selector.selectFeature(data, options);
    101     }
    102 }
    View Code

    三、小结

      其实weka中还提供了一些其它的内嵌特征选择方法,用起来也比较省事儿,但是这里不在赘述。

  • 相关阅读:
    易用性问题回复
    阅读心得2:《余额宝技术架构及演进 》
    假期周进度报告8
    假期周进步报告7
    假期周进度报告6
    假期周进度报告5
    假期周进度报告4
    假期周进度报告3
    JAVA中SSH框架
    一张图说明CDN网络的原理
  • 原文地址:https://www.cnblogs.com/nocml/p/3545611.html
Copyright © 2020-2023  润新知