blob: 0407998ac13393677f68d8f04803f744b1967454 [file] [log] [blame]
#-------------------------------------------------------------
#
# 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.
#
#-------------------------------------------------------------
# Computes the F1 score as the harmonic mean of precision and recall.
# F1 = 2TP / (2TP + FP + FN)
#
# INPUT:
# ------------------------------------------------------------------------------
# P vector of predictions (1-based, recoded)
# Y vector of actual labels (1-based, recoded)
# ------------------------------------------------------------------------------
#
# OUTPUT:
# ------------------------------------------------------------------------------
# score the F1 score
# ------------------------------------------------------------------------------
m_f1Score = function(Matrix[Double] P, Matrix[Double] Y)
return(Double score)
{
[cS, cA] = confusionMatrix(P, Y);
if(nrow(cS)>2 | ncol(cS)>2)
stop("f1Score: currently only supported for binary class labels.");
score = as.scalar(2*cS[1,1] / (2*cS[1,1] + cS[2,1] + cS[1,2]));
}