Greetings,
I am receiving an Overflow error on the following line:
The line is within the script below:
the function is used in the sub below:
the counter is an integer, and the centroid values are integers too. How can this line be written in excess of the function of the variant centroids when the component parts are all integers? any idea on how to troubleshoot this would be much appreciated
I am receiving an Overflow error on the following line:
Code:
centroids(ii, bb) = centroids(ii, bb) / counter
The line is within the script below:
Code:
Public Function ComputeCentroids(X As Variant, idx As Variant, K As Variant) As Variant
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'X Function that calculates the new centroid mean for each feature X
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Dim M As Integer: M = UBound(X, 1) 'number of objects
Dim J As Integer: J = UBound(X, 2) 'number of features
Dim ii As Integer: ii = 1
Dim cc As Integer: cc = 1
Dim bb As Integer: bb = 1
Dim counter As Integer: counter = 0
Dim centroids() As Variant: ReDim centroids(K, J) As Variant
Dim tempSum() As Variant
For ii = 1 To K 'For each centroid
For bb = 1 To J 'for each feature
counter = 0 'reset counter
For cc = 1 To M 'for each observation
If idx(cc) = ii Then 'for objects that belong to this centroid calc the sum
centroids(ii, bb) = centroids(ii, bb) + X(cc, bb)
counter = counter + 1
End If
Next cc
centroids(ii, bb) = centroids(ii, bb) / counter 'divide sum by counter to get mean
Next bb
Next ii
ComputeCentroids = centroids
End Function
the function is used in the sub below:
Code:
Public Sub KMeans()
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'X This macro uses the functions in this module to produce clusters of X
'X related observations given their features X
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
' get the initial centroid data 'initial centroid data can be K sets of observations
Dim InitCentrRange As String: InitCentrRange = ActiveSheet.Range("C14").value
Dim InitialCentroids As Variant: InitialCentroids = ActiveSheet.Range(InitCentrRange)
' get the number of max iterations
Dim MaxIt As Integer: MaxIt = ActiveSheet.Range("C9").value
' get the features data
Dim DataSht As String: DataSht = ActiveSheet.Range("C10").value
Dim DataRange As String: DataRange = ActiveSheet.Range("C11").value
Dim X As Variant: X = Worksheets(DataSht).Range(DataRange)
' assign objects to centroids based on initial centroids
Dim my_idx As Variant: my_idx = FindClosestCentroid(X, InitialCentroids)
Dim J As Integer: J = UBound(X, 2) ' number of features
Dim K As Integer: K = UBound(InitialCentroids, 1) 'number of clusters
Dim M As Integer: M = UBound(X, 1) 'number of objects
Dim centroids As Variant
Dim ii As Integer: ii = 1
For ii = 1 To MaxIt
centroids = ComputeCentroids(X, my_idx, K)
my_idx = FindClosestCentroid(X, centroids)
Next ii
'print the centroids
Dim outputCentroids As String: outputCentroids = ActiveSheet.Range("C15").value
Range(outputCentroids).Resize(K, J).value = centroids
'print the cluster assignment next to the data
Dim ClusterOutputSht As String: ClusterOutputSht = ActiveSheet.Range("C12").value
Dim ClusterOutputRange As String: ClusterOutputRange = ActiveSheet.Range("C13").value
Worksheets(ClusterOutputSht).Range(ClusterOutputRange).Resize(M, 1).value = WorksheetFunction.Transpose(my_idx)
End Sub
the counter is an integer, and the centroid values are integers too. How can this line be written in excess of the function of the variant centroids when the component parts are all integers? any idea on how to troubleshoot this would be much appreciated
Last edited: