-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labeler.vb
66 lines (58 loc) · 2.41 KB
/
Labeler.vb
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
Public Class Labeler
Public Property Font As Font
Public Property Sample As Single()
Sub New(sample As Single())
Me.Sample = sample
Me.Font = New Font("Consolans", 7)
End Sub
Public Sub Analyse(bm As Bitmap, g As Graphics)
Dim analysis As String = Me.GetAnalysis()
Dim labels As SizeF = g.MeasureString(analysis, Me.Font)
Dim background As New RectangleF(5, bm.Height - (labels.Height + 100), labels.Width + 1, labels.Height)
g.FillRectangle(Brushes.White, background)
g.DrawString(analysis, Me.Font, Brushes.Black, background)
End Sub
Private Function GetAnalysis() As String
Dim mean As Single = Me.CalculateMean()
Dim stdDev As Single = Me.CalculateStandardDeviation()
Dim entrp As Single = Me.CalculateEntropy
Dim samples As Integer = Me.Sample.Length
Return $"Samples: {samples} Mean: {mean:F2}, Standard Deviation: {stdDev:F2} Entropy: {entrp:F2}"
End Function
Private Function CalculateMean() As Single
Dim sum As Single = 0
For Each value As Single In Sample
sum += value
Next
Return sum / Sample.Length
End Function
Private Function CalculateStandardDeviation() As Single
Dim mean As Single = CalculateMean()
Dim sumOfSquares As Single = 0
For Each value As Single In Sample
sumOfSquares += (value - mean) * (value - mean)
Next
Return Math.Sqrt(sumOfSquares / Sample.Length)
End Function
Public Function CalculateEntropy() As Double
Dim counts As New Dictionary(Of Single, Integer)()
For Each value As Single In Me.Sample
If counts.ContainsKey(value) Then
counts(value) += 1
Else
counts(value) = 1
End If
Next
Dim totalSamples As Integer = Me.Sample.Length
Dim probabilities As New Dictionary(Of Single, Double)()
For Each kvp As KeyValuePair(Of Single, Integer) In counts
Dim probability As Double = kvp.Value / totalSamples
probabilities(kvp.Key) = probability
Next
Dim entropy As Double = 0.0
For Each probability As Double In probabilities.Values
entropy -= probability * Math.Log(probability, 2)
Next
Return entropy
End Function
End Class