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.classification.explanations.lime;
018
019import org.tribuo.Model;
020import org.tribuo.Prediction;
021import org.tribuo.SparseModel;
022import org.tribuo.classification.Label;
023import org.tribuo.classification.explanations.Explanation;
024import org.tribuo.regression.Regressor;
025import org.tribuo.regression.evaluation.RegressionEvaluation;
026
027import java.util.List;
028
029/**
030 * An {@link Explanation} using LIME.
031 * <p>
032 * Wraps a {@link SparseModel} {@link Regressor} which is trained to predict the probabilities
033 * generated by the true {@link Model}.
034 * <p>
035 * See:
036 * <pre>
037 * Ribeiro MT, Singh S, Guestrin C.
038 * "Why should I trust you?: Explaining the predictions of any classifier"
039 * Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining 2016.
040 * </pre>
041 */
042public class LIMEExplanation implements Explanation<Regressor> {
043    private static final long serialVersionUID = 1L;
044
045    private final SparseModel<Regressor> model;
046
047    private final Prediction<Label> prediction;
048
049    private final RegressionEvaluation evaluation;
050
051    public LIMEExplanation(SparseModel<Regressor> model, Prediction<Label> prediction, RegressionEvaluation evaluation) {
052        this.model = model;
053        this.prediction = prediction;
054        this.evaluation = evaluation;
055    }
056
057    @Override
058    public List<String> getActiveFeatures() {
059        return model.getActiveFeatures().get(prediction.getOutput().getLabel());
060    }
061
062    @Override
063    public SparseModel<Regressor> getModel() {
064        return model;
065    }
066
067    @Override
068    public Prediction<Label> getPrediction() {
069        return prediction;
070    }
071
072    /**
073     * Gets the evaluator which scores how close the sparse model's
074     * predictions are to the complex model's predictions.
075     * @return The evaluation.
076     */
077    public RegressionEvaluation getEvaluation() {
078        return evaluation;
079    }
080
081    /**
082     * Get the RMSE of a specific dimension of the explanation model.
083     * @param name The dimension to look at.
084     * @return The RMSE of the explanation model.
085     */
086    public double getRMSE(String name) {
087        return evaluation.rmse().get(new Regressor.DimensionTuple(name,Double.NaN));
088    }
089
090    @Override
091    public String toString() {
092        return "LIMEExplanation(linearRMSE="+evaluation.rmse()+",modelPrediction="+prediction+",activeFeatures="+getActiveFeatures().toString()+")";
093    }
094}