[ML] Speed up the lat_long function #1102
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Clustering can be the bottleneck for the
lat_long
function.This reworks the calculation of the distances to the selected points in k-means++ initialisation. Before we were creating a k-d tree for each point we added and looking up nearest neighbours. This is unnecessary since we can simply update the distances directly, i.e.
distance_i = min(distance_i, distance(selected, x_i))
. The other main speedup comes from the fact that in #1037 I reworked online k-means to remove the points buffer. This saves us memory and we can spend this memory by accumulating more points before we re-cluster.I also made some tweaks to cutdown the number of allocations (principally by moving various variables into place). Finally, I corrected
CKMeansOnline::split
to copy all the parent clustering parameters into each split and made a couple of other small tidy ups.In total, I get around a 60% improvement in runtime from these changes for
CXMeansOnline
.