本文整理汇总了Java中weka.attributeSelection.InfoGainAttributeEval类的典型用法代码示例。如果您正苦于以下问题:Java InfoGainAttributeEval类的具体用法?Java InfoGainAttributeEval怎么用?Java InfoGainAttributeEval使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
InfoGainAttributeEval类属于weka.attributeSelection包,在下文中一共展示了InfoGainAttributeEval类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: preProcessData
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
public static Instances preProcessData(Instances data) throws Exception{
/*
* Remove useless attributes
*/
RemoveUseless removeUseless = new RemoveUseless();
removeUseless.setOptions(new String[] { "-M", "99" }); // threshold
removeUseless.setInputFormat(data);
data = Filter.useFilter(data, removeUseless);
/*
* Remove useless attributes
*/
ReplaceMissingValues fixMissing = new ReplaceMissingValues();
fixMissing.setInputFormat(data);
data = Filter.useFilter(data, fixMissing);
/*
* Remove useless attributes
*/
Discretize discretizeNumeric = new Discretize();
discretizeNumeric.setOptions(new String[] {
"-O",
"-M", "-1.0",
"-B", "4", // no of bins
"-R", "first-last"}); //range of attributes
fixMissing.setInputFormat(data);
data = Filter.useFilter(data, fixMissing);
/*
* Select only informative attributes
*/
InfoGainAttributeEval eval = new InfoGainAttributeEval();
Ranker search = new Ranker();
search.setOptions(new String[] { "-T", "0.001" }); // information gain threshold
AttributeSelection attSelect = new AttributeSelection();
attSelect.setEvaluator(eval);
attSelect.setSearch(search);
// apply attribute selection
attSelect.SelectAttributes(data);
// remove the attributes not selected in the last run
data = attSelect.reduceDimensionality(data);
return data;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:52,代码来源:KddCup.java
示例2: InfoGainAttributeEval
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
public static AttributeSelection InfoGainAttributeEval(Instances data) throws Exception{
AttributeSelection filter = new AttributeSelection();
InfoGainAttributeEval evaluator = new InfoGainAttributeEval();
filter.setEvaluator(evaluator);
Ranker search = new Ranker();
search.setThreshold(0);
filter.setSearch(search);
filter.setInputFormat(data);
return filter;
}
示例3: calculate
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
/**
* Calculate metric value
*
* @param mlData Multi-label dataset to which calculate the metric
* @return Value of the metric
*/
public double calculate(MultiLabelInstances mlData){
double res = 0.0;
try{
ASEvaluation ase = new InfoGainAttributeEval();
BinaryRelevanceAttributeEvaluator eval = new BinaryRelevanceAttributeEvaluator(ase, mlData, "avg", "none", "eval");
int [] featureIndices = mlData.getFeatureIndices();
for(int i : featureIndices){
res += eval.evaluateAttribute(i);
}
res = res / featureIndices.length;
}
catch(Exception e){
e.printStackTrace();
res = Double.NaN;
}
this.value = res;
return value;
}
示例4: configureCostSensitiveClassifier
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
/**
* Sets a default CostSensitiveClassifier with SMO. The settings of this method must be modified if used for another project.
* @param data weka data containing instances and attributes
* @param costSensitiveClassifier unconfigured Cost Sensitive Classifier
* @param costMatrix Cost Matrix
* @return CostSensitiveClassifier fully configured Cost Sensitive Classifier
* @throws Exception
*/
protected static CostSensitiveClassifier configureCostSensitiveClassifier(CostSensitiveClassifier costSensitiveClassifier,
Instances data, String costMatrix) throws Exception {
String[] CscOptions = {"-cost-matrix", costMatrix, "-S", "1"};
String[] rankerOptions = {"-P",Integer.toString(data.numAttributes() - 1),"-T","-1.7976931348623157E308","-N","-1"};
String[] smoOptions = {"-C", "1.0", "-L", "0.0010", "-P", "1.0E-12", "-N", "0", "-V", "-1", "-W", "1", "-K",
PolyKernel.class.getName()+" -C 250007 -E 2.0"};
InfoGainAttributeEval igAttEval = new InfoGainAttributeEval();
Ranker ranker = new Ranker();
ranker.setOptions(rankerOptions);
AttributeSelection attSelect = new AttributeSelection();
attSelect.setEvaluator(igAttEval);
attSelect.setSearch(ranker);
SMO smo = new SMO();
smo.setOptions(smoOptions);
FilteredClassifier filteredClsfr = new FilteredClassifier();
filteredClsfr.setClassifier(smo);
filteredClsfr.setFilter(attSelect);
costSensitiveClassifier.setOptions(CscOptions);
costSensitiveClassifier.setClassifier(filteredClsfr);
costSensitiveClassifier.buildClassifier(data);
System.out.println("CostSensitiveClassifier built.");
return costSensitiveClassifier;
}
示例5: InformationGain
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
public InformationGain(Instances dataset) throws Exception{
data = dataset;
infoG = new InfoGainAttributeEval();
infoG.buildEvaluator(data);
filter = new AttributeSelection();
search = new Ranker();
search.setThreshold(0.0);
}
示例6: getAttributeSelector
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
private static AttributeSelection getAttributeSelector(
Instances trainingData) throws Exception {
AttributeSelection selector = new AttributeSelection();
InfoGainAttributeEval evaluator = new InfoGainAttributeEval();
Ranker ranker = new Ranker();
ranker.setNumToSelect(Math.min(500, trainingData.numAttributes() - 1));
selector.setEvaluator(evaluator);
selector.setSearch(ranker);
selector.SelectAttributes(trainingData);
return selector;
}
示例7: getEvalResultbyInfoGain
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
/***
* <p>To get 10-fold cross validation in one single arff in <b>path</b></p>
* <p>Use C4.5 and <b>SMOTE</b>, combined with <b>Information Gain</b> to classify the dataset.</p>
* @param path dataset path
* @throws Exception
*/
public static void getEvalResultbyInfoGain(String path, int index) throws Exception{
Instances ins = DataSource.read(path);
int numAttr = ins.numAttributes();
ins.setClassIndex(numAttr - 1);
/**information gain filter to process the whole dataset first*/
InfoGainAttributeEval evall = new InfoGainAttributeEval();
Ranker ranker = new Ranker();
AttributeSelection selector = new AttributeSelection();
selector.setEvaluator(evall);
selector.setSearch(ranker);
selector.setInputFormat(ins);
ins = Filter.useFilter(ins, selector);
SMOTE smote = new SMOTE();
smote.setInputFormat(ins);
/** classifiers setting*/
J48 j48 = new J48();
// j48.setConfidenceFactor(0.4f);
j48.buildClassifier(ins);
FilteredClassifier fc = new FilteredClassifier();
fc.setClassifier(j48);
fc.setFilter(smote);
Evaluation eval = new Evaluation(ins);
eval.crossValidateModel(fc, ins, 10, new Random(1));
// System.out.printf(" %4.3f %4.3f %4.3f", eval.precision(0), eval.recall(0), eval.fMeasure(0));
// System.out.printf(" %4.3f %4.3f %4.3f", eval.precision(1), eval.recall(1), eval.fMeasure(1));
// System.out.printf(" %4.3f \n\n", (1-eval.errorRate()));
results[index][0] = eval.precision(0);
results[index][1] = eval.recall(0);
results[index][2] = eval.fMeasure(0);
results[index][3] = eval.precision(1);
results[index][4] = eval.recall(1);
results[index][5] = eval.fMeasure(1);
results[index][6] = 1-eval.errorRate();
}
示例8: classify
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
public void classify(String trainingFile,String testingFile) {
try {
initTrainingSet(trainingFile);
initTestingSet(testingFile);
// train NaiveBayes
J48 cModel = new J48();
cModel.buildClassifier(TrainingSet);
Instance current;
double pred=0;
for (int i = 0; i < TestingSet.numInstances(); i++) {
current=TestingSet.get(i);
if(featureVectorClassValues.get((int)pred).equalsIgnoreCase("Statement")||featureVectorClassValues.get((int)pred).equalsIgnoreCase("Backchannel Question")||featureVectorClassValues.get((int)pred).equalsIgnoreCase("Yes-No Question")||featureVectorClassValues.get((int)pred).equalsIgnoreCase("Open Question")){
current.setValue(featureVectorAttributes.get(0),featureVectorClassValues.get((int)pred));
System.out.println(pred+" : "+featureVectorClassValues.get((int)pred));
System.out.println(current.toString());
}
pred=cModel.classifyInstance(current);
}
// J48 cModel = new J48();
// cModel.setUnpruned(true);
// cModel.buildClassifier(TrainingSet);
Evaluation eTest = new Evaluation(TrainingSet);
eTest.evaluateModel(cModel, TestingSet);
//print out the results
System.out.println("=====================================================================");
System.out.println("Results for "+this.getClass().getSimpleName());
String strSummary = eTest.toSummaryString();
System.out.println(strSummary);
System.out.println("F-measure : "+eTest.weightedFMeasure());
System.out.println("precision : "+eTest.weightedPrecision());
System.out.println("recall : "+eTest.weightedRecall());
System.out.println("=====================================================================");
InfoGainAttributeEval infoGainAttributeEval = new InfoGainAttributeEval();
infoGainAttributeEval.buildEvaluator(TrainingSet);
for (int i = 0; i <featureVectorAttributes.size()-1; i++) {
double v = infoGainAttributeEval.evaluateAttribute(i);
System.out.print(featureVectorAttributes.get(i).name()+"\t\t");
System.out.println(v);
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: classify
import weka.attributeSelection.InfoGainAttributeEval; //导入依赖的package包/类
public void classify(String trainingFile,String testingFile) {
try {
// initiateBagOfWords(trainingFile);
initTrainingSet(trainingFile);
// initiateBagOfWords(testingFile);
initTestingSet(testingFile);
StringToWordVector filter = new StringToWordVector();
int[] indices= new int[1];
indices[0]=6;
filter.setAttributeIndicesArray(indices);
filter.setInputFormat(TrainingSet);
filter.setWordsToKeep(6);
filter.setDoNotOperateOnPerClassBasis(false);
filter.setTFTransform(true);
filter.setOutputWordCounts(true);
TrainingSet = Filter.useFilter(TrainingSet, filter);
TestingSet = Filter.useFilter(TestingSet, filter);
Classifier cModel = new SimpleLogistic();
cModel.buildClassifier(TrainingSet);
weka.core.SerializationHelper.write(System.getProperty("user.dir")+"/Classification/src/datafiles/cls.model",cModel);
weka.core.SerializationHelper.write(System.getProperty("user.dir")+"/Classification/src/datafiles/testingSet.model",TestingSet);
Evaluation eTest = new Evaluation(TrainingSet);
eTest.evaluateModel(cModel, TestingSet);
//print out the results
System.out.println("=====================================================================");
System.out.println("Results for "+this.getClass().getSimpleName());
String strSummary = eTest.toSummaryString();
System.out.println(strSummary);
InfoGainAttributeEval infoGainAttributeEval = new InfoGainAttributeEval();
infoGainAttributeEval.buildEvaluator(TrainingSet);
for (int i = 0; i <featureVectorAttributes.size()-1; i++) {
double v = infoGainAttributeEval.evaluateAttribute(i);
System.out.print(i+" "+featureVectorAttributes.get(i).name()+"\t\t");
System.out.println(v);
}
System.out.println("=====================================================================");
System.out.println("recall : "+eTest.weightedRecall());
System.out.println("precision : "+eTest.weightedPrecision());
System.out.println("F-measure : "+eTest.weightedFMeasure());
System.out.println("================= Rounded Values =========================");
System.out.println("recall : "+Math.round(eTest.weightedRecall() * 100.0) / 100.0);
System.out.println("precision : "+Math.round(eTest.weightedPrecision() * 100.0) / 100.0);
System.out.println("F-measure : "+Math.round(eTest.weightedFMeasure() * 100.0) / 100.0);
System.out.println("=====================================================================");
printErrors(cModel);
} catch (Exception e) {
e.printStackTrace();
}
}