我想使用数据表公式计算因子(C1)的所有级别的输出sum_logloss(见下文)。但结果并不是我所期望的。这是一个小例子,显示了我得到的以及为什么我期望另一个sum_logloss作为结果。
LogLoss <- function(actual, predicted, eps=0.00001) {
predicted <- pmin(pmax(predicted, eps), 1-eps)
-1/length(actual)*(sum(actual*log(predicted)+(1-actual)*log(1-predicted)))
}
# THIS RETURNS TOTAL LOGLOSS
TotalLogLossVector <- function(actual_vector, predicted_vector) {
sum(mapply(LogLoss, actual_vector, predicted_vector))
}
df = data.frame(C1=c(1,1,2,2,1), C2=c(4,5,4,5,5), click=c(1,0,0,1,1))
df <- data.table(df)
df
C1 C2 click
1: 1 4 1
2: 1 5 0
3: 2 4 0
4: 2 5 1
5: 1 5 1
df[,list(mean_CTR=mean(click),count=.N, sum_logloss=TotalLogLossVector(click,rep(mean_CTR,.N)) ),by=C1]
C1 mean_CTR count sum_logloss
1: 1 0.6666667 3 3.663061
2: 2 0.5000000 2 1.928626
LogLoss(1,0.6666667)
[1] 0.4054651
LogLoss(0,0.6666667)
[1] 1.098612
TotalLogLossVector(c(1,0,1), c(0.6666667,0.6666667,0.6666667))
[1] 1.909543
所以C1 = 1的sum_logloss应该是2 * LogLoss(1,0.6666667)+ 1 * LogLoss(0,0.6666667)= 1.909543,而不是3.663061。
答案 0 :(得分:3)
一个小注释:我建议setDT()
将data.frames转换为data.tables,特别是如果你将data.table分配回同一个变量。
@ akrun的答案很棒,但它分组了两次,我觉得这是不必要的。我就是这样做的:
setDT(df)[, {
tmp = mean(click);
list(mean_CTR = tmp, count = .N, sum_logloss =
TotalLogLossVector(click, tmp))}, by=C1]
答案 1 :(得分:2)
你可以尝试
df[, paste0('V', 1:2):=list(mean(click), .N), by=C1][,
list(mean_CTR=V1[1L], count=V2[1L], sum_logloss=
TotalLogLossVector(click, V1)), by=C1]
# C1 mean_CTR count sum_logloss
#1: 1 0.6666667 3 1.909543
#2: 2 0.5000000 2 1.386294