Search in sources :

Example 31 with EncoderTrainer

use of org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer in project gridgain by gridgain.

the class Step_3_Categorial method main.

/**
 * Run example.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Tutorial step 3 (categorial) example started.");
    try (Ignite ignite = Ignition.start("examples-ml/config/example-ignite.xml")) {
        try {
            IgniteCache<Integer, Vector> dataCache = TitanicUtils.readPassengers(ignite);
            // "pclass", "sibsp", "parch", "sex", "embarked"
            final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 3, 5, 6, 10).labeled(1);
            Preprocessor<Integer, Vector> strEncoderPreprocessor = new EncoderTrainer<Integer, Vector>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(1).withEncodedFeature(4).fit(ignite, dataCache, vectorizer);
            Preprocessor<Integer, Vector> imputingPreprocessor = new ImputerTrainer<Integer, Vector>().fit(ignite, dataCache, strEncoderPreprocessor);
            DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
            // Train decision tree model.
            DecisionTreeNode mdl = trainer.fit(ignite, dataCache, imputingPreprocessor);
            System.out.println("\n>>> Trained model: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, mdl, imputingPreprocessor, new Accuracy<>());
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println("\n>>> Test Error " + (1 - accuracy));
            System.out.println(">>> Tutorial step 3 (categorial) example completed.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } finally {
        System.out.flush();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) Ignite(org.apache.ignite.Ignite) EncoderTrainer(org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DecisionTreeNode(org.apache.ignite.ml.tree.DecisionTreeNode)

Example 32 with EncoderTrainer

use of org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer in project gridgain by gridgain.

the class Step_4_Add_age_fare method main.

/**
 * Run example.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Tutorial step 4 (add age and fare) example started.");
    try (Ignite ignite = Ignition.start("examples-ml/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);
            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);
            DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(5, 0);
            // Train decision tree model.
            DecisionTreeNode mdl = trainer.fit(ignite, dataCache, imputingPreprocessor);
            System.out.println("\n>>> Trained model: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, mdl, imputingPreprocessor, new Accuracy<>());
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println("\n>>> Test Error " + (1 - accuracy));
            System.out.println(">>> Tutorial step 4 (add age and fare) example completed.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } finally {
        System.out.flush();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) DecisionTreeClassificationTrainer(org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer) Ignite(org.apache.ignite.Ignite) EncoderTrainer(org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DecisionTreeNode(org.apache.ignite.ml.tree.DecisionTreeNode)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)32 Ignite (org.apache.ignite.Ignite)32 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)32 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)30 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)27 NormalizationTrainer (org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer)21 CrossValidation (org.apache.ignite.ml.selection.cv.CrossValidation)15 CrossValidationResult (org.apache.ignite.ml.selection.cv.CrossValidationResult)13 ParamGrid (org.apache.ignite.ml.selection.paramgrid.ParamGrid)13 DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)12 DecisionTreeNode (org.apache.ignite.ml.tree.DecisionTreeNode)12 Accuracy (org.apache.ignite.ml.selection.scoring.metric.classification.Accuracy)10 SerializableDoubleConsumer (org.apache.ignite.examples.ml.util.SerializableDoubleConsumer)7 Arrays (java.util.Arrays)6 IgniteCache (org.apache.ignite.IgniteCache)6 Ignition (org.apache.ignite.Ignition)6 SerializableFunction (org.apache.ignite.examples.ml.util.SerializableFunction)6 Vectorizer (org.apache.ignite.ml.dataset.feature.extractor.Vectorizer)6 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)6 Preprocessor (org.apache.ignite.ml.preprocessing.Preprocessor)6