Search in sources :

Example 1 with DMatrix

use of ml.dmlc.xgboost4j.java.DMatrix in project zoltar by spotify.

the class XGBoostModelTest method getXGBoostIrisPredictor.

public static Predictor<Iris, Long> getXGBoostIrisPredictor() throws Exception {
    final URI trainedModelUri = XGBoostModelTest.class.getResource("/iris.model").toURI();
    final URI settingsUri = XGBoostModelTest.class.getResource("/settings.json").toURI();
    final XGBoostPredictFn<Iris, Long> predictFn = (model, vectors) -> {
        final List<CompletableFuture<Prediction<Iris, Long>>> predictions = vectors.stream().map(vector -> {
            return CompletableFuture.supplyAsync(() -> {
                try {
                    final Iterator<LabeledPoint> iterator = Collections.singletonList(vector.value()).iterator();
                    final DMatrix dMatrix = new DMatrix(iterator, null);
                    final float[] score = model.instance().predict(dMatrix)[0];
                    int idx = IntStream.range(0, score.length).reduce((i, j) -> score[i] >= score[j] ? i : j).getAsInt();
                    return Prediction.create(vector.input(), (long) idx);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }).collect(Collectors.toList());
        return CompletableFutures.allAsList(predictions);
    };
    final String settings = new String(Files.readAllBytes(Paths.get(settingsUri)), StandardCharsets.UTF_8);
    final XGBoostLoader model = XGBoostLoader.create(trainedModelUri.toString());
    final ExtractFn<Iris, LabeledPoint> extractFn = FeatranExtractFns.labeledPoints(IrisFeaturesSpec.irisFeaturesSpec(), settings);
    return PredictorsTest.newBuilder(model, extractFn, predictFn).predictor();
}
Also used : IntStream(java.util.stream.IntStream) FeatranExtractFns(com.spotify.zoltar.featran.FeatranExtractFns) CompletableFuture(java.util.concurrent.CompletableFuture) DMatrix(ml.dmlc.xgboost4j.java.DMatrix) IrisFeaturesSpec(com.spotify.zoltar.IrisFeaturesSpec) Duration(java.time.Duration) Map(java.util.Map) IrisHelper(com.spotify.zoltar.IrisHelper) PredictorsTest(com.spotify.zoltar.PredictorsTest) URI(java.net.URI) CompletableFutures(com.spotify.futures.CompletableFutures) Prediction(com.spotify.zoltar.Prediction) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Iris(com.spotify.zoltar.IrisFeaturesSpec.Iris) List(java.util.List) Paths(java.nio.file.Paths) Predictor(com.spotify.zoltar.Predictor) ExtractFn(com.spotify.zoltar.FeatureExtractFns.ExtractFn) Collections(java.util.Collections) LabeledPoint(ml.dmlc.xgboost4j.LabeledPoint) Prediction(com.spotify.zoltar.Prediction) LabeledPoint(ml.dmlc.xgboost4j.LabeledPoint) URI(java.net.URI) LabeledPoint(ml.dmlc.xgboost4j.LabeledPoint) Iris(com.spotify.zoltar.IrisFeaturesSpec.Iris) List(java.util.List) DMatrix(ml.dmlc.xgboost4j.java.DMatrix)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 CompletableFutures (com.spotify.futures.CompletableFutures)1 ExtractFn (com.spotify.zoltar.FeatureExtractFns.ExtractFn)1 IrisFeaturesSpec (com.spotify.zoltar.IrisFeaturesSpec)1 Iris (com.spotify.zoltar.IrisFeaturesSpec.Iris)1 IrisHelper (com.spotify.zoltar.IrisHelper)1 Prediction (com.spotify.zoltar.Prediction)1 Predictor (com.spotify.zoltar.Predictor)1 PredictorsTest (com.spotify.zoltar.PredictorsTest)1 FeatranExtractFns (com.spotify.zoltar.featran.FeatranExtractFns)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1