use of org.apache.ignite.ml.tree.DecisionTreeModel 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.DecisionTreeModel 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.DecisionTreeModel 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.DecisionTreeModel 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();
}
}
use of org.apache.ignite.ml.tree.DecisionTreeModel in project ignite by apache.
the class LabelEncoderExample 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).fillObjectCacheWithCategoricalData(MLSandboxDatasets.MUSHROOMS);
final Vectorizer<Integer, Object[], Integer, Object> vectorizer = new ObjectArrayVectorizer<Integer>(1, 2).labeled(0);
Preprocessor<Integer, Object[]> strEncoderPreprocessor = new EncoderTrainer<Integer, Object[]>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(0).withEncodedFeature(1).fit(ignite, dataCache, vectorizer);
Preprocessor<Integer, Object[]> lbEncoderPreprocessor = new EncoderTrainer<Integer, Object[]>().withEncoderType(EncoderType.LABEL_ENCODER).fit(ignite, dataCache, strEncoderPreprocessor);
DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
// Train decision tree model.
DecisionTreeModel mdl = trainer.fit(ignite, dataCache, lbEncoderPreprocessor);
System.out.println("\n>>> Trained model: " + mdl);
double accuracy = Evaluator.evaluate(dataCache, mdl, lbEncoderPreprocessor, 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