Confusion matrix
Install
npm install -g @lmctl-ai/lmctl
lmvideo 0.1.38 is released. Source examples are pinned to public commit aab2677.
Draw a labeled three-class matrix with contrast-aware cell labels.
Run this example
lmctl autorun https://raw.githubusercontent.com/lmctl-ai/lmvideo/aab2677a4b6264ae6afae8ad64a1d7c1dcd3a052/examples/confusion-matrix.lms
Source
# Multiclass confusion matrix with the same Park-Miller sequence and probabilities as lmplot.
let classes=3;let per_class=80;let p0=[0.82,0.15,0.05];let p1=[0.12,0.70,0.25];let seed=90210;let counts=[];for(i in range(0,9)){counts=push(counts,0);}
for(c in range(0,classes)){for(i in range(0,per_class)){seed=(seed*16807)%2147483647;let u=num(seed)/2147483647.0;let pred=u<p0[c]?0:(u<p0[c]+p1[c]?1:2);counts[c*3+pred]=counts[c*3+pred]+1;}}
let diagonal=counts[0]+counts[4]+counts[8];let accuracy=num(diagonal)/240.0;
mkdir("images");let mp4=ARGV.len() > 0?ARGV[0]:"images/confusion-matrix.mp4";let png=ARGV.len() > 1?ARGV[1]:"images/confusion-matrix.png";
program = _lmcode_ cli="lmvideo" install="github:lmctl-ai/lmvideo@v0.1.38";
video.size(960,540);video.frames(1);video.output(${mp4});video.begin();video.frame();canvas.clear("#f5f7fa");let p=canvas.plot(215.0,20.0,530.0,500.0).title("Confusion matrix · accuracy "+${str(round(accuracy*1000.0)/1000.0)}).xlabel("predicted class").ylabel("true class").x_range(0.0,3.0).y_range(0.0,3.0).legend("none").background("#ffffff");let h=p.heatmap("cases",3,3).palette("magma").range(0.0,80.0).labels(true).colorbar(true);h.cell(2,0,float(${counts[0]})).cell(2,1,float(${counts[1]})).cell(2,2,float(${counts[2]})).cell(1,0,float(${counts[3]})).cell(1,1,float(${counts[4]})).cell(1,2,float(${counts[5]})).cell(0,0,float(${counts[6]})).cell(0,1,float(${counts[7]})).cell(0,2,float(${counts[8]}));p.draw();video.save_frame(${png});video.end();
end _lmcode_
r=run_lmcode(program);if(!r.ok){println((r.ok ? "" : r.message));}assert(r.ok);