use of org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer in project ignite by apache.
the class DecisionTreeClassificationExportImportExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws IOException {
System.out.println(">>> Decision tree 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, LabeledVector<Double>> trainingSetCfg = new CacheConfiguration<>();
trainingSetCfg.setName("TRAINING_SET");
trainingSetCfg.setAffinity(new RendezvousAffinityFunction(false, 10));
IgniteCache<Integer, LabeledVector<Double>> trainingSet = null;
Path jsonMdlPath = 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);
// Train decision tree model.
LabeledDummyVectorizer<Integer, Double> vectorizer = new LabeledDummyVectorizer<>();
DecisionTreeModel mdl = trainer.fit(ignite, trainingSet, vectorizer);
System.out.println("\n>>> Exported Decision tree classification model: " + mdl);
int correctPredictions = evaluateModel(rnd, mdl);
System.out.println("\n>>> Accuracy for exported Decision tree classification model: " + correctPredictions / 10.0 + "%");
jsonMdlPath = Files.createTempFile(null, null);
mdl.toJSON(jsonMdlPath);
DecisionTreeModel modelImportedFromJSON = DecisionTreeModel.fromJSON(jsonMdlPath);
System.out.println("\n>>> Imported Decision tree classification model: " + modelImportedFromJSON);
correctPredictions = evaluateModel(rnd, modelImportedFromJSON);
System.out.println("\n>>> Accuracy for imported Decision tree classification model: " + correctPredictions / 10.0 + "%");
System.out.println("\n>>> Decision tree 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.dataset.feature.extractor.impl.LabeledDummyVectorizer 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.dataset.feature.extractor.impl.LabeledDummyVectorizer in project ignite by apache.
the class RandomForestClassifierTrainerTest method testUpdate.
/**
*/
@Test
public void testUpdate() {
int sampleSize = 1000;
Map<Integer, LabeledVector<Double>> sample = new HashMap<>();
for (int i = 0; i < sampleSize; i++) {
double x1 = i;
double x2 = x1 / 10.0;
double x3 = x2 / 10.0;
double x4 = x3 / 10.0;
sample.put(i, VectorUtils.of(x1, x2, x3, x4).labeled((double) i % 2));
}
ArrayList<FeatureMeta> meta = new ArrayList<>();
for (int i = 0; i < 4; i++) meta.add(new FeatureMeta("", i, false));
DatasetTrainer<RandomForestModel, Double> trainer = new RandomForestClassifierTrainer(meta).withAmountOfTrees(100).withFeaturesCountSelectionStrgy(x -> 2).withEnvironmentBuilder(TestUtils.testEnvBuilder());
RandomForestModel originalMdl = trainer.fit(sample, parts, new LabeledDummyVectorizer<>());
RandomForestModel updatedOnSameDS = trainer.update(originalMdl, sample, parts, new LabeledDummyVectorizer<>());
RandomForestModel updatedOnEmptyDS = trainer.update(originalMdl, new HashMap<Integer, LabeledVector<Double>>(), parts, new LabeledDummyVectorizer<>());
Vector v = VectorUtils.of(5, 0.5, 0.05, 0.005);
assertEquals(originalMdl.predict(v), updatedOnSameDS.predict(v), 0.01);
assertEquals(originalMdl.predict(v), updatedOnEmptyDS.predict(v), 0.01);
}
use of org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer in project ignite by apache.
the class DecisionTreeClassificationTrainerExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String... args) {
System.out.println(">>> Decision tree 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, 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);
// Train decision tree model.
LabeledDummyVectorizer<Integer, Double> vectorizer = new LabeledDummyVectorizer<>();
DecisionTreeModel mdl = trainer.fit(ignite, trainingSet, vectorizer);
System.out.println(">>> Decision tree classification model: " + mdl);
// Calculate score.
int correctPredictions = 0;
for (int i = 0; i < 1000; i++) {
LabeledVector<Double> pnt = generatePoint(rnd);
double prediction = mdl.predict(pnt.features());
double lbl = pnt.label();
if (i % 50 == 1)
System.out.printf(">>> test #: %d\t\t predicted: %.4f\t\tlabel: %.4f\n", i, prediction, lbl);
if (Precision.equals(prediction, lbl, Precision.EPSILON))
correctPredictions++;
}
System.out.println(">>> Accuracy: " + correctPredictions / 10.0 + "%");
System.out.println(">>> Decision tree classification trainer example completed.");
} finally {
trainingSet.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.dataset.feature.extractor.impl.LabeledDummyVectorizer in project ignite by apache.
the class DataStreamGeneratorFillCacheTest method testCacheFilling.
/**
*/
@Test
public void testCacheFilling() {
IgniteConfiguration configuration = new IgniteConfiguration().setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(new TcpDiscoveryVmIpFinder().setAddresses(Arrays.asList("127.0.0.1:47500..47509"))));
String cacheName = "TEST_CACHE";
CacheConfiguration<UUID, LabeledVector<Double>> cacheConfiguration = new CacheConfiguration<UUID, LabeledVector<Double>>(cacheName).setAffinity(new RendezvousAffinityFunction(false, 10));
int datasetSize = 5000;
try (Ignite ignite = Ignition.start(configuration)) {
IgniteCache<UUID, LabeledVector<Double>> cache = ignite.getOrCreateCache(cacheConfiguration);
DataStreamGenerator generator = new GaussRandomProducer(0).vectorize(1).asDataStream();
generator.fillCacheWithVecUUIDAsKey(datasetSize, cache);
LabeledDummyVectorizer<UUID, Double> vectorizer = new LabeledDummyVectorizer<>();
CacheBasedDatasetBuilder<UUID, LabeledVector<Double>> datasetBuilder = new CacheBasedDatasetBuilder<>(ignite, cache);
IgniteFunction<SimpleDatasetData, StatPair> map = data -> new StatPair(DoubleStream.of(data.getFeatures()).sum(), data.getRows());
LearningEnvironment env = LearningEnvironmentBuilder.defaultBuilder().buildForTrainer();
env.deployingContext().initByClientObject(map);
try (CacheBasedDataset<UUID, LabeledVector<Double>, EmptyContext, SimpleDatasetData> dataset = datasetBuilder.build(LearningEnvironmentBuilder.defaultBuilder(), new EmptyContextBuilder<>(), new SimpleDatasetDataBuilder<>(vectorizer), env)) {
StatPair res = dataset.compute(map, StatPair::sum);
assertEquals(datasetSize, res.cntOfRows);
assertEquals(0.0, res.elementsSum / res.cntOfRows, 1e-2);
}
ignite.destroyCache(cacheName);
}
}
Aggregations