鍏蜂綋鐨勫疄鐜版柟娉曡涓嬮潰鐨勬祴璇曚唬鐮侊紙鍙仛紺鴻寖鐢級錛?br />
1
/** *//**
2
*
3
*/
4
package edu.tju.ikse.mi.util;
5
6
import java.io.File;
7
import java.io.IOException;
8
import java.util.Random;
9
10
import weka.attributeSelection.ASEvaluation;
11
import weka.attributeSelection.ASSearch;
12
import weka.attributeSelection.AttributeSelection;
13
import weka.attributeSelection.BestFirst;
14
import weka.attributeSelection.CfsSubsetEval;
15
import weka.core.Instances;
16
import weka.core.converters.ArffLoader;
17
18
/** *//**
19
* @author Jia Yu
20
* @date 2010-11-23
21
*/
22
public class WekaSelector
{
23
24
private ArffLoader loader;
25
private Instances dataSet;
26
private File arffFile;
27
private int sizeOfDataset;
28
private int numOfOldAttributes;
29
private int numOfNewAttributes;
30
private int classIndex;
31
private int[] selectedAttributes;
32
33
public WekaSelector(File file) throws IOException
{
34
loader = new ArffLoader();
35
arffFile = file;
36
loader.setFile(arffFile);
37
dataSet = loader.getDataSet();
38
sizeOfDataset = dataSet.numInstances();
39
numOfOldAttributes = dataSet.numAttributes();
40
classIndex = numOfOldAttributes - 1;
41
dataSet.setClassIndex(classIndex);
42
}
43
44
public void select() throws Exception
{
45
ASEvaluation evaluator = new CfsSubsetEval();
46
ASSearch search = new BestFirst();
47
AttributeSelection eval = null;
48
49
eval = new AttributeSelection();
50
eval.setEvaluator(evaluator);
51
eval.setSearch(search);
52
53
eval.SelectAttributes(dataSet);
54
numOfNewAttributes = eval.numberAttributesSelected();
55
selectedAttributes = eval.selectedAttributes();
56
System.out.println("result is "+eval.toResultsString());
57
/**//*
58
Random random = new Random(seed);
59
dataSet.randomize(random);
60
if (dataSet.attribute(classIndex).isNominal()) {
61
dataSet.stratify(numFolds);
62
}
63
for (int fold = 0; fold < numFolds; fold++) {
64
Instances train = dataSet.trainCV(numFolds, fold, random);
65
eval.selectAttributesCVSplit(train);
66
}
67
System.out.println("result is "+eval.CVResultsString());
68
*/
69
System.out.println("old number of Attributes is "+numOfOldAttributes);
70
System.out.println("new number of Attributes is "+numOfNewAttributes);
71
for(int i=0;i<selectedAttributes.length;i++)
{
72
System.out.println(selectedAttributes[i]);
73
}
74
}
75
76
/** *//**
77
* @param args
78
*/
79
public static void main(String[] args)
{
80
// TODO Auto-generated method stub
81
File file = new File("iris.arff");
82
try
{
83
WekaSelector ws = new WekaSelector(file);
84
ws.select();
85
86
} catch (IOException e)
{
87
// TODO Auto-generated catch block
88
e.printStackTrace();
89
} catch (Exception e)
{
90
// TODO Auto-generated catch block
91
e.printStackTrace();
92
}
93
94
}
95
96
}
97
鍏朵腑鐨勬敞閲婇儴鍒嗘槸浣跨敤浜ゅ弶楠岃瘉鐨勯儴鍒嗐傞粯璁ゆ槸鍗佹姌浜ゅ弶楠岃瘉錛屽綋鐒惰繖涓彲浠ラ氳繃set鏂規硶璁劇疆銆傚叿浣撶殑浣跨敤鎴栬呯敤鍒皉educe dimensionality鐨勬柟娉曞ぇ瀹跺彲浠ュ弬鐪嬫簮浠g爜銆傛瘯绔焪eka寮婧愬緢鏄柟渚褲傛簮浠g爜娑夊強鍒扮殑綾諱富瑕佹槸鏌ョ湅weka.attributeSelection.AttributeSelection綾誨氨鍙互浜嗐傚綋鐒跺浣曡皟鐢ㄥ拰閫夋嫨鍙互鐪嬬湅weka.gui.explorer.AttributeSelectionPanel綾匯?br />
涓婇潰浠g爜鐨勫疄楠岀粨鏋滃涓嬶細
result is
=== Attribute Selection on all input data ===
Search Method:
Best first.
Start set: no attributes
Search direction: forward
Stale search after 5 node expansions
Total number of subsets evaluated: 12
Merit of best subset found: 0.887
Attribute Subset Evaluator (supervised, Class (nominal): 5 class):
CFS Subset Evaluator
Including locally predictive attributes
Selected attributes: 3,4 : 2
petallength
petalwidth
old number of Attributes is 5
new number of Attributes is 2
2
3
4
鍘熸潵鐨刬ris鏁版嵁闆嗕腑鍏辨湁4涓睘鎬э紙鍖呭惈涓涓垎綾葷被鏍囨墍浠ヤ竴鍏?緇達級錛岀粡榪囩壒寰侀夋嫨鍚庯紝鍙湁絎?鍜岀4涓や釜緇村害鐨勭壒寰佷繚鐣欙紝鎵浠ユ柊鐗瑰緛瀛愰泦鏈変袱涓淮搴︼紙涓嶅寘鍚被鏍囷紝鏈夌偣緇曪紝涓嶅ソ鎰忔濓紝鎴戞繪槸榪欐牱錛夈?br />
鏈鍚庣殑2錛?錛?鏄睘鎬ф暟緇勭殑涓嬫爣錛岃〃紺虹粡榪囩壒寰侀夋嫨淇濈暀鐨勫睘鎬у瓙闆嗘槸絎?錛?錛?涓睘鎬с?br />

]]>