【问题标题】:VB.net Histogram - how to bin dataVB.net 直方图 - 如何分箱数据
【发布时间】:2015-03-03 12:25:51
【问题描述】:

我正在研究直方图类,尤其是分箱方法。

对此我有两个问题:

  1. 从逻辑/统计角度看是不是正确/合适的算法

  2. 代码是否最佳或至少不错 - 请告诉我如何改进它

任何帮助都非常感谢 - 提前谢谢。

这是我目前的代码...

Public Class Histo
Dim data() As Double
Dim bins As Integer = 0
Dim bw As Double = 0
Dim _min As Double = 0
Dim _max As Double = 0
Dim arrMax As Double = 0
Dim cht As Chart
Public shared Decimals As Integer

Public Sub New(_arr() As Double, _cht As Chart)
    'One-dimensional array as data
    data = _arr

    'No of bins with Sturges method
    bins  = NoBin_ST(data)

    'calculate bin width
    bw = Range(data) / bins

    'bin boundries for first bin 
    _min = Min(data)
    _max = _min + bw

    'max of data
    arrMax = Max(data)

    'chart object
    cht = _cht

    'no of decimals on x-axis
    Decimals = Dec
End Sub

Public Function Binning() As Integer()
    'Binning "algorihtm" for continuous data
    '
    'RETURN: one-dimensional array with n bins
    '
    Array.Sort(data)
    Dim j As Integer = 0
    Dim mn As Double = _min
    Dim mx As Double = _max
    Dim counter(bins-1) As Integer

    For i As Integer = 0 To data.GetLength(0)-1
        'check if data point is within the boundries of the current bin     
        If data(i) >= mn AndAlso data(i) < mx Then
            'add counter in current bin
            counter(j) += 1
        Else
            'special case: at the end at least one data point will equal max of the last bin
            ' and must be counted in that bin
            If data(i) = arrMax  Then
                counter(j) += 1
                Continue For
            End If
            'the data point has exceeded the boundries of the previous bin 
            ' and must be counted in the next bin
            'min and max is increased with the bin width
            mn += bw
            mx += bw
            'go to next bin
            j += 1
            'count data point in this bin and loop again
            counter(j) += 1
        End If
    Next
    Return counter
End Function

.....

【问题讨论】:

  • 与其将 arrMax 保留在 New 中,不如让 Binning 只循环少一个数据点,然后始终将最后一个数据点复制到最后一个 bin 中。这样就不需要找到最大值了。
  • answer by Garath 解决问题了吗?

标签: vb.net statistics histogram binning


【解决方案1】:

不确定这是否更高效,但我认为它更简单一些。

Function CreateBins(values As IEnumerable(Of Double), numberOfBins As Integer) As IGrouping(Of Integer, Double)()
        If values Is Nothing Then Throw New Exception("Values cannot be null")
        If values.Distinct.Count < 2 Then Throw New Exception("Values must contain at least two ditinct elements")
        If numberOfBins < 1 Then Throw New Exception("numberOfBins must be an integer > 1")

        Dim min = values.Min
        Dim max = values.Max
        Dim binSize = (max - min) / numberOfBins
        ' Checking for two distinct elements should eliminate possibility of min=max and therefore binsize=0

        Dim bins = values.GroupBy(Function(x) Convert.ToInt32(Math.Floor((x - min) / binSize))).ToArray

        ' Group counts available using the ienumerable Count function
        ' Dim counts = bins.Select(Function(x) x.Count)
        ' Or retaining the group key
        ' Dim counts = bins.Select(Function(x) New With {Key x.Key, x.Count})

        Return bins
End Function

现在每个 bin 都是一个组。原始值作为组的一部分保留,允许进行潜在的后续分析。计数可使用组函数 Count()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2013-06-18
    相关资源
    最近更新 更多