-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfp_fmaxc.m
83 lines (73 loc) · 1.76 KB
/
pfp_fmaxc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function [fmax, point, t] = pfp_fmaxc(curve, tau, beta)
%PFP_FMAXC F-measure maximum (from) curve
%
% [fmax, point, t] = PFP_FMAXC(curve, tau);
%
% Returns the maximum F_{1}-measure of a precision-recall curve.
%
% [fmax, point, t] = PFP_FMAXC(curve, tau, beta);
%
% Returns the maximum F_{beta}-measure of a precision-recall curve.
%
% Input
% -----
% [double]
% curve: A k-by-2 precision-recall matrix (i.e. a curve).
%
% [double]
% tau: A 1-by-k (increasing) thresholds.
%
% (optional)
% [double]
% beta: The beta of F_{beta}-measure.
% default: 1
%
% Output
% ------
% [double]
% fmax: The F_{beta}-max.
%
% [double]
% point: The corresponding (pr, rc) that produces the F-max.
%
% [double]
% t: The best corresponding threshold.
% check inputs {{{
if nargin < 2
error('pfp_fmaxc:InputCount', 'Expected >= 2 inputs.');
end
if nargin == 2
beta = 1;
end
% curve
validateattributes(curve, {'double'}, {'ncols', 2}, '', 'curve', 1);
k = size(curve, 1);
% tau
validateattributes(tau, {'double'}, {'numel', k, 'increasing'}, '', 'tau', 2);
% beta
validateattributes(beta, {'double'}, {'real', 'positive'}, '', 'beta', 3);
% }}}
% sanity check {{{
if any(all(isnan(curve), 1))
fmax = NaN;
point = nan(1, 2);
t = NaN;
return;
end
% }}}
% calculation {{{
pr = curve(:, 1);
rc = curve(:, 2);
f = (1 + beta .^ 2) .* pr .* rc ./ (beta .^ 2 .* pr + rc);
fmax = max(f);
% threshold is set to the highest possible one that yields this fmax
index = max(find(f == fmax));
point = curve(index, :);
t = tau(index);
% }}}
return
% -----------
% Yuxiang Jiang ([email protected])
% Department of Computer Science
% Indiana University Bloomington
% Last modified: Tue 24 May 2016 02:35:53 PM E