use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class LogRegFromSparkThroughPMMLExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> Logistic regression model loaded from PMML over partitioned dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.TWO_CLASSED_IRIS);
String path = IgniteUtils.resolveIgnitePath("examples/src/main/resources/models/spark/iris.pmml").toPath().toAbsolutePath().toString();
LogisticRegressionModel mdl = PMMLParser.load(path);
System.out.println(">>> Logistic regression model: " + mdl);
double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, new Accuracy<>());
System.out.println("\n>>> Accuracy " + accuracy);
System.out.println("\n>>> Test Error " + (1 - accuracy));
} finally {
if (dataCache != null)
dataCache.destroy();
}
}
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class DecisionTreeRegressionFromSparkExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Decision tree regression model loaded from Spark through serialization over partitioned dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = TitanicUtils.readPassengersWithoutNulls(ignite);
final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 1, 5, 6).labeled(4);
DecisionTreeModel mdl = (DecisionTreeModel) SparkModelParser.parse(SPARK_MDL_PATH, SupportedSparkModels.DECISION_TREE_REGRESSION, env);
System.out.println(">>> Decision tree regression model: " + mdl);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, Vector> observation : observations) {
LabeledVector<Double> lv = vectorizer.apply(observation.getKey(), observation.getValue());
Vector inputs = lv.features();
double groundTruth = lv.label();
double prediction = mdl.predict(inputs);
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
}
System.out.println(">>> ---------------------------------");
} finally {
dataCache.destroy();
}
}
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class LinearRegressionFromSparkExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Linear regression model loaded from Spark through serialization over partitioned dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = TitanicUtils.readPassengers(ignite);
final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 1, 5, 6).labeled(4);
LinearRegressionModel mdl = (LinearRegressionModel) SparkModelParser.parse(SPARK_MDL_PATH, SupportedSparkModels.LINEAR_REGRESSION, env);
System.out.println(">>> Linear regression model: " + mdl);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, Vector> observation : observations) {
LabeledVector<Double> lv = vectorizer.apply(observation.getKey(), observation.getValue());
Vector inputs = lv.features();
double groundTruth = lv.label();
double prediction = mdl.predict(inputs);
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
}
System.out.println(">>> ---------------------------------");
} finally {
dataCache.destroy();
}
}
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class RandomForestRegressionExportImportExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws IOException {
System.out.println();
System.out.println(">>> Random Forest regression algorithm over cached dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println("\n>>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
Path jsonMdlPath = null;
try {
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.BOSTON_HOUSE_PRICES);
AtomicInteger idx = new AtomicInteger(0);
RandomForestRegressionTrainer trainer = new RandomForestRegressionTrainer(IntStream.range(0, dataCache.get(1).size() - 1).mapToObj(x -> new FeatureMeta("", idx.getAndIncrement(), false)).collect(Collectors.toList())).withAmountOfTrees(101).withFeaturesCountSelectionStrgy(FeaturesCountSelectionStrategies.ONE_THIRD).withMaxDepth(4).withMinImpurityDelta(0.).withSubSampleSize(0.3).withSeed(0);
trainer.withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withParallelismStrategyTypeDependency(ParallelismStrategy.ON_DEFAULT_POOL).withLoggingFactoryDependency(ConsoleLogger.Factory.LOW));
System.out.println("\n>>> Configured trainer: " + trainer.getClass().getSimpleName());
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
RandomForestModel mdl = trainer.fit(ignite, dataCache, vectorizer);
System.out.println("\n>>> Exported Random Forest regression model: " + mdl.toString(true));
double mae = evaluateModel(dataCache, mdl);
System.out.println("\n>>> Mean absolute error (MAE) for exported Random Forest regression model " + mae);
jsonMdlPath = Files.createTempFile(null, null);
mdl.toJSON(jsonMdlPath);
RandomForestModel modelImportedFromJSON = RandomForestModel.fromJSON(jsonMdlPath);
System.out.println("\n>>> Exported Random Forest regression model: " + modelImportedFromJSON.toString(true));
mae = evaluateModel(dataCache, modelImportedFromJSON);
System.out.println("\n>>> Mean absolute error (MAE) for exported Random Forest regression model " + mae);
System.out.println("\n>>> Random Forest regression algorithm over cached dataset usage example completed.");
} finally {
if (dataCache != null)
dataCache.destroy();
if (jsonMdlPath != null)
Files.deleteIfExists(jsonMdlPath);
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class RandomForestRegressionFromSparkExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws FileNotFoundException {
System.out.println();
System.out.println(">>> Random Forest regression model loaded from Spark through serialization over partitioned dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
dataCache = TitanicUtils.readPassengersWithoutNulls(ignite);
final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 1, 5, 6).labeled(4);
ModelsComposition mdl = (ModelsComposition) SparkModelParser.parse(SPARK_MDL_PATH, SupportedSparkModels.RANDOM_FOREST_REGRESSION, env);
System.out.println(">>> Random Forest regression model: " + mdl);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, Vector> observation : observations) {
LabeledVector<Double> lv = vectorizer.apply(observation.getKey(), observation.getValue());
Vector inputs = lv.features();
double groundTruth = lv.label();
double prediction = mdl.predict(inputs);
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
}
System.out.println(">>> ---------------------------------");
} finally {
dataCache.destroy();
}
}
}
Aggregations