use of org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer in project ignite by apache.
the class CrossValidationExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String... args) {
System.out.println(">>> Cross validation score calculator example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
// Create cache with training data.
CacheConfiguration<Integer, LabeledVector<Double>> trainingSetCfg = new CacheConfiguration<>();
trainingSetCfg.setName("TRAINING_SET");
trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10));
IgniteCache<Integer, LabeledVector<Double>> trainingSet = null;
try {
trainingSet = ignite.createCache(trainingSetCfg);
Random rnd = new Random(0);
// Fill training data.
for (int i = 0; i < 1000; i++) trainingSet.put(i, generatePoint(rnd));
// Create classification trainer.
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(4, 0);
LabeledDummyVectorizer<Integer, Double> vectorizer = new LabeledDummyVectorizer<>();
CrossValidation<DecisionTreeModel, Integer, LabeledVector<Double>> scoreCalculator = new CrossValidation<>();
double[] accuracyScores = scoreCalculator.withIgnite(ignite).withUpstreamCache(trainingSet).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(4).isRunningOnPipeline(false).scoreByFolds();
System.out.println(">>> Accuracy: " + Arrays.toString(accuracyScores));
double[] balancedAccuracyScores = scoreCalculator.withMetric(MetricName.ACCURACY).scoreByFolds();
System.out.println(">>> Balanced Accuracy: " + Arrays.toString(balancedAccuracyScores));
System.out.println(">>> Cross validation score calculator example completed.");
} finally {
trainingSet.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer in project ignite by apache.
the class CrossValidationTest method testScoreWithBadDataset.
/**
*/
@Test
public void testScoreWithBadDataset() {
Map<Integer, double[]> data = new HashMap<>();
for (int i = 0; i < 1000; i++) data.put(i, new double[] { i, i % 2 == 0 ? 1.0 : 0.0 });
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST);
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
DebugCrossValidation<DecisionTreeModel, Integer, double[]> scoreCalculator = new DebugCrossValidation<>();
int folds = 4;
scoreCalculator.withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(folds).isRunningOnPipeline(false);
double[] scores = scoreCalculator.scoreByFolds();
assertEquals(folds, scores.length);
for (int i = 0; i < folds; i++) assertTrue(scores[i] < 0.6);
}
use of org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer in project ignite by apache.
the class CrossValidationTest method testScoreWithGoodDatasetAndBinaryMetrics.
/**
*/
@Test
public void testScoreWithGoodDatasetAndBinaryMetrics() {
Map<Integer, double[]> data = new HashMap<>();
for (int i = 0; i < 1000; i++) data.put(i, new double[] { i > 500 ? 1.0 : 0.0, i });
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
DebugCrossValidation<DecisionTreeModel, Integer, double[]> scoreCalculator = new DebugCrossValidation<>();
int folds = 4;
scoreCalculator.withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(folds).isRunningOnPipeline(false);
verifyScores(folds, scoreCalculator.scoreByFolds());
}
use of org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer in project ignite by apache.
the class CrossValidationTest method testScoreWithGoodDataset.
/**
*/
@Test
public void testScoreWithGoodDataset() {
Map<Integer, double[]> data = new HashMap<>();
for (int i = 0; i < 1000; i++) data.put(i, new double[] { i > 500 ? 1.0 : 0.0, i });
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0);
DebugCrossValidation<DecisionTreeModel, Integer, double[]> scoreCalculator = new DebugCrossValidation<>();
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
int folds = 4;
scoreCalculator.withUpstreamMap(data).withAmountOfParts(1).withTrainer(trainer).withMetric(MetricName.ACCURACY).withPreprocessor(vectorizer).withAmountOfFolds(folds).isRunningOnPipeline(false);
verifyScores(folds, scoreCalculator.scoreByFolds());
}
use of org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer in project ignite by apache.
the class EncoderExample method main.
/**
* Run example.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Train Decision Tree model on mushrooms.csv dataset.");
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
try {
IgniteCache<Integer, Object[]> dataCache = new SandboxMLCache(ignite).fillObjectCacheWithDoubleLabels(MLSandboxDatasets.MUSHROOMS);
final Vectorizer<Integer, Object[], Integer, Object> vectorizer = new ObjectArrayVectorizer<Integer>(1, 2, 3).labeled(0);
Preprocessor<Integer, Object[]> encoderPreprocessor = new EncoderTrainer<Integer, Object[]>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(0).withEncodedFeature(1).withEncodedFeature(2).fit(ignite, dataCache, vectorizer);
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
// Train decision tree model.
DecisionTreeModel mdl = trainer.fit(ignite, dataCache, encoderPreprocessor);
System.out.println("\n>>> Trained model: " + mdl);
double accuracy = Evaluator.evaluate(dataCache, mdl, encoderPreprocessor, new Accuracy<>());
System.out.println("\n>>> Accuracy " + accuracy);
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Train Decision Tree model on mushrooms.csv dataset.");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} finally {
System.out.flush();
}
}
Aggregations