一、說明
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 + "\t" 45 + data.attribute(attrIndex[i]).name() + "\t" + "1"); 46 } 47 FileTool.SaveListToFile(attributeWords, outputPath, false, 48 Charset.forName("utf8")); 49 } 50 51 }

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 }
三、小結
其實weka中還提供了一些其它的內嵌特征選擇方法,用起來也比較省事兒,但是這里不在贅述。