CrossValidation extends AbstractCrossValidation<M, L, K, V> { @Override public double[] scoreByFolds() { double[] locScores; locScores = isRunningOnPipeline ? scorePipelineOnIgnite() : scoreOnIgnite(); return locScores; }  @Override double[] scoreByFolds(); CrossValidation<M, L, K, V> withIgnite(Ignite ignite); CrossValidation<M, L, K, V> withUpstreamCache(IgniteCache<K, V> upstreamCache);  }
@Test public void testScoreWithGoodDataset() { Map<Integer, double[]> data = new HashMap<>(); for (int i = 0; i < 1000; i++) data.put(i, new double[] {i > 500 ? 1.0 : 0.0, i}); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0); DebugCrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator = new DebugCrossValidation<>(); Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST); int folds = 4; scoreCalculator .withUpstreamMap(data) .withAmountOfParts(1) .withTrainer(trainer) .withMetric(new Accuracy<>()) .withPreprocessor(vectorizer) .withAmountOfFolds(folds) .isRunningOnPipeline(false); verifyScores(folds, scoreCalculator.scoreByFolds()); }
@Test public void testScoreWithGoodDatasetAndBinaryMetrics() { Map<Integer, double[]> data = new HashMap<>(); for (int i = 0; i < 1000; i++) data.put(i, new double[] {i > 500 ? 1.0 : 0.0, i}); Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0); DebugCrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator = new DebugCrossValidation<>(); int folds = 4; BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics() .withMetric(BinaryClassificationMetricValues::accuracy); scoreCalculator .withUpstreamMap(data) .withAmountOfParts(1) .withTrainer(trainer) .withMetric(metrics) .withPreprocessor(vectorizer) .withAmountOfFolds(folds) .isRunningOnPipeline(false); verifyScores(folds, scoreCalculator.scoreByFolds()); }
@Test public void testBasicFunctionality() { Map<Integer, double[]> data = new HashMap<>(); for (int i = 0; i < twoLinearlySeparableClasses.length; i++) data.put(i, twoLinearlySeparableClasses[i]); LogisticRegressionSGDTrainer trainer = new LogisticRegressionSGDTrainer() .withUpdatesStgy(new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG)) .withMaxIterations(100000) .withLocIterations(100) .withBatchSize(14) .withSeed(123L); Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST); DebugCrossValidation<LogisticRegressionModel, Double, Integer, double[]> scoreCalculator = new DebugCrossValidation<>(); int folds = 4; BinaryClassificationMetrics metrics = (BinaryClassificationMetrics) new BinaryClassificationMetrics() .withMetric(BinaryClassificationMetricValues::accuracy); scoreCalculator .withUpstreamMap(data) .withAmountOfParts(1) .withTrainer(trainer) .withMetric(metrics) .withPreprocessor(vectorizer) .withAmountOfFolds(folds) .isRunningOnPipeline(false); double[] scores = scoreCalculator.scoreByFolds(); assertEquals(0.8389830508474576, scores[0], 1e-6); assertEquals(0.9402985074626866, scores[1], 1e-6); assertEquals(0.8809523809523809, scores[2], 1e-6); assertEquals(0.9921259842519685, scores[3], 1e-6); }
@Test public void testScoreWithBadDataset() { Map<Integer, double[]> data = new HashMap<>(); for (int i = 0; i < 1000; i++) data.put(i, new double[] { i, i % 2 == 0 ? 1.0 : 0.0}); Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST); DecisionTreeClassificationTrainer trainer = new DecisionTreeClassificationTrainer(1, 0); DebugCrossValidation<DecisionTreeNode, Double, Integer, double[]> scoreCalculator = new DebugCrossValidation<>(); int folds = 4; scoreCalculator .withUpstreamMap(data) .withAmountOfParts(1) .withTrainer(trainer) .withMetric(new Accuracy<>()) .withPreprocessor(vectorizer) .withAmountOfFolds(folds) .isRunningOnPipeline(false); double[] scores = scoreCalculator.scoreByFolds(); assertEquals(folds, scores.length); for (int i = 0; i < folds; i++) assertTrue(scores[i] < 0.6); }