com.davidbracewell.ml.classification.Classification.java Source code

Java tutorial

Introduction

Here is the source code for com.davidbracewell.ml.classification.Classification.java

Source

/*
 * (c) 2005 David B. Bracewell
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.davidbracewell.ml.classification;

import com.davidbracewell.ml.Instance;
import com.google.common.base.Preconditions;

import java.util.List;

/**
 * Useful utility methods for classification algorithms and results.
 *
 * @author David B. Bracewell
 */
public final class Classification {

    /**
     * Calculates the log likelihood of  the model give a set of data.
     *
     * @param model The model
     * @param data  The data
     * @return The log likelihood
     */
    public static double logLikelihood(ClassificationModel model, List<Instance> data) {
        Preconditions.checkNotNull(model);
        Preconditions.checkNotNull(data);
        double ll = 0;
        for (Instance inst : data) {
            if (inst.hasTargetValue()) {
                ll += Math.log(model.estimate(inst).getConfidence(inst.getTargetValue()));
            }
        }
        return ll;
    }

    /**
     * Calculates the accuracy of the model given a set of data
     *
     * @param model The model
     * @param data  The data
     * @return The accuracy
     */
    public static double accuracy(ClassificationModel model, List<Instance> data) {
        Preconditions.checkNotNull(model);
        Preconditions.checkNotNull(data);
        double c = 0d;
        double t = 0d;
        for (Instance instance : data) {
            if (instance.hasTargetValue()) {
                t++;
                if (model.estimate(instance).getResult() == instance.getTargetValue()) {
                    c++;
                }
            }
        }
        return c / t;
    }

    /**
     * Calculates the error rate of the model given a set of data
     *
     * @param model The model
     * @param data  The data
     * @return The error rate
     */
    public static double errorRate(ClassificationModel model, List<Instance> data) {
        Preconditions.checkNotNull(model);
        Preconditions.checkNotNull(data);
        return 1d - accuracy(model, data);
    }

}//END OF Classification