Though I’d try using C a bit more (i.e more than never), so here is a Ruby extension in C version of my KNN gem - github.com/reddavis/knn.
user system total real Ruby 2.800000 0.020000 2.820000 ( 2.838406) C 0.160000 0.010000 0.170000 ( 0.170568)
-
Not Complete
require 'rubygems' require 'knn_c' data = Array.new(100000) { Array.new(4) { rand } } knn = KnnC.new(data) knn.nearest_neighbours([1,2,3,4], 4) # ([data], k's) #=> [[4837, 7.43033158269445, [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]], ... # Data is returned in the format # [data index, distance to the input, [data points]] # So if we called queried the data array for 4837... data[4837] #=> [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]
(The MIT License)
Copyright © 2010 FIX
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.