Weka学习四(属性选择)
在這一節我們看看屬性選擇。在數據挖掘的研究中,通常要通過距離來計算樣本之間的距離,而樣本距離是通過屬性值來計算的。我們知道對于不同的屬性,它們在樣本空間的權重是不一樣的,即它們與類別的關聯度是不同的,因此有必要篩選一些屬性或者對各個屬性賦一定的權重。這樣屬性選擇的方法就應運而生了。
在屬性選擇方面InfoGain和GainRatio的比較常見,也是最通俗易懂的方法。它們與Decision Tree的構造原理比較相似,哪個節點擁有的信息量就為哪個節點賦較高的權重。其它的還有根據關聯度的辦法來進行屬性選擇(Correlation-based Feature Subset Selection for Machine Learning)。具體它的工作原理大家可以在網上看論文。
現在我將簡單的屬性選擇實例給大家展示一下:
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;
?
/*
?* Date: 2009.4.4
?* by: Wang Yi
?* Email: wangyi19840906@yahoo.com.cn
?* QQ: 270135367
?*
?*/
public class SimpleAttributeSelection {
?
??? /**
??? ?* @param args
??? ?*/
??? public static void main(String[] args) {
?????? // TODO Auto-generated method stub
?????? 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();
??????????
?????????? //在使用樣本之前一定要首先設置instances的classIndex,否則在使用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的信息。
總結
以上是生活随笔為你收集整理的Weka学习四(属性选择)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Weka学习三(ensemble算法)
- 下一篇: Weka学习五(ROC简介)