一、说明

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

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

二、weka中的使用方法

  1、特征筛选代码

package com.lvxinjian.alg.models.feature;

import java.nio.charset.Charset;
import java.util.ArrayList; import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.AttributeEvaluator;
import weka.attributeSelection.Ranker;
import weka.core.Instances; import com.iminer.tool.common.util.FileTool;
/**
* @Description : 使用Weka的特征筛选方法(目前支持IG、Chi-square)
*
*/
public class FeatureSelectorByWeka { /**
* @function 使用weka内置的算法筛选特征
* @param eval 特征筛选方法的对象实例
* @param data arff格式的数据
* @param maxNumberOfAttribute 支持的最大的特征个数
* @param outputPath lex输出文件
* @throws Exception
*/
public void EvalueAndRank(ASEvaluation eval , Instances data ,int maxNumberOfAttribute , String outputPath) throws Exception
{
Ranker rank = new Ranker();
eval.buildEvaluator(data);
rank.search(eval, data); // 按照特定搜索算法对属性进行筛选 在这里使用的Ranker算法仅仅是属性按照InfoGain/Chi-square的大小进行排序
int[] attrIndex = rank.search(eval, data); // 打印结果信息 在这里我们了属性的排序结果
ArrayList<String> attributeWords = new ArrayList<String>();
for (int i = 0; i < attrIndex.length; i++) {
//如果权重等于0,则跳出循环
if (((AttributeEvaluator) eval).evaluateAttribute(attrIndex[i]) == 0)
break;
if (i >= maxNumberOfAttribute)
break;
attributeWords.add(i + "\t"
+ data.attribute(attrIndex[i]).name() + "\t" + "1");
}
FileTool.SaveListToFile(attributeWords, outputPath, false,
Charset.forName("utf8"));
} }
package com.lvxinjian.alg.models.feature;

import java.io.IOException;

import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.ChiSquaredAttributeEval;
import weka.attributeSelection.InfoGainAttributeEval;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource; import com.iminer.alg.models.generatefile.ParameterUtils; /**
* @Description : IG、Chi-square特征筛选
*
*/
public class WekaFeatureSelector extends FeatureSelector{ /**
* 最大的特征个数
*/
private int maxFeatureNum = 10000;
/**
* 特征文件保存路径
*/
private String outputPath = null;
/**
* @Fields rule 对于特征过滤的规则
*/
private String classname = "CLASS";
/**
* 特征筛选方法,默认为IG
*/
private String selectMethod = "IG"; private boolean Initialization(String options){
try {
String [] paramArrayOfString = options.split(" "); //初始化特征最大个数
String maxFeatureNum = ParameterUtils.getOption("maxFeatureNum", paramArrayOfString);
if(maxFeatureNum.length() != 0)
this.maxFeatureNum = Integer.parseInt(maxFeatureNum);
//初始化类别
String classname = ParameterUtils.getOption("class", paramArrayOfString);
if(classname.length() != 0)
this.classname = classname;
else{
System.out.println("use default class name(\"CLASS\")");
}
//初始化特征保存路径
String outputPath = ParameterUtils.getOption("outputPath", paramArrayOfString);
if(outputPath.length() != 0)
this.outputPath = outputPath;
else{
System.out.println("please initialze output path.");
return false;
}
String selectMethod = ParameterUtils.getOption("selectMethod", paramArrayOfString);
if(selectMethod.length() != 0)
this.selectMethod = selectMethod;
else{
System.out.println("use default select method(IG)");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public boolean selectFeature(Object obj ,String options) throws IOException {
try {
if(!Initialization(options))
return false;
Instances data = (Instances)obj;
data.setClass(data.attribute(this.classname));
ASEvaluation selector = null;
if(this.selectMethod.equals("IG"))
selector = new InfoGainAttributeEval();
else if(this.selectMethod.equals("CHI"))
selector = new ChiSquaredAttributeEval();
FeatureSelectorByWeka attributeSelector = new FeatureSelectorByWeka();
attributeSelector.EvalueAndRank(selector, data ,this.maxFeatureNum ,this.outputPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return true;
} public static void main(String [] args) throws Exception
{
String root = "C:\\Users\\Administrator\\Desktop\\12_05\\模型训练\\1219\\";
WekaFeatureSelector selector = new WekaFeatureSelector();
Instances data = DataSource.read(root + "train.Bigram.arff");
String options = "-maxFeatureNum 10000 -outputPath lex.txt";
selector.selectFeature(data, options);
}
}

参考:

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

Weka学习四(属性选择)

weka特征选择(IG、chi-square)的更多相关文章

  1. Chi Square Distance

    The chi squared distance d(x,y) is, as you already know, a distance between two histograms x=[x_1,.. ...

  2. 特征选择之Chi卡方检验

    特征选择之Chi卡方检验 卡方值越大,说明对原假设的偏离越大,选择的过程也变成了为每个词计算它与类别Ci的卡方值,从大到小排个序(此时开方值越大越相关),取前k个就可以. 针对英文纯文本的实验结果表明 ...

  3. 【Machine Learning】wekaの特征选择简介

    看过这篇博客的都应该明白,特征选择代码实现应该包括3个部分: 搜索算法: 评估函数: 数据: 因此,代码的一般形式为: AttributeSelection attsel = new Attribut ...

  4. BendFord's law's Chi square test

    http://www.siam.org/students/siuro/vol1issue1/S01009.pdf bendford'law e=log10(1+l/n) o=freq of first ...

  5. 文本挖掘之特征选择(python 实现)

    机器学习算法的空间.时间复杂度依赖于输入数据的规模,维度规约(Dimensionality reduction)则是一种被用于降低输入数据维数的方法.维度规约可以分为两类: 特征选择(feature ...

  6. 使用Python的文本挖掘的特征选择/提取

    在文本挖掘与文本分类的有关问题中,文本最初始的数据是将文档表示成向量空间模型的一个矩阵,而这个矩阵所拥有的就是不同的词,常采用特征选择方法.原因是文本的特征一般都是单词(term),具有语义信息,使用 ...

  7. scikit-learn:在实际项目中用到过的知识点(总结)

    零.全部项目通用的: http://blog.csdn.net/mmc2015/article/details/46851245(数据集格式和预測器) http://blog.csdn.net/mmc ...

  8. NLP-特征选择

    文本分类之特征选择 1 研究背景 对于高纬度的分类问题,我们在分类之前一般会进行特征降维,特征降维的技术一般会有特征提取和特征选择.而对于文本分类问题,我们一般使用特征选择方法. 特征提取:PCA.线 ...

  9. 用R进行市场调查和消费者感知分析

    // // 问题到数据 理解问题 理解客户的问题:谁是客户(某航空公司)?交流,交流,交流! 问题要具体 某航空公司: 乘客体验如何?哪方面需要提高? 类别:比较.描述.聚类,判别还是回归 需要什么样 ...

随机推荐

  1. Yii源码阅读笔记(五)

    Object 是一个基础类,实现了属性的功能,其基本内容如下: namespace yii\base; use Yii; /** * Object is the base class that imp ...

  2. vi 整行 多行 复制与粘贴

    vi编辑器中的整行(多行)复制与粘贴就非常必要了. 1.复制 1)单行复制 在命令模式下,将光标移动到将要复制的行处,按“yy”进行复制: 2)多行复制 在命令模式下,将光标移动到将要复制的首行处,按 ...

  3. page fault rate

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION A program computes th ...

  4. CC254x(cc2540/cc2541)的微信AirSync调试笔记

    一.前言 本尊自诩为IOT小能手,一直没涉足蓝牙实在说不过去.刚好上个月底的时候计划做个BLE设备,这阵子利用业余时间自学了BLE协议栈,了解了GATT,磕磕绊绊完成CC254x(cc2540/cc2 ...

  5. sqlserver 计算 百分比

    ,),))+'%' As 百分比 NUMERIC(P,S) P的默认值是:38 S的默认值是:-84~127 numeric(a,b)函数有两个参数,前面一个为总的位数,后面一个参数是小数点后的位数, ...

  6. 【Android测试】【第六节】Monkey——认识和使用

    ◆版权声明:本文出自carter_dream的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4688880.html 自动化工具 接触安 ...

  7. SQL backup&restore

    --完整备份Backup Database NorthwindCSTo disk='G:\Backup\NorthwindCS_Full_20070908.bak' --差异备份Backup Data ...

  8. self.nsme 和 _name 的问题

    .h文件 @interface myclass:NSObject @property(nonatomic,retain)NSArray*MyArray; @end .m文件 @implementati ...

  9. 转载:如何运用VI编辑器进行查找替换

    使用vi编辑器编辑长文件时,常常是头昏眼花,也找不到需要更改的内容. 这时,使用查找功能尤为重要. 方法如下: 1.命令模式下输入“/字符串”,例如“/Section 3”. 2.如果查找下一个,按“ ...

  10. http文件的断点续传和下载

    http://www.tuicool.com/articles/ZbyymqJ Content-Disposition:inline; filename= "c501b_01_h264_sd ...