本文整理汇总了Java中weka.classifiers.trees.RandomForest类的典型用法代码示例。如果您正苦于以下问题:Java RandomForest类的具体用法?Java RandomForest怎么用?Java RandomForest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RandomForest类属于weka.classifiers.trees包,在下文中一共展示了RandomForest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildFilteredClassifier
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public void buildFilteredClassifier(){
rf = new RandomForest();
Remove rm = new Remove();
rm.setAttributeIndices("1");
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
fc.setClassifier(rf);
try{
fc.buildClassifier(weather);
for (int i = 0; i < weather.numInstances(); i++){
double pred = fc.classifyInstance(weather.instance(i));
System.out.print("given value: " + weather.classAttribute().value((int) weather.instance(i).classValue()));
System.out.println("---predicted value: " + weather.classAttribute().value((int) pred));
}
} catch (Exception e) {
}
}
示例2: useClassifier
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
/**
* uses the meta-classifier
*/
protected static void useClassifier(Instances data) throws Exception {
System.out.println("\n1. Meta-classfier");
AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
CfsSubsetEval eval = new CfsSubsetEval();
//GreedyStepwise search = new GreedyStepwise();
GeneticSearch search = new GeneticSearch();
// search.setSearchBackwards(false);
RandomForest base = new RandomForest();
classifier.setClassifier(base);
System.out.println("Set the classifier : " + base.toString());
classifier.setEvaluator(eval);
System.out.println("Set the evaluator : " + eval.toString());
// classifier.setSearch( search );
System.out.println("Set the search : " + search.toString());
Evaluation evaluation = new Evaluation(data);
evaluation.crossValidateModel(classifier, data, 10, new Random(1));
System.out.println(evaluation.toSummaryString());
}
示例3: train
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
private void train(String name) {
try {
Classifier randomForest = new RandomForest();
ConverterUtils.DataSource source = new ConverterUtils.DataSource(FOLDER + name);
dataSet = source.getDataSet();
dataSet.setClassIndex(dataSet.numAttributes() - 1);
randomForest.buildClassifier(dataSet);
classifier = randomForest;
} catch (Exception e) {
e.printStackTrace();
}
}
示例4: train
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public void train() {
try {
Classifier randomForest = new RandomForest();
ConverterUtils.DataSource source = new ConverterUtils.DataSource(FOLDER + "question-classifier.arff");
dataSet = source.getDataSet();
dataSet.setClassIndex(dataSet.numAttributes() - 1);
randomForest.buildClassifier(dataSet);
classifier = randomForest;
} catch (Exception e) {
e.printStackTrace();
}
}
示例5: trainRandomForest
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public static void trainRandomForest(final Instances trainingSet) throws Exception {
// Create a classifier
final RandomForest tree = new RandomForest();
tree.buildClassifier(trainingSet);
// Test the model
final Evaluation eval = new Evaluation(trainingSet);
// eval.crossValidateModel(tree, trainingSet, 10, new Random(1));
eval.evaluateModel(tree, trainingSet);
// Print the result à la Weka explorer:
logger.info(eval.toSummaryString());
logger.info(eval.toMatrixString());
logger.info(tree.toString());
}
示例6: main
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
XMLWikipediaExtractor xwe=new XMLWikipediaExtractor();
int bestTopic=xwe.getAllArticlesUnderCategory(WikiHelper.getSpecificProperty("categoryForTraining"));
JtopiaUsage.loadComponents();
URLCrawlerForTopic.loadStanfordComponents();
// //SummarizationSolver.loadCoreNLP();
SummarizationSolver.loadLM();
SummarizationSolver.loadStanfordComponents();
//Load the topic model
//int bestTopic=40;
TopicInferencer inferencer=TopicModelGenerator.getBestTopicModelInferer(WikiHelper.getSpecificProperty("categoryForTraining"), bestTopic);
RandomForest classifier=PassageClassifier.getRFBestClassifier(bestTopic, WikiHelper.getSpecificProperty("categoryForTraining"));
ExistingArticleGenerator eag=new ExistingArticleGenerator();
xwe.storeStubArticles(WikiHelper.getSpecificProperty("categoryToGenerate"));
eag.chooseRandomArticlesToCreate(WikiHelper.getSpecificProperty("categoryToGenerate"),bestTopic,
inferencer,classifier);
}
示例7: trainModel
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public static void trainModel(String trainingPath, String outputPath, String learner) {
if(learner==null) {
learner = "SMO";
}
System.out.println("Training "+ learner+" model...");
if(learner.equals("SMO")) {
SMO.main(new String[] {
"-M",
"-d", outputPath + "/pageclassifier.model",
"-t", trainingPath + "/weka.arff",
"-C", "0.01"
});
} else if(learner.equals("RandomForest")) {
RandomForest.main(new String[] {
// "-K", "5", // k-fold cross validation
"-I", "100", // Number of trees to build
"-d", outputPath + "/pageclassifier.model",
"-t", trainingPath + "/weka.arff"
});
} else {
System.out.println("Unknow learner: "+learner);
return;
}
}
示例8: getClassifier
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public static Classifier getClassifier(AlgorithmType type) {
Classifier classifier = null;
try {
if(type == null) {
type = AlgorithmType.RANDOM_FOREST;
}
Constructor constructor = type.getClazz().getConstructor();
classifier = (Classifier)constructor.newInstance();
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
logger.warn("Unable to instantiate classifier : " + type.getClazz().getName());
classifier = (Classifier) new RandomForest();
}
return classifier;
}
示例9: LearnRandomForest
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
@Override
public void LearnRandomForest() throws Exception
{
trainedData.setClassIndex(trainedData.numAttributes()-1);
filter=new StringToWordVector();
classifier=new FilteredClassifier();
classifier.setFilter(filter);
classifier.setClassifier(new RandomForest());
classifier.buildClassifier(trainedData);
}
开发者ID:unsw-cse-soc,项目名称:Data-curation-API,代码行数:11,代码来源:ExtractClassificationTextRandomForestImpl.java
示例10: getClassifierFScore
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
public static double getClassifierFScore(int numTopics, String categoryName) throws Exception
{
//int numTopics=40;
int seed = 1;
int folds = 10;
DataSource trainSource = new DataSource("inputFiles/rawFiles/ARFF-files/"+categoryName+"-ARFF/"
+categoryName+"-"+numTopics+".ARFF");
Instances trainingSet = trainSource.getDataSet();
if (trainingSet.classIndex() == -1)
trainingSet.setClassIndex(trainingSet.numAttributes() - 1);
// Resample for minority class
Resample reSample=new Resample();
reSample.setInputFormat(trainingSet);
//reSample.s(1);
trainingSet=Filter.useFilter(trainingSet, reSample);
// trainingSet=Filter.useFilter(trainingSet, reSample);
// trainingSet=Filter.useFilter(trainingSet, reSample);
// trainingSet=Filter.useFilter(trainingSet, reSample);
Random rand = new Random(seed);
trainingSet.randomize(rand);
if (trainingSet.classAttribute().isNominal())
trainingSet.stratify(folds);
RandomForest classifier=new RandomForest();
//System.out.println("Training with "+classifier.getClass().getName());
//System.out.println(trainingSet.numInstances());
//classifier.buildClassifier(trainingSet);
// perform cross-validation
//Object[] obj={"hello"};
Evaluation eval = new Evaluation(trainingSet);
//Object[] forPredictionsPrinting = {"a","10","true"};
eval.crossValidateModel(classifier, trainingSet, 10, new Random(1), new Object[] { });
return eval.weightedFMeasure();
}
示例11: train
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
@Override
public RandomForest train(Instances instances) {
RandomForest randomForest = new RandomForest();
randomForest.setNumTrees(numTrees);
try {
randomForest.buildClassifier(instances);
} catch (Exception e) {
throw new ClassifierBuildingException("Exception occured while building classifier: " + e.getMessage(), e);
}
return randomForest;
}
示例12: train_zeroInstancesRf_resultIsRandomForestClassifier
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
@Test
public void train_zeroInstancesRf_resultIsRandomForestClassifier() {
trainer = createGeneralTrainer("weka.classifiers.trees.RandomForest");
Instances instances = getTestInstances();
// Act
Classifier classifier = trainer.train(instances);
// Assert
assertTrue(classifier instanceof RandomForest);
}
示例13: train_setForestSize_randomForestHasCorrectSetting
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
@Test
public void train_setForestSize_randomForestHasCorrectSetting() {
// Arrange
// Act
GeneralTrainer randomForestTrainer = createGeneralTrainer("weka.classifiers.trees.RandomForest -I 800"); // -I for number or trees
RandomForest randomForest = (RandomForest) randomForestTrainer.train(getTestInstances());
// Assert
assertEquals(800, randomForest.getNumTrees());
}
示例14: train_zeroInstances_resultIsRfClassifier
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
@Test
public void train_zeroInstances_resultIsRfClassifier() {
trainer = createRandomForestTrainer();
Instances instances = getTestInstances();
// Act
Classifier classifier = trainer.train(instances);
// Assert
assertTrue(classifier instanceof RandomForest);
}
示例15: constructor_numTreeGiven_rfHasCorrectNumTree
import weka.classifiers.trees.RandomForest; //导入依赖的package包/类
@Test
public void constructor_numTreeGiven_rfHasCorrectNumTree() {
// Arrange
// Act
RandomForestTrainer rfTrainer = new RandomForestTrainer(123);
RandomForest rf = rfTrainer.train(getTestInstances());
// Assert
assertEquals(123, rf.getNumTrees());
}