-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.js
90 lines (81 loc) · 2.57 KB
/
plot.js
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
84
85
86
87
88
89
90
const numBins = 11;
const selectedstr = sessionStorage.getItem('selected');
const probs = JSON.parse(selectedstr);
const correctstr = sessionStorage.getItem('correct');
const accuracy = JSON.parse(correctstr);
// console.log(probs);
// console.log(accuracy);
let ssd = 0;
for (let i = 0; i < accuracy.length; i++) {
ssd += Math.pow((probs[i] - accuracy[i]), 2);
}
score = ssd / accuracy.length;
document.getElementById('score').textContent = Number(score.toFixed(3));
let result = document.getElementById('result');
let bins = new Array(numBins).fill(0).map(() => ({sumPredicted: 0, sumActual: 0, count: 0}));
// console.log(bins);
probs.forEach((prob, index) => {
const binIndex = Math.min(Math.floor(prob * numBins), numBins - 1);
bins[binIndex].sumPredicted += prob;
bins[binIndex].sumActual += accuracy[index];
bins[binIndex].count += 1;
});
// Calculate average predicted probability and actual frequency for each bin
let avgPredicted = [];
let avgActual = [];
for (let bin of bins) {
if (bin.count > 0) {
avgPredicted.push(bin.sumPredicted / bin.count);
avgActual.push(bin.sumActual / bin.count);
} else {
avgPredicted.push(null);
avgActual.push(null);
}
}
const ctx = document.getElementById('myChart').getContext('2d');
const calibrationChart = new Chart(ctx, {
type: 'line',
data: {
labels: avgPredicted.map((_, index) => (index + 0.5) / numBins),
datasets: [{
label: 'Calibration Curve',
data: avgActual,
borderColor: 'blue',
borderWidth: 2,
fill: false
}, {
label: 'Perfectly Calibrated',
data: avgPredicted.map((_, index) => (index + 0.5) / numBins),
borderColor: 'red',
borderWidth: 1,
fill: false,
borderDash: [5, 5]
}]
},
options: {
scales: {
x: {
ticks: {
callback: function(value, index, values) {
return (value / numBins).toFixed(2);
}
},
title: {
display: true,
text: 'Mean Predicted Probability'
}
},
y: {
ticks: {
callback: function(value, index, values) {
return value.toFixed(1);
}
},
title: {
display: true,
text: 'Fraction of Positives'
}
}
}
}
});