001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.clustering.evaluation;
018
019import org.tribuo.Model;
020import org.tribuo.Prediction;
021import org.tribuo.clustering.ClusterID;
022import org.tribuo.evaluation.AbstractEvaluator;
023import org.tribuo.evaluation.Evaluator;
024import org.tribuo.evaluation.metrics.EvaluationMetric.Average;
025import org.tribuo.evaluation.metrics.MetricID;
026import org.tribuo.evaluation.metrics.MetricTarget;
027import org.tribuo.provenance.EvaluationProvenance;
028
029import java.util.HashSet;
030import java.util.List;
031import java.util.Map;
032import java.util.Set;
033
034/**
035 * A {@link Evaluator} for clustering using {@link ClusterID}s.
036 * <p>
037 * If the dataset contains an unassigned cluster id (as generated by {@link org.tribuo.clustering.ClusteringFactory#getUnknownOutput()})
038 * then the evaluate methods will throw {@link IllegalArgumentException} with an appropriate message.
039 */
040public class ClusteringEvaluator extends AbstractEvaluator<ClusterID, ClusteringMetric.Context, ClusteringEvaluation, ClusteringMetric> {
041
042    @Override
043    protected Set<ClusteringMetric> createMetrics(Model<ClusterID> model) {
044        Set<ClusteringMetric> metrics = new HashSet<>();
045
046        // Just using Average.micro here arbitrarily, NMI/AMI don't actually need targets
047        MetricTarget<ClusterID> target = new MetricTarget<>(Average.MICRO);
048        metrics.add(ClusteringMetrics.NORMALIZED_MI.forTarget(target));
049        metrics.add(ClusteringMetrics.ADJUSTED_MI.forTarget(target));
050
051        return metrics;
052    }
053
054    @Override
055    protected ClusteringMetric.Context createContext(Model<ClusterID> model, List<Prediction<ClusterID>> predictions) {
056        return new ClusteringMetric.Context(model, predictions);
057    }
058
059    @Override
060    protected ClusteringEvaluation createEvaluation(ClusteringMetric.Context context,
061                                                    Map<MetricID<ClusterID>, Double> results,
062                                                    EvaluationProvenance provenance) {
063        return new ClusteringEvaluationImpl(results, context, provenance);
064    }
065}