package base.evaluate;
import java.util.HashMap;
import java.util.List;
public class ConfusionMatrix {
private HashMap<String, Integer> labelCntMap = new HashMap<>();
private String combineLabel(String trueLabel, String predictLabel) {
return trueLabel + "\t" + predictLabel;
}
private void sysPrint(String value) {
System.out.print(value);
}
public void add(int trueLabel, int predictLabel) {
add(String.valueOf(trueLabel), String.valueOf(predictLabel));
}
public void add(String trueLabel, String predictLabel) {
String tp = combineLabel(trueLabel, predictLabel);
labelCntMap.put(tp, labelCntMap.getOrDefault(tp, 0) + 1);
}
public void printResult(List<String> labels) {
sysPrint("\t");
for (String predictLabel: labels) {
sysPrint("\t" + predictLabel);
}
sysPrint("\r\n");
for (String trueLabel: labels) {
sysPrint(trueLabel);
for (String predictLabel: labels) {
String tp = combineLabel(trueLabel, predictLabel);
int cnt = labelCntMap.getOrDefault(tp, 0);
sysPrint("\t"+cnt);
}
sysPrint("\r\n");
}
for (String label: labels) {
double precision = .0;
double recall = 0.;
int pu = labelCntMap.getOrDefault(combineLabel(label, label), 0);
int ru = pu;
int pd = 0;
int rd = 0;
for (String otherLabel: labels) {
pd += labelCntMap.getOrDefault(combineLabel(otherLabel, label), 0);
rd += labelCntMap.getOrDefault(combineLabel(label, otherLabel), 0);
}
precision = Double.valueOf(String.format("%.2f", pu * 100 / Double.valueOf(pd)));
recall = Double.valueOf(String.format("%.2f", ru * 100 / Double.valueOf(rd)));
double f1 = Double.valueOf(String.format("%.2f", 2 * precision * recall / (precision + recall)));
sysPrint(label + "\t" + precision + "%\t" + recall + "%\t" + f1 + "%\r\n");
}
}
}
混淆矩阵java代码
最近热门
- 论文 | DHEN: A Deep and Hierarchical Ensemble Network for Large-Scale Click-Through Rate Prediction
- GradNorm
- SO-PMI(Semantic Orientation Pointwise Mutual Information,情感倾向点互信息算法)
- kimi api
- NPU(Neural Processing Unit,神经网络处理器)
- vue3 vditor
- Straight-Through Estimator(STE, 直推估计器)
- 流匹配(Flow Matching,FM)
- 模型证据下界(Evidence Lower Bound,ELBO)
- LLM | Chain of Thought(CoT,思维链)
最常浏览
- 016 推荐系统 | 排序学习(LTR - Learning To Rank)
- 偏微分符号
- i.i.d(又称IID)
- 利普希茨连续条件(Lipschitz continuity)
- (error) MOVED 原因和解决方案
- TextCNN详解
- 找不到com.google.protobuf.GeneratedMessageV3的类文件
- Deployment failed: repository element was not specified in the POM inside distributionManagement
- cannot access com.google.protobuf.GeneratedMessageV3 解决方案
- CLUSTERDOWN Hash slot not served 问题原因和解决办法
×