use of org.apache.ignite.ml.pipeline.PipelineMdl in project ignite by apache.
the class TrainingWithCustomPreprocessorsExample method main.
/**
* Run example.
*
* @param args Command line arguments.
* @throws Exception Exception.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
IgniteCache<Integer, Vector> trainingSet = null;
try {
trainingSet = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.BOSTON_HOUSE_PRICES);
Vectorizer<Integer, Vector, Integer, Double> basicVectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>().fit(ignite, trainingSet, basicVectorizer);
// In-place definition of custom preprocessor by lambda expression.
Preprocessor<Integer, Vector> customPreprocessor = (k, v) -> {
LabeledVector res = imputingPreprocessor.apply(k, v);
double fifthFeature = res.features().get(5);
Vector updatedVector = res.features().set(5, fifthFeature > 0 ? Math.log(fifthFeature) : -1);
return updatedVector.labeled(res.label());
};
Vectorizer9000 customVectorizer = new Vectorizer9000(customPreprocessor);
PipelineMdl<Integer, Vector> mdl = new Pipeline<Integer, Vector, Integer, Double>().addVectorizer(customVectorizer).addPreprocessingTrainer(new MinMaxScalerTrainer<Integer, Vector>()).addPreprocessingTrainer(new NormalizationTrainer<Integer, Vector>().withP(1)).addPreprocessingTrainer(getCustomTrainer()).addTrainer(new DecisionTreeClassificationTrainer(5, 0)).fit(ignite, trainingSet);
System.out.println(">>> Perform scoring.");
double score = Evaluator.evaluate(trainingSet, mdl, mdl.getPreprocessor(), MetricName.R2);
System.out.println(">>> R^2 score: " + score);
} finally {
if (trainingSet != null)
trainingSet.destroy();
}
} finally {
System.out.flush();
}
}
Aggregations