/*
* Debellor
*
* Copyright (C) 2008-2009 by Marcin Wojnarski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package org.debellor.base.evaluator.score;
import org.debellor.core.DataObject;
/**
* Calculates accuracy of a decision system (typically a classifier),
* i.e. the fraction of samples for which predicted decision
* was exactly the same as the target one.
*
* @author Marcin Wojnarski
*
*/
public class Accuracy extends Score {
private int good;
private int bad;
public Accuracy() {
reset();
}
@Override
public void reset() {
good = bad = 0;
}
@Override
public void add(DataObject target, DataObject prediction) {
if(target == null) return;
if(prediction == null) {
bad++;
return;
}
if(target.equals(prediction))
good++;
else
bad++;
}
@Override
public String report() {
double all = good + bad;
if(all > 0) {
double goodP = good / all * 100;
double badP = bad / all * 100;
return "Correctly classified: " + good + "\t" + goodP + "%" + "\n" +
"Misclassified: " + bad + "\t" + badP + "%";
}
else return "No test samples";
}
@Override
public double result() {
return good / (double) (good + bad);
}
}