use of org.apache.ignite.ml.composition.boosting.GDBModel in project ignite by apache.
the class GDBOnTreesClassificationExportImportExample method main.
/**
* Run example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> GDB classification trainer example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println("\n>>> Ignite grid started.");
// Create cache with training data.
CacheConfiguration<Integer, double[]> trainingSetCfg = createCacheConfiguration();
IgniteCache<Integer, double[]> trainingSet = null;
Path jsonMdlPath = null;
try {
trainingSet = fillTrainingData(ignite, trainingSetCfg);
// Create classification trainer.
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
// Train decision tree model.
GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
System.out.println("\n>>> Exported GDB classification model: " + mdl.toString(true));
predictOnGeneratedData(mdl);
jsonMdlPath = Files.createTempFile(null, null);
mdl.toJSON(jsonMdlPath);
IgniteFunction<Double, Double> lbMapper = lb -> lb > 0.5 ? 1.0 : 0.0;
GDBModel modelImportedFromJSON = GDBModel.fromJSON(jsonMdlPath).withLblMapping(lbMapper);
System.out.println("\n>>> Imported GDB classification model: " + modelImportedFromJSON.toString(true));
predictOnGeneratedData(modelImportedFromJSON);
System.out.println(">>> GDB classification trainer example completed.");
} finally {
if (trainingSet != null)
trainingSet.destroy();
if (jsonMdlPath != null)
Files.deleteIfExists(jsonMdlPath);
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.composition.boosting.GDBModel in project ignite by apache.
the class GDBOnTreesRegressionTrainerExample method main.
/**
* Run example.
*
* @param args Command line arguments, none required.
*/
public static void main(String... args) {
System.out.println();
System.out.println(">>> GDB regression trainer 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, double[]> trainingSetCfg = createCacheConfiguration();
IgniteCache<Integer, double[]> trainingSet = null;
try {
trainingSet = fillTrainingData(ignite, trainingSetCfg);
// Create regression trainer.
GDBTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001));
// Train decision tree model.
GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Valid answer \t|");
System.out.println(">>> ---------------------------------");
// Calculate score.
for (int x = -5; x < 5; x++) {
double predicted = mdl.predict(VectorUtils.of(x));
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.pow(x, 2));
}
System.out.println(">>> ---------------------------------");
System.out.println(">>> GDB regression trainer example completed.");
} finally {
trainingSet.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.composition.boosting.GDBModel in project ignite by apache.
the class GDBOnTreesClassificationTrainerExample method main.
/**
* Run example.
*
* @param args Command line arguments, none required.
*/
public static void main(String... args) {
System.out.println();
System.out.println(">>> GDB classification trainer 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, double[]> trainingSetCfg = createCacheConfiguration();
IgniteCache<Integer, double[]> trainingSet = null;
try {
trainingSet = fillTrainingData(ignite, trainingSetCfg);
// Create classification trainer.
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(1.0, 300, 2, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.1));
// Train decision tree model.
GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Valid answer\t|");
System.out.println(">>> ---------------------------------");
// Calculate score.
for (int x = -5; x < 5; x++) {
double predicted = mdl.predict(VectorUtils.of(x));
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", predicted, Math.sin(x) < 0 ? 0.0 : 1.0);
}
System.out.println(">>> ---------------------------------");
System.out.println(">>> Count of trees = " + mdl.getModels().size());
System.out.println(">>> ---------------------------------");
System.out.println(">>> GDB classification trainer example completed.");
} finally {
trainingSet.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.composition.boosting.GDBModel in project ignite by apache.
the class Step_11_Boosting method main.
/**
* Run example.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> Tutorial step 11 (Boosting) example started.");
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
try {
IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
// Extracts "pclass", "sibsp", "parch", "sex", "embarked", "age", "fare".
final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 3, 4, 5, 6, 8, 10).labeled(1);
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.75);
Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(1).withEncodedFeature(// <--- Changed index here.
6).fit(ignite, dataCache, vectorizer);
Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>().fit(ignite, dataCache, strEncoderPreprocessor);
Preprocessor<Integer, Vector> minMaxScalerPreprocessor = new MinMaxScalerTrainer<Integer, Vector>().fit(ignite, dataCache, imputingPreprocessor);
Preprocessor<Integer, Vector> normalizationPreprocessor = new NormalizationTrainer<Integer, Vector>().withP(1).fit(ignite, dataCache, minMaxScalerPreprocessor);
// Create classification trainer.
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(0.5, 500, 4, 0.).withCheckConvergenceStgyFactory(new MedianOfMedianConvergenceCheckerFactory(0.1));
// Train decision tree model.
GDBModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), normalizationPreprocessor);
System.out.println("\n>>> Trained model: " + mdl.toString(true));
double accuracy = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, normalizationPreprocessor, MetricName.ACCURACY);
System.out.println("\n>>> Accuracy " + accuracy);
System.out.println("\n>>> Test Error " + (1 - accuracy));
System.out.println(">>> Tutorial step 11 (Boosting) example completed.");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.composition.boosting.GDBModel in project ignite by apache.
the class GDBOnTreesRegressionExportImportExample method main.
/**
* Run example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> GDB regression trainer 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, double[]> trainingSetCfg = createCacheConfiguration();
IgniteCache<Integer, double[]> trainingSet = null;
Path jsonMdlPath = null;
try {
trainingSet = fillTrainingData(ignite, trainingSetCfg);
// Create regression trainer.
GDBTrainer trainer = new GDBRegressionOnTreesTrainer(1.0, 2000, 1, 0.).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.001));
// Train decision tree model.
GDBModel mdl = trainer.fit(ignite, trainingSet, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
System.out.println("\n>>> Exported GDB regression model: " + mdl.toString(true));
predictOnGeneratedData(mdl);
jsonMdlPath = Files.createTempFile(null, null);
mdl.toJSON(jsonMdlPath);
IgniteFunction<Double, Double> lbMapper = lb -> lb;
GDBModel modelImportedFromJSON = GDBModel.fromJSON(jsonMdlPath).withLblMapping(lbMapper);
System.out.println("\n>>> Imported GDB regression model: " + modelImportedFromJSON.toString(true));
predictOnGeneratedData(modelImportedFromJSON);
System.out.println(">>> GDB regression trainer example completed.");
} finally {
if (trainingSet != null)
trainingSet.destroy();
if (jsonMdlPath != null)
Files.deleteIfExists(jsonMdlPath);
}
} finally {
System.out.flush();
}
}
Aggregations