001/** 002 * Copyright (C) 2007 - 2016, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package org.dllearner.accuracymethods; 020 021import org.dllearner.core.ComponentAnn; 022import org.dllearner.core.config.ConfigOption; 023import org.dllearner.learningproblems.Heuristics; 024 025@ComponentAnn(name = "Weighted FMeasure", shortName = "weighted.fmeasure", version = 0) 026public class AccMethodFMeasureWeighted implements AccMethodTwoValued, AccMethodWithBeta { 027 028 @ConfigOption(defaultValue = "false", description = "balance the weights to relative set size") 029 private boolean balanced = false; 030 @ConfigOption(defaultValue = "1", description = "weight on the positive examples") 031 private double posWeight = 1; 032 @ConfigOption(defaultValue = "1", description = "weight on the negative examples") 033 private double negWeight = 1; 034 @ConfigOption(description = "beta factor (0 = do not use)", defaultValue = "0") 035 private double beta = 0; 036 037 @Override 038 public void init() { 039 } 040 041 public AccMethodFMeasureWeighted() { 042 } 043 044 public AccMethodFMeasureWeighted(boolean init) { 045 if (init) init(); 046 } 047 048 @Override 049 public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) { 050 int posExamples = tp + fn; 051 int negExamples = fp + tn; 052 053 double recall = Heuristics.divideOrZero( tp , tp+fn ); 054 055 if(recall == 0 || recall < 1 - noise) { 056 return -1; 057 } 058 059 if (balanced) { 060 posWeight = 1/(double)posExamples; 061 negWeight = 1/(double)negExamples; 062 } 063 double precision = tp == 0 ? 0 : ( tp*posWeight ) / ( tp*posWeight+fp*negWeight ); 064 if (beta == 0) { 065 return Heuristics.getFScore(recall, precision); 066 } else { 067 return Heuristics.getFScore(recall, precision, beta); 068 } 069 } 070 071 public boolean isBalanced() { 072 return balanced; 073 } 074 075 public void setBalanced(boolean balanced) { 076 this.balanced = balanced; 077 } 078 079 public double getPosWeight() { 080 return posWeight; 081 } 082 083 public void setPosWeight(double posWeight) { 084 this.posWeight = posWeight; 085 } 086 087 public double getNegWeight() { 088 return negWeight; 089 } 090 091 public void setNegWeight(double negWeight) { 092 this.negWeight = negWeight; 093 } 094 095 @Override 096 public void setBeta(double beta) { 097 this.beta = beta; 098 } 099}